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/ReportWetMixerSaveHelper.cs

270 lines
11 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 ReportWetMixerSaveHelper
{
private static readonly Lazy<ReportWetMixerSaveHelper> lazy = new Lazy<ReportWetMixerSaveHelper>(() => new ReportWetMixerSaveHelper());
public static ReportWetMixerSaveHelper Instance
{
get
{
return lazy.Value;
}
}
private ReportWetMixerSaveHelper() { }
/// <summary>
/// 湿混机报表数据保存
/// </summary>
public void SaveWetMixerReport()
{
try
{
ICSharpCode.Core.LoggingService<ReportWetMixerSaveHelper>.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<ReportWetMixerSaveHelper>.Error(String.Format("湿混机报表数据保存异常:{0}", ex.Message), ex);
}
}
/// <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);
_wetMixer.mix = readWetMixerMix(machineType, 10);
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<ReportWetMixerSaveHelper>.Error(String.Format("通过PLC读取湿混机报表数读取异常{0}", ex.Message), ex);
}
return _wetMixer;
}
/// <summary>
/// 读取湿混机数据
/// </summary>
/// <param name="machineType"></param>
/// <param name="amount"></param>
/// <returns></returns>
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;
}
/// <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 实例化参数
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;
}
/// <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);
}
if (_wetMixer.mix != null)
{
InsertReportWetMixerDetail(_wetMixer.report_Id, _wetMixer.mix);
}
}
ICSharpCode.Core.LoggingService<ReportWetMixerSaveHelper>.Warn($"报表数据打印:{str}");
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService<ReportWetMixerSaveHelper>.Error(String.Format("保存报表数据至数据库异常:{0}", ex.Message), ex);
}
}
/// <summary>
/// 保存湿混机报表数据
/// </summary>
/// <param name="reportId"></param>
/// <param name="_wetMixerDos"></param>
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<ReportWetMixerSaveHelper>.Error(String.Format("保存湿混机报表数据异常:{0}", ex.Message), ex);
}
}
/// <summary>
/// 保存湿混机报表明细数据
/// </summary>
/// <param name="reportId"></param>
/// <param name="wetMixerMix"></param>
private void InsertReportWetMixerDetail(Guid reportId, List<WetMixerMix> 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<ReportWetMixerSaveHelper>.Error(String.Format("保存湿混机报表明细数据异常:{0}", ex.Message), ex);
}
}
}
}