MacModelDal.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using AutoMapper;
  2. using Cksoft.Data;
  3. using Cksoft.Unity;
  4. using DllEapEntity;
  5. using DllEapEntity.Dtos;
  6. using DllEapEntity.ExportDtos;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. namespace DllEapDal
  12. {
  13. public class MacModelDal
  14. {
  15. private IDatabase CurrDb = null;
  16. private IDatabase SecDb = null;
  17. private IMapper _mapper;
  18. public MacModelDal(IDatabase db)
  19. {
  20. CurrDb = db;
  21. }
  22. public MacModelDal(IDatabase db, IMapper mapper)
  23. {
  24. CurrDb = db;
  25. _mapper = mapper;
  26. }
  27. public MacModelDal(IDatabase db, IDatabase secDb)
  28. {
  29. CurrDb = db;
  30. SecDb = secDb;
  31. }
  32. private MacModel IUMacModel(MacModel mst, string usercode, ref string errorinfo)
  33. {
  34. try
  35. {
  36. int result = 0;
  37. int id = mst.ID;
  38. if (mst.EntityStatusID == 1)
  39. {
  40. //mst.RecCode = usercode;
  41. //mst.RecTime = DateTime.Now;
  42. //mst.ModCode = usercode;
  43. //mst.ModTime = DateTime.Now;
  44. //mst.ID = Guid.NewGuid().ToString();
  45. result = CurrDb.InsertFor(mst, usercode);
  46. if (result < 0)
  47. {
  48. return null;
  49. }
  50. object objid = CurrDb.FindObject("select @@IDENTITY");
  51. if (objid.ToString() == "")
  52. {
  53. return null;
  54. }
  55. id = int.Parse(objid.ToString());
  56. }
  57. else
  58. {
  59. //mst.ModCode = usercode;
  60. //mst.ModTime = DateTime.Now;
  61. result = CurrDb.UpdateFor(mst, usercode);
  62. if (result < 0)
  63. {
  64. return null;
  65. }
  66. }
  67. mst = CurrDb.FindEntityFor<MacModel>(id);
  68. return mst;
  69. }
  70. catch (Exception e)
  71. {
  72. errorinfo = e.Message;
  73. return null;
  74. }
  75. }
  76. public MacModel IUMacModel(MacModel mst, List<MMSecDetail> details, string usercode, ref string errorinfo)
  77. {
  78. try
  79. {
  80. mst = IUMacModel(mst, usercode, ref errorinfo);
  81. if (mst == null)
  82. return null;
  83. int result = IUMMSecDetail(mst, details, usercode, ref errorinfo);
  84. if (result < 0)
  85. return null;
  86. return mst;
  87. }
  88. catch (Exception e)
  89. {
  90. errorinfo = e.Message;
  91. return null;
  92. }
  93. }
  94. private int IUMMSecDetail(MacModel mst, List<MMSecDetail> details, string usercode, ref string errorinfo)
  95. {
  96. try
  97. {
  98. List<MMSecDetail> templists = details.Where(t => t.EntityStatusID < 0).ToList();
  99. int result = CurrDb.DeleteForEntity<MMSecDetail>(templists);
  100. if (result < 0)
  101. return -1;
  102. templists = details.Where(t => t.EntityStatusID == 1).ToList();
  103. foreach (var item in templists)
  104. item.PreID = mst.ID;
  105. result = CurrDb.InsertFor<MMSecDetail>(templists, usercode);
  106. if (result < 0)
  107. return -1;
  108. templists = details.Where(t => t.EntityStatusID == 2).ToList();
  109. result = CurrDb.UpdateFor<MMSecDetail>(templists, usercode);
  110. if (result < 0)
  111. return -1;
  112. return 1;
  113. }
  114. catch (Exception e)
  115. {
  116. errorinfo = e.Message;
  117. return -1;
  118. }
  119. }
  120. public IEnumerable<SecMacModel> Get()
  121. {
  122. return CurrDb.FindList<SecMacModel>("select * from MacModel");
  123. }
  124. public IEnumerable<SecMacModel> GetSecMacModels()
  125. {
  126. string sql = $"select a.ID,a.Fname,a.FCode,c.PCode as TPCode from MacModel a "
  127. + "left join Machine b on a.id=b.mmodeid "
  128. + "left join MacTProcess c on b.id=c.MacId";
  129. return CurrDb.FindList<SecMacModel>(sql);
  130. }
  131. public IEnumerable<MacModel> GetEapMacModels()
  132. {
  133. string sql = @"select a.Fname,a.FCode,c.PCode as TPCode,a.id
  134. from MacModel a
  135. left
  136. join Machine b on a.id = b.MModeID
  137. left
  138. join MacTProcess c on b.id = c.MacId";
  139. return CurrDb.FindList<MacModel>(sql);
  140. }
  141. #region Web
  142. public IEnumerable<MacModel> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  143. {
  144. var pros = CurrDb.FindListForCondition<MacModel>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  145. return pros;
  146. }
  147. public int GetCount(string filter)
  148. {
  149. string errorinfo = string.Empty;
  150. //string sql = $"select count(1) from ProgramRule a where 1=1 {filter}";
  151. var entities = CurrDb.FindListForCondition<MacModel>(filter, ref errorinfo);
  152. if (entities != null)
  153. {
  154. return entities.Count();
  155. }
  156. return 0;
  157. }
  158. public MacModel Get(int id)
  159. {
  160. var pro = CurrDb.FindEntityFor<MacModel>(id);
  161. return pro;
  162. }
  163. public IEnumerable<MacModel> GetAllMacmodels(string filter, ref string errorinfo)
  164. {
  165. return CurrDb.FindListForCondition<MacModel>(filter, ref errorinfo);
  166. }
  167. public async Task<IEnumerable<MacModelExportDto>> GetExportData(string filter)
  168. {
  169. string erorinfo = string.Empty;
  170. var datas = await CurrDb.FindListForConditionAsync<MacModel>(filter, ref erorinfo);
  171. var models = _mapper.Map<IEnumerable<MacModelExportDto>>(datas).ToList();
  172. return models;
  173. }
  174. /// <summary>
  175. /// 添加角色并返回角色Id
  176. /// </summary>
  177. /// <param name="role"></param>
  178. /// <param name="userCode"></param>
  179. /// <returns></returns>
  180. public int Add(MacModel pro, string userCode, ref string errorinfo)
  181. {
  182. var entities = CurrDb.FindListForCondition<MacModel>($" and a.FCode='{pro.FCode}' ", ref errorinfo);
  183. if (entities != null && entities.Count() > 0)
  184. {
  185. errorinfo = "机型已存在,请确认";
  186. return -1;
  187. }
  188. pro.RecCode = pro.ModCode = userCode;
  189. pro.RecTime = pro.ModTime = DateTime.Now;
  190. string sql = $"insert into MacModel(FCode,FName,Remark,RecCode,RecTime," +
  191. $"ModCode,ModTime,SoftVersion,SupplierID) values('{pro.FCode}','{pro.FName}'," +
  192. $"'{pro.Remark}','{pro.RecCode}','{pro.RecTime.ToString("yyyy-MM-dd")}'" +
  193. $",'{pro.ModCode}','{pro.ModTime.ToString("yyyy-MM-dd")}','{pro.SoftVersion}','{pro.SupplierID}');";
  194. sql += "select @@identity;";
  195. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  196. if (pro.MMSecDetails != null && pro.MMSecDetails.Count() > 0)
  197. {
  198. foreach (var item in pro.MMSecDetails)
  199. {
  200. item.PreID = id;
  201. if (CurrDb.InsertFor<MMSecDetail>(item, userCode) < 0)
  202. {
  203. return -1;
  204. }
  205. }
  206. }
  207. return id;
  208. }
  209. public int Update(MacModel role, string userCode, ref string errorinfo)
  210. {
  211. var entities = CurrDb.FindListForCondition<MacModel>($" and a.FCode='{role.FCode}' " +
  212. $"and a.ID<>{role.ID}", ref errorinfo);
  213. if (entities != null && entities.Count() > 0)
  214. {
  215. errorinfo = "已存在相同的机型,请确认";
  216. return -1;
  217. }
  218. if (role.MMSecDetails != null && role.MMSecDetails.Count() > 0)
  219. {
  220. var exists = CurrDb.FindListForCondition<MMSecDetail>($" and a.PreID={role.ID}", ref errorinfo);
  221. var updates = role.MMSecDetails.Where(c => exists.Any(f => f.SecFCode == c.SecFCode));
  222. var adds = role.MMSecDetails.Where(c => exists.All(f => f.SecFCode != c.SecFCode));
  223. var deletes = exists.Where(c => role.MMSecDetails.All(f => f.SecFCode != c.SecFCode));
  224. if (adds != null && adds.Count() > 0)
  225. {
  226. foreach (var i in adds)
  227. {
  228. if (CurrDb.InsertFor(i, userCode) < 0)
  229. {
  230. errorinfo = "新增参数失败";
  231. return -1;
  232. }
  233. }
  234. }
  235. if (updates != null && updates.Count() > 0)
  236. {
  237. foreach (var i in updates)
  238. {
  239. if (CurrDb.UpdateFor(i, userCode) < 0)
  240. {
  241. errorinfo = "更新参数失败";
  242. return -1;
  243. }
  244. }
  245. }
  246. if (deletes != null && deletes.Count() > 0)
  247. {
  248. foreach (var item in deletes)
  249. {
  250. if (CurrDb.DeleteFor<MMSecDetail>(item.ID) < 0)
  251. {
  252. errorinfo = "删除参数失败";
  253. return -1;
  254. }
  255. }
  256. }
  257. }
  258. else
  259. {
  260. string sql = string.Format($"delete from MMSecDetail where PreID='{role.ID}'");
  261. if (CurrDb.ExecuteBySql(sql) < 0)
  262. {
  263. errorinfo = "当前参数正在被使用,不能删除";
  264. return -1;
  265. }
  266. }
  267. if (CurrDb.UpdateFor(role, userCode) < 0)
  268. {
  269. return -1;
  270. }
  271. return role.ID;
  272. }
  273. public int Delete(int id, ref string msg)
  274. {
  275. string sql = $"delete from modelsubdetail where preid in (select id from MMSecDetail where preid={id})";
  276. if (CurrDb.ExecuteBySql(sql) < 0)
  277. {
  278. msg = "删除机型子参数失败";
  279. return -1;
  280. }
  281. sql = $"delete from eventreportdetail where preid in (select id from orderevent " +
  282. $"where MMSecDetailID in(select id from MMSecDetail where preid={id}))";
  283. if (CurrDb.ExecuteBySql(sql) < 0)
  284. {
  285. msg = "删除事件报告参数失败";
  286. return -1;
  287. }
  288. sql = $"delete from orderevent where MMSecDetailID in(select id from MMSecDetail where preid={id})";
  289. if (CurrDb.ExecuteBySql(sql) < 0)
  290. {
  291. msg = "删除该该机型下指令事件失败";
  292. return -1;
  293. }
  294. sql = $"delete from MMSecDetail where PreID='{id}'";
  295. if (CurrDb.ExecuteBySql(sql) < 0)
  296. {
  297. msg = "删除参数信息时失败";
  298. return -1;
  299. }
  300. sql = $"delete from fileparams where ModelID={id}";
  301. if (CurrDb.ExecuteBySql(sql) < 0)
  302. {
  303. msg = "删除文件参数时失败";
  304. return -1;
  305. }
  306. if (CurrDb.DeleteFor<MacModel>(id) < 0)
  307. {
  308. msg = "删除失败";
  309. return -1;
  310. }
  311. msg = string.Empty;
  312. return 1;
  313. }
  314. public MacModel AddOrGet(MacModel macModel, string userCode, ref string errorinfo)
  315. {
  316. var exist = CurrDb.FindListForCondition<MacModel>($" and a.FCode='{macModel.FCode.Trim()}'",
  317. ref errorinfo).FirstOrDefault();
  318. if (exist != null)
  319. return exist;
  320. if (CurrDb.InsertFor(macModel, userCode) < 0)
  321. {
  322. errorinfo = "插入数据库失败";
  323. return null;
  324. }
  325. exist = CurrDb.FindListForCondition<MacModel>($" and a.FCode='{macModel.FCode.Trim()}'",
  326. ref errorinfo).FirstOrDefault();
  327. return exist;
  328. }
  329. #endregion
  330. #region 同步Sec数据库
  331. public int AddToSec(MacModel model)
  332. {
  333. var secMacModel = new SecMacModel
  334. {
  335. ID = Guid.NewGuid().ToString(),
  336. FCode = model.FCode,
  337. FName = model.FName,
  338. TPCode = model.TPCode,
  339. reccode = model.RecCode,
  340. rectime = model.RecTime,
  341. modcode = model.ModCode,
  342. modtime = model.ModTime,
  343. Remark = model.Remark
  344. };
  345. return SecDb.Insert<SecMacModel>(secMacModel);
  346. }
  347. //public int UpdateToSec(MacModel model)
  348. //{
  349. // var secMacmodel = SecDb.FindList<SecMacModel>($"select * from MacModel where FCode='{model.FCode}'").FirstOrDefault();
  350. // if (secMacmodel == null)
  351. // {
  352. // return AddToSec(model);
  353. // }
  354. // else
  355. // {
  356. // secMacmodel.FCode = model.FCode;
  357. // secMacmodel.FName = model.FName;
  358. // }
  359. //}
  360. #endregion
  361. }
  362. }