ReadLot2Thread.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using EInk.Models;
  2. using EInk.Dtos;
  3. using SqlSugar;
  4. using System.ComponentModel;
  5. using Newtonsoft.Json;
  6. using System.Collections.Generic;
  7. using EInk.Tools;
  8. using Microsoft.Extensions.FileSystemGlobbing.Internal;
  9. using System.Linq;
  10. namespace EInk.TaskThread
  11. {
  12. public class ReadLot2Thread
  13. {
  14. public void ReadThreadStart()
  15. {
  16. Thread t = new Thread(ReadTask);
  17. t.Start();
  18. }
  19. static void ReadTask(Object dblist)
  20. {
  21. IConfiguration _configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
  22. var db = new SqlSugarClient(new ConnectionConfig()
  23. {
  24. DbType = DbType.PostgreSQL,
  25. ConnectionString = _configuration.GetValue<string>("TargetDbConnectionString"),
  26. IsAutoCloseConnection = true
  27. });
  28. var local_db = new SqlSugarClient(new ConnectionConfig()
  29. {
  30. DbType = DbType.PostgreSQL,
  31. ConnectionString = _configuration.GetValue<string>("LocalDbConnectionString"),
  32. IsAutoCloseConnection = true
  33. });
  34. string topic = _configuration.GetValue<string>("Topic");
  35. List<string> patternlist = _configuration.GetValue<string>("Lot2PatternList").Split(",").ToList();
  36. int product_startid = _configuration.GetValue<int>("ProductStartID");
  37. ISqlSugarClient _db = (ISqlSugarClient)db;
  38. ISqlSugarClient _local_db = (ISqlSugarClient)local_db;
  39. while (true)
  40. {
  41. try
  42. {
  43. List<InitLot2Model> read_list = new List<InitLot2Model>();
  44. read_list = _db.Queryable<InitLot2Model>().Where(x => x.id > product_startid).Where(x => (x.productno != null && x.station != null && x.pattern != null) ).OrderBy(x => x.id).Take(120).ToList();
  45. if (read_list != null && read_list.Count > 0)
  46. {
  47. try
  48. {
  49. _local_db.Ado.BeginTran();
  50. foreach (var read_lot2 in read_list)
  51. {
  52. product_startid=read_lot2.id;
  53. if (string.IsNullOrEmpty(read_lot2.productno) || string.IsNullOrEmpty(read_lot2.station) || string.IsNullOrEmpty(read_lot2.result))
  54. {
  55. continue;
  56. }
  57. List<InitLot2DetailDto> lot2JsonList = new List<InitLot2DetailDto>();
  58. try
  59. {
  60. //校验json格式
  61. lot2JsonList = JsonConvert.DeserializeObject<List<InitLot2DetailDto>>(read_lot2.detail);
  62. }
  63. catch (Exception ex)
  64. {
  65. LogerHelper.RecordLogTxt("ReadLot2Task,read_lot2Json有误" + ex.Message + "," + JsonConvert.SerializeObject(read_lot2));
  66. }
  67. List<Lot2Lot3Model> Lot2ModelList = _local_db.Queryable<Lot2Lot3Model>().Where(x => x.product_no == read_lot2.productno).ToList();
  68. if (Lot2ModelList == null || Lot2ModelList.Count == 0)
  69. {
  70. Lot2Lot3Dto lot2Detail = new()
  71. {
  72. id = new GuidDto().Id,
  73. slidesNumber = read_lot2.productno.Split('_')[0],
  74. topic = topic,
  75. createTime = read_lot2.created_at.ToString(),
  76. result = read_lot2.result == "enOK" ? "OK" : "NG",
  77. itemCheckDatas = new List<itemCheckData>()
  78. };
  79. itemCheckData lot2item = new itemCheckData()
  80. {
  81. station = read_lot2.station,
  82. method = "1",
  83. pattern = read_lot2.pattern,
  84. result = read_lot2.result == "enOK" ? "OK" : "NG",
  85. details = new List<detail>()
  86. };
  87. if (lot2JsonList != null && lot2JsonList.Count > 0)
  88. {
  89. foreach (var item in lot2JsonList)
  90. {
  91. string defectCode = item.Classification;
  92. string defectName = "null";
  93. if (!string.IsNullOrEmpty(item.Classification.Trim()))
  94. {
  95. List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
  96. if (defectCodelist != null && defectCodelist.Count != 0)
  97. {
  98. foreach (var item1 in defectCodelist)
  99. {
  100. if (item1.classification.Split(',').Contains(item.Classification))
  101. {
  102. defectCode = item1.defect_code;
  103. defectName = item1.defect_name;
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. lot2item.details.Add(new detail()
  110. {
  111. defectCode = defectCode,
  112. defectName = defectName,
  113. x = item.CenterX,
  114. y = item.CenterY,
  115. width = item.SideXLength,
  116. height = item.SideYLength,
  117. area = "0",
  118. rotation = item.Rotation
  119. });
  120. }
  121. }
  122. lot2Detail.itemCheckDatas.Add(lot2item);
  123. Lot2Lot3Model lot2model = new()
  124. {
  125. lot_id = lot2Detail.id,
  126. is_send = 2,
  127. content = JsonConvert.SerializeObject(lot2Detail),
  128. create_time = DateTime.Now,
  129. product_no = read_lot2.productno,
  130. product_id = read_lot2.id,
  131. operation_time = read_lot2.created_at
  132. };
  133. _local_db.Insertable(lot2model).ExecuteCommand();
  134. }
  135. else
  136. {
  137. if (Lot2ModelList[0].is_send == 2)
  138. {
  139. Lot2ModelList[0].create_time = DateTime.Now;
  140. Lot2Lot3Dto lot2Detail = new Lot2Lot3Dto();
  141. try
  142. {
  143. lot2Detail = JsonConvert.DeserializeObject<Lot2Lot3Dto>(Lot2ModelList[0].content);
  144. }
  145. catch (Exception ex)
  146. {
  147. LogerHelper.RecordLogTxt("ReadLot2Task,lot2DetailJson有误" + ex.Message + "," + JsonConvert.SerializeObject(Lot2ModelList[0].content));
  148. }
  149. //is_send:0、等待发送;1、已发送;2、等待补充信息;3、异常数据;
  150. lot2Detail.createTime = read_lot2.created_at.ToString();
  151. bool send_flag = true;
  152. foreach (var item in patternlist)
  153. {
  154. bool flag = false;
  155. if (item == read_lot2.pattern)
  156. {
  157. flag = true;
  158. }
  159. else
  160. {
  161. foreach (var item1 in lot2Detail.itemCheckDatas)
  162. {
  163. if (item == item1.pattern)
  164. {
  165. flag = true;
  166. break;
  167. }
  168. }
  169. }
  170. send_flag = send_flag & flag;
  171. }
  172. Lot2ModelList[0].is_send = send_flag ? 0 : 2;
  173. Lot2ModelList[0].product_id = read_lot2.id;
  174. Lot2ModelList[0].operation_time = read_lot2.created_at;
  175. if (Lot2ModelList[0].is_send == 0)
  176. {
  177. if (string.IsNullOrEmpty(lot2Detail.slidesNumber))
  178. {
  179. Lot2ModelList[0].is_send = 3;
  180. }
  181. }
  182. if (read_lot2.result != "enOK")
  183. {
  184. lot2Detail.result = "NG";
  185. }
  186. itemCheckData lot2item = new itemCheckData()
  187. {
  188. station = read_lot2.station,
  189. method = "1",
  190. pattern = read_lot2.pattern,
  191. result = read_lot2.result == "enOK" ? "OK" : "NG",
  192. details = new List<detail>()
  193. };
  194. if (lot2JsonList != null && lot2JsonList.Count > 0)
  195. {
  196. foreach (var item in lot2JsonList)
  197. {
  198. string defectCode = item.Classification;
  199. string defectName = "null";
  200. if (!string.IsNullOrEmpty(item.Classification.Trim()))
  201. {
  202. List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
  203. if (defectCodelist != null && defectCodelist.Count != 0)
  204. {
  205. foreach (var item1 in defectCodelist)
  206. {
  207. if (item1.classification.Split(',').Contains(item.Classification))
  208. {
  209. defectCode = item1.defect_code;
  210. defectName = item1.defect_name;
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. lot2item.details.Add(new detail()
  217. {
  218. defectCode = defectCode,
  219. defectName = defectName,
  220. x = item.CenterX,
  221. y = item.CenterY,
  222. width = item.SideXLength,
  223. height = item.SideYLength,
  224. area = "0",
  225. rotation = item.Rotation
  226. });
  227. }
  228. }
  229. lot2Detail.itemCheckDatas.Add(lot2item);
  230. Lot2ModelList[0].content = JsonConvert.SerializeObject(lot2Detail);
  231. _local_db.Updateable<Lot2Lot3Model>(Lot2ModelList[0]).WhereColumns(s => s.id).ExecuteCommand();
  232. }
  233. else
  234. {
  235. Lot2Lot3Dto lot2Detail = new()
  236. {
  237. id = new GuidDto().Id,
  238. slidesNumber = read_lot2.productno.Split('_')[0],
  239. topic = topic,
  240. createTime = read_lot2.created_at.ToString(),
  241. result = read_lot2.result == "enOK" ? "OK" : "NG",
  242. itemCheckDatas = new List<itemCheckData>()
  243. };
  244. itemCheckData lot2item = new itemCheckData()
  245. {
  246. station = read_lot2.station,
  247. method = "1",
  248. pattern = read_lot2.pattern,
  249. result = read_lot2.result == "enOK" ? "OK" : "NG",
  250. details = new List<detail>()
  251. };
  252. if (lot2JsonList != null && lot2JsonList.Count > 0)
  253. {
  254. foreach (var item in lot2JsonList)
  255. {
  256. string defectCode = item.Classification;
  257. string defectName = "null";
  258. if (!string.IsNullOrEmpty(item.Classification.Trim()))
  259. {
  260. List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
  261. if (defectCodelist != null && defectCodelist.Count != 0)
  262. {
  263. foreach (var item1 in defectCodelist)
  264. {
  265. if (item1.classification.Split(',').Contains(item.Classification))
  266. {
  267. defectCode = item1.defect_code;
  268. defectName = item1.defect_name;
  269. break;
  270. }
  271. }
  272. }
  273. }
  274. lot2item.details.Add(new detail()
  275. {
  276. defectCode = defectCode,
  277. defectName = defectName,
  278. x = item.CenterX,
  279. y = item.CenterY,
  280. width = item.SideXLength,
  281. height = item.SideYLength,
  282. area = "0",
  283. rotation = item.Rotation
  284. });
  285. }
  286. }
  287. lot2Detail.itemCheckDatas.Add(lot2item);
  288. Lot2Lot3Model lot2model = new()
  289. {
  290. lot_id = lot2Detail.id,
  291. is_send = 3,
  292. content = JsonConvert.SerializeObject(lot2Detail),
  293. create_time = DateTime.Now,
  294. product_no = read_lot2.productno,
  295. product_id = read_lot2.id,
  296. operation_time = read_lot2.created_at
  297. };
  298. _local_db.Insertable(lot2model).ExecuteCommand();
  299. }
  300. }
  301. }
  302. _local_db.Ado.CommitTran();
  303. _configuration.GetValue<int>("ProductStartID");
  304. }
  305. catch (Exception ex)
  306. {
  307. LogerHelper.RecordLogTxt($"ReadLot2Task[1], StackTrace:{ex.StackTrace}, Message:{ex.Message}");
  308. _local_db.Ado.RollbackTran();
  309. }
  310. }
  311. }
  312. catch (Exception ex)
  313. {
  314. LogerHelper.RecordLogTxt($"ReadLot2Task[2], StackTrace:{ex.StackTrace}, Message:{ex.Message}");
  315. }
  316. finally
  317. {
  318. Thread.Sleep(500);
  319. }
  320. }
  321. }
  322. }
  323. }