using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using ICSharpCode.Core;
using Mesnac.Controls.Base;
using Mesnac.Action.Base;
using Mesnac.Codd.Session;
using Mesnac.Action.ChemicalWeighing.Entity;
namespace Mesnac.Action.ChemicalWeighing.Product.PptPlan
{
///
/// 生产管理-当班计划-终止计划业务
///
public class StopAction : ChemicalWeighingAction, IAction
{
#region 字段定义
private RuntimeParameter _runtime;
private DbMCControl _clientGridControl = null; //本机计划控件
#endregion
#region 事件定义
///
/// 刷新计划事件
///
public static event EventHandler OnStopPlan;
#endregion
#region 业务入口
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime); //必须要调用的
if (runtime.Sender is Control)
{
(runtime.Sender as Control).Enabled = false;
}
try
{
ICSharpCode.Core.LoggingService.Debug("生产管理-生产计划-终止计划业务...");
#region 定义变量
string selectedPlanId = null; //选中的计划号
int selectedPlanSerial = 0; //保存选中的计划序号
int selectedPlanNum = 0; //保存选中计划的计划数
int selectedPlanUnfinished = 0; //计划的剩余数
#endregion
#region 检验执行终止计划的条件
#region 判断当班计划界面是否打开
if (PlanHelper.IsInit == false)
{
string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_StopAction_msg1")); // 当班计划还未初始化,请先打开当班计划,再进行此操作!
MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
#endregion
#region 检验本地数据连接
DbHelper localHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (localHelper == null)
{
string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_StopAction_msg2")); //本地数据库连接异常,不能终止计划{0}请在数据库连接正常后再进行“终止”操作。
msg2 = String.Format(msg2, "\r\n");
MessageBox.Show(msg2, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
#endregion
#region 获取界面控件
DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "RT_plan").FirstOrDefault();
if (clientGridControl == null)
{
ICSharpCode.Core.LoggingService.Error("{生产计划-终止计划} 缺少计划列表网格控件...");
return;
}
this._runtime = runtime;
this._clientGridControl = clientGridControl;
DataGridView clientGridView = this._clientGridControl.BaseControl as DataGridView;
#endregion
#region 判断是否选择了要终止的计划
if (clientGridView.SelectedRows.Count != 1)
{
string msg6_1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_StopAction_msg6_1")); //请选择一条要终止的计划!
MessageBox.Show(msg6_1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
this._runtime.IsReturn = true;
return;
}
selectedPlanId = clientGridView.SelectedRows[0].Cells["Plan_Id"].Value as string; //获取选中的计划号
selectedPlanSerial = Convert.ToInt32(clientGridView.SelectedRows[0].Cells["Plan_Serial"].Value); //获取选中的计划序号
selectedPlanNum = Convert.ToInt32(clientGridView.SelectedRows[0].Cells["Plan_Num"].Value); //获取选中的计划数
selectedPlanUnfinished = selectedPlanNum - Convert.ToInt32(clientGridView.SelectedRows[0].Cells["Real_Num"].Value); //获取选中的剩余数
#endregion
#region 判断计划状态是否合法
//PlanState planState = (PlanState)Convert.ToInt32(clientGridView.SelectedRows[0].Cells["Plan_State"].Value);
PlanState planState = (PlanState)PlanHelper.StateToValue(clientGridView.SelectedRows[0].Cells["Plan_StateText"].Value.ToString());
if (planState == PlanState.Terminated)
{
string msg7 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_StopAction_msg7_2")); //当前计划已终止,不能执行此操作!
MessageBox.Show(msg7, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (planState == PlanState.Completed)
{
string msg7 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_StopAction_msg7_3")); //当前计划已完成,不能执行此操作!
MessageBox.Show(msg7, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
#endregion
#endregion
#region 终止计划业务
string msg8 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_StopAction_msg8")); //当前计划序号{0}设定{1}车,{2}您确认暂停?
msg8 = String.Format(msg8, selectedPlanSerial, selectedPlanNum, "\r\n");
DialogResult result = MessageBox.Show(msg8, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
List msgList = new List();
#region 刷新数据库
if (planState == PlanState.Producting)
{
string weighbridge = clientGridView.SelectedRows[0].Cells["Weighbridge"].Value.ToString();
if (weighbridge == "1号地磅")
{
BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.solvent_stopScale, new object[] { 0 });
}
if (weighbridge == "2号地磅")
{
BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.solvent2_stopScale, new object[] { 0 });
}
}
PlanHelper.UpdatePlanState(selectedPlanId, 7);
#endregion
#region 消息提示
string msg11 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_StopAction_msg11")); //终止计划命令发送成功!
if (Global.PublicVar.Instance.MessageDialogMode == 0)
{
Mesnac.Basic.MessageBoxTimeOut.Show(msg11, Mesnac.Basic.LanguageHelper.Caption, 3000);
}
else
{
MessageBox.Show(msg11, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#endregion
#region 判断等待计划数和正在运行的计划数。如果两个都是0,清空RT_Plan表。
//int waitPlanCount = Product.PptPlan.PlanHelper.GetUnExePlanNum();
//int runningPlanCount = Product.PptPlan.PlanHelper.GetRunningPlanNum();
//int downPlanCount = Product.PptPlan.PlanHelper.GetDownPlanNum();
//if (waitPlanCount == 0 && runningPlanCount == 0 && downPlanCount == 0)
//{
// Product.PptPlan.PlanHelper.ClearRT_Plan();
//}
#endregion
#region 触发终止计划事件
if (OnStopPlan != null)
{
OnStopPlan(runtime, System.EventArgs.Empty);
}
#endregion
}
#endregion
}
catch (Exception ex)
{
ICSharpCode.Core.LoggingService.Error("终止计划失败:" + ex.Message, ex);
#region 记录操作日志
//base.DBLog(ex.Message);
#endregion
MessageBox.Show(ex.Message, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
if (runtime.Sender is Control)
{
(runtime.Sender as Control).Enabled = true;
}
}
}
#endregion
}
}