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.

435 lines
19 KiB
C#

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.Qingquan.Sys;
using Mesnac.Action.Feeding.Qingquan.FeedingPlc;
using Mesnac.Basic;
namespace Mesnac.Action.Feeding.Qingquan.BasicInfo
{
/// <summary>
/// 配方下传类
/// </summary>
public class PLCRecipe
{
#region 基本方法
/// <summary>
/// 获取本地连接
/// </summary>
/// <returns></returns>
private DbHelper getLocalHelper()
{
return new DatabaseAction().NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
}
/// <summary>
/// 获取网络连接
/// </summary>
/// <returns></returns>
private DbHelper getServerHelper()
{
return new DatabaseAction().NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Server);
}
#endregion
#region 更新密炼信息(密炼步骤)
/// <summary>
/// 更新密炼信息(密炼步骤)
/// </summary>
/// <param name="RecipeObjID">配方Id</param>
/// <returns></returns>
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 为胶料秤客户端创建胶料列表
/// <summary>
/// 为胶料秤客户端创建胶料列表
/// </summary>
/// <param name="RecipeObjID"></param>
/// <returns></returns>
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<DataTable>(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++;
//new FeedingAction().RefreshCustomEquip();
return iResult;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("为胶料秤客户端创建胶料列表失败:" + ex.Message, ex);
return 0;
}
}
#endregion
#region 为胶料秤客户端创建小料列表
/// <summary>
/// 为胶料秤客户端创建小料列表
/// </summary>
/// <param name="RecipeObjID"></param>
/// <returns></returns>
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<DataTable>(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++;
//new FeedingAction().RefreshCustomEquip();
return iResult;
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("为胶料秤客户端创建小料列表失败:" + ex.Message);
return 0;
}
}
#endregion
#region 根据计划号获取配方ID
/// <summary>
/// 根据计划号获取配方ID
/// </summary>
/// <param name="planid">计划号</param>
/// <returns>成功返回配方ID失败返回String.Empty</returns>
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 配方下传方法
/// <summary>
/// 配方下传方法
/// </summary>
/// <param name="planid">计划号</param>
/// <returns>成功返回1其他值失败</returns>
public int SendPlan(string planid, out List<string> 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)
{
new FeedingAction().RefreshCustomEquip(); //刷新自定义设备变量
return 1;
}
else
{
return 0;
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error(ex.Message, ex);
msgList = new List<string>();
return 0;
}
}
#endregion
#region 配方重传
/// <summary>
/// 配方重传
/// </summary>
/// <returns>成功返回1失败返回0</returns>
public int RecipeReset()
{
try
{
List<string> 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;
}
new FeedingAction().RefreshCustomEquip(); //刷新自定义设备变量
}
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 缓存配方信息
/// <summary>
/// 缓存配方信息、并保存缓存配方至SysKeyValue表
/// </summary>
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<RecipeData.RecipeInfo>(RecipeCache.Instance.RecipeInfo);
string xmlRecipeCache_PlanInfo = SerializeHandler.SerializeObject<RecipeData.PptPlanInfo>(RecipeCache.Instance.PlanInfo);
string xmlRecipeCache_AllWeightInfo = SerializeHandler.SerializeObject<List<RecipeData.RecipeWeightInfo>>(RecipeCache.Instance.AllWeightInfo);
string xmlRecipeCache_AllMixingInfo = SerializeHandler.SerializeObject<List<RecipeData.RecipeMixingInfo>>(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<DataTable>(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
}
}