You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lj_plc/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/FinishBatch/SaveHelper/ReportSaveHelper.cs

516 lines
23 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 ReportSaveHelper
{
private static readonly Lazy<ReportSaveHelper> lazy = new Lazy<ReportSaveHelper>(() => new ReportSaveHelper());
public static ReportSaveHelper Instance
{
get
{
return lazy.Value;
}
}
private ReportSaveHelper() { }
/// <summary>
/// 干混机报表数据保存
/// </summary>
public void SaveDryMixerReport()
{
try
{
ICSharpCode.Core.LoggingService<ReportSaveHelper>.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<ReportSaveHelper>.Error(String.Format("干混机报表数据保存异常:{0}", ex.Message), ex);
}
}
/// <summary>
/// 湿混机报表数据保存
/// </summary>
public void SaveWetMixerReport()
{
try
{
ICSharpCode.Core.LoggingService<ReportSaveHelper>.Debug("湿混机报表数据保存开始!!!");
//准备信号读取
Report_WetMixer WetMixer_dos0 = readWetMixer("A"); //读取报表数据
SaveReportDb(WetMixer_dos0); //保存报表数据
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<ReportSaveHelper>.Error(String.Format("湿混机报表数据保存异常:{0}", ex.Message), ex);
}
}
/// <summary>
/// 通过PLC读取干混机报表数据
/// </summary>
/// <param name="machineType">干混机A、干混机B</param>
/// <param name="dos">操作步骤Dos[0]、Dos[1]、Dos[2]、Dos[3]</param>
/// <returns></returns>
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<ReportSaveHelper>.Error(String.Format("通过PLC读取干混机报表数读取异常{0}", ex.Message), ex);
}
return _dryMixer;
}
/// <summary>
/// 通过PLC读取湿混机报表数据
/// </summary>
/// <param name="machineType">湿混机A、湿混机B</param>
/// <returns></returns>
private Report_WetMixer readWetMixer(string machineType)
{
Report_WetMixer _wetMixer = null;
try
{
_wetMixer = new Report_WetMixer();
_wetMixer.wetmixer = readWetMixers(machineType, 4);
_wetMixer.mix = readWetMixerMix(machineType, 10);
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<ReportSaveHelper>.Error(String.Format("通过PLC读取湿混机报表数读取异常{0}", ex.Message), ex);
}
return _wetMixer;
}
/// <summary>
/// 读取干混机Dos数据
/// </summary>
/// <param name="machineType"></param>
/// <param name="amount"></param>
/// <returns></returns>
private List<DryMixerDos> readDryMixerDos(string machineType,int amount)
{
List<DryMixerDos> dryMixerDoes = new List<DryMixerDos>();
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;
}
/// <summary>
/// 读取干混机Mix数据
/// </summary>
/// <param name="machineType"></param>
/// <param name="amount"></param>
/// <returns></returns>
private List<DryMixerMix> readDryMixerMix(string machineType, int amount)
{
List<DryMixerMix> dryMixerMixes = new List<DryMixerMix>();
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;
}
/// <summary>
/// 读取湿混机数据
/// </summary>
/// <param name="machineType"></param>
/// <param name="amount"></param>
/// <returns></returns>
private List<WetMixer> readWetMixers(string machineType, int amount)
{
List<WetMixer> wetMixers = new List<WetMixer>();
for (int mixer = 0; mixer < 10; mixer++)
{
#region 实例化参数
BasePlcHelper.Instance.dryer_eqNo = new Entity.DataKeyValue($"wetMixing{machineType}_{mixer}_eqNo");
#endregion
WetMixer wetMixer = new WetMixer()
{
eqNo = BasePlcHelper.Instance.dryer_eqNo.NowValue.ToInt(),
recordTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
};
wetMixers.Add(wetMixer);
}
return wetMixers;
}
/// <summary>
/// 读取湿混机Mix数据
/// </summary>
/// <param name="machineType"></param>
/// <param name="amount"></param>
/// <returns></returns>
private List<WetMixerMix> readWetMixerMix(string machineType, int amount)
{
List<WetMixerMix> wetMixerMixes = new List<WetMixerMix>();
for (int mix = 0; mix < 10; mix++)
{
#region 实例化参数
BasePlcHelper.Instance.dryer_mixBatch = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_mixBatch");
BasePlcHelper.Instance.dryer_mixStep = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_mixStep");
BasePlcHelper.Instance.dryer_actCode = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_actCode");
BasePlcHelper.Instance.dryer_mixTime = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_mixTime");
BasePlcHelper.Instance.dryer_mixTemp = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_mixTemp");
BasePlcHelper.Instance.dryer_mixSpeed = new Entity.DataKeyValue($"wetMixing{machineType}_{mix}_mixSpeed");
#endregion
WetMixerMix wetMixerMix = new WetMixerMix()
{
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")
};
wetMixerMixes.Add(wetMixerMix);
}
return wetMixerMixes;
}
/// <summary>
/// 保存干混机报表数据至数据库
/// </summary>
/// <param name="_dryMixer"></param>
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<ReportSaveHelper>.Warn($"报表数据打印:{str}");
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<ReportSaveHelper>.Error(String.Format("保存报表数据至数据库异常:{0}", ex.Message), ex);
}
}
/// <summary>
/// 保存湿混机报表数据至数据库
/// </summary>
/// <param name="_wetMixer"></param>
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);
}
else if (_wetMixer.mix != null)
{
InsertReportWetMixerDetail(_wetMixer.report_Id, _wetMixer.mix);
}
}
ICSharpCode.Core.LoggingService<ReportSaveHelper>.Warn($"报表数据打印:{str}");
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<ReportSaveHelper>.Error(String.Format("保存报表数据至数据库异常:{0}", ex.Message), ex);
}
}
/// <summary>
/// 保存干混机报表数据
/// </summary>
/// <param name="reportId"></param>
/// <param name="_dryMixerDos"></param>
private void InsertReportDryMixer(Guid reportId,List<DryMixerDos> 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<ReportSaveHelper>.Error(String.Format("保存干混机报表数据异常:{0}", ex.Message), ex);
}
}
/// <summary>
/// 保存干混机报表明细数据
/// </summary>
/// <param name="reportId"></param>
/// <param name="_dryMixerMix"></param>
private void InsertReportDryMixerDetail(Guid reportId, List<DryMixerMix> 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<ReportSaveHelper>.Error(String.Format("保存干混机报表明细数据异常:{0}", ex.Message), ex);
}
}
/// <summary>
/// 保存湿混机报表数据
/// </summary>
/// <param name="reportId"></param>
/// <param name="_dryMixerDos"></param>
private void InsertReportWetMixer(Guid reportId, List<WetMixer> wetMixers)
{
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)";
foreach (WetMixer _wetMixer in wetMixers)
{
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<ReportSaveHelper>.Error(String.Format("保存湿混机报表数据异常:{0}", ex.Message), ex);
}
}
/// <summary>
/// 保存湿混机报表明细数据
/// </summary>
/// <param name="reportId"></param>
/// <param name="_dryMixerMix"></param>
private void InsertReportWetMixerDetail(Guid reportId, List<WetMixerMix> wetMixers)
{
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 wetMixers)
{
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<ReportSaveHelper>.Error(String.Format("保存湿混机报表明细数据异常:{0}", ex.Message), ex);
}
}
}
}