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 ReportWetMixerSaveHelper { private static readonly Lazy lazy = new Lazy(() => new ReportWetMixerSaveHelper()); public static ReportWetMixerSaveHelper Instance { get { return lazy.Value; } } private ReportWetMixerSaveHelper() { } /// /// 湿混机报表数据保存 /// public void SaveWetMixerReport() { try { ICSharpCode.Core.LoggingService.Debug("湿混机报表数据保存开始!!!"); //准备信号读取 //if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 1) //{ // Report_WetMixer wetMixer_dos0 = readWetMixer("A"); //读取报表数据 // SaveReportDb(wetMixer_dos0); //保存报表数据 //} //else if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 2) //{ // Report_WetMixer wetMixer_dos0 = readWetMixer("B"); //读取报表数据 // SaveReportDb(wetMixer_dos0); //保存报表数据 //} //else if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 3) //{ // Report_WetMixer wetMixer_dos0 = readWetMixer("C"); //读取报表数据 // SaveReportDb(wetMixer_dos0); //保存报表数据 //} //else if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 4) //{ // Report_WetMixer wetMixer_dos0 = readWetMixer("D"); //读取报表数据 // SaveReportDb(wetMixer_dos0); //保存报表数据 //} Report_WetMixer WetMixer_dos0 = readWetMixer("A"); //读取报表数据 SaveReportDb(WetMixer_dos0); //保存报表数据 } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("湿混机报表数据保存异常:{0}", ex.Message), ex); } } /// /// 通过PLC读取湿混机报表数据 /// /// 湿混机A、湿混机B /// private Report_WetMixer readWetMixer(string machineType) { Report_WetMixer _wetMixer = null; try { _wetMixer = new Report_WetMixer(); _wetMixer.wetmixer = readWetMixers(machineType); _wetMixer.mix = readWetMixerMix(machineType, 10); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("通过PLC读取湿混机报表数读取异常:{0}", ex.Message), ex); } return _wetMixer; } /// /// 读取湿混机数据 /// /// /// /// private WetMixerDevice readWetMixers(string machineType) { var eqNo = new Entity.DataKeyValue($"wetMixing{machineType}_1_eqNo"); WetMixerDevice wetMixer = new WetMixerDevice() { eqNo = eqNo.NowValue.ToInt(), recordTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; return wetMixer; } /// /// 读取湿混机Mix数据 /// /// /// /// private List readWetMixerMix(string machineType, int amount) { List wetMixerMixes = new List(); for (int mix = 0; mix < 10; mix++) { #region 实例化参数 var eqNo = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_eqNo"); var mixBatch = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_mixBatch"); var mixStep = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_mixStep"); var actCode = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_actCode"); var mixTime = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_mixTime"); var mixTemp = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_mixTemp"); var mixSpeed = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_mixSpeed"); #endregion WetMixerMix wetMixerMix = new WetMixerMix() { 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") }; wetMixerMixes.Add(wetMixerMix); } return wetMixerMixes; } /// /// 保存湿混机报表数据至数据库 /// /// private void SaveReportDb(Report_WetMixer _wetMixer) { JavaScriptSerializer serializer = new JavaScriptSerializer(); try { string str = serializer.Serialize(_wetMixer); if (_wetMixer != null) { if (_wetMixer.wetmixer != null) { InsertReportWetMixer(_wetMixer.report_Id, _wetMixer.wetmixer); } if (_wetMixer.mix != null) { InsertReportWetMixerDetail(_wetMixer.report_Id, _wetMixer.mix); } } ICSharpCode.Core.LoggingService.Warn($"报表数据打印:{str}"); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("保存报表数据至数据库异常:{0}", ex.Message), ex); } } /// /// 保存湿混机报表数据 /// /// /// private void InsertReportWetMixer(Guid reportId, WetMixerDevice wetMixer) { 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_WetMixer (reportId, eqNo) values (@reportId, @eqNo)"; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlStr; dbHelper.AddParameter("@eqNo", wetMixer.eqNo); dbHelper.AddParameter("@reportId", reportId); dbHelper.ExecuteNonQuery(); }); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("保存湿混机报表数据异常:{0}", ex.Message), ex); } } /// /// 保存湿混机报表明细数据 /// /// /// private void InsertReportWetMixerDetail(Guid reportId, List wetMixerMix) { 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_WetMixer_Detail (reportId, eqNo, mixBatch, mixStep, actCode, mixTime, mixTemp, mixSpeed, mix) values (@reportId, @eqNo, @mixBatch, @mixStep, @actCode, @mixTime, @mixTemp, @mixSpeed, @mix)"; foreach (WetMixerMix _wetMixerMix in wetMixerMix) { if (_wetMixerMix.actCode == 0) continue; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlStr; dbHelper.AddParameter("@reportId", reportId); dbHelper.AddParameter("@eqNo", _wetMixerMix.eqNo); dbHelper.AddParameter("@mixBatch", _wetMixerMix.mixBatch); dbHelper.AddParameter("@mixStep", _wetMixerMix.mixStep); dbHelper.AddParameter("@actCode", _wetMixerMix.actCode); dbHelper.AddParameter("@mixTime", _wetMixerMix.mixTime); dbHelper.AddParameter("@mixTemp", _wetMixerMix.mixTemp); dbHelper.AddParameter("@mixSpeed", _wetMixerMix.mixSpeed); dbHelper.AddParameter("@mix", _wetMixerMix.mix); dbHelper.ExecuteNonQuery(); } }); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("保存湿混机报表明细数据异常:{0}", ex.Message), ex); } } } }