MachineMaterialService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using Cksoft.Unity.Log4NetConfig;
  5. using DllEapDal.OFILM;
  6. using DllEapEntity;
  7. using DllEapEntity.OFILM;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. namespace WebUpload.Controllers
  15. {
  16. public class MachineMaterialService
  17. {
  18. /// <summary>
  19. /// 存储单个文件
  20. /// </summary>
  21. /// <param name="db"></param>
  22. /// <param name="macFile"></param>
  23. /// <param name="errorinfo"></param>
  24. /// <returns></returns>
  25. public int UploadFile(IDatabase db, EapMacFile macFile, ref string errorinfo)
  26. {
  27. var mstDal = new MachineMaterialMstDal(db);
  28. var fileNameWithoutExt = Path.GetFileNameWithoutExtension(macFile.FileName);
  29. var date = fileNameWithoutExt.Substring(0, 8);
  30. var shift = fileNameWithoutExt.Substring(8, 1);
  31. var recipe = fileNameWithoutExt.Substring(9);
  32. if (string.IsNullOrEmpty(recipe))
  33. {
  34. errorinfo = "文件名不合法";
  35. LogHelper<MachineMaterialService>.LogError($"机台号[{macFile.MacCode}]的文件[{macFile.FileName}]保存失败," +
  36. $"原因为[{errorinfo}]", "AA文件解析", string.Empty);
  37. return -1;
  38. }
  39. date = Convert.ToDateTime(date.Insert(4, "-").Insert(7, "-")).ToString("yyyy-MM-dd");
  40. var mac = db.FindListForCondition<Machine>($" and a.FCode='{macFile.MacCode}'",
  41. ref errorinfo).FirstOrDefault();
  42. if (mac == null)
  43. {
  44. errorinfo = "机台未录入EAP系统";
  45. return -1;
  46. }
  47. var exist = mstDal.Get($" and b.FCode='{macFile.MacCode}' and a.FDate='{date}' and a.Shift='{shift}' " +
  48. $"and a.recipe = '{recipe}'", ref errorinfo).FirstOrDefault();
  49. if (exist != null && exist.Md5Val == macFile.Md5)
  50. {
  51. errorinfo = $"机台[{macFile.MacCode}]的文件[{macFile.FileName}]已存在";
  52. LogHelper<MachineMaterialService>.LogError($"机台号[{macFile.MacCode}]的文件[{macFile.FileName}]保存失败," +
  53. $"原因为{errorinfo}", "AA文件解析", string.Empty);
  54. return 1;
  55. }
  56. // 测试环境
  57. var rootPath = AppConfigurtaionServices.Configuration["AAMaterialWinSavePath"];
  58. //正式环境
  59. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  60. {
  61. rootPath = AppConfigurtaionServices.Configuration["AAMaterialSavePath"];
  62. }
  63. if (string.IsNullOrEmpty(rootPath))
  64. {
  65. errorinfo = "文件保存路径未设置";
  66. return -1;
  67. }
  68. var savePath = $"{rootPath}/{macFile.MacCode}";
  69. if (!Directory.Exists(savePath))
  70. {
  71. Directory.CreateDirectory(savePath);
  72. }
  73. var filePath = $"{savePath}/{macFile.FileName}";
  74. FileStream fs = new FileStream(filePath, FileMode.Create);
  75. var bytes = Encoding.UTF8.GetBytes(macFile.FileContent);
  76. fs.Write(bytes, 0, bytes.Length);
  77. fs.Dispose();
  78. fs.Close();
  79. if (exist != null && exist.Md5Val != macFile.Md5)
  80. {
  81. var sql = $"DELETE FROM MachineMaterialDetail where mstid={exist.Id}";
  82. if (db.ExecuteBySql(sql) < 0)
  83. {
  84. errorinfo = "删除原数据失败";
  85. return -1;
  86. }
  87. sql = $"DELETE FROM MaterialErrorMessage where MstId={exist.Id}";
  88. if (db.ExecuteBySql(sql) < 0)
  89. {
  90. errorinfo = "删除原数据失败";
  91. return -1;
  92. }
  93. sql = $"DELETE FROM MachineMaterialMst WHERE ID={exist.Id} ";
  94. if (db.ExecuteBySql(sql) < 0)
  95. {
  96. errorinfo = "删除原数据失败";
  97. LogHelper<MachineMaterialService>.LogError($"机台号[{macFile.MacCode}]的文件[{macFile.FileName}]保存失败," +
  98. $"原因为[{errorinfo}]", "AA文件解析", string.Empty);
  99. return -1;
  100. }
  101. }
  102. var mst = new MachineMaterialMst
  103. {
  104. MacId = mac.ID,
  105. FDate = Convert.ToDateTime(date),
  106. FilePath = filePath,
  107. IsAnalysised = -1,
  108. Shift = shift,
  109. Md5Val = macFile.Md5,
  110. Recipe = recipe
  111. };
  112. if (mstDal.Insert(mst, ref errorinfo) < 0)
  113. return -1;
  114. LogHelper<MachineMaterialService>.LogError($"机台号[{macFile.MacCode}]的文件[{macFile.FileName}]保存成功", "AA文件解析", string.Empty);
  115. return 1;
  116. }
  117. /// <summary>
  118. /// 批量存储
  119. /// </summary>
  120. /// <param name="model"></param>
  121. /// <param name="errorinfo"></param>
  122. /// <returns></returns>
  123. public int UploadTrans(EapMacUploadModel model, ref string errorinfo)
  124. {
  125. using (IDatabase db = DbFactory.Base("eap"))
  126. {
  127. db.BeginTrans();
  128. foreach (var item in model.Files)
  129. {
  130. if (this.UploadFile(db, item, ref errorinfo) < 0)
  131. {
  132. db.Rollback();
  133. return -1;
  134. }
  135. }
  136. db.Commit();
  137. return 1;
  138. }
  139. }
  140. public int UploadTableTrans(LHAMachineModel model, ref string errorinfo)
  141. {
  142. try
  143. {
  144. using (IDatabase db = DbFactory.Base("eap"))
  145. {
  146. db.BeginTrans();
  147. var mstDal = new CodelinkDal(db);
  148. LogHelper<MachineMaterialService>.LogError($"准备采集", "LHA数据采集", string.Empty);
  149. //string delSql = $@"delete from Postbondview where MacCode = '{model.MacCode}' and TIME<'{DateTime.Now.ToString("yyyy-MM-dd 00:00")}' and TIME> '{DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00")}'";
  150. //LogHelper<MachineMaterialService>.LogError("删除:"+delSql, "LHA数据采集", string.Empty);
  151. //db.ExecuteBySql(delSql);
  152. foreach (var item in model.Postbondviews)
  153. {
  154. if (mstDal.Insert(item, model.MacCode, ref errorinfo) < 0)
  155. {
  156. db.Rollback();
  157. return -1;
  158. }
  159. }
  160. db.Commit();
  161. LogHelper<MachineMaterialService>.LogError($"采集完成", "LHA数据采集", string.Empty);
  162. return 1;
  163. }
  164. }
  165. catch (Exception ex)
  166. {
  167. LogHelper<MachineMaterialService>.LogError(ex.ToString() + ex.StackTrace, "LHA数据采集", string.Empty);
  168. return -1;
  169. }
  170. }
  171. }
  172. }