|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using Mesnac.Codd.Session;
|
|
|
using Mesnac.Action.Feeding.BasicInfo;
|
|
|
using System.Data;
|
|
|
using System.Data.Common;
|
|
|
|
|
|
namespace Mesnac.Action.Feeding.FinishBatch
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 密炼数据存盘辅助类
|
|
|
/// </summary>
|
|
|
public class PptMixingDataHelper
|
|
|
{
|
|
|
private LotBarcode lotBarcode = null;
|
|
|
private List<RecipeData.RecipeMixingInfo> allMixingInfo = null;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 构造方法
|
|
|
/// </summary>
|
|
|
/// <param name="_lotBarcode">架子信息</param>
|
|
|
/// <param name="_allMixingInfo">密炼信息</param>
|
|
|
public PptMixingDataHelper(LotBarcode _lotBarcode, List<RecipeData.RecipeMixingInfo> _allMixingInfo)
|
|
|
{
|
|
|
this.lotBarcode = _lotBarcode;
|
|
|
this.allMixingInfo = _allMixingInfo;
|
|
|
}
|
|
|
|
|
|
#region SavePptMixingData - 保存当前车密炼数据
|
|
|
/// <summary>
|
|
|
/// 保存当前车密炼数据
|
|
|
/// </summary>
|
|
|
/// <param name="dbHelper"></param>
|
|
|
/// <returns></returns>
|
|
|
public bool SavePptMixingData(Mesnac.Basic.DataSourceFactory.MCDbType dbType, DbHelper dbHelper)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
#region 基本信息
|
|
|
PlcRecipeReader reader = PlcSchemaHandler.Instance.GetPlcRecipeReader("PptMixData").FirstOrDefault();
|
|
|
#endregion
|
|
|
int[] buff;
|
|
|
if (!PlcData.Instance.PlcLastValueRead(reader.PlcFiledName, out buff))
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService.Warn("读取PLC的密炼数据失败!");
|
|
|
return true;
|
|
|
}
|
|
|
int shifting = 0;
|
|
|
for (int i = 0; i < allMixingInfo.Count; i++)
|
|
|
{
|
|
|
RecipeData.RecipeMixingInfo mixing = allMixingInfo[i];
|
|
|
|
|
|
#region 基本信息
|
|
|
|
|
|
dbHelper.CommandType = CommandType.Text;
|
|
|
//dbHelper.ClearParameter();
|
|
|
|
|
|
List<DbParameter>[] parameters = new List<DbParameter>[mixing.SynchMixingInfos.Count];
|
|
|
StringBuilder[] sqlstr_fields = new StringBuilder[mixing.SynchMixingInfos.Count];
|
|
|
StringBuilder[] sqlstr_values = new StringBuilder[mixing.SynchMixingInfos.Count];
|
|
|
|
|
|
for (int index = 0; index < mixing.SynchMixingInfos.Count; index++)
|
|
|
{
|
|
|
parameters[index] = new List<DbParameter>();
|
|
|
sqlstr_fields[index] = new StringBuilder();
|
|
|
sqlstr_values[index] = new StringBuilder();
|
|
|
}
|
|
|
|
|
|
string fieldName = string.Empty;
|
|
|
string valueName = string.Empty;
|
|
|
if (dbType == Mesnac.Basic.DataSourceFactory.MCDbType.Local)
|
|
|
{
|
|
|
for (int index = 0; index < mixing.SynchMixingInfos.Count; index++)
|
|
|
{
|
|
|
sqlstr_fields[index].Append("INSERT INTO PptMixData(");
|
|
|
}
|
|
|
}
|
|
|
if (dbType == Mesnac.Basic.DataSourceFactory.MCDbType.Server)
|
|
|
{
|
|
|
for (int index = 0; index < mixing.SynchMixingInfos.Count; index++)
|
|
|
{
|
|
|
sqlstr_fields[index].Append("INSERT INTO Ppt_MixData(");
|
|
|
}
|
|
|
}
|
|
|
for (int index = 0; index < mixing.SynchMixingInfos.Count; index++)
|
|
|
{
|
|
|
sqlstr_values[index].Append(" ) VALUES (");
|
|
|
}
|
|
|
#region 数据整理
|
|
|
#region SaveTime
|
|
|
fieldName = "Save_Time";
|
|
|
valueName = "getdate()";
|
|
|
for (int index = 0; index < mixing.SynchMixingInfos.Count; index++)
|
|
|
{
|
|
|
sqlstr_fields[index].Append(fieldName).Append(",");
|
|
|
sqlstr_values[index].Append(valueName).Append(",");
|
|
|
}
|
|
|
#endregion
|
|
|
#region MixId
|
|
|
fieldName = "Mix_id";
|
|
|
valueName = "@MixID";
|
|
|
for (int index = 0; index < mixing.SynchMixingInfos.Count; index++)
|
|
|
{
|
|
|
sqlstr_fields[index].Append(fieldName).Append(",");
|
|
|
sqlstr_values[index].Append(valueName).Append(",");
|
|
|
parameters[index].Add(dbHelper.CreateParameter(valueName, mixing.SynchMixingInfos[index].MixingStep));
|
|
|
//dbHelper.AddParameter(valueName, mixing.MixingStep);
|
|
|
}
|
|
|
#endregion
|
|
|
#endregion
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 存储PLCSchema.xml中配置字段的数据
|
|
|
|
|
|
foreach (PlcRecipeReaderItem item in reader.ItemList)
|
|
|
{
|
|
|
int[] data = new int[item.ValueLen];
|
|
|
for (int idx = 0; idx < data.Length; idx++)
|
|
|
{
|
|
|
data[idx] = buff[idx + shifting];
|
|
|
}
|
|
|
shifting += item.ValueLen;
|
|
|
item.SetValue = data;
|
|
|
|
|
|
fieldName = item.DataFieldName;
|
|
|
valueName = "@" + fieldName;
|
|
|
for (int index = 0; index < mixing.SynchMixingInfos.Count; index++)
|
|
|
{
|
|
|
sqlstr_fields[index].Append(fieldName).Append(",");
|
|
|
sqlstr_values[index].Append(valueName).Append(",");
|
|
|
if (fieldName.Equals("Term_code", StringComparison.CurrentCultureIgnoreCase))
|
|
|
{
|
|
|
int value = 0;
|
|
|
if (int.TryParse(item.GetValue().ToString(), out value))
|
|
|
{
|
|
|
if (value > 0)
|
|
|
{
|
|
|
parameters[index].Add(dbHelper.CreateParameter(valueName, value));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
parameters[index].Add(dbHelper.CreateParameter(valueName, mixing.SynchMixingInfos[index].TermCodeAddress));
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
parameters[index].Add(dbHelper.CreateParameter(valueName, mixing.SynchMixingInfos[index].TermCodeAddress));
|
|
|
}
|
|
|
}
|
|
|
else if (fieldName.Equals("Act_code", StringComparison.CurrentCultureIgnoreCase))
|
|
|
{
|
|
|
int actionCode = 0;
|
|
|
if (int.TryParse(item.GetValue().ToString(), out actionCode))
|
|
|
{
|
|
|
if (actionCode == mixing.ActionCodeAddress)
|
|
|
{
|
|
|
parameters[index].Add(dbHelper.CreateParameter(valueName, mixing.SynchMixingInfos[index].ActionCodeAddress));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
//ICSharpCode.Core.LoggingService.Warn("密炼数据存盘时,从PLC获取的Act_code与配方中的密炼动作不一致:密炼步骤:" + mixing.SynchMixingInfos[index].MixingStep + ",动作地址:" + actionCode);
|
|
|
parameters[index].Add(dbHelper.CreateParameter(valueName, mixing.SynchMixingInfos[index].ActionCodeAddress));
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService.Warn("密炼数据存盘时,从PLC获取Act_code的值失败");
|
|
|
parameters[index].Add(dbHelper.CreateParameter(valueName, mixing.SynchMixingInfos[index].ActionCodeAddress));
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
parameters[index].Add(dbHelper.CreateParameter(valueName, item.GetValue()));
|
|
|
}
|
|
|
//dbHelper.AddParameter(valueName, item.GetValue());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Barcode
|
|
|
fieldName = "Barcode";
|
|
|
valueName = "@" + fieldName;
|
|
|
for (int index = 0; index < mixing.SynchMixingInfos.Count; index++)
|
|
|
{
|
|
|
sqlstr_fields[index].Append(fieldName);
|
|
|
sqlstr_values[index].Append(valueName);
|
|
|
parameters[index].Add(dbHelper.CreateParameter(valueName, lotBarcode.Barcode.Trim()));
|
|
|
//dbHelper.AddParameter(valueName, lotBarcode.Barcode.Trim());
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 执行存储
|
|
|
for (int index = 0; index < mixing.SynchMixingInfos.Count; index++)
|
|
|
{
|
|
|
sqlstr_values[index].Append(")"); //结束SQL语句
|
|
|
dbHelper.ClearParameter();
|
|
|
dbHelper.CommandText = sqlstr_fields[index].ToString() + sqlstr_values[index].ToString();
|
|
|
foreach (DbParameter p in parameters[index])
|
|
|
{
|
|
|
dbHelper.AddParameter(p);
|
|
|
}
|
|
|
dbHelper.ExecuteNonQuery();
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService.Error("存盘业务-保存当前车密炼数据失败:" + ex.Message);
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 更新本地库密炼数据的上传标志
|
|
|
/// <summary>
|
|
|
/// 更新本地库密炼数据的上传标志
|
|
|
/// </summary>
|
|
|
/// <param name="localHelper">本地库数据连接对象</param>
|
|
|
public void UpdateIsUpFlag(DbHelper localHelper)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
string strSql = "update PptMixData set IsUpFlag = 1 where Barcode = @Barcode";
|
|
|
localHelper.ClearParameter();
|
|
|
localHelper.CommandText = strSql;
|
|
|
localHelper.AddParameter("@Barcode", this.lotBarcode.Barcode.Trim());
|
|
|
localHelper.ExecuteNonQuery();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
ICSharpCode.Core.LoggingService.Error(String.Format("更新本地库密炼数据[{0}]上传标志失败:{1}", this.lotBarcode.Barcode, ex.Message));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
}
|
|
|
}
|