using DevExpress.DataAccess.Native.Json; using DevExpress.Office.Utils; using DevExpress.XtraPrinting.Native.WebClientUIControl; using Mesnac.Action.ChemicalWeighing.Entity.Report; using Mesnac.Codd.Session; using Mesnac.PlcUtils; using Microsoft.Office.Interop.Excel; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Script.Serialization; namespace Mesnac.Action.ChemicalWeighing.FinishBatch.SaveHelper { public sealed class ReportSaveHelper { private static readonly Lazy lazy = new Lazy(() => new ReportSaveHelper()); public static ReportSaveHelper Instance { get { return lazy.Value; } } private ReportSaveHelper() { } /// /// 干混机报表数据保存 /// public void SaveDryMixerReport() { try { ICSharpCode.Core.LoggingService.Debug("干混机报表数据保存开始!!!"); //准备信号读取 //if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 1) //{ // Report_DryMixer dryMixer_dos0 = readDryMixer("A"); //读取报表数据 // SaveReportDb(dryMixer_dos0); //保存报表数据 //} //else if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 2) //{ // Report_DryMixer dryMixer_dos0 = readDryMixer("B"); //读取报表数据 // SaveReportDb(dryMixer_dos0); //保存报表数据 //} //else if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 3) //{ // Report_DryMixer dryMixer_dos0 = readDryMixer("C"); //读取报表数据 // SaveReportDb(dryMixer_dos0); //保存报表数据 //} //else if (BasePlcHelper.Instance.RdyRpt_D1.NowValue.ToInt() == 4) //{ // Report_DryMixer dryMixer_dos0 = readDryMixer("D"); //读取报表数据 // SaveReportDb(dryMixer_dos0); //保存报表数据 //} Report_DryMixer dryMixer_dos0 = readDryMixer("A"); //读取报表数据 SaveReportDb(dryMixer_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_DryMixer readDryMixer( string machineType) { Report_DryMixer _dryMixer = null; try { _dryMixer = new Report_DryMixer(); _dryMixer.dos = readDryMixerDos(machineType, 4); _dryMixer.mix = readDryMixerMix(machineType, 10); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("通过PLC读取干混机报表数读取异常:{0}", ex.Message), ex); } return _dryMixer; } /// /// 读取干混机Dos数据 /// /// /// /// private List readDryMixerDos(string machineType,int amount) { List dryMixerDoes = new List(); for (int dos = 0; dos < amount; dos++) { #region 实例化参数 BasePlcHelper.Instance.dryer_eqNo = new Entity.DataKeyValue($"dryer{machineType}_dos{dos}_eqNo"); BasePlcHelper.Instance.dryer_batch = new Entity.DataKeyValue($"dryer{machineType}_dos{dos}_batch"); BasePlcHelper.Instance.dryer_matCode = new Entity.DataKeyValue($"dryer{machineType}_dos{dos}_matCode"); BasePlcHelper.Instance.dryer_setValue = new Entity.DataKeyValue($"dryer{machineType}_dos{dos}_setValue"); BasePlcHelper.Instance.dryer_setToler = new Entity.DataKeyValue($"dryer{machineType}_dos{dos}_setToler"); BasePlcHelper.Instance.dryer_actValue = new Entity.DataKeyValue($"dryer{machineType}_dos{dos}_actValue"); BasePlcHelper.Instance.dryer_actToLer = new Entity.DataKeyValue($"dryer{machineType}_dos{dos}_actToLer"); #endregion DryMixerDos dryMixerDos = new DryMixerDos() { eqNo = BasePlcHelper.Instance.dryer_eqNo.NowValue.ToInt(), batch = BasePlcHelper.Instance.dryer_batch.NowValue.ToInt(), matCode = BasePlcHelper.Instance.dryer_matCode.NowValue.ToInt(), setValue = BasePlcHelper.Instance.dryer_setValue.NowValue.ToDouble(), setToler = BasePlcHelper.Instance.dryer_setToler.NowValue.ToDouble(), actValue = BasePlcHelper.Instance.dryer_actValue.NowValue.ToDouble(), actToLer = BasePlcHelper.Instance.dryer_actToLer.NowValue.ToDouble(), dos = dos, recordTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; dryMixerDoes.Add(dryMixerDos); } return dryMixerDoes; } /// /// 读取干混机Mix数据 /// /// /// /// private List readDryMixerMix(string machineType, int amount) { List dryMixerMixes = new List(); for (int mix = 0; mix < 10; mix++) { #region 实例化参数 BasePlcHelper.Instance.dryer_mixBatch = new Entity.DataKeyValue($"dryer{machineType}_mix{mix}_mixBatch"); BasePlcHelper.Instance.dryer_mixStep = new Entity.DataKeyValue($"dryer{machineType}_mix{mix}_mixStep"); BasePlcHelper.Instance.dryer_actCode = new Entity.DataKeyValue($"dryer{machineType}_mix{mix}_actCode"); BasePlcHelper.Instance.dryer_mixTime = new Entity.DataKeyValue($"dryer{machineType}_mix{mix}_mixTime"); BasePlcHelper.Instance.dryer_mixTemp = new Entity.DataKeyValue($"dryer{machineType}_mix{mix}_mixTemp"); BasePlcHelper.Instance.dryer_mixSpeed = new Entity.DataKeyValue($"dryer{machineType}_mix{mix}_mixSpeed"); #endregion DryMixerMix dryMixerMix = new DryMixerMix() { eqNo = BasePlcHelper.Instance.dryer_eqNo.NowValue.ToInt(), mixBatch = BasePlcHelper.Instance.dryer_mixBatch.NowValue.ToInt(), mixStep = BasePlcHelper.Instance.dryer_matCode.NowValue.ToInt(), actCode = BasePlcHelper.Instance.dryer_actCode.NowValue.ToInt(), mixTime = BasePlcHelper.Instance.dryer_mixTime.NowValue.ToInt(), mixTemp = BasePlcHelper.Instance.dryer_mixTemp.NowValue.ToDouble(), mixSpeed = BasePlcHelper.Instance.dryer_mixSpeed.NowValue.ToDouble(), mix = mix, recordTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }; dryMixerMixes.Add(dryMixerMix); } return dryMixerMixes; } /// /// 保存报表数据至数据库 /// /// private void SaveReportDb(Report_DryMixer _dryMixer) { JavaScriptSerializer serializer = new JavaScriptSerializer(); try { string str = serializer.Serialize(_dryMixer); if(_dryMixer != null) { if(_dryMixer.dos != null) { InsertReportDryMixer(_dryMixer.report_Id,_dryMixer.dos); }else if(_dryMixer.mix != null) { InsertReportDryMixerDetail(_dryMixer.report_Id, _dryMixer.mix); } } ICSharpCode.Core.LoggingService.Warn($"报表数据打印:{str}"); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("保存报表数据至数据库异常:{0}", ex.Message), ex); } } /// /// 保存干混机报表数据 /// /// /// private void InsertReportDryMixer(Guid reportId,List dryMixers) { 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_DryMixer (reportId, eqNo, batch, matCode, setValue, setToler, actValue, actToLer, dos) values (@reportId, @eqNo, @batch, @matCode, @setValue, @setToler, @actValue, @actToLer, @dos)"; foreach (DryMixerDos _dryMixerDos in dryMixers) { if (_dryMixerDos.actValue == 0) continue; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlStr; dbHelper.AddParameter("@reportId", reportId); dbHelper.AddParameter("@eqNo", _dryMixerDos.eqNo); dbHelper.AddParameter("@batch", _dryMixerDos.batch); dbHelper.AddParameter("@matCode", _dryMixerDos.matCode); dbHelper.AddParameter("@setValue", _dryMixerDos.setValue); dbHelper.AddParameter("@setToler", _dryMixerDos.setToler); dbHelper.AddParameter("@actValue", _dryMixerDos.actValue); dbHelper.AddParameter("@actToLer", _dryMixerDos.actToLer); dbHelper.AddParameter("@dos", _dryMixerDos.dos); dbHelper.ExecuteNonQuery(); } }); }catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("保存干混机报表数据异常:{0}", ex.Message), ex); } } /// /// 保存干混机报表明细数据 /// /// /// private void InsertReportDryMixerDetail(Guid reportId, List dryMixers) { 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_DryMixer_Detail (reportId, eqNo, mixBatch, mixStep, actCode, mixTime, mixTemp, mixSpeed, mix) values (@reportId, @eqNo, @mixBatch, @mixStep, @actCode, @mixTime, @mixTemp, @mixSpeed, @mix)"; foreach (DryMixerMix _dryMixerMix in dryMixers) { if (_dryMixerMix.actCode == 0) continue; dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlStr; dbHelper.AddParameter("@reportId", reportId); dbHelper.AddParameter("@eqNo", _dryMixerMix.eqNo); dbHelper.AddParameter("@mixBatch", _dryMixerMix.mixBatch); dbHelper.AddParameter("@mixStep", _dryMixerMix.mixStep); dbHelper.AddParameter("@actCode", _dryMixerMix.actCode); dbHelper.AddParameter("@mixTime", _dryMixerMix.mixTime); dbHelper.AddParameter("@mixTemp", _dryMixerMix.mixTemp); dbHelper.AddParameter("@mixSpeed", _dryMixerMix.mixSpeed); dbHelper.AddParameter("@mix", _dryMixerMix.mix); dbHelper.ExecuteNonQuery(); } }); }catch(Exception ex) { ICSharpCode.Core.LoggingService.Error(String.Format("保存干混机报表明细数据异常:{0}", ex.Message), ex); } } } }