using Admin.Core.Common;
using Admin.Core.IService;
using Admin.Core.Model;
using Admin.Core.Model.Model_New;
using log4net;
using Serilog;
using StackExchange.Profiling.Internal;
using System.Collections.Generic;
using System.Timers;
///
/// 测温数据采集
///
namespace Aucma.Core.TemperatureTask
{
public class AucamTemperatureMeasurementService: IAucamTemperatureMeasurementService
{
public delegate Task RefreshBoxFoamDataDelegate();
public static event RefreshBoxFoamDataDelegate? RefreshBoxFoamDataDelegateEvent;
private static readonly log4net.ILog logHelper = LogManager.GetLogger(typeof(AucamTemperatureMeasurementService));
private readonly ITestedCodeMESServices _codeMESServices;
private readonly ITemperatureHistoryServices _temperatureHistoryServices;
private readonly IMaterialCompletionServices _materialCompletionServices;
private readonly IPrintBarCodeServices _printBarCodeServices;
private readonly IReportQualityInspectionServices _reportQualityInspectionServices;
System.Timers.Timer timer1 = new System.Timers.Timer(5000);
bool flag = true;
public AucamTemperatureMeasurementService(ITemperatureHistoryServices temperatureHistoryServices,
ITestedCodeMESServices codeMESServices, IMaterialCompletionServices materialCompletionServices,
IPrintBarCodeServices printBarCodeServices, IReportQualityInspectionServices reportQualityInspectionServices)
{
_codeMESServices = codeMESServices;
_temperatureHistoryServices = temperatureHistoryServices;
_materialCompletionServices = materialCompletionServices;
_printBarCodeServices = printBarCodeServices;
_reportQualityInspectionServices = reportQualityInspectionServices;
}
public void Execute()
{
timer1.Elapsed += new System.Timers.ElapsedEventHandler(Run); //到达时间的时候执行事件;
timer1.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
timer1.Enabled = true;//需要调用 timer.Start()或者timer.Enabled = true来启动它,
timer1.Start();//timer.Start()的内部原理还是设置timer.Enabled = true;
}
#region 采集任务处理
///
/// 采集任务处理
///
///
private async void Run(object? sender, ElapsedEventArgs e)
{
try
{
if (flag)
{
try
{
flag = false;
var historyList = _codeMESServices.QueryAsync(d => d.IsSync == 0).Result;
if (historyList == null || historyList.Count == 0) return;
Console.WriteLine($"【{DateTime.Now}】开始读取测温数据");
Console.WriteLine(historyList.ToJson());
var query = (from a in historyList
select new TemperatureHistory
{
Line_No = a.Line_No,
GongwNo = a.GongwNo,
CpNo = a.CpNo,
CpModel = a.CpModel,
FactoryModel = a.factoryModel,
Voltage = a.Voltage,
Frequency = a.Frequency,
Point2 = a.Point2,
TestTime = a.TestTime,
TestedTime = a.TestedTime,
Etemp = a.Etemp,
BeginDateTime = Convert.ToDateTime(a.BeginDateTime),
CpResult = a.CpResult,
EndDateTime = Convert.ToDateTime(a.EndDateTime),
UserName = a.UserName,
Remark = a.Remark,
TestNo = a.TestNo,
Temp_Mes = a.Temp_Mes,
Power_Mes = a.Power_Mes,
Part_Mes = a.Part_Mes,
LowV_Time = a.LowV_Time,
LowV_PowerMin = a.LowV_PowerMin,
LowV_PowerMax = a.LowV_PowerMax,
LowV_PowerFact = a.LowV_PowerFact,
LowVoltage = a.LowVoltage,
SyncDate = DateTime.Now,
IsFlag = 0
}).ToList();
if (query == null || query.Count == 0) return;
logHelper.Error($"读取数据个数:{historyList.Count()}");
Console.WriteLine($"【{DateTime.Now}】读取数据个数:{historyList.Count()}");
var result = _temperatureHistoryServices.AddAsync(query).Result;
if (result > 0)
{
historyList.ForEach(d => d.IsSync = 1);
int r = _codeMESServices.UpateTemperatureHistoryFlag(historyList);
if (r > 0)
{
InsertToCompleteAndCheck(query);//保存过点数据
ToMes();//更新测温质检数据
Console.WriteLine($"【{DateTime.Now}】保存测温数据成功!保存数量:{query.Count}");
}
}
Console.WriteLine($"【{DateTime.Now}】测温数据同步成功!数量:{query.Count}");
}
catch (Exception ex)
{
Console.WriteLine($"【{DateTime.Now}】 {ex.Message}");
logHelper.Error(ex.Message);
}
finally { flag = true; }
}
}
catch (Exception ex)
{
logHelper.Error($"测温数据处理异常:{ex.Message}");
Console.WriteLine($"测温数据处理异常:{ex.Message}");
}
}
#endregion
#region 保存过点数和测温质检
///
/// 保存过点数和测温质检
///
///
///
public bool InsertToCompleteAndCheck(List list)
{
List completionList = new List();
var printBarCodeList = _printBarCodeServices.QueryAsync().Result;
foreach (var temperatureHistory in list)
{
PrintBarCode barcode = printBarCodeList.First(d => d.MaterialBarcode == temperatureHistory.CpNo);
if (barcode == null) return false;
#region 更新过点数据
MaterialCompletion completion = new MaterialCompletion();
completion.OrderCode = barcode.OrderCode;
completion.MaterialBarcode = temperatureHistory.CpNo;
completion.MaterialCode = barcode.MaterialCode;
completion.MaterialName = barcode.MaterialName;
string stationName = Appsettings.app("Common", "StationName");
completion.StationName = stationName;
completion.ProductLineCode = "CX_02";
completion.CompleteDate = DateTime.Now;
completionList.Add(completion);
#endregion
}
int r = _materialCompletionServices.AddAsync(completionList).Result;
if (r > 0) return true;
else return false;
}
#endregion
#region 发送质检请求
///
/// 发送质检请求
///
///
///
public void ToMes()
{
try
{
var list = _temperatureHistoryServices.QueryAsync(d => d.IsFlag == 0).Result;
if (list == null || list.Count == 0) return;
Console.WriteLine($"【{DateTime.Now}】开始向质检发送测温数据");
foreach (var temperatureHistory in list)
{
//var r= _reportQualityInspectionServices.FirstAsync(d => d.BarCode == temperatureHistory.CpNo && d.TreatmentMeasure == "3"&& d.StationCode=="2009").Result;
//if (r != null)
//{
// var checkList = _temperatureHistoryServices.GetAllIsFlag(temperatureHistory.ObjId).Result;
// if (checkList != null)
// {
// var check = checkList.First();
// r.FinishTime = check.FinishTime;
// r.BarCode = check.BarCode;
// r.MaterialName = check.MaterialName;
// r.ProcessCode = check.ProcessCode;
// r.TestItemCode = check.TestItemCode;
// r.QualityDefectCode = check.QualityDefectCode;
// r.QualityDefectName = check.QualityDefectName;
// r.TreatmentMeasure = check.TreatmentMeasure;
// r.ProcessResult = check.ProcessResult;
// r.GroupCode = check.GroupCode;
// r.InspectorCode = check.InspectorCode;
// r.InspectorTime = check.InspectorTime;
// r.ReworkNumber = check.ReworkNumber;
// r.IsFlag = check.IsFlag;
// r.StationCode = check.StationCode;
// r.UpdatedTime = check.UpdateTime;
// bool reportResult = _reportQualityInspectionServices.UpdateAsync(r).Result;
// if (reportResult)
// {
// temperatureHistory.IsFlag = 1;
// _ = _temperatureHistoryServices.UpdateAsync(temperatureHistory).Result;
// }
// }
//}
//else
{
var checkList = _temperatureHistoryServices.GetAllIsFlag(temperatureHistory.ObjId).Result;
if (checkList != null)
{
var check = checkList.First();
ReportQualityInsPection report = new ReportQualityInsPection();
if (check.TreatmentMeasure == "3")
{
report.FinishTime = null;
report.UpdatedTime = null;
}
else
{
//report.FinishTime = check.FinishTime;
report.UpdatedTime = null;
}
report.BarCode = check.BarCode;
report.MaterialName = check.MaterialName;
report.ProcessCode = check.ProcessCode;
report.TestItemCode = check.TestItemCode;
report.QualityDefectCode = check.QualityDefectCode;
report.QualityDefectName = check.QualityDefectName;
report.TreatmentMeasure = check.TreatmentMeasure;
//report.ProcessResult = check.ProcessResult;
report.GroupCode = check.GroupCode;
report.InspectorCode = check.InspectorCode;
report.InspectorTime = check.InspectorTime;
//report.ReworkNumber = check.ReworkNumber;
//report.IsFlag = check.IsFlag;
report.StationCode = check.StationCode;
report.UpdatedTime = check.UpdateTime;
int reportResult = _reportQualityInspectionServices.AddAsync(report).Result;
if (reportResult > 0)
{
temperatureHistory.IsFlag = 1;
_ = _temperatureHistoryServices.UpdateAsync(temperatureHistory).Result;
}
}
}
}
Console.WriteLine($"【{DateTime.Now}】更新测温质检数据成功!");
}
catch (Exception ex)
{
Console.WriteLine($"【{DateTime.Now}】更新测温质检数据失败!原因:{ex.Message}");
}
}
#endregion
}
}