123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- 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<string>("TargetDbConnectionString"),
- IsAutoCloseConnection = true
- });
- var local_db = new SqlSugarClient(new ConnectionConfig()
- {
- DbType = DbType.PostgreSQL,
- ConnectionString = _configuration.GetValue<string>("LocalDbConnectionString"),
- IsAutoCloseConnection = true
- });
- string topic = _configuration.GetValue<string>("Topic");
- List<string> patternlist = _configuration.GetValue<string>("Lot2PatternList").Split(",").ToList();
- int product_startid = _configuration.GetValue<int>("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<InitLot2Model> read_list = new List<InitLot2Model>();
- 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();
- //if (product_startid == 0)
- //{
- // 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();
- //}
- //else
- //{
- // 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();
- //}
- 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<InitLot2DetailDto> lot2JsonList = new List<InitLot2DetailDto>();
- try
- {
- //校验json格式
- lot2JsonList = JsonConvert.DeserializeObject<List<InitLot2DetailDto>>(read_lot2.detail);
- }
- catch (Exception ex)
- {
- LogerHelper.RecordLogTxt("ReadLot2Task,read_lot2Json有误" + ex.Message + "," + JsonConvert.SerializeObject(read_lot2));
- }
- List<Lot2Lot3Model> Lot2ModelList = _local_db.Queryable<Lot2Lot3Model>().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>()
- };
- itemCheckData lot2item = new itemCheckData()
- {
- station = read_lot2.station,
- method = "1",
- pattern = read_lot2.pattern,
- result = read_lot2.result == "enOK" ? "OK" : "NG",
- details = new List<detail>()
- };
- if (lot2JsonList != null && lot2JsonList.Count > 0)
- {
- foreach (var item in lot2JsonList)
- {
- string defectCode = "null";
- string defectName = "null";
- if (!string.IsNullOrEmpty(item.Classification.Trim()))
- {
- List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
- //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().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<Lot2Lot3Dto>(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<detail>()
- };
- if (lot2JsonList != null && lot2JsonList.Count > 0)
- {
- foreach (var item in lot2JsonList)
- {
- string defectCode = "null";
- string defectName = "null";
- if (!string.IsNullOrEmpty(item.Classification.Trim()))
- {
- List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
- //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().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<Lot2Lot3Model>(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>()
- };
- itemCheckData lot2item = new itemCheckData()
- {
- station = read_lot2.station,
- method = "1",
- pattern = read_lot2.pattern,
- result = read_lot2.result == "enOK" ? "OK" : "NG",
- details = new List<detail>()
- };
- if (lot2JsonList != null && lot2JsonList.Count > 0)
- {
- foreach (var item in lot2JsonList)
- {
- string defectCode = "null";
- string defectName = "null";
- if (!string.IsNullOrEmpty(item.Classification.Trim()))
- {
- List<DefectModel> defectCodelist = _local_db.SqlQueryable<DefectModel>("SELECT * FROM public.defect_info where classification ~* '" + item.Classification + "'").ToList();
- //List<DefectModel> defectCodelist = _local_db.Queryable<DefectModel>().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<int>("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);
- }
- }
- }
- }
- }
|