using EInk.Models; using EInk.Dtos; using SqlSugar; using System.ComponentModel; using Newtonsoft.Json; using System.Collections.Generic; using EInk.Tools; using Microsoft.Extensions.FileSystemGlobbing.Internal; using System.Linq; namespace EInk.TaskThread { public class ReadLot2Thread { public void ReadThreadStart() { Thread t = new Thread(ReadTask); t.Start(); } static void ReadTask(Object dblist) { IConfiguration _configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build(); var db = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.PostgreSQL, ConnectionString = _configuration.GetValue("TargetDbConnectionString"), IsAutoCloseConnection = true }); var local_db = new SqlSugarClient(new ConnectionConfig() { DbType = DbType.PostgreSQL, ConnectionString = _configuration.GetValue("LocalDbConnectionString"), IsAutoCloseConnection = true }); string topic = _configuration.GetValue("Topic"); List patternlist = _configuration.GetValue("Lot2PatternList").Split(",").ToList(); int product_startid = _configuration.GetValue("ProductStartID"); ISqlSugarClient _db = (ISqlSugarClient)db; ISqlSugarClient _local_db = (ISqlSugarClient)local_db; while (true) { try { //int max_productid = _local_db.Ado.GetInt("select max(product_id) from lot2lot3_info"); List read_list = new List(); read_list = _db.Queryable().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(); //if (product_startid == 0) //{ // read_list = _db.Queryable().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(); //} //else //{ // read_list = _db.Queryable().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(); //} if (read_list != null && read_list.Count > 0) { try { _local_db.Ado.BeginTran(); foreach (var read_lot2 in read_list) { product_startid=read_lot2.id; if (string.IsNullOrEmpty(read_lot2.productno) || string.IsNullOrEmpty(read_lot2.station) || string.IsNullOrEmpty(read_lot2.result)) { continue; } List lot2JsonList = new List(); try { //校验json格式 lot2JsonList = JsonConvert.DeserializeObject>(read_lot2.detail); } catch (Exception ex) { LogerHelper.RecordLogTxt("ReadLot2Task,read_lot2Json有误" + ex.Message + "," + JsonConvert.SerializeObject(read_lot2)); } List Lot2ModelList = _local_db.Queryable().Where(x => x.product_no == read_lot2.productno).ToList(); if (Lot2ModelList == null || Lot2ModelList.Count == 0) { Lot2Lot3Dto lot2Detail = new() { id = new GuidDto().Id, slidesNumber = read_lot2.productno.Split('_')[0], topic = topic, createTime = read_lot2.created_at.ToString(), result = read_lot2.result == "enOK" ? "OK" : "NG", itemCheckDatas = new List() }; itemCheckData lot2item = new itemCheckData() { station = read_lot2.station, method = "1", pattern = read_lot2.pattern, result = read_lot2.result == "enOK" ? "OK" : "NG", details = new List() }; if (lot2JsonList != null && lot2JsonList.Count > 0) { foreach (var item in lot2JsonList) { string defectCode = "null"; string defectName = "null"; if (!string.IsNullOrEmpty(item.Classification.Trim())) { List defectCodelist = _local_db.SqlQueryable("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList(); //List defectCodelist = _local_db.Queryable().Where(x => x.defect_name == item.Classification).ToList(); if (defectCodelist != null && defectCodelist.Count != 0) { foreach (var defect_info in defectCodelist) { foreach (var defect_classification in defect_info.classification.Split(',')) { if (item.Classification == defect_classification) { defectCode = defect_info.defect_code; defectName = defect_info.defect_name; break; } } } } } lot2item.details.Add(new detail() { defectCode = defectCode, defectName = defectName, x = item.ContourRect_X, y = item.ContourRect_Y, width = item.ContourRect_Width, height = item.ContourRect_Height, area = item.ContourRect_Area }); } } lot2Detail.itemCheckDatas.Add(lot2item); Lot2Lot3Model lot2model = new() { lot_id = lot2Detail.id, is_send = 2, content = JsonConvert.SerializeObject(lot2Detail), create_time = DateTime.Now, product_no = read_lot2.productno, product_id = read_lot2.id, operation_time = read_lot2.created_at }; _local_db.Insertable(lot2model).ExecuteCommand(); } else { if (Lot2ModelList[0].is_send == 2) { Lot2ModelList[0].create_time = DateTime.Now; Lot2Lot3Dto lot2Detail = new Lot2Lot3Dto(); try { lot2Detail = JsonConvert.DeserializeObject(Lot2ModelList[0].content); } catch (Exception ex) { LogerHelper.RecordLogTxt("ReadLot2Task,lot2DetailJson有误" + ex.Message + "," + JsonConvert.SerializeObject(Lot2ModelList[0].content)); } //is_send:0、等待发送;1、已发送;2、等待补充信息;3、异常数据; lot2Detail.createTime = read_lot2.created_at.ToString(); bool send_flag = true; foreach (var item in patternlist) { bool flag = false; if (item == read_lot2.pattern) { flag = true; } else { foreach (var item1 in lot2Detail.itemCheckDatas) { if (item == item1.pattern) { flag = true; break; } } } send_flag = send_flag & flag; } Lot2ModelList[0].is_send = send_flag ? 0 : 2; Lot2ModelList[0].product_id = read_lot2.id; Lot2ModelList[0].operation_time = read_lot2.created_at; if (Lot2ModelList[0].is_send == 0) { if (string.IsNullOrEmpty(lot2Detail.slidesNumber)) { Lot2ModelList[0].is_send = 3; } } if (read_lot2.result == "enNG") { lot2Detail.result = "NG"; } itemCheckData lot2item = new itemCheckData() { station = read_lot2.station, method = "1", pattern = read_lot2.pattern, result = read_lot2.result == "enOK" ? "OK" : "NG", details = new List() }; if (lot2JsonList != null && lot2JsonList.Count > 0) { foreach (var item in lot2JsonList) { string defectCode = "null"; string defectName = "null"; if (!string.IsNullOrEmpty(item.Classification.Trim())) { List defectCodelist = _local_db.SqlQueryable("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList(); //List defectCodelist = _local_db.Queryable().Where(x => x.defect_name == item.Classification).ToList(); if (defectCodelist != null && defectCodelist.Count != 0) { foreach (var defect_info in defectCodelist) { foreach (var defect_classification in defect_info.classification.Split(',')) { if (item.Classification == defect_classification) { defectCode = defect_info.defect_code; defectName = defect_info.defect_name; break; } } } } } lot2item.details.Add(new detail() { defectCode = defectCode, defectName = defectName, x = item.ContourRect_X, y = item.ContourRect_Y, width = item.ContourRect_Width, height = item.ContourRect_Height, area = item.ContourRect_Area }); } } lot2Detail.itemCheckDatas.Add(lot2item); Lot2ModelList[0].content = JsonConvert.SerializeObject(lot2Detail); _local_db.Updateable(Lot2ModelList[0]).WhereColumns(s => s.id).ExecuteCommand(); } else { Lot2Lot3Dto lot2Detail = new() { id = new GuidDto().Id, slidesNumber = read_lot2.productno.Split('_')[0], topic = topic, createTime = read_lot2.created_at.ToString(), result = read_lot2.result == "enOK" ? "OK" : "NG", itemCheckDatas = new List() }; itemCheckData lot2item = new itemCheckData() { station = read_lot2.station, method = "1", pattern = read_lot2.pattern, result = read_lot2.result == "enOK" ? "OK" : "NG", details = new List() }; if (lot2JsonList != null && lot2JsonList.Count > 0) { foreach (var item in lot2JsonList) { string defectCode = "null"; string defectName = "null"; if (!string.IsNullOrEmpty(item.Classification.Trim())) { List defectCodelist = _local_db.SqlQueryable("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList(); //List defectCodelist = _local_db.Queryable().Where(x => x.defect_name == item.Classification).ToList(); if (defectCodelist != null && defectCodelist.Count != 0) { foreach (var defect_info in defectCodelist) { foreach (var defect_classification in defect_info.classification.Split(',')) { if (item.Classification == defect_classification) { defectCode = defect_info.defect_code; defectName = defect_info.defect_name; break; } } } } } lot2item.details.Add(new detail() { defectCode = defectCode, defectName = defectName, x = item.ContourRect_X, y = item.ContourRect_Y, width = item.ContourRect_Width, height = item.ContourRect_Height, area = item.ContourRect_Area }); } } lot2Detail.itemCheckDatas.Add(lot2item); Lot2Lot3Model lot2model = new() { lot_id = lot2Detail.id, is_send = 3, content = JsonConvert.SerializeObject(lot2Detail), create_time = DateTime.Now, product_no = read_lot2.productno, product_id = read_lot2.id, operation_time = read_lot2.created_at }; _local_db.Insertable(lot2model).ExecuteCommand(); } } Thread.Sleep(200); } _local_db.Ado.CommitTran(); _configuration.GetValue("ProductStartID"); } catch (Exception ex) { LogerHelper.RecordLogTxt($"ReadLot2Task[1], StackTrace:{ex.StackTrace}, Message:{ex.Message}"); _local_db.Ado.RollbackTran(); } } } catch (Exception ex) { LogerHelper.RecordLogTxt($"ReadLot2Task[2], StackTrace:{ex.StackTrace}, Message:{ex.Message}"); } finally { Thread.Sleep(10000); } } } } }