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.

479 lines
24 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 System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Text;
using System.Linq;
using System.Data;
using System.Threading;
using Mesnac.Action.Base;
using Mesnac.Controls.Base;
using System.Windows.Forms;
using Mesnac.Codd.Session;
using System.IO;
using Mesnac.Action.Feeding.BasicInfo;
namespace Mesnac.Action.Feeding.FeedingPlc
{
public class ModifyPlanNum : FeedingAction, IAction
{
#region 事件定义
/// <summary>
/// 刷新计划事件
/// </summary>
public static event EventHandler OnRefreshPlan;
#endregion
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
try
{
string msg = String.Empty;
string currentPlanId = String.Empty; //当前计划号
int planNum = 0; //原始计划数
int realNum = 0; //实际完成数
int newPlanNum = 0; //新设定的计划数
base.LogDebug("修改次数...");
#region 判断当班计划界面是否打开
//if (PlanCommon.IsInit == false)
//{
// msg = "当班计划还为初始化,请先打开当班计划,再进行此操作!";
// ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
// return;
//}
#endregion
//获取当前正在执行的计划
PlanLog log = PlanCommon.PlanLog;
if (log == null || String.IsNullOrEmpty(log.LastPlanID))
{
msg = "没有正在执行的计划!\r\n不允许执行此操作";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
PlanStates planStates = PlanCommon.GetPlanState(log.LastPlanID);
if (planStates == PlanStates.Completed)
{
msg = "当前计划已完成,不能执行此操作!";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
currentPlanId = log.LastPlanID;
DataRow planRow = PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, currentPlanId);
if (planRow == null)
{
base.LogError("获取当前计划数据失败...");
return;
}
planNum = planRow["PlanNum"] == null || planRow["PlanNum"] == System.DBNull.Value ? 0 : Convert.ToInt32(planRow["PlanNum"]);
realNum = planRow["RealNum"] == null || planRow["RealNum"] == System.DBNull.Value ? 0 : Convert.ToInt32(planRow["RealNum"]);
//FrmModifyPlanNum frmModifyPlanNum = new FrmModifyPlanNum(planNum);
FrmModifyPlanNum frmModifyPlanNum = new FrmModifyPlanNum();
DialogResult result = frmModifyPlanNum.ShowDialog();
if (result == DialogResult.OK)
{
newPlanNum = frmModifyPlanNum.NewPlanNum;
frmModifyPlanNum.Dispose();
if (planNum != 0 && realNum < planNum && PlcData.Instance.OilFinishedCount.LastValue.ToInt() >= planNum)
{
msg = "当前计划油1已经称量完成\r\n请当前计划完毕后操作";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (planNum != 0 && realNum < planNum && PlcData.Instance.Oil2FinishedCount.LastValue.ToInt() >= planNum)
{
msg = "当前计划油2已经称量完成\r\n请当前计划完毕后操作";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (planNum != 0 && realNum < planNum && PlcData.Instance.PloyFinishedCount.LastValue.ToInt() >= planNum)
{
msg = "当前计划胶料已经称量完成!\r\n请当前计划完毕后操作";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (planNum != 0 && realNum < planNum && PlcData.Instance.CarbonFinishedCount.LastValue.ToInt() >= planNum)
{
msg = "当前计划炭黑已经称量完成!\r\n请当前计划完毕后操作";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (planNum != 0 && realNum < planNum && PlcData.Instance.PowderFinishedCount.LastValue.ToInt() >= planNum)
{
msg = "当前计划粉料已经称量完成!\r\n请当前计划完毕后操作";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (newPlanNum < PlcData.Instance.CarbonFinishedCount.LastValue.ToInt())
{
msg = "当前计划设定次数不能低于炭黑称量完成数量!";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (newPlanNum < PlcData.Instance.PloyFinishedCount.LastValue.ToInt())
{
msg = "当前计划设定次数不能低于胶料称量完成数量!";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (newPlanNum < PlcData.Instance.OilFinishedCount.LastValue.ToInt())
{
msg = "当前计划设定次数不能低于油1称量完成数量";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (newPlanNum < PlcData.Instance.Oil2FinishedCount.LastValue.ToInt())
{
msg = "当前计划设定次数不能低于油2称量完成数量";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (newPlanNum < PlcData.Instance.PowderFinishedCount.LastValue.ToInt())
{
msg = "当前计划设定次数不能低于粉料称量完成数量!";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
int maxNum = 0; //记录称量次数的最大值
if (PlcData.Instance.OilFinishedCount.LastValue.ToInt() > maxNum)
{
maxNum = PlcData.Instance.OilFinishedCount.LastValue.ToInt();
}
if (PlcData.Instance.Oil2FinishedCount.LastValue.ToInt() > maxNum)
{
maxNum = PlcData.Instance.Oil2FinishedCount.LastValue.ToInt();
}
if (PlcData.Instance.PloyFinishedCount.LastValue.ToInt() > maxNum)
{
maxNum = PlcData.Instance.PloyFinishedCount.LastValue.ToInt();
}
if (PlcData.Instance.CarbonFinishedCount.LastValue.ToInt() > maxNum)
{
maxNum = PlcData.Instance.CarbonFinishedCount.LastValue.ToInt();
}
if (PlcData.Instance.PowderFinishedCount.LastValue.ToInt() > maxNum)
{
maxNum = PlcData.Instance.PowderFinishedCount.LastValue.ToInt();
}
if (newPlanNum < maxNum + 1)
{
msg = "当前计划设定次数不能[低于]最大完成数 + [1]";
ShowMsg(msg, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
msg = String.Format("确认修改设定次数为{0}?", newPlanNum);
if (MessageBox.Show(msg, Language(1), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
//记录数据库日志
msg = "修改计划:{0}从{1}车到{2}车";
string recipe_code = PlanCommon.GetRecipeCodeByPlanID(currentPlanId);
msg = String.Format(msg, recipe_code, planNum, newPlanNum);
base.DBLog("基本操作", "修改次数", msg);
//发送网络消息
string netMsg = "{0}:{1}/"; //通知预分散上位机车数修改
netMsg = String.Format(netMsg, Global.ProtocalHeader.ReceiveModifyPlanNumber, newPlanNum);
Mesnac.Communication.TcpService.Instance.NetSendMsg(netMsg);
//修改PLC计划数
if (!this.DownLoadModifyNumber(newPlanNum))
{
ICSharpCode.Core.LoggingService.Error("修改计划数失败把新计划数下传至PLC失败");
return;
}
//更新本地数据库
PlanCommon.UpdatePlanNum(currentPlanId, newPlanNum);
//如果合并一次法计划标志为1则更新一次法本地库计划状态修改计划数则把一次法计划状态改为2
if (base.GetConfigValue("IsCombinedOne", "0") == "1")
{
PlanCommon.UpdatePlanOne(currentPlanId.Trim(), newPlanNum, 2, true);
}
if (base.NetType == NetTypes.Net)
{
//更新网络库计划数
PlanCommon.UpdatePlanNum(Basic.DataSourceFactory.MCDbType.Server, currentPlanId, newPlanNum);
//
try
{
string procedureName = "Proc_PlanToYFSUpdate";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@PlanID", currentPlanId);
parameters.Add("@PlanNum", newPlanNum);
base.ExecuteProcedure(Basic.DataSourceFactory.MCDbType.Server, procedureName, parameters);
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("更新计划数,预分散存储过程调用失败:" + ex.Message, ex);
}
}
//更新批次信息
this.UpdateGroupLot(newPlanNum);
//发送网络消息
if (base.NetType == NetTypes.Net)
{
netMsg = "{0}:{1}/";
netMsg = String.Format(netMsg, Global.ProtocalHeader.ReceiveRecipeDownLoadStatu, "101");
Mesnac.Communication.TcpService.Instance.NetSendMsg(netMsg);
string xlruba = String.Empty; //小料物料名称
string xlrubaCode = String.Empty; //小料物料代码
RecipeData recipeData = new RecipeData();
List<RecipeData.RecipeWeightInfo> lst = recipeData.GetCurrentRecipeWeightInfo();
foreach (RecipeData.RecipeWeightInfo rwi in lst)
{
if (rwi.WeightType == (int)RecipeData.WeightType.)
{
xlruba = rwi.MaterialName;
xlrubaCode = rwi.MaterialCode;
}
}
//netMsg = "{0}:{1}|{2}|{3}|{4}|{5:yyyy-MM-dd}|{6}|{7}|{8}|{9}/";
//netMsg = String.Format(netMsg, Global.ProtocalHeader.ReceiveBaseStatu, Convert.ToInt32(planRow["ActionOrder"]) - 1, newPlanNum, realNum, log.LastSelectShiftID, log.LastSelectDate, recipe_code, currentPlanId, xlruba, xlrubaCode);
//Mesnac.Communication.TcpService.Instance.NetSendMsg(netMsg);
#region 把当前计划列表发送给客户端
if (PlanCommon.PlanLog != null)
{
PlanCommon.SendInit(base.CurrEquipCode, PlanCommon.PlanLog.LastSelectDate, PlanCommon.PlanLog.LastSelectShiftID, PlanCommon.PlanLog.LastClassID, base.GetConfigValue("LastUserID", "0"));
}
else
{
string sbNetMsg = Global.ProtocalHeader.ReceivePlanIpNumber + ":/";
Mesnac.Communication.TcpService.Instance.NetSendMsg(sbNetMsg.ToString()); //发送网络消息
ICSharpCode.Core.LoggingService.Debug("没有计划日志!");
}
#endregion
}
if (base.FarControlType == 1)
{
//SendWeighParam();
}
}
}
else
{
frmModifyPlanNum.Dispose();
}
//触发刷新计划事件
if (ModifyPlanNum.OnRefreshPlan != null)
{
ModifyPlanNum.OnRefreshPlan(null, System.EventArgs.Empty);
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("修改次数失败:" + ex.Message, ex);
ShowMsg(ex.Message, Language(1), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#region 简化版修改计划车数用于处理客户端发送的Socket指令
/// <summary>
/// 简化版修改计划车数用于处理客户端发送的Socket指令
/// </summary>
/// <param name="planId">要修改的计划号</param>
/// <param name="planNum">新的生产车数</param>
public void SimpleModifyPlanNum(string planId, int newPlanNum)
{
try
{
string msg = String.Empty;
string currentPlanId = String.Empty; //当前计划号
int planNum = 0; //原始计划数
int realNum = 0; //实际完成数
base.LogDebug("修改次数...");
//获取当前正在执行的计划
PlanLog log = PlanCommon.PlanLog;
if (log == null || String.IsNullOrEmpty(log.LastPlanID))
{
msg = "没有正在执行的计划!\r\n不允许执行此操作";
ICSharpCode.Core.LoggingService.Warn(msg);
return;
}
currentPlanId = log.LastPlanID;
DataRow planRow = PlanCommon.GetPlanData(Basic.DataSourceFactory.MCDbType.Local, currentPlanId);
if (planRow == null)
{
ICSharpCode.Core.LoggingService.Error("获取当前计划数据失败...");
return;
}
planNum = planRow["PlanNum"] == null || planRow["PlanNum"] == System.DBNull.Value ? 0 : Convert.ToInt32(planRow["PlanNum"]);
realNum = planRow["RealNum"] == null || planRow["RealNum"] == System.DBNull.Value ? 0 : Convert.ToInt32(planRow["RealNum"]);
if (planNum != 0 && realNum < planNum && PlcData.Instance.OilFinishedCount.LastValue.ToInt() >= planNum)
{
msg = "当前计划油料已经称量完成!\r\n请当前计划完毕后操作";
ICSharpCode.Core.LoggingService.Warn(msg);
return;
}
if (planNum != 0 && realNum < planNum && PlcData.Instance.PloyFinishedCount.LastValue.ToInt() >= planNum)
{
msg = "当前计划胶料已经称量完成!\r\n请当前计划完毕后操作";
ICSharpCode.Core.LoggingService.Warn(msg);
return;
}
if (planNum != 0 && realNum < planNum && PlcData.Instance.CarbonFinishedCount.LastValue.ToInt() >= planNum)
{
msg = "当前计划炭黑已经称量完成!\r\n请当前计划完毕后操作";
ICSharpCode.Core.LoggingService.Warn(msg);
return;
}
if (newPlanNum < PlcData.Instance.CarbonFinishedCount.LastValue.ToInt())
{
msg = "当前计划设定次数不能低于炭黑称量完成数量!";
ICSharpCode.Core.LoggingService.Warn(msg);
return;
}
if (newPlanNum < PlcData.Instance.PloyFinishedCount.LastValue.ToInt())
{
msg = "当前计划设定次数不能低于胶料称量完成数量!";
ICSharpCode.Core.LoggingService.Warn(msg);
return;
}
if (newPlanNum < PlcData.Instance.OilFinishedCount.LastValue.ToInt())
{
msg = "当前计划设定次数不能低于油料称量完成数量!";
ICSharpCode.Core.LoggingService.Warn(msg);
return;
}
//记录数据库日志
msg = "修改计划:{0}从{1}车到{2}车";
string recipe_code = PlanCommon.GetRecipeCodeByPlanID(currentPlanId);
msg = String.Format(msg, recipe_code, planNum, newPlanNum);
base.DBLog("基本操作", "修改次数", msg);
//发送网络消息
string netMsg = "{0}:{1}/"; //通知预分散上位机车数修改
netMsg = String.Format(netMsg, Global.ProtocalHeader.ReceiveModifyPlanNumber, newPlanNum);
Mesnac.Communication.TcpService.Instance.NetSendMsg(netMsg);
//修改PLC计划数
if (!this.DownLoadModifyNumber(newPlanNum))
{
ICSharpCode.Core.LoggingService.Error("修改计划数失败把新计划数下传至PLC失败");
return;
}
//更新本地数据库
PlanCommon.UpdatePlanNum(currentPlanId, newPlanNum);
if (base.NetType == NetTypes.Net)
{
try
{
string procedureName = "Proc_PlanToYFSUpdate";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@PlanID", currentPlanId);
parameters.Add("@PlanNum", newPlanNum);
base.ExecuteProcedure(Basic.DataSourceFactory.MCDbType.Server, procedureName, parameters);
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("更新计划数,预分散存储过程调用失败:" + ex.Message, ex);
}
}
//更新批次信息
this.UpdateGroupLot(newPlanNum);
//发送网络消息
if (base.NetType == NetTypes.Net)
{
netMsg = "{0}:{1}/";
netMsg = String.Format(netMsg, Global.ProtocalHeader.ReceiveRecipeDownLoadStatu, "101");
Mesnac.Communication.TcpService.Instance.NetSendMsg(netMsg);
string xlruba = String.Empty; //小料物料名称
string xlrubaCode = String.Empty; //小料物料代码
RecipeData recipeData = new RecipeData();
List<RecipeData.RecipeWeightInfo> lst = recipeData.GetCurrentRecipeWeightInfo();
foreach (RecipeData.RecipeWeightInfo rwi in lst)
{
if (rwi.WeightType == (int)RecipeData.WeightType.)
{
xlruba = rwi.MaterialName;
xlrubaCode = rwi.MaterialCode;
}
}
netMsg = "{0}:{1}|{2}|{3}|{4}|{5:yyyy-MM-dd}|{6}|{7}|{8}|{9}/";
netMsg = String.Format(netMsg, Global.ProtocalHeader.ReceiveBaseStatu, Convert.ToInt32(planRow["ActionOrder"]) - 1, newPlanNum, realNum, log.LastSelectShiftID, log.LastSelectDate, recipe_code, currentPlanId, xlruba, xlrubaCode);
Mesnac.Communication.TcpService.Instance.NetSendMsg(netMsg);
}
if (base.FarControlType == 1)
{
//SendWeighParam();
}
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("更新计划数失败:" + ex.Message, ex);
}
}
#endregion
/// <summary>
/// 更新本地库批次信息PptGroupLot
/// </summary>
/// <param name="newPlanNum">新设定的计划数</param>
public void UpdateGroupLot(int newPlanNum)
{
DbHelper dbHelper = base.NewDbHelper(Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
base.LogError("获取网络连接失败...");
return;
}
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
string strSql = "Update PptGroupLot Set SetNumber= @SetNumber where FinishTag=0";
dbHelper.CommandText = strSql;
dbHelper.AddParameter("@SetNumber", newPlanNum);
dbHelper.ExecuteNonQuery();
}
/// <summary>
/// 下传计划数车数到PLC
/// </summary>
/// <param name="newPlanNum">新设定的计划数</param>
/// <returns>成功返回true失败返回false</returns>
public bool DownLoadModifyNumber(int newPlanNum)
{
bool result = false;
result = this.DownLoadSetNumber(newPlanNum) && this.DownLoadRubNumber(newPlanNum);
return result;
}
/// <summary>
/// 下传密炼设定次数到PLC
/// </summary>
/// <returns>成功返回true失败返回false</returns>
public bool DownLoadSetNumber(int nNumber)
{
bool result = PlcData.Instance.PlcWriteByDataKey(PlcData.Instance.RecipeSetNumber, new object[] { nNumber });
if (result == false)
{
base.LogError("PLC通讯故障:[下传密炼设定次数到PLC]");
}
return result;
}
/// <summary>
/// 下传称量设定次数到PLC
/// </summary>
/// <param name="mNumber">设定的次数</param>
/// <returns>成功返回true失败返回false</returns>
public bool DownLoadRubNumber(int nNumber)
{
bool result = PlcData.Instance.PlcWriteByDataKey(PlcData.Instance.WeightSetNumber, new object[] { nNumber });
if(result == false)
{
base.LogError("PLC通讯故障:[下传称量设定次数到PLC]");
}
return result;
}
}
}