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;
using Mesnac.Action.ChemicalWeighing.Product.PptPlan.entity;

namespace Mesnac.Action.ChemicalWeighing.Product.PptPlan
{
    /// <summary>
    /// 生产管理-当班计划-终止计划业务
    /// </summary>
    public class StopAction : ChemicalWeighingAction, IAction
    {
        #region 字段定义

        private RuntimeParameter _runtime;
        private DbMCControl _clientGridControl = null;      //本机计划控件

        #endregion

        #region 事件定义

        /// <summary>
        /// 刷新计划事件
        /// </summary>
        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<StopAction>.Debug("生产管理-生产计划-终止计划业务...");

                #region 定义变量

                string selectedPlanId = null;                 //选中的计划号
                int selectedPlanSerial = 0;                   //保存选中的计划序号
                int selectedPlanCommand = 5;                  //计划命令:pcStart = 1 , pcHold = 2 , pcEdit = 3 , pcDelete = 4 , pcTerminate=5
                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, "Base_PlanInfo").FirstOrDefault();
                if (clientGridControl == null)
                {
                    ICSharpCode.Core.LoggingService<StopAction>.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_order"].Value);                        //获取选中的计划序号
                //selectedPlanNum = Convert.ToInt32(clientGridView.SelectedRows[0].Cells["plan_amount"].Value);                              //获取选中的计划数
                //selectedPlanUnfinished = selectedPlanNum - Convert.ToInt32(clientGridView.SelectedRows[0].Cells["real_amount"].Value);     //获取选中的剩余数
                //int.TryParse(clientGridView.SelectedRows[0].Cells["PlanNum"].Value.ToString(), out selectedPlanNum);

                #endregion

                #region 判断计划状态是否合法

                //PlanState planState = (PlanState)Convert.ToInt32(clientGridView.SelectedRows[0].Cells["Plan_State"].Value);
                string planState = clientGridView.SelectedRows[0].Cells["plan_state"].Value.ToString();
                if (planState == "待执行")
                {
                    string msg6 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_StopAction_msg7_1"));      //当前计划已终止,不能执行此操作!
                    MessageBox.Show(msg6, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (planState == "已终止")
                {
                    string msg7 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_StopAction_msg7_2"));      //当前计划已终止,不能执行此操作!
                    MessageBox.Show(msg7, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (planState == "已完成")
                {
                    string msg7 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_StopAction_msg7_3"));      //当前计划已完成,不能执行此操作!
                    MessageBox.Show(msg7, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    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<string> msgList = new List<string>();
                    if (ChemicalWeighingPlc.PlcPlanHelper.ExecStop(selectedPlanSerial,selectedPlanCommand,selectedPlanNum,selectedPlanUnfinished, true, msgList))
                    {
                        #region 刷新数据库

                        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
                    }
                    else
                    {
                        string logContent = Mesnac.Basic.DataProcessor.CombineMsgList(msgList);
                        //base.DBLog(logContent);
                    }
                }*/




                #endregion

                //下传终止信号
                bool writeRpCancel = false;

                if (writeRpCancel)
                {
                    Base_PlanInfo planInfo = PlanHelper.getPlanInfoByPlanId(selectedPlanId);
                    planInfo.plan_State = 3;
                    PlanHelper.UpdateBasePlanInfo(planInfo);
                    #region 触发事件

                    if (OnStopPlan != null)
                    {
                        OnStopPlan(null, System.EventArgs.Empty);
                    }
                    ICSharpCode.Core.LoggingService<StopAction>.Debug("终止计划成功!");
                    #endregion
                    MessageBox.Show("终止信号下传成功", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    ICSharpCode.Core.LoggingService<StopAction>.Debug("终止计划失败!");
                    MessageBox.Show("终止信号下传失败", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                ICSharpCode.Core.LoggingService<StopAction>.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
    }
}