ReadLot2Thread.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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>("PatternList").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. int max_productid = _local_db.Ado.GetInt("select max(product_id) from lot2lot3_info");
  44. List<InitLot2Model> read_list = new List<InitLot2Model>();
  45. if (product_startid == 0)
  46. {
  47. 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();
  48. }
  49. else
  50. {
  51. 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();
  52. }
  53. if (read_list != null && read_list.Count > 0)
  54. {
  55. try
  56. {
  57. _local_db.Ado.BeginTran();
  58. foreach (var read_lot2 in read_list)
  59. {
  60. if (string.IsNullOrEmpty(read_lot2.productno) || string.IsNullOrEmpty(read_lot2.station) || string.IsNullOrEmpty(read_lot2.result))
  61. {
  62. continue;
  63. }
  64. List<InitLot2DetailDto> lot2JsonList = new List<InitLot2DetailDto>();
  65. try
  66. {
  67. //校验json格式
  68. lot2JsonList = JsonConvert.DeserializeObject<List<InitLot2DetailDto>>(read_lot2.detail);
  69. }
  70. catch (Exception ex)
  71. {
  72. LogerHelper.RecordLogTxt("ReadLot2Task,read_lot2Json有误" + ex.Message + "," + JsonConvert.SerializeObject(read_lot2));
  73. }
  74. List<Lot2Lot3Model> Lot2ModelList = _local_db.Queryable<Lot2Lot3Model>().Where(x => x.product_no == read_lot2.productno).ToList();
  75. if (Lot2ModelList == null || Lot2ModelList.Count == 0)
  76. {
  77. Lot2Lot3Dto lot2Detail = new()
  78. {
  79. id = new GuidDto().Id,
  80. slidesNumber = read_lot2.productno.Split('_')[0],
  81. topic = topic,
  82. createTime = read_lot2.created_at.ToString(),
  83. result = read_lot2.result == "enOK" ? "OK" : "NG",
  84. itemCheckDatas = new List<itemCheckData>()
  85. };
  86. itemCheckData lot2item = new itemCheckData()
  87. {
  88. station = read_lot2.station,
  89. method = "1",
  90. pattern = read_lot2.pattern,
  91. result = read_lot2.result == "enOK" ? "OK" : "NG",
  92. details = new List<detail>()
  93. };
  94. if (lot2JsonList != null && lot2JsonList.Count > 0)
  95. {
  96. foreach (var item in lot2JsonList)
  97. {
  98. string defectCode = "null";
  99. string defectName = "null";
  100. if (!string.IsNullOrEmpty(item.Classification.Trim()))
  101. {
  102. List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
  103. //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().Where(x => x.defect_name == item.Classification).ToList();
  104. if (defectCodelist != null && defectCodelist.Count != 0)
  105. {
  106. foreach (var defect_info in defectCodelist)
  107. {
  108. foreach (var defect_classification in defect_info.classification.Split(','))
  109. {
  110. if (item.Classification == defect_classification)
  111. {
  112. defectCode = defect_info.defect_code;
  113. defectName = defect_info.defect_name;
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. lot2item.details.Add(new detail()
  121. {
  122. defectCode = defectCode,
  123. defectName = defectName,
  124. x = item.ContourRect_X,
  125. y = item.ContourRect_Y,
  126. width = item.ContourRect_Width,
  127. height = item.ContourRect_Height,
  128. area = item.ContourRect_Area
  129. });
  130. }
  131. }
  132. lot2Detail.itemCheckDatas.Add(lot2item);
  133. Lot2Lot3Model lot2model = new()
  134. {
  135. lot_id = lot2Detail.id,
  136. is_send = 2,
  137. content = JsonConvert.SerializeObject(lot2Detail),
  138. create_time = DateTime.Now,
  139. product_no = read_lot2.productno,
  140. product_id = read_lot2.id
  141. };
  142. _local_db.Insertable(lot2model).ExecuteCommand();
  143. }
  144. else
  145. {
  146. if (Lot2ModelList[0].is_send == 2)
  147. {
  148. Lot2ModelList[0].create_time = DateTime.Now;
  149. Lot2Lot3Dto lot2Detail = new Lot2Lot3Dto();
  150. try
  151. {
  152. lot2Detail = JsonConvert.DeserializeObject<Lot2Lot3Dto>(Lot2ModelList[0].content);
  153. }
  154. catch (Exception ex)
  155. {
  156. LogerHelper.RecordLogTxt("ReadLot2Task,lot2DetailJson有误" + ex.Message + "," + JsonConvert.SerializeObject(Lot2ModelList[0].content));
  157. }
  158. //is_send:0、等待发送;1、已发送;2、等待补充信息;3、异常数据;
  159. lot2Detail.createTime = read_lot2.created_at.ToString();
  160. bool send_flag = true;
  161. foreach (var item in patternlist)
  162. {
  163. bool flag = false;
  164. if (item == read_lot2.pattern)
  165. {
  166. flag = true;
  167. }
  168. else
  169. {
  170. foreach (var item1 in lot2Detail.itemCheckDatas)
  171. {
  172. if (item == item1.pattern)
  173. {
  174. flag = true;
  175. break;
  176. }
  177. }
  178. }
  179. send_flag = send_flag & flag;
  180. }
  181. Lot2ModelList[0].is_send = send_flag ? 0 : 2;
  182. Lot2ModelList[0].product_id = read_lot2.id;
  183. if (Lot2ModelList[0].is_send == 0)
  184. {
  185. if (string.IsNullOrEmpty(lot2Detail.slidesNumber))
  186. {
  187. Lot2ModelList[0].is_send = 3;
  188. }
  189. }
  190. if (read_lot2.result == "enNG")
  191. {
  192. lot2Detail.result = "NG";
  193. }
  194. itemCheckData lot2item = new itemCheckData()
  195. {
  196. station = read_lot2.station,
  197. method = "1",
  198. pattern = read_lot2.pattern,
  199. result = read_lot2.result == "enOK" ? "OK" : "NG",
  200. details = new List<detail>()
  201. };
  202. if (lot2JsonList != null && lot2JsonList.Count > 0)
  203. {
  204. foreach (var item in lot2JsonList)
  205. {
  206. string defectCode = "null";
  207. string defectName = "null";
  208. if (!string.IsNullOrEmpty(item.Classification.Trim()))
  209. {
  210. List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
  211. //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().Where(x => x.defect_name == item.Classification).ToList();
  212. if (defectCodelist != null && defectCodelist.Count != 0)
  213. {
  214. foreach (var defect_info in defectCodelist)
  215. {
  216. foreach (var defect_classification in defect_info.classification.Split(','))
  217. {
  218. if (item.Classification == defect_classification)
  219. {
  220. defectCode = defect_info.defect_code;
  221. defectName = defect_info.defect_name;
  222. break;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. lot2item.details.Add(new detail()
  229. {
  230. defectCode = defectCode,
  231. defectName = defectName,
  232. x = item.ContourRect_X,
  233. y = item.ContourRect_Y,
  234. width = item.ContourRect_Width,
  235. height = item.ContourRect_Height,
  236. area = item.ContourRect_Area
  237. });
  238. }
  239. }
  240. lot2Detail.itemCheckDatas.Add(lot2item);
  241. Lot2ModelList[0].content = JsonConvert.SerializeObject(lot2Detail);
  242. _local_db.Updateable<Lot2Lot3Model>(Lot2ModelList[0]).WhereColumns(s => s.id).ExecuteCommand();
  243. }
  244. else
  245. {
  246. Lot2Lot3Dto lot2Detail = new()
  247. {
  248. id = new GuidDto().Id,
  249. slidesNumber = read_lot2.productno.Split('_')[0],
  250. topic = topic,
  251. createTime = read_lot2.created_at.ToString(),
  252. result = read_lot2.result == "enOK" ? "OK" : "NG",
  253. itemCheckDatas = new List<itemCheckData>()
  254. };
  255. itemCheckData lot2item = new itemCheckData()
  256. {
  257. station = read_lot2.station,
  258. method = "1",
  259. pattern = read_lot2.pattern,
  260. result = read_lot2.result == "enOK" ? "OK" : "NG",
  261. details = new List<detail>()
  262. };
  263. if (lot2JsonList != null && lot2JsonList.Count > 0)
  264. {
  265. foreach (var item in lot2JsonList)
  266. {
  267. string defectCode = "null";
  268. string defectName = "null";
  269. if (!string.IsNullOrEmpty(item.Classification.Trim()))
  270. {
  271. List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
  272. //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().Where(x => x.defect_name == item.Classification).ToList();
  273. if (defectCodelist != null && defectCodelist.Count != 0)
  274. {
  275. foreach (var defect_info in defectCodelist)
  276. {
  277. foreach (var defect_classification in defect_info.classification.Split(','))
  278. {
  279. if (item.Classification == defect_classification)
  280. {
  281. defectCode = defect_info.defect_code;
  282. defectName = defect_info.defect_name;
  283. break;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. lot2item.details.Add(new detail()
  290. {
  291. defectCode = defectCode,
  292. defectName = defectName,
  293. x = item.ContourRect_X,
  294. y = item.ContourRect_Y,
  295. width = item.ContourRect_Width,
  296. height = item.ContourRect_Height,
  297. area = item.ContourRect_Area
  298. });
  299. }
  300. }
  301. lot2Detail.itemCheckDatas.Add(lot2item);
  302. Lot2Lot3Model lot2model = new()
  303. {
  304. lot_id = lot2Detail.id,
  305. is_send = 3,
  306. content = JsonConvert.SerializeObject(lot2Detail),
  307. create_time = DateTime.Now,
  308. product_no = read_lot2.productno,
  309. product_id = read_lot2.id
  310. };
  311. _local_db.Insertable(lot2model).ExecuteCommand();
  312. }
  313. }
  314. Thread.Sleep(1000);
  315. }
  316. _local_db.Ado.CommitTran();
  317. }
  318. catch (Exception ex)
  319. {
  320. LogerHelper.RecordLogTxt($"ReadLot2Task[1], StackTrace:{ex.StackTrace}, Message:{ex.Message}");
  321. _local_db.Ado.RollbackTran();
  322. }
  323. }
  324. }
  325. catch (Exception ex)
  326. {
  327. LogerHelper.RecordLogTxt($"ReadLot2Task[2], StackTrace:{ex.StackTrace}, Message:{ex.Message}");
  328. }
  329. finally
  330. {
  331. Thread.Sleep(10000);
  332. }
  333. }
  334. }
  335. }
  336. }