MachineMaterialService.cs 8.0 KB

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