using Mesnac.Action.ChemicalWeighing.Entity.Report; using Mesnac.Codd.Session; using System; using System.Collections.Generic; using System.Data; using System.Threading.Tasks; using System.Web.Script.Serialization; namespace Mesnac.Action.ChemicalWeighing.FinishBatch.SaveHelper { public sealed class ReportGelDoserSaveHelper { private static readonly Lazy lazy = new Lazy(() => new ReportGelDoserSaveHelper()); public static ReportGelDoserSaveHelper Instance { get { return lazy.Value; } } private ReportGelDoserSaveHelper() { } /// /// 糊化机报表数据保存 /// public void SaveGelDoserReport() { try { ICSharpCode.Core.LoggingService.Debug("糊化机报表数据保存开始!!!"); //准备信号读取 //if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 1) //{ // Report_GelDoser gelDoser_dos0 = readGelDoser("A"); //读取报表数据 // SaveReportDb(gelDoser_dos0); //保存报表数据 //} //else if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 2) //{ // Report_GelDoser gelDoser_dos0 = readGelDoser("B"); //读取报表数据 // SaveReportDb(gelDoser_dos0); //保存报表数据 //} //else if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 3) //{ // Report_GelDoser gelDoser_dos0 = readGelDoser("C"); //读取报表数据 // SaveReportDb(gelDoser_dos0); //保存报表数据 //} //else if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 4) //{ // Report_GelDoser gelDoser_dos0 = readGelDoser("D"); //读取报表数据 // SaveReportDb(gelDoser_dos0); //保存报表数据 //} Report_GelDoser gelDoser_dos0 = readGelDoser("A"); //读取报表数据 SaveReportDb(gelDoser_dos0); //保存报表数据 } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("糊化机报表数据保存异常:{0}", ex.Message), ex); } } /// /// 通过PLC读取糊化机报表数据 /// /// 糊化机A、糊化机B /// 操作步骤:Dos[0]、Dos[1]、Dos[2]、Dos[3] /// private Report_GelDoser readGelDoser(string machineType) { Report_GelDoser _gelDoser = null; try { _gelDoser = new Report_GelDoser(); _gelDoser.dos = readGelDoserDos(machineType, 6); _gelDoser.mix = readGelDoserMix(machineType, 10); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("通过PLC读取糊化机报表数读取异常:{0}", ex.Message), ex); } return _gelDoser; } /// /// 读取糊化机Dos数据 /// /// /// /// private List readGelDoserDos(string machineType, int amount) { List gelDoserDoes = new List(); for (int dos = 0; dos < amount; dos++) { #region 实例化参数 if (dos < 4) { BasePlcHelper.Instance.geler_dos_eqNo = new Entity.DataKeyValue($"gelDosing{machineType}_dos{dos}_eqNo"); BasePlcHelper.Instance.geler_dos_batch = new Entity.DataKeyValue($"gelDosing{machineType}_dos{dos}_batch"); BasePlcHelper.Instance.geler_dos_matCode = new Entity.DataKeyValue($"gelDosing{machineType}_dos{dos}_matCode"); BasePlcHelper.Instance.geler_dos_setValue = new Entity.DataKeyValue($"gelDosing{machineType}_dos{dos}_setValue"); BasePlcHelper.Instance.geler_dos_setToler = new Entity.DataKeyValue($"gelDosing{machineType}_dos{dos}_setToler"); BasePlcHelper.Instance.geler_dos_actValue = new Entity.DataKeyValue($"gelDosing{machineType}_dos{dos}_actValue"); BasePlcHelper.Instance.geler_dos_actToLer = new Entity.DataKeyValue($"gelDosing{machineType}_dos{dos}_actToLer"); } else if (dos == 4) { BasePlcHelper.Instance.geler_dos_eqNo = new Entity.DataKeyValue($"gelDosing{machineType}_hotWater_eqNo"); BasePlcHelper.Instance.geler_dos_batch = new Entity.DataKeyValue($"gelDosing{machineType}_hotWater_batch"); BasePlcHelper.Instance.geler_dos_matCode = new Entity.DataKeyValue($"gelDosing{machineType}_hotWater_matCode"); BasePlcHelper.Instance.geler_dos_setValue = new Entity.DataKeyValue($"gelDosing{machineType}_hotWater_setValue"); BasePlcHelper.Instance.geler_dos_setToler = new Entity.DataKeyValue($"gelDosing{machineType}_hotWater_setToler"); BasePlcHelper.Instance.geler_dos_actValue = new Entity.DataKeyValue($"gelDosing{machineType}_hotWater_actValue"); BasePlcHelper.Instance.geler_dos_actToLer = new Entity.DataKeyValue($"gelDosing{machineType}_hotWater_actToLer"); } else if(dos == 5) { BasePlcHelper.Instance.geler_dos_eqNo = new Entity.DataKeyValue($"gelDosing{machineType}_coolWater_eqNo"); BasePlcHelper.Instance.geler_dos_batch = new Entity.DataKeyValue($"gelDosing{machineType}_coolWater_batch"); BasePlcHelper.Instance.geler_dos_matCode = new Entity.DataKeyValue($"gelDosing{machineType}_coolWater_matCode"); BasePlcHelper.Instance.geler_dos_setValue = new Entity.DataKeyValue($"gelDosing{machineType}_coolWater_setValue"); BasePlcHelper.Instance.geler_dos_setToler = new Entity.DataKeyValue($"gelDosing{machineType}_coolWater_setToler"); BasePlcHelper.Instance.geler_dos_actValue = new Entity.DataKeyValue($"gelDosing{machineType}_coolWater_actValue"); BasePlcHelper.Instance.geler_dos_actToLer = new Entity.DataKeyValue($"gelDosing{machineType}_coolWater_actToLer"); } #endregion GelDoserDos_Water gelDoserDos = new GelDoserDos_Water() { eqNo = BasePlcHelper.Instance.geler_dos_eqNo.NowValue.ToInt(), batch = BasePlcHelper.Instance.geler_dos_batch.NowValue.ToInt(), matCode = BasePlcHelper.Instance.geler_dos_matCode.NowValue.ToInt(), setValue = BasePlcHelper.Instance.geler_dos_setValue.NowValue.ToDouble(), setToler = BasePlcHelper.Instance.geler_dos_setToler.NowValue.ToDouble(), actValue = BasePlcHelper.Instance.geler_dos_actValue.NowValue.ToDouble(), actToLer = BasePlcHelper.Instance.geler_dos_actToLer.NowValue.ToDouble(), dos = dos, recordTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; gelDoserDoes.Add(gelDoserDos); } return gelDoserDoes; } /// /// 读取糊化机Mix数据 /// /// /// /// private List readGelDoserMix(string machineType, int amount) { List gelDoserMixes = new List(); for (int mix = 0; mix < amount; mix++) { #region 实例化参数 var eqNo = new Entity.DataKeyValue($"gelDosing{machineType}_gelMixing{mix}_eqNo"); var mixBatch = new Entity.DataKeyValue($"gelDosing{machineType}_gelMixing{mix}_mixBatch"); var mixStep = new Entity.DataKeyValue($"gelDosing{machineType}_gelMixing{mix}_mixStep"); var actCode = new Entity.DataKeyValue($"gelDosing{machineType}_gelMixing{mix}_actCode"); var mixTime = new Entity.DataKeyValue($"gelDosing{machineType}_gelMixing{mix}_mixTime"); var mixTemp = new Entity.DataKeyValue($"gelDosing{machineType}_gelMixing{mix}_mixTemp"); var mixSpeed = new Entity.DataKeyValue($"gelDosing{machineType}_gelMixing{mix}_mixSpeed"); #endregion GelDoserMix gelDoserMix = new GelDoserMix() { eqNo = eqNo.NowValue.ToInt(), mixBatch = mixBatch.NowValue.ToInt(), mixStep = mixStep.NowValue.ToInt(), actCode = actCode.NowValue.ToInt(), mixTime = mixTime.NowValue.ToInt(), mixTemp = mixTemp.NowValue.ToDouble(), mixSpeed = mixSpeed.NowValue.ToDouble(), mix = mix, recordTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; gelDoserMixes.Add(gelDoserMix); } return gelDoserMixes; } /// /// 保存糊化机报表数据至数据库 /// /// private void SaveReportDb(Report_GelDoser _gelDoser) { JavaScriptSerializer serializer = new JavaScriptSerializer(); try { string str = serializer.Serialize(_gelDoser); if (_gelDoser != null) { if (_gelDoser.dos != null) { InsertReportGelDoser(_gelDoser.report_Id, _gelDoser.dos); } if (_gelDoser.mix != null) { InsertReportGelDoserDetail(_gelDoser.report_Id, _gelDoser.mix); } } ICSharpCode.Core.LoggingService.Warn($"报表数据打印:{str}"); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("保存报表数据至数据库异常:{0}", ex.Message), ex); } } /// /// 保存糊化机报表数据 /// /// /// private void InsertReportGelDoser(Guid reportId, List gelDosers) { try { Task.Run(() => { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlStr = @"insert into Report_GelDoser (reportId, eqNo, batch, matCode, setValue, setToler, actValue, actToLer, dos) values (@reportId, @eqNo, @batch, @matCode, @setValue, @setToler, @actValue, @actToLer, @dos)"; foreach (GelDoserDos_Water _gelDoserDos in gelDosers) { if (_gelDoserDos.actValue == 0) continue; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlStr; dbHelper.AddParameter("@reportId", reportId); dbHelper.AddParameter("@eqNo", _gelDoserDos.eqNo); dbHelper.AddParameter("@batch", _gelDoserDos.batch); dbHelper.AddParameter("@matCode", _gelDoserDos.matCode); dbHelper.AddParameter("@setValue", _gelDoserDos.setValue); dbHelper.AddParameter("@setToler", _gelDoserDos.setToler); dbHelper.AddParameter("@actValue", _gelDoserDos.actValue); dbHelper.AddParameter("@actToLer", _gelDoserDos.actToLer); dbHelper.AddParameter("@dos", _gelDoserDos.dos); dbHelper.ExecuteNonQuery(); } }); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("保存糊化机报表数据异常:{0}", ex.Message), ex); } } /// /// 保存糊化机报表明细数据 /// /// /// private void InsertReportGelDoserDetail(Guid reportId, List gelDosers) { try { Task.Run(() => { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } string sqlStr = @"insert into Report_GelDoser_Detail (reportId, eqNo, mixBatch, mixStep, actCode, mixTime, mixTemp, mixSpeed, mix) values (@reportId, @eqNo, @mixBatch, @mixStep, @actCode, @mixTime, @mixTemp, @mixSpeed, @mix)"; foreach (GelDoserMix _gelDoserMix in gelDosers) { if (_gelDoserMix.actCode == 0) continue; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlStr; dbHelper.AddParameter("@reportId", reportId); dbHelper.AddParameter("@eqNo", _gelDoserMix.eqNo); dbHelper.AddParameter("@mixBatch", _gelDoserMix.mixBatch); dbHelper.AddParameter("@mixStep", _gelDoserMix.mixStep); dbHelper.AddParameter("@actCode", _gelDoserMix.actCode); dbHelper.AddParameter("@mixTime", _gelDoserMix.mixTime); dbHelper.AddParameter("@mixTemp", _gelDoserMix.mixTemp); dbHelper.AddParameter("@mixSpeed", _gelDoserMix.mixSpeed); dbHelper.AddParameter("@mix", _gelDoserMix.mix); dbHelper.ExecuteNonQuery(); } }); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("保存糊化机报表明细数据异常:{0}", ex.Message), ex); } } } }