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 ICommLayer;
using CommLayerFactory;
using Microsoft.VisualBasic;
namespace ControlSystem
{
    /// <summary>
    /// Creator:Richard.liu
    /// 下达手工任务--单一设备
    /// </summary>
    public partial class FrmHandCommand : Form
    {
        private static FrmHandCommand _formInstance;

        public static FrmHandCommand FormInstance
        {
            get 
            {
                if (_formInstance  == null)
                {
                    _formInstance = new FrmHandCommand();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        CCommonFunction ccf = new CCommonFunction();
        DBOperator dbo = CStaticClass.dbo;
        #region ====变量定义

        CommonClassLib.UCellPanel cellPanel = null;

        #endregion
        public FrmHandCommand()
        {
            InitializeComponent();
            _formInstance = this;
            dbo.Open();

        }
        
        private void FrmHandCommand_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
            if (cellPanel == null)
            {
                cellPanel = new CommonClassLib.UCellPanel(this, txtCellCode, null, null, string.Empty);
                cellPanel.TaskType = "100";
                cellPanel.GoodsName = "";
                cellPanel.Dock = DockStyle.Fill;
                cellPanel.AllowWareHouseChanged = false;
                plCell.Controls.Add(cellPanel);

            }

            //20090910  and F_DBW1Address is not null 
            DataView dv = dbo.ExceSQL("SELECT T_Base_Device.F_DeviceIndex, T_Base_Device.F_DeviceKindIndex," +
                " T_Base_Device.F_DeviceName, T_Base_Device_Kind.F_GoodsMoveKindIndex FROM T_Base_Device,T_Base_Device_Kind " +
                " where T_Base_Device.F_DeviceKindIndex = T_Base_Device_Kind.F_DeviceKindIndex and F_DBW1Address is not null and  " +
                " (T_Base_Device_Kind.F_GoodsMoveKindIndex = 1 OR T_Base_Device_Kind.F_GoodsMoveKindIndex = 2)").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                this.cbDeviceName.DisplayMember = "F_DeviceName";
                this.cbDeviceName.ValueMember = "F_DeviceIndex";
                this.cbDeviceName.DataSource = dv;

                this.cbDeviceName.Text = "";
                this.cbDeviceName.SelectedValue = 0;
            }
            this.cbEndPosition.Text = "";
            this.cbEndPosition.SelectedValue = 0;

            txtCellCode.Text = "";
        }

        private void cbDeviceName_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataView dv;
            this.cbDeviceCommand.DataSource=null;
            DataSet ds = dbo.ExceSQL("SELECT T_Base_Device.F_DeviceKindIndex, F_DeviceCommandName,F_DeviceCommandIndex FROM " +
                "T_Base_Device , T_Base_Device_Command WHERE T_Base_Device.F_DeviceKindIndex"+
                "= T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" +
                Convert.ToInt32(cbDeviceName.SelectedValue) + " and F_DeviceCommandIndex <> -1");
            if (ds.Tables[0].DefaultView.Count > 0)
            {
                

                this.cbDeviceCommand.DisplayMember = "F_DeviceCommandName";
                this.cbDeviceCommand.ValueMember = "F_DeviceCommandIndex";
                this.cbDeviceCommand.DataSource = ds.Tables[0].DefaultView;
                this.cbDeviceCommand.Text = "";
                this.cbDeviceCommand.SelectedValue = 0;
                switch (ccf.GetDeviceKindIdx(Convert.ToInt32(cbDeviceName.SelectedValue)))
                {
                    case 1://堆垛机
                        

                        dv = dbo.ExceSQL("SELECT F_DeviceIndex, F_DeviceName FROM T_Base_Lane_Gate,T_Base_LaneInfo,T_Base_Device where F_LaneIndex = F_LaneDeviceIndex and T_Base_LaneInfo.F_StackIndex=" + Convert.ToInt32(cbDeviceName.SelectedValue) + " and T_Base_Lane_Gate.F_LaneGateDeviceIndex=T_Base_Device.F_DeviceIndex").Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            cbEndPosition.DataSource = null;
                            cbEndPosition.DisplayMember = "F_DeviceName";
                            cbEndPosition.ValueMember = "F_DeviceIndex";
                            cbEndPosition.DataSource =dv;
                            txtCellCode.Enabled = true;
                            plCell.Enabled = true;
                            cellPanel.StackDevice = this.cbDeviceName.SelectedValue.ToString();
                        }
                        break;
                    case 2://输送机
                        //

                        dv = dbo.ExceSQL("SELECT F_DeviceIndex, F_DeviceName FROM T_Base_Device WHERE F_DeviceKindIndex =2 OR F_DeviceKindIndex =3").Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            cbEndPosition.DataSource = null;
                            cbEndPosition.DisplayMember = "F_DeviceName";
                            cbEndPosition.ValueMember = "F_DeviceIndex";
                            cbEndPosition.DataSource = dv;
                            txtCellCode.Enabled = false;
                            plCell.Enabled = false;
                        }
                        
                        
                        break;

                    case 4://RGV
                        

                        dv = dbo.ExceSQL("SELECT F_RGVGateDeviceIndex,F_DeviceName FROM T_Base_Device,T_Base_RGV_Gate WHERE F_DeviceIndex=F_RGVGateDeviceIndex and F_ChannelsIndex =" + Convert.ToInt32(cbDeviceName.SelectedValue)).Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            cbEndPosition.DataSource = null;
                            cbEndPosition.DisplayMember = "F_DeviceName";
                            cbEndPosition.ValueMember = "F_RGVGateDeviceIndex";
                            cbEndPosition.DataSource = dv;
                            txtCellCode.Enabled = false;
                            plCell.Enabled = false;
                        }
                        
                        break;
                }
                this.cbEndPosition.Text = "";
                txtCellCode.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要增加手工任务吗？", "操作提示：", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }

            //向调度任务表插入手工任务，管理类型４，
            //管理索引（如果调度任务表没有手工任务，
            //在T_Base_Manage_Task_Index_Hand_Task表去找个基数然后加一）
            try
            {
                //20090910
                if (cbDeviceName.SelectedValue == null)
                {
                    MessageBox.Show("请选择设备！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbDeviceName.Focus();
                    return;
                }
                if (this.cbDeviceName.Text.Trim() == "")
                {
                    MessageBox.Show("请选择设备！","操作提示：",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    this.cbDeviceName.Focus();
                    return;
                }
                if (this.cbDeviceCommand.Text.Trim() == "")
                {
                    MessageBox.Show("请选择设备命令！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbDeviceCommand.Focus();
                    return;
                }

                if ((cbDeviceCommand.Text == "复位") || (cbDeviceCommand.Text == "回原点"))
                {
                }
                else
                 {
                    if (this.txtCellCode.Enabled == true)
                    {
                        DataView dv0;
                        if (this.cbEndPosition.Text == "立库货位")
                        {
                            dv0 = dbo.ExceSQL("SELECT FCELLCODE FROM ST_CELL WHERE (FCELLSTATUS<> '-1' and FCELLCODE ='" + txtCellCode.Text + "')").Tables[0].DefaultView;
                            if (dv0.Count <= 0)
                            {
                                MessageBox.Show("请输入或选择合适的货位编码！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                this.txtCellCode.Focus();
                                return;
                            }
                        }
                        else
                        {

                            dv0 = dbo.ExceSQL("SELECT F_LaneGateDeviceIndex FROM T_Base_Lane_Gate where F_LaneGateDeviceIndex=" + cbEndPosition.SelectedValue + " and F_ZXY='" + txtCellCode.Text + "'").Tables[0].DefaultView;
                            if (dv0.Count <= 0)
                            {
                                MessageBox.Show("请输入或选择合适的货位编码！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                this.txtCellCode.Focus();
                                return;

                            }
                        }

                    }
                }
                int routeIDsub = GetRouteIDsub(Convert.ToInt32(cbDeviceName.SelectedValue));

                if (routeIDsub == -1)
                {

                    MessageBox.Show("您选择的设备错误！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                DataSet ds;
                string Sql;
                DataView dv = dbo.ExceSQL("SELECT F_ManageTASKKINDINDEX, F_DeviceIndex FROM T_Monitor_Task where F_ManageTASKKINDINDEX=4 and F_DeviceIndex=" + this.cbDeviceName.SelectedValue + "").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    //MessageBox.Show("在调度队列中已经存在这个设备的手工命令！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    
                    //return;
                }
                DataSet dss = dbo.ExceSQL("SELECT T_Base_Device.F_DeviceKindIndex, F_DeviceCommandName,F_DeviceCommandIndex FROM " +
                "T_Base_Device , T_Base_Device_Command WHERE T_Base_Device.F_DeviceKindIndex"+
                "= T_Base_Device_Command.F_DeviceKindIndex and F_DeviceIndex=" +
                Convert.ToInt32(cbDeviceName.SelectedValue) + " and F_DeviceCommandIndex="+ Convert.ToInt32(this.cbDeviceCommand.SelectedValue) +" and F_DeviceCommandIndex <> -1");
                if (dss.Tables[0].DefaultView.Count <= 0)
                {
                    MessageBox.Show("请选择设备和对应的有效命令！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }


                //20101028
                string dtime = DateTime.Now.ToString("u");
                dtime = dtime.Substring(0, dtime.Length - 1);
                 string qcell = string.Empty, scell = string.Empty;
                int hidx =0;
                int hmindx = 0; int rehidx = 0;
                //20101028
                int z=0, x=0, y=0, z1=0, x1=0, y1 = 0;
                //2	将取,3	将送,4	取货,5	送货
                int devicekind=ccf.GetDeviceKindIdx(Convert.ToInt32(cbDeviceName.SelectedValue));//12002
                if (devicekind==1)
                {
                    Model.MDevice device = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(cbDeviceName.SelectedValue)); ;
                    //if(device.)
                    //{

                    //}
                }
                switch (devicekind)
                {
                    case 1://堆垛机
                        char[] cc = new char[1] { '-' };
                        string[] sp = this.txtCellCode.Text.Split(cc);
                        switch (this.cbDeviceCommand.SelectedValue.ToString())
                        {

                            case "2":
                                z =Convert.ToInt32( sp[0]);
                                x = Convert.ToInt32(sp[1]);
                                y = Convert.ToInt32(sp[2]);
                                qcell = txtCellCode.Text;//20101028
                                break;
                            case "4":
                                z = Convert.ToInt32(sp[0]);
                                x = Convert.ToInt32(sp[1]);
                                y = Convert.ToInt32(sp[2]);
                                qcell = txtCellCode.Text;//20101028
                                break;
                            case "3":
                                z1 = Convert.ToInt32(sp[0]);
                                x1 = Convert.ToInt32(sp[1]);
                                y1 = Convert.ToInt32(sp[2]);
                                scell = txtCellCode.Text;//20101028
                                break;
                            case "5":
                                z1 = Convert.ToInt32(sp[0]);
                                x1 = Convert.ToInt32(sp[1]);
                                y1 = Convert.ToInt32(sp[2]);
                                scell = txtCellCode.Text;//20101028
                                break;
                            
                        }
                        break;
                    case 2://输送机
                        if (cbEndPosition.Text.Trim().Length==0 )
                        {
                            MessageBox.Show("请选择目标位置！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            this.cbEndPosition.Focus();
                            return;
                        }
                        else
                        if (12049 ==Convert.ToInt32(cbEndPosition.SelectedValue )|| 12054 == Convert.ToInt32(cbEndPosition.SelectedValue))
                        {
                            //if (cbBoxFloor.Text.Trim().Length <= 0)
                            //{
                            //    MessageBox.Show("请选择表箱层数！", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                            //    return;
                            //}
                            //x1=Convert.ToInt32( cbBoxFloor.Text);
                        }
                        else
                            if (32080 == Convert.ToInt32(cbDeviceName.SelectedValue) && cbDeviceCommand.SelectedValue.ToString() == "7")
                        {
                            //if (cbUnpack.Text.Trim().Length <= 0)
                            //{
                            //    MessageBox.Show("请选择拆盘数量！", "误操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                            //    return;
                            //}
                            //x1 = Convert.ToInt32(cbUnpack.Text);
                        }
                        if (cbDeviceCommand.SelectedValue.ToString() != "6")
                        {
                            cbEndPosition.Text = "0";
                            z = Convert.ToInt32(cbDeviceName.SelectedValue);
                            z1 = Convert.ToInt32(cbEndPosition.Text);
                        }
                        else
                        {

                            z = Convert.ToInt32(cbDeviceName.SelectedValue);//12002
                            z1 = Convert.ToInt32(cbEndPosition.SelectedValue);//12001
                        }
                        z = Convert.ToInt32(cbDeviceName.SelectedValue);
                        z1 = Convert.ToInt32(cbEndPosition.SelectedValue);
                        break;
                    case 4://RGV
                        if (cbEndPosition.SelectedValue == null)
                        {
                            MessageBox.Show("请选择目标位置！", "操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            this.cbEndPosition.Focus();
                            return;
                        }
                        z = Convert.ToInt32(cbEndPosition.SelectedValue);
                        break;
                }
                string useawayfork = "-"; int xx = 0; int xx1 = 0;
                string useawayfork1 = "-";//20101220
                if (this.comboBox1.Text == "近货叉")//进这里
                {
                    useawayfork = "0";
                    
                    xx = x + 1;
                    xx1 = x1 + 1;
                    useawayfork1 = "1";//20101220
                }
                else
                {
                    xx = x - 1;
                    xx1 = x1 - 1;
                    useawayfork = "1";
                    useawayfork1 = "0";//20101220
                    
                }
                //if (this.checkBox1.Checked == true)
                //{
                //    useawayfork = "-";
                //}
                

                if (this.checkBox1.Checked == true)
                {
                    if ((ccf.GetDeviceKindIdx(Convert.ToInt32(cbDeviceName.SelectedValue)) == 2) && (ccf.GetIfCorrelDoubleFork(Convert.ToInt32(cbDeviceName.SelectedValue)) == "1") && ((cbDeviceCommand.Text == "送出")))//20101220出库站台送出任务关联
                    {
                        rehidx = GetManageHandIdx();
                        hmindx = ccf.GetMonitorIndex(hidx, 4);
                        Sql = "insert into T_Monitor_Task(F_RouteID,F_ManageTaskIndex,F_ManageTaskKindIndex,F_MonitorIndex," +
                            "F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2,F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6,F_UseAwayFork) " +
                            "values(" + routeIDsub + "," + rehidx + ",4," + hmindx + "," + Convert.ToInt32(cbDeviceName.SelectedValue)
                            + "," + Convert.ToInt32(cbDeviceCommand.SelectedValue) + "," + z + "," + x + "," + y + "," + z1 + "," + x1 + "," + y1 + ",'" + useawayfork1 + "')";
                       dbo.ExceSQL(Sql);

                        dbo.ExceSQL("insert into T_Manage_Task(FID,F_ManageTaskKindIndex,F_RELATIVECONTORLID,FIntoStepOK,FSTARTDEVICE,FREMARK,FSTARTCELL,FENDDEVICE,FENDCELL,FBEGTIME) values(" +
                            rehidx + ",4,-1,1," + Convert.ToInt32(cbDeviceName.SelectedValue) + ",'" + cbDeviceCommand.SelectedValue + "','" + z.ToString() + "-" + x.ToString() + "-" + y.ToString()
                            + "'," + Convert.ToInt32(cbDeviceName.SelectedValue) + ",'" + z1.ToString() + "-" + x1.ToString() + "-" + y1.ToString() + "','" + dtime + "')");
                        RecordMaxHandTaskFID(rehidx);
                    }
                    if ((ccf.GetDeviceKindIdx(Convert.ToInt32(cbDeviceName.SelectedValue)) == 1) && (((cbDeviceCommand.Text == "取货") || ((cbDeviceCommand.Text == "送货")))))
                    {
                        rehidx= GetManageHandIdx();
                        hmindx = ccf.GetMonitorIndex(hidx, 4);
                        if (cbDeviceCommand.Text == "取货")
                        {//z-x-y

                            if (x == 1)
                            {
                                xx = 1;
                            }
                            else
                            {
                                dv = dbo.ExceSQL("SELECT FID FROM ST_CELL WHERE (F_Z = " + z + ") AND (F_X = " + xx + ") AND (F_Y = " + y + ")").Tables[0].DefaultView;
                                if (dv.Count == 0)
                                {
                                    return;

                                }
                            }

                            qcell = z.ToString() + "-" + xx.ToString() + "-" + y.ToString();//20101028
                            dbo.ExceSQL("insert into T_Monitor_Task(F_RouteID,F_ManageTaskIndex,F_ManageTaskKindIndex,F_MonitorIndex," +
                             "F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2,F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6,F_UseAwayFork) " +
                             "values(" + routeIDsub + "," + rehidx + ",4," + hmindx + "," + Convert.ToInt32(cbDeviceName.SelectedValue)
                             + "," + Convert.ToInt32(cbDeviceCommand.SelectedValue) + "," + z + "," + xx + "," + y + ",0,0,0,'" + useawayfork1 + "')");//20101220
                        }
                        if (cbDeviceCommand.Text == "送货")
                        {//z1-x1-y1

                            if (x1 == 1)
                            {
                                xx1 = 1;
                            }
                            else
                            {
                                dv = dbo.ExceSQL("SELECT FID FROM ST_CELL WHERE (F_Z = " + z1 + ") AND (F_X = " + xx1 + ") AND (F_Y = " + y1 + ")").Tables[0].DefaultView;
                                if (dv.Count == 0)
                                {
                                    return;

                                }
                            }

                            scell = z1.ToString() + "-" + xx1.ToString() + "-" + y1.ToString();//20101028
                            dbo.ExceSQL("insert into T_Monitor_Task(F_RouteID,F_ManageTaskIndex,F_ManageTaskKindIndex,F_MonitorIndex," +
                         "F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2,F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6,F_UseAwayFork) " +
                         "values(" + routeIDsub + "," + rehidx + ",4," + hmindx + "," + Convert.ToInt32(cbDeviceName.SelectedValue)
                         + "," + Convert.ToInt32(cbDeviceCommand.SelectedValue) + ",0,0,0," + z1 + "," + xx1 + "," + y1 + ",'" + useawayfork1 + "')");//20101220

                        }

                        dbo.ExceSQL("insert into T_Manage_Task(FID,F_ManageTaskKindIndex,F_RELATIVECONTORLID,FIntoStepOK,FSTARTDEVICE,FREMARK,FSTARTCELL,FENDDEVICE,FENDCELL,FBEGTIME) values(" +
                            rehidx + ",4,-1,1," + Convert.ToInt32(cbDeviceName.SelectedValue) + ",'"+cbDeviceCommand.SelectedValue+"','" + qcell + "'," + Convert.ToInt32(cbDeviceName.SelectedValue) + ",'" + scell + "','" + dtime + "')");
                        
                        RecordMaxHandTaskFID(rehidx);
                        //20101028

                    }
                }
                //20101028
                hidx = GetManageHandIdx();
                hmindx = ccf.GetMonitorIndex(hidx, 4);
                Sql = "insert into T_Monitor_Task(F_RouteID,F_ManageTaskIndex,F_ManageTaskKindIndex,F_MonitorIndex," +
                        "F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1,F_NumParam2,F_NumParam3,F_NumParam4,F_NumParam5,F_NumParam6,F_UseAwayFork) " +
                        "values(" + routeIDsub + "," + hidx + ",4," + hmindx + "," + Convert.ToInt32(cbDeviceName.SelectedValue)
                        + "," + Convert.ToInt32(cbDeviceCommand.SelectedValue) + "," + z + "," + x + "," + y + "," + z1 + "," + x1 + "," + y1 + ",'" + useawayfork + "')";
                ds = dbo.ExceSQL(Sql);

                dbo.ExceSQL("insert into T_Manage_Task(FID,F_ManageTaskKindIndex,F_RELATIVECONTORLID,FIntoStepOK,FSTARTDEVICE,FREMARK,FSTARTCELL,FENDDEVICE,FENDCELL,FBEGTIME) values(" +
                    hidx + ",4,-1,1," + Convert.ToInt32(cbDeviceName.SelectedValue) + ",'" + cbDeviceCommand.SelectedValue + "','" + z.ToString()+"-"+x.ToString()+"-"+y.ToString()
                    + "'," + Convert.ToInt32(cbDeviceName.SelectedValue) + ",'"+z1.ToString()+"-"+x1.ToString()+"-"+y1.ToString()+"','" + dtime + "')");
                
                if (rehidx > 0)
                {
                    dbo.ExecuteSql("update T_Manage_Task set F_RELATIVECONTORLID=" + rehidx + " where F_ManageTaskKindIndex=4 and FID=" + hidx);
                    dbo.ExecuteSql("update T_Manage_Task set F_RELATIVECONTORLID=" + hidx + " where F_ManageTaskKindIndex=4 and FID=" + rehidx);
                }
                RecordMaxHandTaskFID(hidx);
                //20101028 

            }
            catch(Exception ex)
            {
                throw ex;
            }

        }
        int GetManageHandIdx()
        {
            DataSet ds = dbo.ExceSQL("SELECT F_ManageTaskKindIndex, max(FID) as mFID FROM T_Manage_Task Where F_ManageTaskKindIndex=4 group by F_ManageTaskKindIndex");
            if (ds.Tables[0].DefaultView.Count > 0)
            {
                return (Convert.ToInt32(ds.Tables[0].DefaultView[0]["mFID"]) + 1);
            }
            else
            {
                DataSet dss = dbo.ExceSQL("SELECT F_ManageTaskIndex FROM T_Base_Manage_Task_Index_Hand_Task");
                if (dss.Tables[0].DefaultView.Count > 0)
                {
                    if ((Convert.ToInt32(dss.Tables[0].DefaultView[0]["F_ManageTaskIndex"]) + 1) >= 29998)
                    {
                        return 20001;
                    }
                    else
                    {
                        return (Convert.ToInt32(dss.Tables[0].DefaultView[0]["F_ManageTaskIndex"]) + 1);
                    }
                }
                else
                {
                    return 20001;
                }
            }
        }
        void RecordMaxHandTaskFID(int fid)
        {
            DataSet ds = dbo.ExceSQL("select F_ManageTaskIndex from T_Base_Manage_Task_Index_Hand_Task");
            DataView dv = ds.Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                if (fid == 29998)
                {
                    dbo.ExceSQL("UPDATE T_Base_Manage_Task_Index_Hand_Task SET F_ManageTaskIndex =20001" );
                    return;
                }
                if (fid > Convert.ToInt32(dv[0]["F_ManageTaskIndex"]))
                {
                    dbo.ExceSQL("UPDATE T_Base_Manage_Task_Index_Hand_Task SET F_ManageTaskIndex =" + fid);
                    return;
                }
            }
            else
            {
                dbo.ExceSQL("INSERT INTO T_Base_Manage_Task_Index_Hand_Task (F_ManageTaskIndex)VALUES (" + fid + ")");
                return;
            }
        }


        

        
        

       

       

        



        int GetRouteIDsub(int device)
        {
            DataView dv = dbo.ExceSQL("SELECT F_RouteIDSub, F_DeviceIndex, F_RouteID FROM T_Base_Route_Device where F_DeviceIndex="+device+"").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                return Convert.ToInt32(dv[0]["F_RouteIDSub"]);
            }
            else
            {
                return -1;
            }
        }

        private void cbDeviceCommand_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbDeviceCommand.SelectedValue != null && this.cbDeviceName.SelectedValue != null && this.cbDeviceCommand.Text.Trim() != "")
            {
                switch (ccf.GetDeviceKindIdx(Convert.ToInt32( this.cbDeviceName.SelectedValue)))
                {
                    case 1:
                        if (cbDeviceCommand.SelectedValue.ToString() == "1")
                        {
                            txtCellCode.Text = "00-00-00";
                        }
                        else
                        {
                            txtCellCode.Text = "";
                        }
                        break;
                    case 2:
                        
                        if (cbDeviceCommand.SelectedValue.ToString() != "6")
                        {
                            
                            cbEndPosition.SelectedValue = 0;
                            cbEndPosition.Text = "0";
                        }
                        else
                        {
                            
                            cbEndPosition.SelectedValue = -1;
                            cbEndPosition.Text = "";
                        }
                        txtCellCode.Text = "";
                        break;
                }
            }
        }

       
        

        

        private void cbEndPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((Information.IsNumeric(this.cbEndPosition.SelectedValue) == true))
            {
                DataView dv = dbo.ExceSQL("SELECT F_ZXY FROM T_Base_Lane_Gate where F_LaneGateDeviceIndex=" + this.cbEndPosition.SelectedValue + "").Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    txtCellCode.Text = dv[0][0].ToString();

                }
                else
                {
                    
                    txtCellCode.Text = "";
                }
                
                
                
            }
            
            
        }

        private void txtCellCode_TextChanged(object sender, EventArgs e)
        {
            if(this.cbDeviceName.SelectedValue==null)return;
            if(ccf.GetDeviceKindIdx(Convert.ToInt32( this.cbDeviceName.SelectedValue))!=1)return;
            DataView dv0 = dbo.ExceSQL("SELECT F_LaneGateDeviceIndex FROM T_Base_Lane_Gate WHERE  F_ZXY='" + txtCellCode.Text + "' and F_LaneIndex=" + ccf.GetLaneWay(Convert.ToInt32(cbDeviceName.SelectedValue)) + "").Tables[0].DefaultView;
            if (dv0.Count > 0)
            {
                this.cbEndPosition.SelectedValue = dv0[0][0];
            }
            else
            {
                dv0 = dbo.ExceSQL("SELECT FCELLCODE FROM ST_CELL WHERE (FCELLCODE ='" + txtCellCode.Text + "')").Tables[0].DefaultView;
                if (dv0.Count > 0)
                {

                    this.cbEndPosition.SelectedValue = 0;
                    this.cbEndPosition.Text = "立库货位";
                }
            }
        }

        
       
        
    }
}