ProgramMstDal.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Cksoft.Data;
  6. using DllEapEntity.Dtos;
  7. using DllEapEntity.Rms;
  8. using DllEapEntity;
  9. using MacTProcess = DllEapEntity.Rms.MacTProcess;
  10. namespace DllEapDal
  11. {
  12. public class ProgramMstDal
  13. {
  14. private IDatabase CurrDb;
  15. public ProgramMstDal(IDatabase db)
  16. {
  17. this.CurrDb = db;
  18. }
  19. public IEnumerable<ProgramMst> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  20. {
  21. if (!string.IsNullOrEmpty(filter))
  22. filter = filter.Replace("%2B", "+");
  23. var pros = CurrDb.FindListForCondition<ProgramMst>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo).ToList();
  24. // 加入参数信息
  25. if (pros != null && pros.Count() > 0)
  26. {
  27. for (var i = 0; i < pros.Count(); i++)
  28. {
  29. var detail = CurrDb.FindListForCondition<ProgramParamsDetail>($" and a.PreID='{pros[i].ID}'", ref errorinfo);
  30. if (detail == null)
  31. {
  32. detail = new List<ProgramParamsDetail>();
  33. }
  34. pros[i].ParamsDetail = detail;
  35. }
  36. }
  37. return pros;
  38. }
  39. public int GetCount(string filter)
  40. {
  41. if (!string.IsNullOrEmpty(filter))
  42. filter = filter.Replace("%2B", "+");
  43. string errorinfo = string.Empty;
  44. var entities = CurrDb.FindListForCondition<ProgramMst>(filter, ref errorinfo);
  45. if (entities != null)
  46. {
  47. return entities.Count();
  48. }
  49. return 0;
  50. }
  51. public ProgramMst Get(int id)
  52. {
  53. string errorinfo = string.Empty;
  54. var pro = CurrDb.FindEntityFor<ProgramMst>(id);
  55. if (pro != null)
  56. {
  57. var detail = CurrDb.FindListForCondition<ProgramParamsDetail>($" and a.PreID='{pro.ID}'", ref errorinfo);
  58. pro.ParamsDetail = detail;
  59. }
  60. return pro;
  61. }
  62. /// <summary>
  63. /// 添加角色并返回角色Id
  64. /// </summary>
  65. /// <param name="role"></param>
  66. /// <param name="userCode"></param>
  67. /// <returns></returns>
  68. public int Add(ProgramMst pro, string userCode, ref string errorinfo)
  69. {
  70. pro.RecCode = pro.ModCode = userCode;
  71. pro.RecTime = pro.ModTime = DateTime.Now;
  72. var entity = CurrDb.FindListForCondition<ProgramMst>($" and a.FName ='{pro.FName}' and ModelID='{pro.ModelID}' " +
  73. $"and ProcessCode='{pro.ProcessCode}' ", ref errorinfo).FirstOrDefault();
  74. if (entity != null)
  75. {
  76. errorinfo = "程序名已存在,请修改后重新添加";
  77. return -1;
  78. }
  79. string sql = $"insert into ProgramMst(FName,ModelID,ProcessCode,Remark,RecCode,RecTime,ModCode,ModTime) values('{pro.FName}','{pro.ModelID}'," +
  80. $"'{pro.ProcessCode}','{pro.Remark}','{pro.RecCode}','{pro.RecTime.ToString("yyyy-MM-dd HH:mm:ss")}','{pro.ModCode}','{pro.ModTime.ToString("yyyy-MM-dd HH:mm:ss")}');";
  81. sql += "select @@identity;";
  82. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  83. if (pro.ParamsDetail != null && pro.ParamsDetail.Count() > 0)
  84. {
  85. var detailDal = new ProgramMstDetailDal(CurrDb);
  86. int index = 1;
  87. foreach (var item in pro.ParamsDetail)
  88. {
  89. item.PreID = id;
  90. item.FNum = index++;
  91. if (detailDal.Add(item, userCode) < 0)
  92. {
  93. errorinfo = "新增程序参数失败,请联系管理员";
  94. return -1;
  95. }
  96. }
  97. }
  98. errorinfo = "";
  99. return id;
  100. }
  101. public int Update(ProgramMst role, string userCode, ref string error)
  102. {
  103. var entity = CurrDb.FindListForCondition<ProgramMst>($" and a.FName ='{role.FName}' and ModelID='{role.ModelID}' " +
  104. $"and ProcessCode='{role.ProcessCode}' and a.id <> '{role.ID}'", ref error).FirstOrDefault();
  105. if (entity != null)
  106. {
  107. error = "程序名已存在,请确认";
  108. return -1;
  109. }
  110. var t = CurrDb.FindEntity<ProgramMst>(role.ID);
  111. role.ServerPath = t.ServerPath;
  112. var detail = role.ParamsDetail;
  113. if (detail != null && detail.Count() > 0)
  114. {
  115. string strsql = $"select * from programparamsdetail where PreId={role.ID}";
  116. var exists = CurrDb.FindList<ProgramParamsDetail>(strsql);
  117. var existsIds = exists?.Select(c => c.ID);
  118. var upIds = role.ParamsDetail.Select(c => c.ID);
  119. var deleteIds = existsIds.Where(c => !upIds.Contains(c));
  120. var sql = string.Empty;
  121. if (deleteIds != null && deleteIds.Count() > 0)
  122. {
  123. sql = $"delete from PPSubDetail where preid in ({string.Join(",", deleteIds.Select(c => $"{c}"))})";
  124. if (CurrDb.ExecuteBySql(sql) < 0)
  125. {
  126. error = "删除子参数失败,请联系管理员";
  127. return -1;
  128. }
  129. sql = $" delete from programparamsdetail where id in({string.Join(",", deleteIds.Select(c => $"{c}"))})";
  130. if (CurrDb.ExecuteBySql(sql) < 0)
  131. {
  132. error = "删除原参数失败,请联系管理员";
  133. return -1;
  134. }
  135. }
  136. var updates = role.ParamsDetail.Where(c => existsIds.Contains(c.ID));
  137. if (updates != null && updates.Count() > 0)
  138. {
  139. foreach (var item in updates)
  140. {
  141. if (CurrDb.UpdateFor<ProgramParamsDetail>(item, userCode) < 0)
  142. {
  143. error = "更新原有参数失败,请联系管理员";
  144. return -1;
  145. }
  146. }
  147. }
  148. var adds = role.ParamsDetail.Where(c => !existsIds.Contains(c.ID));
  149. if (adds != null && adds.Count() > 0)
  150. {
  151. var detailDal = new ProgramMstDetailDal(CurrDb);
  152. var maxFnum = exists.Max(c => c.FNum) == null ? 0 : exists.Max(c => c.FNum);
  153. foreach (var item in detail)
  154. {
  155. item.PreID = role.ID;
  156. item.FNum = ++maxFnum;
  157. if (detailDal.Add(item, userCode) < 0)
  158. {
  159. error = "新增程序参数失败,请联系管理员";
  160. return -1;
  161. }
  162. }
  163. }
  164. }
  165. else
  166. {
  167. var str = $"select * from programparamsdetail where preid={role.ID}";
  168. var details = CurrDb.FindList<ProgramParamsDetail>(str);
  169. if (details != null && details.Count() > 0)
  170. {
  171. var deleteFilter = string.Join(",", details.Select(c => c.ID));
  172. str = $"delete from PPSubDetail where preid in ({deleteFilter})";
  173. CurrDb.ExecuteBySql(str);
  174. str = $"delete from programparamsdetail where PreId={role.ID}";
  175. CurrDb.ExecuteBySql(str);
  176. }
  177. }
  178. if (CurrDb.UpdateFor(role, userCode) > 0)
  179. {
  180. return role.ID;
  181. }
  182. return -1;
  183. }
  184. public IEnumerable<PPSubDetail> getppsubdetail(int id)
  185. {
  186. string sql = $"select * from ppsubdetail where preid in (select id from ProgramParamsDetail where preid={id})";
  187. return CurrDb.FindList<PPSubDetail>(sql);
  188. }
  189. public IEnumerable<ProgramParamsDetail> getProgramParamsDetail(int id)
  190. {
  191. string sql = $"select * from ProgramParamsDetail where PreId='{id}'";
  192. return CurrDb.FindList<ProgramParamsDetail>(sql);
  193. }
  194. public int Delete(int id, ref string msg)
  195. {
  196. // 判断是否有产品正在使用该程序
  197. string sql = $"select * from ProgramRule where ProgramMstID='{id}'";
  198. var rule = CurrDb.FindList<ProgramRule>(sql).FirstOrDefault();
  199. if (rule != null)
  200. {
  201. msg = "该程序已经设置规则,如需删除,请先删除对应的规则";
  202. return -1;
  203. }
  204. sql = $"delete from ppsubdetail where preid in (select id from ProgramParamsDetail where preid={id})";
  205. if (CurrDb.ExecuteBySql(sql) < 0)
  206. {
  207. msg = "删除程序二级参数时失败,请联系管理员";
  208. return -1;
  209. }
  210. sql = $"delete from ProgramParamsDetail where PreId='{id}'";
  211. if (CurrDb.ExecuteBySql(sql) < 0)
  212. {
  213. msg = "删除程序参数时出错,请联系管理员";
  214. return -1;
  215. }
  216. if (CurrDb.DeleteFor<ProgramMst>(id) < 0)
  217. {
  218. msg = "删除程序主表时出错,请联系管理员";
  219. return -1;
  220. }
  221. msg = string.Empty;
  222. return 1;
  223. }
  224. public int UpdateFileParams(ProgramMst mst, string userCode, ref string error)
  225. {
  226. var details = mst.FileParamsDetail;
  227. var sql = $"delete from programfileparams where preid={mst.ID}";
  228. CurrDb.ExecuteBySql(sql);
  229. if (details != null && details.Count() > 0)
  230. {
  231. if (CurrDb.Insert(details) < 0)
  232. {
  233. error = "插入文件参数失败";
  234. return -1;
  235. }
  236. }
  237. return 1;
  238. }
  239. public IEnumerable<CascaderDto> GetCasMaModels()
  240. {
  241. string sql = "select * from tprocess";
  242. var processes = CurrDb.FindList<ProcessInfo>(sql);
  243. var macmodelDal = new MacModelDal(CurrDb);
  244. var macmodels = macmodelDal.GetEapMacModels();
  245. var list = new List<CascaderDto>();
  246. for (var i = 0; i < processes.Count(); i++)
  247. {
  248. var casDto = new CascaderDto()
  249. {
  250. Label = processes.ElementAt(i).FCode,
  251. Value = processes.ElementAt(i).FCode,
  252. IsLeaf = false
  253. };
  254. var children = macmodels.Where(c => c.TPCode == casDto.Value);
  255. var cList = new List<CascaderDto>();
  256. if (children != null && children.Count() > 0)
  257. {
  258. foreach (var item in children)
  259. {
  260. if (cList.Count == 0 || cList.FirstOrDefault(c => c.Value == item.FCode) == null)
  261. {
  262. cList.Add(new CascaderDto
  263. {
  264. Id = item.ID,
  265. Label = item.FCode,
  266. Value = item.FCode,
  267. Children = null,
  268. IsLeaf = true
  269. });
  270. }
  271. }
  272. casDto.Children = cList;
  273. }
  274. else
  275. {
  276. casDto.IsLeaf = true;
  277. }
  278. list.Add(casDto);
  279. }
  280. return list;
  281. }
  282. public int IsExists(string name, ref string errorinfo)
  283. {
  284. var entity = CurrDb.FindListForCondition<ProgramMst>($" and a.FName ='{name}'", ref errorinfo).FirstOrDefault();
  285. if (entity != null)
  286. return 1;
  287. else
  288. return -1;
  289. }
  290. public IEnumerable<SelectDto<int>> GetParamsList(string macmodelCode)
  291. {
  292. string sql = $"select a.id as value,b.FName as label,b.FCode as Code from MMSecDetail a " +
  293. $"inner join Sec b on a.SecID=b.id " +
  294. $"inner join MacModel c on a.PreID=c.id " +
  295. $"where 1=1 and c.FCode='{macmodelCode}' and b.FType<>3";
  296. return CurrDb.FindList<SelectDto<int>>(sql);
  297. }
  298. public IEnumerable<SelectDto<int>> GetPrograms()
  299. {
  300. string sql = @"select a.id as value, concat(a.fname,' | ',a.ProcessCode,' | ',b.FCode) as label from ProgramMst a
  301. left join MacModel b on a.ModelId=b.id";
  302. return CurrDb.FindList<SelectDto<int>>(sql);
  303. }
  304. public int IUProgramMst(Machine mac, string programname, ref string errorinfo)
  305. {
  306. try
  307. {
  308. string tpcode = "";
  309. string condition = $" and a.MacID={mac.ID}";
  310. List<MacTProcess> mactps = CurrDb.FindListForCondition<MacTProcess>(condition, ref errorinfo).ToList();
  311. if (mactps.Count > 0)
  312. {
  313. tpcode = mactps[0].PCode;
  314. }
  315. condition = $" and a.MacID={mac.ID}";
  316. List<MacTProcess> mactp = CurrDb.FindListForCondition<MacTProcess>(condition, ref errorinfo).ToList();
  317. if (mactp.Count > 0)
  318. {
  319. tpcode = mactp[0].PCode;
  320. }
  321. condition = $" and a.ModelID={mac.MModeID} and a.FName='{programname}' and a.ProcessCode='{tpcode}'";
  322. List<ProgramMst> pmsts = CurrDb.FindListForCondition<ProgramMst>(condition, ref errorinfo).ToList();
  323. if (pmsts.Count > 0)
  324. return pmsts[0].ID;
  325. ProgramMst entity = new ProgramMst();
  326. entity.ModelID = mac.MModeID;
  327. entity.ProcessCode = tpcode;
  328. entity.FName = programname;
  329. CurrDb.InsertFor<ProgramMst>(entity, "");
  330. return 1;
  331. }
  332. catch (Exception ex)
  333. {
  334. errorinfo = ex.Message.ToString();
  335. return -1;
  336. }
  337. }
  338. }
  339. }