ReadLot2Thread.cs 22 KB

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