ReadLot2Thread.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 (Lot2ModelList[0].is_send == 0)
  180. {
  181. if (string.IsNullOrEmpty(lot2Detail.slidesNumber))
  182. {
  183. Lot2ModelList[0].is_send = 3;
  184. }
  185. }
  186. if (read_lot2.result == "enNG")
  187. {
  188. lot2Detail.result = "NG";
  189. }
  190. itemCheckData lot2item = new itemCheckData()
  191. {
  192. station = read_lot2.station,
  193. method = "1",
  194. pattern = read_lot2.pattern,
  195. result = read_lot2.result == "enOK" ? "OK" : "NG",
  196. details = new List<detail>()
  197. };
  198. if (lot2JsonList != null && lot2JsonList.Count > 0)
  199. {
  200. foreach (var item in lot2JsonList)
  201. {
  202. string defectCode = "null";
  203. string defectName = "null";
  204. if (!string.IsNullOrEmpty(item.Classification.Trim()))
  205. {
  206. List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
  207. //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().Where(x => x.defect_name == item.Classification).ToList();
  208. if (defectCodelist != null && defectCodelist.Count != 0)
  209. {
  210. foreach (var defect_info in defectCodelist)
  211. {
  212. foreach (var defect_classification in defect_info.classification.Split(','))
  213. {
  214. if (item.Classification == defect_classification)
  215. {
  216. defectCode = defect_info.defect_code;
  217. defectName = defect_info.defect_name;
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. lot2item.details.Add(new detail()
  225. {
  226. defectCode = defectCode,
  227. defectName = defectName,
  228. x = item.ContourRect_X,
  229. y = item.ContourRect_Y,
  230. width = item.ContourRect_Width,
  231. height = item.ContourRect_Height,
  232. area = item.ContourRect_Area
  233. });
  234. }
  235. }
  236. lot2Detail.itemCheckDatas.Add(lot2item);
  237. Lot2ModelList[0].content = JsonConvert.SerializeObject(lot2Detail);
  238. _local_db.Updateable<Lot2Lot3Model>(Lot2ModelList[0]).WhereColumns(s => s.id).ExecuteCommand();
  239. }
  240. else
  241. {
  242. Lot2Lot3Dto lot2Detail = new()
  243. {
  244. id = new GuidDto().Id,
  245. slidesNumber = read_lot2.productno.Split('_')[0],
  246. topic = topic,
  247. createTime = read_lot2.created_at.ToString(),
  248. result = read_lot2.result == "enOK" ? "OK" : "NG",
  249. itemCheckDatas = new List<itemCheckData>()
  250. };
  251. itemCheckData lot2item = new itemCheckData()
  252. {
  253. station = read_lot2.station,
  254. method = "1",
  255. pattern = read_lot2.pattern,
  256. result = read_lot2.result == "enOK" ? "OK" : "NG",
  257. details = new List<detail>()
  258. };
  259. if (lot2JsonList != null && lot2JsonList.Count > 0)
  260. {
  261. foreach (var item in lot2JsonList)
  262. {
  263. string defectCode = "null";
  264. string defectName = "null";
  265. if (!string.IsNullOrEmpty(item.Classification.Trim()))
  266. {
  267. List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
  268. //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().Where(x => x.defect_name == item.Classification).ToList();
  269. if (defectCodelist != null && defectCodelist.Count != 0)
  270. {
  271. foreach (var defect_info in defectCodelist)
  272. {
  273. foreach (var defect_classification in defect_info.classification.Split(','))
  274. {
  275. if (item.Classification == defect_classification)
  276. {
  277. defectCode = defect_info.defect_code;
  278. defectName = defect_info.defect_name;
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. }
  285. lot2item.details.Add(new detail()
  286. {
  287. defectCode = defectCode,
  288. defectName = defectName,
  289. x = item.ContourRect_X,
  290. y = item.ContourRect_Y,
  291. width = item.ContourRect_Width,
  292. height = item.ContourRect_Height,
  293. area = item.ContourRect_Area
  294. });
  295. }
  296. }
  297. lot2Detail.itemCheckDatas.Add(lot2item);
  298. Lot2Lot3Model lot2model = new()
  299. {
  300. lot_id = lot2Detail.id,
  301. is_send = 3,
  302. content = JsonConvert.SerializeObject(lot2Detail),
  303. create_time = DateTime.Now,
  304. product_no = read_lot2.productno,
  305. product_id = read_lot2.id
  306. };
  307. _local_db.Insertable(lot2model).ExecuteCommand();
  308. }
  309. }
  310. Thread.Sleep(1000);
  311. }
  312. _local_db.Ado.CommitTran();
  313. }
  314. catch (Exception ex)
  315. {
  316. LogerHelper.RecordLogTxt("ReadLot2Task[1]," + ex.Message);
  317. _local_db.Ado.RollbackTran();
  318. }
  319. }
  320. }
  321. catch (Exception ex)
  322. {
  323. LogerHelper.RecordLogTxt("ReadLot2Task[2]," + ex.Message);
  324. }
  325. finally
  326. {
  327. Thread.Sleep(5000);
  328. }
  329. }
  330. }
  331. }
  332. }