using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DBFactory;
using System.Net.Sockets;
using System.Threading;
using ICommLayer;
using CommLayerFactory;
using Microsoft.VisualBasic;


namespace ControlSystem
{
    /// <summary>
    /// Creator:Richard.liu
    /// 调度任务管理
    /// </summary>
    public partial class FrmModifyManage : Form
    {
        string _manstatus= " F_Status<>-1 ";
        private static FrmModifyManage _formInstance;

        public static FrmModifyManage FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmModifyManage();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        Model.MDevice devinfo;
        DBOperator dbo = CStaticClass.dbo;
        DBOperator dboM = CStaticClass.dboM;
        public FrmModifyManage()
        {
            InitializeComponent();
            _formInstance = this;
        }

        private void rbmanAll_CheckedChanged(object sender, EventArgs e)
        {

            RadioButton rb = (RadioButton)sender;
            if (rb.Checked == true)
            {
                _manstatus = " F_Status<>-1 ";
            }
            TSMrefresh_Click(sender, e);
        }

        private void TSMrefresh_Click(object sender, EventArgs e)
        {
            CStaticClass.RealRefresh = true;
            this.dgvManager.DataSource = dbo.ExceSQL("select * from V_Manage_Task where " + _manstatus).Tables[0].DefaultView;
        }

        private void rbmanRun_CheckedChanged(object sender, EventArgs e)
        {
            if (rbmanRun.Checked == true)
            {
                _manstatus = " F_Status>=1 ";
            }
            TSMrefresh_Click(sender, e);
        }

        private void rbmanWait_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked == true)
            {
                _manstatus = " F_Status=0 ";
            }
            TSMrefresh_Click(sender, e);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (this.textBox1.Text.Trim().Length == 0) return;    
            int index = 0;
            bool isint = int.TryParse(this.textBox1.Text.Trim(), out index);
            if (this.comboBox1.Text == "表箱条码")
            {
                //20091016
                _manstatus = " 表箱条码 like'%" + this.textBox1.Text.Trim() + "%' ";
            }

            if (this.comboBox1.Text == "调度任务索引")
            {
                if(isint==false)
                {
                    //MessageBox.Show("请输入数字！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox1.Text = string.Empty;
                    return;
                }
                
                _manstatus = " 调度任务索引='" + this.textBox1.Text.Trim() + "' ";

            }
            TSMrefresh_Click(sender, e); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            _manstatus = " F_Status<>-1 ";

            TSMrefresh_Click(sender, e);
        }

        private void dgvManager_DataSourceChanged(object sender, EventArgs e)
        {
            dgvManager.Columns["F_Status"].Visible = false;
        }

        private void cmManager_Opening(object sender, CancelEventArgs e)
        {
            if (this.dgvManager.CurrentRow == null)
            {
                TSMmanagerOK.Visible = false;
                //TSMrefresh.Visible = false;
                TSManager.Visible = false;
                toolStripSeparator1.Visible = false;
                //TSMmanagerDisassemble.Visible = false;
                toolStripMenuItem1.Visible = false;
                toolStripSeparator2.Visible = false;
                return;
            }
            if (Convert.ToInt32(this.dgvManager.CurrentRow.Cells["调度任务索引"].Value) > 0)
            {
                TSMmanagerOK.Visible = true;
                
                TSManager.Visible = true;
                toolStripSeparator1.Visible = true;
                toolStripSeparator2.Visible = true;
                //TSMmanagerDisassemble.Visible = true;
                toolStripMenuItem1.Visible = true;
                TSManager.Text = "任务类型：" + this.dgvManager.CurrentRow.Cells["任务类型"].Value.ToString() +
                    "；调度任务索引：" + this.dgvManager.CurrentRow.Cells["调度任务索引"].Value.ToString();


            }
        }

        private void TSMmanagerOK_Click(object sender, EventArgs e)
        {
            try
            {
                
                char[] cc = new char[1] { '：' };
                string[] split = TSManager.Text.Split(cc);
                if ((split[1] == "") || (split[2] == ""))
                {
                    return;
                }
                char[] scc = new char[1] { '；' };
                string[] strsplit = split[1].Split(scc);
                int mti = 0;
                int fid = Convert.ToInt32(split[2]);
                if (strsplit[0] == "") return;
                switch (strsplit[0])
                {
                    case "调度任务":
                        mti = 1;
                        break;
                    case "自动任务":
                        mti = 2;
                        break;
                    case "临时任务":
                        mti = 3;
                        break;
                    case "手工任务":
                        mti = 4;
                        break;
                }
                string cap = strsplit[0];
                string hw = "";
                if (this.dgvManager.CurrentRow.Cells["终点货位"].Value.ToString() != "-")
                {
                    hw = ",终点货位:" + this.dgvManager.CurrentRow.Cells["终点货位"].Value.ToString();
                }
                //20100108
                if (MessageBox.Show("您确认表箱条码：“" + this.dgvManager.CurrentRow.Cells["表箱条码"].Value.ToString()+"”，" + cap + split[2] + "已经被搬运到终点设备“" + this.dgvManager.CurrentRow.Cells["终点"].Value.ToString() + hw + "”了吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                {
                    return;
                }
                CGetState cgs = new CGetState();
                dbo.ExceSQL("update T_Monitor_Task set F_STATUS=-1 where F_ManageTaskKindIndex=" + mti + " and F_ManageTaskIndex=" + fid + " and F_STATUS=0");
                DataView dv = dbo.ExceSQL("SELECT F_ManageTaskKindIndex, F_ManageTaskIndex,F_DeviceIndex,F_MonitorIndex,F_Status,F_DeviceCommandIndex " +
                    ",F_NumParam2,F_NumParam5,F_TxtParam FROM T_Monitor_Task Where  F_ManageTaskIndex=" + fid + " and F_ManageTaskKindIndex= " +
                    mti).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    string sss = "调度任务索引:" + fid.ToString();
                    //20100108
                    CommonClassLib.CCarryConvert.WriteDarkCasket("调度任务管理", "调度任务手工报告完成", "表箱条码：" + this.dgvManager.CurrentRow.Cells["表箱条码"].Value.ToString(), sss);
                    for (int i = 0; i < dv.Count; i++)
                    {
                        if (Convert.ToInt32(dv[i]["F_DeviceIndex"]) == 1001)
                        {
                            cgs.ActionComplete(Convert.ToInt32(dv[i]["F_DeviceIndex"]), Convert.ToInt32(dv[i]["F_MonitorIndex"]), Model.CGeneralFunction.TASKFINISH);
                            //1	辅料出库;2	空托盘回收;3残托盘回收
                            if (Convert.ToInt32(dv[i]["F_DeviceCommandIndex"]) == 1)
                            {
                                DataTable tableAGVGate = dbo.ExceSQL("SELECT F_AGVGateDeviceIndex FROM T_BASE_AGV_GATE WHERE F_ADDRESS = " + Convert.ToInt32(dv[i]["F_NumParam5"]) + "").Tables[0];
                                if (tableAGVGate.Rows.Count > 0)
                                {

                                    dbo.ExecuteSql("UPDATE T_BASE_DEVICE SET F_PALLETBARCODE='" + dv[i]["F_TxtParam"] + "',F_HAVEGOODS = 1 WHERE F_DEVICEINDEX = " + Convert.ToInt32(tableAGVGate.Rows[0]["F_AGVGateDeviceIndex"]) + "");

                                }
                            }
                            if ((Convert.ToInt32(dv[i]["F_DeviceCommandIndex"]) == 2) || (Convert.ToInt32(dv[i]["F_DeviceCommandIndex"]) == 3))
                            {
                                DataTable tableAGVGate = dbo.ExceSQL("SELECT F_AGVGateDeviceIndex FROM T_BASE_AGV_GATE WHERE F_ADDRESS = " + Convert.ToInt32(dv[i]["F_NumParam2"]) + "").Tables[0];
                                if (tableAGVGate.Rows.Count > 0)
                                {
                                    if (mti == 1)
                                    {
                                        //20090817给管理置无托盘信息
                                        dboM.ExecuteSql("UPDATE T_ITEMDEVICE SET devicestatus  ='0' WHERE devicecode = '" + tableAGVGate.Rows[0]["F_AGVGateDeviceIndex"] + "'");
                                    }
                                    dbo.ExecuteSql("UPDATE T_BASE_DEVICE SET F_PALLETBARCODE='-',F_HAVEGOODS = 0 WHERE F_DEVICEINDEX = " + Convert.ToInt32(tableAGVGate.Rows[0]["F_AGVGateDeviceIndex"]) + "");

                                }
                            }
                        }
                        else
                        {
                            if (Convert.ToInt32(dv[i]["F_Status"]) >= 1)
                            {
                                cgs.ActionComplete(Convert.ToInt32(dv[i]["F_DeviceIndex"]), Convert.ToInt32(dv[i]["F_MonitorIndex"]), 1);
                            }
                            else
                            {
                                cgs.ActionComplete(Convert.ToInt32(dv[i]["F_DeviceIndex"]), Convert.ToInt32(dv[i]["F_MonitorIndex"]), Model.CGeneralFunction.TASKFINISH);
                            }
                        }
                    }
                }
                else
                {

                    if (mti == 1)
                    {
                        int startdevice= Convert.ToInt32(this.dgvManager.CurrentRow.Cells["起点"].Value);
                        int enddevice = Convert.ToInt32(this.dgvManager.CurrentRow.Cells["终点"].Value);
                       
                        if(enddevice==18007||enddevice==12049||enddevice==12054)
                        {

                            //提示表箱所在输送机
                         //   dboM.ExceSQL("update IO_Control set  Control_STATUS=" + Model.CGeneralFunction.TASKPACKFINISH + " where Control_ID=" + fid);
                        }
                        else
                        if(startdevice==32080&&enddevice==32080)
                        {
                          //dboM.ExceSQL("update IO_Control set  Control_STATUS=" + Model.CGeneralFunction.TASKUNPACKFINISH + " where Control_ID=" + fid);

                        }
                        else
                        {

                        dboM.ExceSQL("update IO_Control set  Control_STATUS=" + Model.CGeneralFunction.TASKFINISH + " where Control_ID=" + fid);
                        }
                    }
                    //回写管理表
                    cgs.ReturnManageInfo(fid, mti, cap, true);
                }
                this.dgvManager.DataSource = dbo.ExceSQL("select * from V_Manage_Task where " + _manstatus).Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                //throw ex;
                if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("调度任务手工完成时：" + ex.Message) < 0)
                {

                    FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "调度任务手工完成时：" + ex.Message, true);
                }
            }
        }

        private void TSMmanagerDisassemble_Click(object sender, EventArgs e)
        {
            try
            {
                char[] cc = new char[1] { '：' };
                string[] split = TSManager.Text.Split(cc);
                if ((split[1] == "") || (split[2] == ""))
                {
                    return;
                }
                char[] scc = new char[1] { '；' };
                string[] strsplit = split[1].Split(scc);
                int mti = 0;
                int fid = Convert.ToInt32(split[2]);
                if (strsplit[0] == "") return;
                switch (strsplit[0])
                {
                    case "调度任务":
                        mti = 1;
                        break;
                    case "自动任务":
                        mti = 2;
                        break;
                    case "临时任务":
                        mti = 3;
                        break;
                    case "手工任务":
                        mti = 4;
                        break;
                }
                if (mti == 1)
                {
                    dbo.ExceSQL("update T_Manage_Task set FIntoStepOK='0' where FID=" + fid + " and FIntoStepOK='-' ");
                }
            }
            catch (Exception ex)
            {
                //throw ex;
                if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("手工拆分调度任务时：" + ex.Message) < 0)
                {

                    FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "手工拆分调度任务时：" + ex.Message, true);
                }
            }
        }

        private void FrmModifyManage_Load(object sender, EventArgs e)
        {
            rbmanRun_CheckedChanged(sender, e);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //删除调度任务900
            try
            {
                char[] cc = new char[1] { '：' };
                string[] split = TSManager.Text.Split(cc);
                if ((split[1] == "") || (split[2] == ""))
                {
                    return;
                }
                char[] scc = new char[1] { '；' };
                string[] strsplit = split[1].Split(scc);
                int mti = 0;
                int fid = Convert.ToInt32(split[2]);
                if (strsplit[0] == "") return;
                switch (strsplit[0])
                {
                    case "调度任务":
                        mti = 1;
                        break;
                    case "自动任务":
                        mti = 2;
                        break;
                    case "临时任务":
                        mti = 3;
                        break;
                    case "手工任务":
                        mti = 4;
                        break;
                }
                string hw = "";
                if (this.dgvManager.CurrentRow.Cells["起点货位"].Value.ToString() != "-")
                {
                    hw = ",起点货位:" + this.dgvManager.CurrentRow.Cells["起点货位"].Value.ToString();
                }
                string cap = strsplit[0];
                if (MessageBox.Show("您确认表箱条码：“" + this.dgvManager.CurrentRow.Cells["表箱条码"].Value.ToString() + "”，" + cap + split[2] + "已经被取消操作，而且人工搬运托盘退回到起点设备“" + this.dgvManager.CurrentRow.Cells["起点"].Value.ToString() + hw + "”了吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                {
                    return;
                }
                CGetState cgs = new CGetState();
                //20090902
                dbo.ExceSQL("update T_Manage_Task set FExceptionNO=" + Model.CGeneralFunction.TASKDELETE + " where (F_ManageTaskKindIndex = " + mti + ") AND (FID = " + fid + ")");
                
                dbo.ExceSQL("update T_Monitor_Task set F_STATUS=-1 where F_ManageTaskKindIndex=" + mti + " and F_ManageTaskIndex=" + fid + " and F_STATUS=0");
                DataView dv = dbo.ExceSQL("SELECT F_ManageTaskKindIndex, F_ManageTaskIndex,F_DeviceIndex,F_MonitorIndex,F_Status,F_DeviceCommandIndex " +
                    ",F_NumParam2,F_NumParam5,F_TxtParam FROM T_Monitor_Task Where  F_ManageTaskIndex=" + fid + " and F_ManageTaskKindIndex= " +
                    mti).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    string sss = "调度任务索引:" + fid.ToString();
                    //20100108
                    CommonClassLib.CCarryConvert.WriteDarkCasket("调度任务管理", "调度任务被手工删除！", "表箱条码：" + this.dgvManager.CurrentRow.Cells["表箱条码"].Value.ToString(), sss);
                    for (int i = 0; i < dv.Count; i++)
                    {
                        if (Convert.ToInt32(dv[i]["F_DeviceIndex"]) == 1001)
                        {
                            cgs.ActionComplete(Convert.ToInt32(dv[i]["F_DeviceIndex"]), Convert.ToInt32(dv[i]["F_MonitorIndex"]), Model.CGeneralFunction.TASKDELETE);
                            //1	辅料出库;2	空托盘回收;3残托盘回收
                            
                            if ((Convert.ToInt32(dv[i]["F_DeviceCommandIndex"]) == 2) || (Convert.ToInt32(dv[i]["F_DeviceCommandIndex"]) == 3))
                            {
                                DataTable tableAGVGate = dbo.ExceSQL("SELECT F_AGVGateDeviceIndex FROM T_BASE_AGV_GATE WHERE F_ADDRESS = " + Convert.ToInt32(dv[i]["F_NumParam2"]) + "").Tables[0];
                                if (tableAGVGate.Rows.Count > 0)
                                {

                                    dbo.ExecuteSql("UPDATE T_BASE_DEVICE SET F_PALLETBARCODE='" + dv[i]["F_TxtParam"] + "',F_HAVEGOODS = 1 WHERE F_DEVICEINDEX = " + Convert.ToInt32(tableAGVGate.Rows[0]["F_AGVGateDeviceIndex"]) + "");

                                }
                            }
                        }
                        else
                        {
                            if (Convert.ToInt32(dv[i]["F_Status"]) >= 1)
                            {
                                cgs.ActionComplete(Convert.ToInt32(dv[i]["F_DeviceIndex"]), Convert.ToInt32(dv[i]["F_MonitorIndex"]), 1);
                            }
                            else
                            {
                                cgs.ActionComplete(Convert.ToInt32(dv[i]["F_DeviceIndex"]), Convert.ToInt32(dv[i]["F_MonitorIndex"]), Model.CGeneralFunction.TASKDELETE);
                            }
                        }
                    }
                }
                else
                {//20091107
                    if (this.dgvManager.CurrentRow.Cells["终点"].Value.ToString() == "2240")
                    {
                        dbo.ExceSQL("update T_Base_Device set F_CreateLock='0' where F_DeviceIndex=" + this.dgvManager.CurrentRow.Cells["终点"].Value);
                    }
                    if (mti == 1)
                    {
                        dboM.ExceSQL("update IO_Control set Control_STATUS=" + Model.CGeneralFunction.TASKDELETE + " where Control_ID=" + fid);
                    }
                    //回写管理表
                    cgs.ReturnManageInfo(fid, mti, cap, false);
                }
                this.dgvManager.DataSource = dbo.ExceSQL("select * from V_Manage_Task where " + _manstatus).Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                //throw ex;
                if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("调度任务手工完成时：" + ex.Message) < 0)
                {

                    FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "调度任务手工完成时：" + ex.Message, true);
                }
            }
        }
       
        int GetDeviceKindIdx(int devIdx)
        {
            try
            {
                devinfo = Model.CGetInfo.GetDeviceInfo(devIdx);
                return devinfo.DeviceKind;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        int GetManageTaskIndexfromMonitor(int monitorIdx)
        {
            DataView dv = dbo.ExceSQL("SELECT F_ManageTaskIndex FROM T_Monitor_Task WHERE (F_MonitorIndex = " + monitorIdx + ")").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                return Convert.ToInt32(dv[0]["F_ManageTaskIndex"]);
            }
            else
            {
                return -1;
            }
        }
        int GetManageTaskKindIndexFromMonitor(int monitorIdx)
        {
            DataView dv = dbo.ExceSQL("SELECT F_ManageTaskKindIndex FROM T_Monitor_Task WHERE (F_MonitorIndex = " + monitorIdx + ")").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                return Convert.ToInt32(dv[0]["F_ManageTaskKindIndex"]);
            }
            else
            {
                return -1;
            }
        }
        int GetDeviceOrderFromMonitor(int MonitorIndex)
        {
            try
            {
                string sql = "select F_DeviceCommandIndex from T_Monitor_Task where (F_DeviceCommandIndex IS NOT NULL) and F_MonitorIndex=" + MonitorIndex;
                DataView dv = dbo.ExceSQL(sql).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    return Convert.ToInt32(dv[0]["F_DeviceCommandIndex"]);
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    

        private void dgvManager_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if ((e.RowIndex >= 0) && (e.Button == MouseButtons.Right) && (e.ColumnIndex>=0))
            {
                dgvManager.ClearSelection();
                dgvManager.Rows[e.RowIndex].Selected = true;
                dgvManager.CurrentCell = dgvManager.Rows[e.RowIndex].Cells[e.ColumnIndex];

            }
        }

        private void TSMIMANAGEEXCEPTION_Click(object sender, EventArgs e)
        {
            try
            {

                char[] cc = new char[1] { '：' };
                string[] split = TSManager.Text.Split(cc);
                if ((split[1] == "") || (split[2] == ""))
                {
                    return;
                }
                char[] scc = new char[1] { '；' };
                string[] strsplit = split[1].Split(scc);
                int mti = 0;
                int fid = Convert.ToInt32(split[2]);
                if (strsplit[0] == "") return;
                switch (strsplit[0])
                {
                    case "调度任务":
                        mti = 1;
                        break;
                    case "自动任务":
                        mti = 2;
                        break;
                    case "临时任务":
                        mti = 3;
                        break;
                    case "手工任务":
                        mti = 4;
                        break;
                }
                string cap = strsplit[0];
                string hw = "";
                if (this.dgvManager.CurrentRow.Cells["终点货位"].Value.ToString() != "-")
                {
                    hw = ",终点货位:" + this.dgvManager.CurrentRow.Cells["终点货位"].Value.ToString();
                }
                //20100108
                if (MessageBox.Show("您确认将表箱条码：“" + this.dgvManager.CurrentRow.Cells["表箱条码"].Value.ToString() + "的任务异常完成吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                {
                    return;
                }
                CGetState cgs = new CGetState();
                dbo.ExceSQL("update T_Manage_Task set FExceptionNO=" + Model.CGeneralFunction.TASKABEND + ",FERRORCODE= '手工报异常完成' where (F_ManageTaskKindIndex = " + mti + ") AND (FID = " + fid + ")");
                dbo.ExceSQL("update T_Monitor_Task set F_STATUS=-1 where F_ManageTaskKindIndex=" + mti + " and F_ManageTaskIndex=" + fid + " and F_STATUS=0");
                DataView dv = dbo.ExceSQL("SELECT F_ManageTaskKindIndex, F_ManageTaskIndex,F_DeviceIndex,F_MonitorIndex,F_Status,F_DeviceCommandIndex " +
                    ",F_NumParam2,F_NumParam5,F_TxtParam FROM T_Monitor_Task Where  F_ManageTaskIndex=" + fid + " and F_ManageTaskKindIndex= " +
                    mti).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    string sss = "调度任务索引:" + fid.ToString();
                    //20100108
                    CommonClassLib.CCarryConvert.WriteDarkCasket("调度任务管理", "调度任务手工报告异常完成", "表箱条码：" + this.dgvManager.CurrentRow.Cells["表箱条码"].Value.ToString(), sss);
                    for (int i = 0; i < dv.Count; i++)
                    {
                        if (Convert.ToInt32(dv[i]["F_DeviceIndex"]) == 1001)
                        {
                            cgs.ActionComplete(Convert.ToInt32(dv[i]["F_DeviceIndex"]), Convert.ToInt32(dv[i]["F_MonitorIndex"]), Model.CGeneralFunction.TASKFINISH);
                            //1	辅料出库;2	空托盘回收;3残托盘回收
                            if (Convert.ToInt32(dv[i]["F_DeviceCommandIndex"]) == 1)
                            {
                                DataTable tableAGVGate = dbo.ExceSQL("SELECT F_AGVGateDeviceIndex FROM T_BASE_AGV_GATE WHERE F_ADDRESS = " + Convert.ToInt32(dv[i]["F_NumParam5"]) + "").Tables[0];
                                if (tableAGVGate.Rows.Count > 0)
                                {

                                    dbo.ExecuteSql("UPDATE T_BASE_DEVICE SET F_PALLETBARCODE='" + dv[i]["F_TxtParam"] + "',F_HAVEGOODS = 1 WHERE F_DEVICEINDEX = " + Convert.ToInt32(tableAGVGate.Rows[0]["F_AGVGateDeviceIndex"]) + "");

                                }
                            }
                            if ((Convert.ToInt32(dv[i]["F_DeviceCommandIndex"]) == 2) || (Convert.ToInt32(dv[i]["F_DeviceCommandIndex"]) == 3))
                            {
                                DataTable tableAGVGate = dbo.ExceSQL("SELECT F_AGVGateDeviceIndex FROM T_BASE_AGV_GATE WHERE F_ADDRESS = " + Convert.ToInt32(dv[i]["F_NumParam2"]) + "").Tables[0];
                                if (tableAGVGate.Rows.Count > 0)
                                {
                                    if (mti == 1)
                                    {
                                        //20090817给管理置无托盘信息
                                        dboM.ExecuteSql("UPDATE T_ITEMDEVICE SET devicestatus  ='0' WHERE devicecode = '" + tableAGVGate.Rows[0]["F_AGVGateDeviceIndex"] + "'");
                                    }
                                    dbo.ExecuteSql("UPDATE T_BASE_DEVICE SET F_PALLETBARCODE='-',F_HAVEGOODS = 0 WHERE F_DEVICEINDEX = " + Convert.ToInt32(tableAGVGate.Rows[0]["F_AGVGateDeviceIndex"]) + "");

                                }
                            }
                        }
                        else
                        {
                            if (Convert.ToInt32(dv[i]["F_Status"]) >= 1)
                            {
                                cgs.ActionComplete(Convert.ToInt32(dv[i]["F_DeviceIndex"]), Convert.ToInt32(dv[i]["F_MonitorIndex"]), 1);
                            }
                            else
                            {
                                cgs.ActionComplete(Convert.ToInt32(dv[i]["F_DeviceIndex"]), Convert.ToInt32(dv[i]["F_MonitorIndex"]), Model.CGeneralFunction.TASKABEND);
                            }
                        }
                    }
                }
                else
                {

                    if (mti == 1)
                    {
                        int startdevice = Convert.ToInt32(this.dgvManager.CurrentRow.Cells["起点"].Value);
                        int enddevice = Convert.ToInt32(this.dgvManager.CurrentRow.Cells["终点"].Value);

                        if (enddevice == 18007 || enddevice == 12049 || enddevice == 12054)
                        {

                            //提示表箱所在输送机
                            dboM.ExceSQL("update IO_Control set  Control_STATUS=" + Model.CGeneralFunction.TASKPACKFINISH + " where Control_ID=" + fid);
                        }
                        else
                            if (startdevice == 32080 && enddevice == 32080)
                            {
                                dboM.ExceSQL("update IO_Control set  Control_STATUS=" + Model.CGeneralFunction.TASKUNPACKFINISH + " where Control_ID=" + fid);

                            }
                            else
                            {

                                dboM.ExceSQL("update IO_Control set  Control_STATUS=" + Model.CGeneralFunction.TASKABEND + " where Control_ID=" + fid);
                            }
                    }
                    //回写管理表
                    cgs.ReturnManageInfo(fid, mti, cap, true);
                }
                this.dgvManager.DataSource = dbo.ExceSQL("select * from V_Manage_Task where " + _manstatus).Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                //throw ex;
                if (FrmControlMonitor.FormInstance.GetObjectText("tsStatus").IndexOf("调度任务手工报异常完成时：" + ex.Message) < 0)
                {

                    FrmControlMonitor.FormInstance.FlashPanit("tsStatus", "调度任务手工报异常完成时：" + ex.Message, true);
                }
            }
        }

        private void dgvManager_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {

        }

        
    }
}