using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mesnac.Codd.Session;
using System.Data;
using System.IO;
using System.Xml.Serialization;
using Mesnac.Action.Base;
using Mesnac.Action.Feeding.Sys;
using Mesnac.Action.Feeding.FeedingPlc;
using Mesnac.Basic;
namespace Mesnac.Action.Feeding.BasicInfo
{
///
/// 配方下传类
///
public class PLCRecipe
{
#region 基本方法
///
/// 获取本地连接
///
///
private DbHelper getLocalHelper()
{
return new DatabaseAction().NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
}
///
/// 获取网络连接
///
///
private DbHelper getServerHelper()
{
return new DatabaseAction().NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Server);
}
#endregion
#region 更新密炼信息(密炼步骤)
///
/// 更新密炼信息(密炼步骤)
///
/// 配方Id
///
private int SendRecipe(string RecipeObjID)
{
try
{
FeedingAction action = new FeedingAction();
DbHelper localHelper = getLocalHelper();
int iResult = 0;
//localHelper.CommandType = CommandType.Text;
//localHelper.CommandText = "UPDATE SysKeyValue SET ssValue = " + RecipeObjID + " WHERE ssKey='RecipeID'";
//localHelper.ExecuteNonQuery();
action.UpdateSysValue("RecipeID", RecipeObjID);
localHelper.CommandText = "UPDATE SysKeyValue SET ssValue = t.mater_name FROM pmt_recipe t WHERE ssKey='RecipeMixingName' AND t.ObjID=" + RecipeObjID;
localHelper.ExecuteNonQuery();
localHelper.CommandText = "SELECT ssRemark FROM dbo.SysKeyValue WHERE ssKey='RecipeMixing'";
string sqlstr = localHelper.ToScalar().ToString();
sqlstr = string.Format(sqlstr, RecipeObjID);
iResult++;
localHelper.CommandText = sqlstr;
DataTable dt = localHelper.ToDataTable();
dt.TableName = String.Format("T_{0}_{1}", RecipeObjID, Guid.NewGuid().ToString());
string xml = SerializeHandler.SerializeDataTable(dt);
iResult++;
localHelper.CommandText = "INSERT INTO dbo.PmtRecipeMixingXml(RecipeID, DataXml) VALUES (@RecipeID,@DataXml);SELECT SCOPE_IDENTITY();";
localHelper.AddParameter("@RecipeID", RecipeObjID);
localHelper.AddParameter("@DataXml", xml);
sqlstr = localHelper.ToScalar().ToString();
iResult++;
localHelper.CommandText = "UPDATE SysKeyValue SET ssValue =@ssValue WHERE ssKey='RecipeMixing'";
localHelper.ClearParameter();
localHelper.AddParameter("@ssValue", sqlstr);
localHelper.ExecuteNonQuery();
iResult++;
return iResult;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("缓存混炼步骤失败:" + ex.Message, ex);
return 0;
}
}
#endregion
#region 为胶料秤客户端创建胶料列表
///
/// 为胶料秤客户端创建胶料列表
///
///
///
private int CreatePlyList(string RecipeObjID)
{
try
{
int iResult = 0;
DbHelper localHelper = getLocalHelper();
localHelper.CommandType = CommandType.Text;
//localHelper.CommandText = "UPDATE SysKeyValue SET ssValue = '" + RecipeObjID + "' WHERE ssKey='JLCLHM_PlyList'";
//localHelper.ExecuteNonQuery();
//localHelper.CommandText = "SELECT ssRemark FROM dbo.SysKeyValue WHERE ssKey='JLCLHM_PlyList'";
//string sqlstr = localHelper.ToScalar().ToString();
//sqlstr = string.Format(sqlstr, RecipeObjID);
//iResult++;
string sqlstr = "select weight_id as 序号 ,child_name as 名称,Convert(numeric(18,2),set_weight) as 设定, Convert(numeric(18,2),0.00) as 实际,Convert(numeric(18,2),error_allow) as 误差 from pmt_weigh where RecipeObjID='{0}' and weigh_type='2' order by weight_id";
sqlstr = string.Format(sqlstr, RecipeObjID);
localHelper.CommandText = sqlstr;
DataTable plyListTable = new DataTable();
plyListTable = localHelper.ToDataTable();
plyListTable.TableName = String.Format("T_PlyList_{0}_{1}", RecipeObjID, Guid.NewGuid().ToString());
for (int i = 0; i < plyListTable.Rows.Count; i++)
{
plyListTable.Rows[i]["名称"] = Mesnac.Basic.DataProcessor.RowValue(plyListTable.Rows[i], "名称", String.Empty).Trim();
}
string xmlRecipeCache_JLCLHM_PlyList = SerializeHandler.SerializeObject(plyListTable);
FeedingAction action = new FeedingAction();
action.UpdateSysValue("JLCLHM_PlyList", xmlRecipeCache_JLCLHM_PlyList); //缓存胶料XML数据至SysKeyValue
PlcData.Instance.JLCLHM_plyList.LastValue = new PlcData.DataKeyValue.Value(plyListTable);
iResult++;
//Mesnac.Equips.Factory.Instance.Read();
new FeedingAction().RefreshCustomEquip();
return iResult;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("为胶料秤客户端创建胶料列表失败:" + ex.Message, ex);
return 0;
}
}
#endregion
#region 为胶料秤客户端创建小料列表
///
/// 为胶料秤客户端创建小料列表
///
///
///
private int CreateSmallMaterialList(string RecipeObjID)
{
try
{
int iResult = 0;
DbHelper localHelper = getLocalHelper();
localHelper.CommandType = CommandType.Text;
//localHelper.CommandText = "UPDATE SysKeyValue SET ssValue = '" + RecipeObjID + "' WHERE ssKey='JLCLHM_SmallMaterialList'";
//localHelper.ExecuteNonQuery();
//localHelper.CommandText = "SELECT ssRemark FROM dbo.SysKeyValue WHERE ssKey='JLCLHM_SmallMaterialList'";
//string sqlstr = localHelper.ToScalar().ToString();
//sqlstr = string.Format(sqlstr, RecipeObjID);
//iResult++;
string sqlstr = "select weight_id as 序号 ,child_name as 小料名称,Convert(numeric(18,2),set_weight) as 小料标重, Convert(numeric(18,2),0.00) as 实重,Convert(numeric(18,2),error_allow) as 误差 from pmt_weigh where RecipeObjID='{0}' and weigh_type='4' and act_code='0' order by weight_id";
sqlstr = string.Format(sqlstr, RecipeObjID);
localHelper.CommandText = sqlstr;
DataTable smallMaterialListTable = new DataTable();
smallMaterialListTable = localHelper.ToDataTable();
smallMaterialListTable.TableName = String.Format("T_SmallMaterialList_{0}_{1}", RecipeObjID, Guid.NewGuid().ToString());
for (int i = 0; i < smallMaterialListTable.Rows.Count; i++)
{
smallMaterialListTable.Rows[i]["小料名称"] = Mesnac.Basic.DataProcessor.RowValue(smallMaterialListTable.Rows[i], "小料名称", String.Empty).Trim();
}
string xmlRecipeCache_JLCLHM_SmallMaterialList = SerializeHandler.SerializeObject(smallMaterialListTable);
FeedingAction action = new FeedingAction();
action.UpdateSysValue("JLCLHM_SmallMaterialList", xmlRecipeCache_JLCLHM_SmallMaterialList); //缓存小料XML数据至SysKeyValue
PlcData.Instance.JLCLHM_SmallMaterialList.LastValue = new PlcData.DataKeyValue.Value(smallMaterialListTable);
iResult++;
//Mesnac.Equips.Factory.Instance.Read();
new FeedingAction().RefreshCustomEquip();
return iResult;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("为胶料秤客户端创建小料列表失败:" + ex.Message);
return 0;
}
}
#endregion
#region 根据计划号获取配方ID
///
/// 根据计划号获取配方ID
///
/// 计划号
/// 成功返回配方ID,失败返回String.Empty
private string GetRecipeObjID(string planid)
{
try
{
DbHelper localHelper = getLocalHelper();
//localHelper.CommandType = CommandType.Text;
//localHelper.CommandText = "UPDATE SysKeyValue SET ssValue = '" + planid + "' WHERE ssKey='PlanID'";
//localHelper.ExecuteNonQuery();
localHelper.ClearParameter();
localHelper.CommandText = @"SELECT t2.ObjID FROM ppt_plan t1
INNER JOIN pmt_recipe t2 ON substring(rtrim(t1.equip_code),4,2)=t2.equip_code
AND t1.mater_code=t2.mater_code AND t1.edt_code=t2.edt_code
WHERE t1.plan_id=@PlanID";
localHelper.AddParameter("@PlanID", planid);
object result = localHelper.ToScalar();
if (result != null && result != System.DBNull.Value)
{
return result.ToString();
}
return String.Empty;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error(ex.Message, ex);
return String.Empty;
}
}
#endregion
#region 配方下传方法
///
/// 配方下传方法
///
/// 计划号
/// 成功返回1,其他值失败
public int SendPlan(string planid, out List msgList)
{
try
{
//如果是网络版,从网络数据库中下载配方数据
if (new BaseAction().NetType == BaseAction.NetTypes.Net)
{
new NetRecipe().GetRecipeByPlanID(planid);
}
bool isSuccess = true;
isSuccess = new RecipeToDatabase().Run(out msgList); //把配方PLC点对应数据先写入数据库中
//if (isSuccess)
//{
// isSuccess = new DatabaseToPlc().Run(); //把PLCData表中PLC点的值写入PLC
//}
if (isSuccess)
{
this.CacheRecipe(); //缓存配方信息
RecipeWeighCache.Instance.CacheFlag = false; //配方下传时,清除称量信息下传缓存
string recipeid = GetRecipeObjID(planid); //获取配方ID
if (SendRecipe(recipeid) == 0)
{
return 0;
}
if (CreatePlyList(recipeid) == 0)
{
return 0;
}
if (CreateSmallMaterialList(recipeid) == 0)
{
return 0;
}
}
if (isSuccess)
{
return 1;
}
else
{
return 0;
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error(ex.Message, ex);
msgList = new List();
return 0;
}
}
#endregion
#region 配方重传
///
/// 配方重传
///
/// 成功返回1,失败返回0
public int RecipeReset()
{
try
{
List msgList = null;
if (RecipeWeighCache.Instance.CacheFlag == true)
{
ICSharpCode.Core.LoggingService.Warn("下一个计划的称量信息已下传,不允许配方重传...");
return 0;
}
RecipeData recipe = new RecipeData();
string planid = recipe.GetCurrentPlanID();
bool isSuccess = true;
isSuccess = new RecipeToDatabase().Run(out msgList);
//if (isSuccess)
//{
// isSuccess = new DatabaseToPlc().Run();
//}
if (isSuccess)
{
this.CacheRecipe(); //缓存配方信息
RecipeWeighCache.Instance.CacheFlag = false; //清除称量信息下传缓存
string recipeid = GetRecipeObjID(planid);
if (SendRecipe(recipeid) == 0)
{
isSuccess = false;
}
if (CreatePlyList(recipeid) == 0)
{
isSuccess = false;
}
if (CreateSmallMaterialList(recipeid) == 0)
{
isSuccess = false;
}
}
FeedingAction action = new FeedingAction();
#region 把当前计划列表发送给客户端
if (PlanCommon.PlanLog != null)
{
PlanCommon.SendInit(action.CurrEquipCode, PlanCommon.PlanLog.LastSelectDate, PlanCommon.PlanLog.LastSelectShiftID, PlanCommon.PlanLog.LastClassID, action.GetConfigValue("LastUserID", "0"));
}
else
{
string sbNetMsg = Global.ProtocalHeader.ReceivePlanIpNumber + ":/";
Mesnac.Communication.TcpService.Instance.NetSendMsg(sbNetMsg.ToString()); //发送网络消息
ICSharpCode.Core.LoggingService.Debug("没有计划日志!");
}
#endregion
#region 如果合并一次法计划标志为1,则更新一次法本地库计划状态,配方重传,则把一次法计划状态更新为3
if (action.GetConfigValue("IsCombinedOne", "0") == "1")
{
PlanCommon.UpdatePlanOne(planid.Trim(), 0, 3, false);
}
#endregion
if (isSuccess)
{
return 1;
}
else
{
return 0;
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error(ex.Message, ex);
return 0;
}
}
#endregion
#region 缓存配方信息
///
/// 缓存配方信息、并保存缓存配方至SysKeyValue表
///
public void CacheRecipe()
{
try
{
RecipeData recipeData = new RecipeData();
RecipeCache.Instance.CacheRecipeInfo(recipeData.GetCurrentRecipeInfo());
RecipeCache.Instance.CachePlanInfo(recipeData.GetCurrentPptPlanInfo());
RecipeCache.Instance.CacheAllWeightInfo(recipeData.GetCurrentRecipeWeightInfo());
RecipeCache.Instance.CacheAllMixingInfo(recipeData.GetCurrentRecipeMixingInfo());
string xmlRecipeCache_RecipeInfo = SerializeHandler.SerializeObject(RecipeCache.Instance.RecipeInfo);
string xmlRecipeCache_PlanInfo = SerializeHandler.SerializeObject(RecipeCache.Instance.PlanInfo);
string xmlRecipeCache_AllWeightInfo = SerializeHandler.SerializeObject>(RecipeCache.Instance.AllWeightInfo);
string xmlRecipeCache_AllMixingInfo = SerializeHandler.SerializeObject>(RecipeCache.Instance.AllMixingInfo);
DataTable weightInfoTable = new DataTable();
weightInfoTable.TableName="WeightListTable_" + Guid.NewGuid().ToString();
weightInfoTable.Columns.Add("WeightType");
weightInfoTable.Columns.Add("JarNum");
weightInfoTable.Columns.Add("SetWeight");
weightInfoTable.Columns.Add("ErrorAllow");
foreach (RecipeData.RecipeWeightInfo weightInfo in RecipeCache.Instance.AllWeightInfo)
{
if (weightInfo.ActCode == 1)
{
DataRow row = weightInfoTable.NewRow();
row["WeightType"] = weightInfo.WeightType;
row["JarNum"] = weightInfo.JarNum;
row["SetWeight"] = weightInfo.SetWeight;
row["ErrorAllow"] = weightInfo.ErrorAllow;
weightInfoTable.Rows.Add(row);
}
}
string xmlRecipeCache_WeightListTable = SerializeHandler.SerializeObject(weightInfoTable);
FeedingAction action = new FeedingAction();
action.UpdateSysValue("RecipeCache_RecipeInfo", xmlRecipeCache_RecipeInfo);
action.UpdateSysValue("RecipeCache_PlanInfo", xmlRecipeCache_PlanInfo);
action.UpdateSysValue("RecipeCache_AllWeightInfo", xmlRecipeCache_AllWeightInfo);
action.UpdateSysValue("RecipeCache_AllMixingInfo", xmlRecipeCache_AllMixingInfo);
action.UpdateSysValue("RecipeCache_WeightListTable", xmlRecipeCache_WeightListTable);
PlcData.Instance.RecipeCache_WeightListTable.LastValue = new PlcData.DataKeyValue.Value(weightInfoTable); //把称量信息保存至PLC变量
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("保存缓存配方数据至SysKeyValue表失败:" + ex.Message, ex);
}
}
#endregion
}
}