ReadLot2Thread.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. private readonly ISqlSugarClient _db;
  15. private readonly ISqlSugarClient _local_db;
  16. public ReadLot2Thread(ISqlSugarClient db, ISqlSugarClient local_db)
  17. {
  18. _db = db;
  19. _local_db = local_db;
  20. }
  21. public void ReadThreadStart()
  22. {
  23. Thread t = new Thread(ReadTask);
  24. t.Start((_db, _local_db));
  25. }
  26. static void ReadTask(Object dblist)
  27. {
  28. var (db, local_db) = ((ISqlSugarClient, ISqlSugarClient))dblist;
  29. IConfiguration _configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
  30. string topic = _configuration.GetValue<string>("Topic");
  31. List<string> patternlist = _configuration.GetValue<string>("PatternList").Split(",").ToList();
  32. int product_startid = _configuration.GetValue<int>("ProductStartID");
  33. ISqlSugarClient _db = (ISqlSugarClient)db;
  34. ISqlSugarClient _local_db = (ISqlSugarClient)local_db;
  35. while (true)
  36. {
  37. try
  38. {
  39. int max_productid = _local_db.Ado.GetInt("select max(product_id) from lot2lot3_info");
  40. List<InitLot2Model> read_list = new List<InitLot2Model>();
  41. if (product_startid == 0)
  42. {
  43. read_list = _db.Queryable<InitLot2Model>().Where(x => x.id > max_productid).Where(x => (x.productno != null && x.station != null && x.pattern != null) && (x.result == "enOK" || x.result == "enNG")).OrderBy(x => x.id).Take(30).ToList();
  44. }
  45. else
  46. {
  47. read_list = _db.Queryable<InitLot2Model>().Where(x => x.id >= product_startid).Where(x => x.id > max_productid).Where(x => (x.productno != null && x.station != null && x.pattern != null) && (x.result == "enOK" || x.result == "enNG")).Take(60).OrderBy(x => x.id).Take(30).ToList();
  48. }
  49. if (read_list != null && read_list.Count > 0)
  50. {
  51. try
  52. {
  53. _local_db.Ado.BeginTran();
  54. foreach (var read_lot2 in read_list)
  55. {
  56. if (string.IsNullOrEmpty(read_lot2.productno) || string.IsNullOrEmpty(read_lot2.station) || string.IsNullOrEmpty(read_lot2.result))
  57. {
  58. continue;
  59. }
  60. List<InitLot2DetailDto> lot2JsonList = new List<InitLot2DetailDto>();
  61. try
  62. {
  63. //校验json格式
  64. lot2JsonList = JsonConvert.DeserializeObject<List<InitLot2DetailDto>>(read_lot2.detail);
  65. }
  66. catch (Exception ex)
  67. {
  68. LogerHelper.RecordLogTxt("ReadLot2Task,read_lot2Json有误" + ex.Message + "," + JsonConvert.SerializeObject(read_lot2));
  69. }
  70. List<Lot2Lot3Model> Lot2ModelList = _local_db.Queryable<Lot2Lot3Model>().Where(x => x.product_no == read_lot2.productno).ToList();
  71. if (Lot2ModelList == null || Lot2ModelList.Count == 0)
  72. {
  73. Lot2Lot3Dto lot2Detail = new()
  74. {
  75. id = new GuidDto().Id,
  76. slidesNumber = read_lot2.productno.Split('_')[0],
  77. topic = topic,
  78. createTime = read_lot2.created_at.ToString(),
  79. result = read_lot2.result == "enOK" ? "OK" : "NG",
  80. itemCheckDatas = new List<itemCheckData>()
  81. };
  82. itemCheckData lot2item = new itemCheckData()
  83. {
  84. station = read_lot2.station,
  85. method = "1",
  86. pattern = read_lot2.pattern,
  87. result = read_lot2.result == "enOK" ? "OK" : "NG",
  88. details = new List<detail>()
  89. };
  90. if (lot2JsonList != null && lot2JsonList.Count > 0)
  91. {
  92. foreach (var item in lot2JsonList)
  93. {
  94. string defectCode = "null";
  95. string defectName = "null";
  96. if (!string.IsNullOrEmpty(item.Classification.Trim()))
  97. {
  98. List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
  99. //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().Where(x => x.defect_name == item.Classification).ToList();
  100. if (defectCodelist != null && defectCodelist.Count != 0)
  101. {
  102. foreach (var defect_info in defectCodelist)
  103. {
  104. foreach (var defect_classification in defect_info.classification.Split(','))
  105. {
  106. if (item.Classification == defect_classification)
  107. {
  108. defectCode = defect_info.defect_code;
  109. defectName = defect_info.defect_name;
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. lot2item.details.Add(new detail()
  117. {
  118. defectCode = defectCode,
  119. defectName = defectName,
  120. x = item.ContourRect_X,
  121. y = item.ContourRect_Y,
  122. width = item.ContourRect_Width,
  123. height = item.ContourRect_Height,
  124. area = item.ContourRect_Area
  125. });
  126. }
  127. }
  128. lot2Detail.itemCheckDatas.Add(lot2item);
  129. Lot2Lot3Model lot2model = new()
  130. {
  131. lot_id = lot2Detail.id,
  132. is_send = 2,
  133. content = JsonConvert.SerializeObject(lot2Detail),
  134. create_time = DateTime.Now,
  135. product_no = read_lot2.productno,
  136. product_id = read_lot2.id
  137. };
  138. _local_db.Insertable(lot2model).ExecuteCommand();
  139. }
  140. else
  141. {
  142. if (Lot2ModelList[0].is_send == 2)
  143. {
  144. Lot2ModelList[0].create_time = DateTime.Now;
  145. Lot2Lot3Dto lot2Detail = new Lot2Lot3Dto();
  146. try
  147. {
  148. lot2Detail = JsonConvert.DeserializeObject<Lot2Lot3Dto>(Lot2ModelList[0].content);
  149. }
  150. catch (Exception ex)
  151. {
  152. LogerHelper.RecordLogTxt("ReadLot2Task,lot2DetailJson有误" + ex.Message + "," + JsonConvert.SerializeObject(Lot2ModelList[0].content));
  153. }
  154. //is_send:0、等待发送;1、已发送;2、等待补充信息;3、重复插入;
  155. lot2Detail.createTime = read_lot2.created_at.ToString();
  156. bool send_flag = true;
  157. foreach (var item in patternlist)
  158. {
  159. bool flag = false;
  160. if (item == read_lot2.pattern)
  161. {
  162. flag = true;
  163. }
  164. else
  165. {
  166. foreach (var item1 in lot2Detail.itemCheckDatas)
  167. {
  168. if (item == item1.pattern)
  169. {
  170. flag = true;
  171. break;
  172. }
  173. }
  174. }
  175. send_flag = send_flag & flag;
  176. }
  177. Lot2ModelList[0].is_send = send_flag ? 0 : 2;
  178. Lot2ModelList[0].product_id = read_lot2.id;
  179. if (read_lot2.result == "enNG")
  180. {
  181. lot2Detail.result = "NG";
  182. }
  183. itemCheckData lot2item = new itemCheckData()
  184. {
  185. station = read_lot2.station,
  186. method = "1",
  187. pattern = read_lot2.pattern,
  188. result = read_lot2.result == "enOK" ? "OK" : "NG",
  189. details = new List<detail>()
  190. };
  191. if (lot2JsonList != null && lot2JsonList.Count > 0)
  192. {
  193. foreach (var item in lot2JsonList)
  194. {
  195. string defectCode = "null";
  196. string defectName = "null";
  197. if (!string.IsNullOrEmpty(item.Classification.Trim()))
  198. {
  199. List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
  200. //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().Where(x => x.defect_name == item.Classification).ToList();
  201. if (defectCodelist != null && defectCodelist.Count != 0)
  202. {
  203. foreach (var defect_info in defectCodelist)
  204. {
  205. foreach (var defect_classification in defect_info.classification.Split(','))
  206. {
  207. if (item.Classification == defect_classification)
  208. {
  209. defectCode = defect_info.defect_code;
  210. defectName = defect_info.defect_name;
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. lot2item.details.Add(new detail()
  218. {
  219. defectCode = defectCode,
  220. defectName = defectName,
  221. x = item.ContourRect_X,
  222. y = item.ContourRect_Y,
  223. width = item.ContourRect_Width,
  224. height = item.ContourRect_Height,
  225. area = item.ContourRect_Area
  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 = "null";
  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. //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().Where(x => x.defect_name == item.Classification).ToList();
  262. if (defectCodelist != null && defectCodelist.Count != 0)
  263. {
  264. foreach (var defect_info in defectCodelist)
  265. {
  266. foreach (var defect_classification in defect_info.classification.Split(','))
  267. {
  268. if (item.Classification == defect_classification)
  269. {
  270. defectCode = defect_info.defect_code;
  271. defectName = defect_info.defect_name;
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. lot2item.details.Add(new detail()
  279. {
  280. defectCode = defectCode,
  281. defectName = defectName,
  282. x = item.ContourRect_X,
  283. y = item.ContourRect_Y,
  284. width = item.ContourRect_Width,
  285. height = item.ContourRect_Height,
  286. area = item.ContourRect_Area
  287. });
  288. }
  289. }
  290. lot2Detail.itemCheckDatas.Add(lot2item);
  291. Lot2Lot3Model lot2model = new()
  292. {
  293. lot_id = lot2Detail.id,
  294. is_send = 3,
  295. content = JsonConvert.SerializeObject(lot2Detail),
  296. create_time = DateTime.Now,
  297. product_no = read_lot2.productno,
  298. product_id = read_lot2.id
  299. };
  300. _local_db.Insertable(lot2model).ExecuteCommand();
  301. }
  302. }
  303. Thread.Sleep(500);
  304. }
  305. _local_db.Ado.CommitTran();
  306. }
  307. catch (Exception ex)
  308. {
  309. LogerHelper.RecordLogTxt("ReadLot2Task[1]," + ex.Message);
  310. _local_db.Ado.RollbackTran();
  311. }
  312. }
  313. }
  314. catch (Exception ex)
  315. {
  316. LogerHelper.RecordLogTxt("ReadLot2Task[2]," + ex.Message);
  317. }
  318. finally
  319. {
  320. Thread.Sleep(5000);
  321. }
  322. }
  323. }
  324. }
  325. }