MacModelProgramDal.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using Cksoft.Data;
  2. using DllEapEntity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using Microsoft.Extensions.Configuration;
  7. using Cksoft.Unity;
  8. using System.Linq;
  9. using System.IO;
  10. using DllEapEntity.Dtos;
  11. using Cksoft.Data.Repository;
  12. using DllEapEntity.Rms;
  13. using Cksoft.Unity.Log4NetConfig;
  14. namespace DllEapDal
  15. {
  16. public class MacModelProgramDal
  17. {
  18. private string programBaseDir;
  19. private IDatabase CurrDb;
  20. private string commonFilter = null;
  21. public MacModelProgramDal(IDatabase db)
  22. {
  23. CurrDb = db;
  24. this.programBaseDir = AppConfigurtaionServices.Configuration.GetValue<string>("MacModelProgramDir");
  25. }
  26. public MacModelProgramDal(IDatabase db, string userCode)
  27. {
  28. CurrDb = db;
  29. this.programBaseDir = AppConfigurtaionServices.Configuration.GetValue<string>("MacModelProgramDir");
  30. //var smDal = new StaffMachineDal(CurrDb);
  31. //string errorinfo = string.Empty;
  32. // var idFilter = smDal.GetFilter(userCode, ref errorinfo);
  33. // commonFilter = idFilter.Replace("a.id", "b.id");
  34. }
  35. #region MacModelProgram
  36. public IEnumerable<MacModelProgram> Get()
  37. {
  38. var models = CurrDb.FindList<MacModelProgram>();
  39. return models;
  40. }
  41. public IEnumerable<MacModelProgram> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  42. {
  43. var versions = CurrDb.FindListForCondition<BusinessFileRelation>($"{filter} order by {sort} {order} limit {start - 1},{length} ", ref errorinfo);
  44. return versions.Select(c => new MacModelProgram
  45. {
  46. ID = c.ID,
  47. FileOrgName = c.FileOrgName,
  48. IsDefault = c.IsDefault,
  49. ModCode = c.ModCode,
  50. MacModelID = c.OrgID,
  51. Version = c.Version,
  52. ModTime = c.ModTime,
  53. RecCode = c.RecCode,
  54. ProgramName = c.ProgramName,
  55. RecTime = c.RecTime,
  56. Remark = c.remark,
  57. FileInfoRemark = c.FileInfoRemark,
  58. UploadMacCode = c.UploadMacCode
  59. });
  60. }
  61. public int GetVersionCount(string filter)
  62. {
  63. string errorinfo = string.Empty;
  64. var versions = CurrDb.FindListForCondition<BusinessFileRelation>($"{filter}", ref errorinfo);
  65. return versions.Count();
  66. }
  67. /// <summary>
  68. /// 获取机型程序列表
  69. /// </summary>
  70. /// <param name="start"></param>
  71. /// <param name="length"></param>
  72. /// <param name="order"></param>
  73. /// <param name="sort"></param>
  74. /// <param name="filter"></param>
  75. /// <param name="errorinfo"></param>
  76. /// <returns></returns>
  77. public IEnumerable<MacProgramDto> GetGrouped(int start, int length, string order, string sort, string filter, string errorinfo)
  78. {
  79. if (!string.IsNullOrEmpty(filter))
  80. filter = filter.Replace("%2B", "+");
  81. var sql = $"select a.Id,a.FName as ProgramName,b.FCode MacModelCode,b.FName MacModelName,b.Id MacModelId " +
  82. $"from businessfile a " +
  83. $"inner join MacModel b on a.OrgId=b.Id and a.OrgTypeID=2 " +
  84. $"where 1=1 {filter} and a.OrgId in (select MModeId from machine where 1=1 {commonFilter} ) " +
  85. $"order by {sort} {order} limit {start - 1},{length} ";
  86. var datas = CurrDb.FindList<MacProgramDto>(sql);
  87. return datas;
  88. }
  89. public int GetGroupedCount(string filter)
  90. {
  91. if (!string.IsNullOrEmpty(filter))
  92. filter = filter.Replace("%2B", "+");
  93. string sql = $"select count(1) " +
  94. $"from businessfile a " +
  95. $"inner join MacModel b on a.OrgId=b.Id and a.OrgTypeID=2 " +
  96. $"where 1=1 {filter} and a.OrgId in (select MModeId from machine where 1=1 {commonFilter} ) ";
  97. return Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "0");
  98. }
  99. /// <summary>
  100. /// 设置默认版次
  101. /// </summary>
  102. /// <param name="id"></param>
  103. /// <param name="isDefault"></param>
  104. /// <param name="macId"></param>
  105. /// <param name="programName"></param>
  106. /// <param name="errorinfo"></param>
  107. /// <returns></returns>
  108. public int UpdateField(int id, int isDefault, int macId, string programName, ref string errorinfo)
  109. {
  110. if (isDefault != -1)
  111. {
  112. var filter = $" and a.IsDefault=1 and b.OrgId={macId} and b.OrgTypeID=2 and b.FName='{programName}'";
  113. var entity = CurrDb.FindListForCondition<BusinessFileRelation>(filter, ref errorinfo).FirstOrDefault();
  114. if (entity != null)
  115. {
  116. if (entity.ID != id)
  117. {
  118. errorinfo = "当前机型已经存在默认程序,如需重新设置请先取消原来的默认程序";
  119. }
  120. else
  121. {
  122. errorinfo = "当前程序版次已经是默认版次,无需重复设置";
  123. }
  124. return -1;
  125. }
  126. }
  127. string sql = $"update BusinessFileRelation set isDefault={isDefault} where id={id}";
  128. return CurrDb.ExecuteBySql(sql);
  129. }
  130. #endregion
  131. public BusinessFileRelation getBusinessFileRelation(int id)
  132. {
  133. return CurrDb.FindEntityFor<BusinessFileRelation>(id);
  134. }
  135. public IEnumerable<BusinessFile> getBusinessFile(BusinessFileRelation macProgram)
  136. {
  137. string sql = $"select * from BusinessFile where OrgId='{macProgram.OrgID}' and Fname='{macProgram.ProgramName}' and OrgTypeID =2 ";
  138. return CurrDb.FindList<BusinessFile>(sql);
  139. }
  140. /// <summary>
  141. /// 删除单个版次
  142. /// </summary>
  143. /// <param name="id"></param>
  144. /// <param name="macModelCode"></param>
  145. /// <param name="errorinfo"></param>
  146. /// <returns></returns>
  147. public int DeleteSingVersion(int id, string macModelCode, ref string errorinfo)
  148. {
  149. var macmodel = CurrDb.FindListForCondition<MacModel>($" and a.FCode='{macModelCode}'", ref errorinfo);
  150. var macProgram = CurrDb.FindEntityFor<BusinessFileRelation>(id);
  151. if (macProgram != null)
  152. {
  153. var sql = $"delete from BusinessFileRelation where id={id}";
  154. if (CurrDb.ExecuteBySql(sql) < 0)
  155. {
  156. errorinfo = "删除数据库中程序版次失败";
  157. return -1;
  158. }
  159. var macPros = CurrDb.FindListForCondition<BusinessFileRelation>($" and b.OrgId='{macProgram.OrgID}' and b.OrgTypeID=2 " +
  160. $"and b.FName='{macProgram.ProgramName}'", ref errorinfo);
  161. if (macPros == null || macPros.Count() <= 0)
  162. {
  163. sql = $"delete from BusinessFile where OrgId='{macProgram.OrgID}' and Fname='{macProgram.ProgramName}' and OrgTypeID =2 ";
  164. if (CurrDb.ExecuteBySql(sql) < 0)
  165. {
  166. errorinfo = "删除程序版次失败";
  167. return -1;
  168. }
  169. }
  170. }
  171. return 1;
  172. }
  173. public int getBusinessFilebusinessId(string proName, int macModelId, ref string errorinfo)
  174. {
  175. var businessFile = CurrDb.FindListForCondition<BusinessFile>($" and a.OrgId={macModelId} and OrgTypeID=2 " +
  176. $" and a.FName='{proName}'", ref errorinfo)
  177. .FirstOrDefault();
  178. return businessFile.ID;
  179. }
  180. public IEnumerable<BusinessFileRelation> getBusinessFileRelationbusinessId(int businessId)
  181. {
  182. string sql = "select * from businessfilerelation where BusinessFileId = '{businessId}'";
  183. return CurrDb.FindList<BusinessFileRelation>(sql);
  184. }
  185. public BusinessFile getBusinessFilebusinessId(int businessId)
  186. {
  187. return CurrDb.FindEntityFor<BusinessFile>(businessId);
  188. }
  189. /// <summary>
  190. /// 删除整个程序
  191. /// </summary>
  192. /// <param name="proName"></param>
  193. /// <param name="macId"></param>
  194. /// <param name="macModelId"></param>
  195. /// <param name="macModelCode"></param>
  196. /// <param name="errorinfo"></param>
  197. /// <returns></returns>
  198. public int DeleteWholeProgram(string proName, int macId, int macModelId, string macModelCode, ref string errorinfo)
  199. {
  200. var businessFile = CurrDb.FindListForCondition<BusinessFile>($" and a.OrgId={macModelId} and OrgTypeID=2 " +
  201. $" and a.FName='{proName}'", ref errorinfo)
  202. .FirstOrDefault();
  203. var businessId = businessFile.ID;
  204. var sql = $"delete from businessfilerelation where BusinessFileId='{businessId}'";
  205. if (CurrDb.ExecuteBySql(sql) < 0)
  206. {
  207. errorinfo = "删除版次失败";
  208. return -1;
  209. }
  210. sql = $"delete from businessfile where id={businessId}";
  211. if (CurrDb.ExecuteBySql(sql) < 0)
  212. {
  213. errorinfo = "删除机型程序表失败";
  214. return -1;
  215. }
  216. return 1;
  217. }
  218. /// <summary>
  219. /// 机台复制程序产生新程序
  220. /// </summary>
  221. /// <param name="proId"></param>
  222. /// <param name="newProName"></param>
  223. /// <param name="errorinfo"></param>
  224. /// <returns></returns>
  225. public int CopyProgramWithNewName(string proName, string newProName, int macModelId, string userCode, ref string errorinfo)
  226. {
  227. using (IDatabase db = DbFactory.Base("eap"))
  228. {
  229. db.BeginTrans();
  230. var businessFile = CurrDb.FindListForCondition<BusinessFile>($" and a.OrgId={macModelId} and a.OrgTypeID=2 " +
  231. $" and a.FName='{proName}'",
  232. ref errorinfo).FirstOrDefault();
  233. if (businessFile == null)
  234. {
  235. errorinfo = "该程序不存在或已被删除";
  236. return -1;
  237. }
  238. var temp = db.FindListForCondition<BusinessFile>($" and a.OrgId={macModelId} and a.OrgTypeID=2 and a.FName='{newProName}'",
  239. ref errorinfo);
  240. if (temp != null && temp.Count() > 0)
  241. {
  242. errorinfo = "程序名已存在";
  243. return -1;
  244. }
  245. businessFile.FName = newProName;
  246. if (CurrDb.InsertFor(businessFile, userCode) < 0)
  247. {
  248. errorinfo = "复制失败";
  249. return -1;
  250. }
  251. var sql = $"select @@identity;";
  252. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  253. var mac = CurrDb.FindListForCondition<Machine>($" and a.MModeID={macModelId}",
  254. ref errorinfo).FirstOrDefault();
  255. var versions = CurrDb.FindListForCondition<BusinessFileRelation>($" and a.businessFileId={businessFile.ID}", ref errorinfo);
  256. if (versions != null && versions.Count() > 0)
  257. {
  258. foreach (var item in versions)
  259. {
  260. var newPro = new BusinessFileRelation()
  261. {
  262. BusinessFileID = id,
  263. FileInfoID = item.FileInfoID,
  264. Version = item.Version,
  265. IsDefault = item.IsDefault,
  266. RecTime = DateTime.Now,
  267. ModTime = DateTime.Now
  268. };
  269. if (CurrDb.InsertFor(newPro, userCode) < 0)
  270. {
  271. errorinfo = "程序信息保存数据库失败";
  272. db.Rollback();
  273. return -1;
  274. }
  275. }
  276. }
  277. var program = CurrDb.FindListForCondition<ProgramMst>($" and a.FName='{newProName}' " +
  278. $"and a.ModelID='{macModelId}' and a.ProcessCode='{mac.PCode}' ",
  279. ref errorinfo).FirstOrDefault();
  280. if (program == null)
  281. {
  282. var proMst = new ProgramMst
  283. {
  284. FName = newProName,
  285. ModelID = mac.MModeID,
  286. ProcessCode = mac.PCode,
  287. RecCode = userCode,
  288. RecTime = DateTime.Now,
  289. ModCode = userCode,
  290. ModTime = DateTime.Now,
  291. Remark = $"从机型【{mac.MModeCode}】上复制而来"
  292. };
  293. if (CurrDb.InsertFor(proMst, userCode) < 0)
  294. {
  295. errorinfo = "程序表新增数据失败";
  296. return -1;
  297. }
  298. }
  299. db.Commit();
  300. return 1;
  301. }
  302. }
  303. /// <summary>
  304. /// 将机台程序设置为机型通用程序
  305. /// </summary>
  306. /// <param name="macCode"></param>
  307. /// <param name="userCode"></param>
  308. /// <param name="errorinfo"></param>
  309. /// <returns></returns>
  310. public int CopyMacProgrmToModel(int macId, int macModelId, string userCode, string fName, ref string errorinfo)
  311. {
  312. using (IDatabase db = DbFactory.Base("eap"))
  313. {
  314. db.BeginTrans();
  315. var macPrograms = CurrDb.FindListForCondition<BusinessFile>($" and a.OrgTypeID=1 and a.OrgID={macId} and FName='{fName}'", ref errorinfo);
  316. if (macPrograms == null || macPrograms.Count() <= 0)
  317. {
  318. errorinfo = "该机台下没有程序";
  319. return -1;
  320. }
  321. foreach (var item in macPrograms)
  322. {
  323. var temp = CurrDb.FindListForCondition<BusinessFile>($" and a.FName='{item.FName}' " +
  324. $"and a.OrgTypeID=2 and a.OrgId={macModelId}", ref errorinfo).FirstOrDefault();
  325. var id = 0;
  326. if (temp != null)
  327. {
  328. id = temp.ID;
  329. }
  330. else
  331. {
  332. var newPro = new BusinessFile()
  333. {
  334. FName = item.FName,
  335. ModTime = DateTime.Now,
  336. OrgID = macModelId,
  337. OrgTypeID = 2,
  338. RecTime = DateTime.Now,
  339. };
  340. if (CurrDb.InsertFor(newPro, userCode) < 0)
  341. {
  342. errorinfo = "程序信息保存数据库失败";
  343. db.Rollback();
  344. return -1;
  345. }
  346. var sql = $"select @@identity;";
  347. id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  348. }
  349. var versions = CurrDb.FindListForCondition<BusinessFileRelation>($" and a.businessFileId={item.ID}", ref errorinfo);
  350. if (versions != null && versions.Count() > 0)
  351. {
  352. //var exists = CurrDb.FindListForCondition<BusinessFileRelation>($" and b.FName='{item.FName}' " +
  353. // $"and b.OrgTypeID=2 and b.OrgId={macModelId}", ref errorinfo);
  354. var exists = CurrDb.FindListForCondition<BusinessFileRelation>($" and a.businessFileId={id}", ref errorinfo);
  355. if (exists != null && exists.Count() > 0)
  356. {
  357. var sql = $"delete from BusinessFileRelation where id in ({string.Join(",", exists.Select(c => c.ID))})";
  358. if (CurrDb.ExecuteBySql(sql) < 0)
  359. {
  360. errorinfo = "删除原程序关系失败";
  361. return -1;
  362. }
  363. }
  364. foreach (var v in versions)
  365. {
  366. var obj = new BusinessFileRelation
  367. {
  368. FileInfoID = v.FileInfoID,
  369. IsDefault = v.IsDefault,
  370. Version = v.Version,
  371. BusinessFileID = id,
  372. RecTime = DateTime.Now,
  373. ModTime = DateTime.Now
  374. };
  375. if (CurrDb.InsertFor(obj, userCode) < 0)
  376. {
  377. db.Rollback();
  378. errorinfo = "新增失败";
  379. return -1;
  380. }
  381. }
  382. }
  383. var machine = CurrDb.FindEntityFor<Machine>(macId);
  384. var program = CurrDb.FindListForCondition<ProgramMst>($" and a.FName='{item.FName}' " +
  385. $"and a.ModelID='{macModelId}' and a.ProcessCode='{machine.PCode}' ",
  386. ref errorinfo).FirstOrDefault();
  387. if (program == null)
  388. {
  389. var proMst = new ProgramMst
  390. {
  391. FName = item.FName,
  392. ModelID = machine.MModeID,
  393. ProcessCode = machine.PCode,
  394. RecCode = userCode,
  395. RecTime = DateTime.Now,
  396. ModCode = userCode,
  397. ModTime = DateTime.Now,
  398. Remark = $"从机型【{machine.MModeCode}】上复制而来"
  399. };
  400. if (CurrDb.InsertFor(proMst, userCode) < 0)
  401. {
  402. errorinfo = "程序表新增数据失败";
  403. return -1;
  404. }
  405. }
  406. }
  407. db.Commit();
  408. return 1;
  409. }
  410. }
  411. public IEnumerable<MacModelProgram> GetMacModelProgramVersionList(int start, int length, string order, string sort, string filter, string errorinfo)
  412. {
  413. if (!string.IsNullOrEmpty(filter))
  414. filter = filter.Replace("%2B", "+");
  415. string sql = $@"select a.ID,a.BusinessFileID,a.FileInfoID,a.Version,a.IsDefault,a.remark,a.RecTime,
  416. b.OrgID,b.OrgTypeID,b.FName ProgramName,c.UploadMacID,c.FName FileOrgName,c.remark FileInfoRemark,
  417. d.FCode UploadMacCode,e.FCode MacModelCode,e.FName MacModelName,e.Id MacModelId
  418. from businessfilerelation a
  419. left join businessfile b on a.BusinessFileId =b.id
  420. left join tfileinfo c on a.FileInfoId =c.id
  421. left join machine d on c.UploadMacID =d.id
  422. left join macmodel e on b.OrgId=e.Id
  423. where b.OrgTypeID=2 {filter}
  424. order by {sort} {order} limit {start - 1},{length} ";
  425. return CurrDb.FindList<MacModelProgram>(sql);
  426. }
  427. public int GetMacModelProgramVersionListCount(string filter)
  428. {
  429. if (!string.IsNullOrEmpty(filter))
  430. filter = filter.Replace("%2B", "+");
  431. string sql = $@"select count(1) from businessfilerelation a
  432. left join businessfile b on a.BusinessFileId =b.id
  433. where b.OrgTypeID=2 {filter}";
  434. return Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "0");
  435. }
  436. }
  437. }