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

        public static FrmHandCommand FormInstance
        {
            get 
            {
                if (_formInstance  == null)
                {
                    _formInstance = new FrmHandCommand();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        
        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.cbInWorkBench.SelectedIndex = 2;
            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);

            }
            string StrSql = string.Format(" and F_WorkArea  like '%{0}%'", CStaticClass.UserArea);
            //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)" + StrSql).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;
            }

            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 >= 0");
            if (ds.Tables[0].DefaultView.Count > 0)
            {

                deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(cbDeviceName.SelectedValue));
                if (deviceInfo.IfCorrelDoubleFork == "1")// && deviceInfo.DeviceKind==1)
                {
                    pnDblFork.Visible = true;
                }
                else
                {
                    pnDblFork.Visible = false;
                }
                if ((deviceInfo.DeviceIndex >= 31001 && deviceInfo.DeviceIndex <= 31004)||(deviceInfo.DeviceIndex<20000))
                {
                    panelType.Visible = false;

                    comboBoxType.SelectedIndex = -1;
                    comboBoxType.Text = "";
                }
                else
                {
                    panelType.Visible = false;
                }

                if (deviceInfo.DeviceKind == 13)
                {
                    pnGd.Visible = true;
                }
                else
                {
                    pnGd.Visible = false;
                }
                this.cbDeviceCommand.DisplayMember = "F_DeviceCommandName";
                this.cbDeviceCommand.ValueMember = "F_DeviceCommandIndex";
                this.cbDeviceCommand.DataSource = ds.Tables[0].DefaultView;
                this.cbDeviceCommand.Text = "";
                this.cbDeviceCommand.SelectedValue = 0;
                int rgvdev = 0;//20130510
                switch (CStaticClass.GetDeviceKindIdx (Convert.ToInt32(cbDeviceName.SelectedValue)))
                {
                        
                    case 1://堆垛机
                        int devic=deviceInfo.DeviceIndex;
                        if (deviceInfo.VirtualStack>0)
                        {
                            devic = deviceInfo.VirtualStack;
                        }
                        
                        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=" + devic + " 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;
                            comboBox1.Enabled = true;
                            DataView dv1 = dbo.ExceSQL(string.Format("SELECT F_ForkAmount FROM T_Base_StackInfo WHERE (F_StackIndex = {0})", Convert.ToInt32(cbDeviceName.SelectedValue))).Tables[0].DefaultView;
                            if (dv1.Count > 0)
                            {
                               
                                comboBox1.Items.Clear();
                                
                                for (int j = 1; j <= Convert.ToInt32(dv1[0]["F_ForkAmount"]); j++)
                                {
                                    comboBox1.Items.Add(j);
                                }
                                comboBox1.Text = "0";
                            }
                            else
                            {
                                comboBox1.Text = "0";
                               
                            }

                            dv1 = dbo.ExceSQL(string.Format("SELECT FStack  from ST_CELL WHERE (FStack = {0})", devic)).Tables[0].DefaultView;
                            if(dv1.Count >0)
                            {
                            plCell.Enabled = true;
                            cellPanel.StackDevice =devic.ToString();
                            cellPanel.WareHouse = GetWarehouse(devic);
                            }
                            else
                            {
                                plCell.Enabled =false;
                            }
                        }
                        break;
                    case 2://输送机

                        dv = dbo.ExceSQL("SELECT F_DeviceIndex, F_DeviceName FROM T_Base_Device WHERE F_DeviceKindIndex =2 and F_DBW1Address is not null and f_s7connection='"+deviceInfo.S7Connection+"'").Tables[0].DefaultView;
                        if (dv.Count > 0)
                        {
                            cbEndPosition.DataSource = null;
                            cbEndPosition.DisplayMember = "F_DeviceName";
                            cbEndPosition.ValueMember = "F_DeviceIndex";
                            cbEndPosition.DataSource = dv;
                            txtCellCode.Enabled = false;
                            comboBox1.Enabled = false;
                            if (deviceInfo.IfCorrelDoubleFork == "1")
                            {
                                comboBox1.Enabled = true;
                            }


                            plCell.Enabled = false;
                        }
                        comboBox1.Text = "0";
                        
                        break;
                    case 13://高端输送机
                        comboBox1.Text = "0";
                        
                        dv = dbo.ExceSQL(string.Format("SELECT FUCONVEYOR,F_DeviceName FROM ST_CELL,T_Base_Device  WHERE FUCONVEYOR = F_DeviceIndex  and  (FUCODE = {0})", Convert.ToInt32(this.cbDeviceName.SelectedValue))).Tables[0].DefaultView;
                
                        if (dv.Count > 0)
                        {
                            cbBeginDev.DataSource = null;
                            cbBeginDev.DisplayMember = "F_DeviceName";
                            cbBeginDev.ValueMember = "FUCONVEYOR";
                            cbBeginDev.DataSource = dv;
                            DataView dv1 = dbo.ExceSQL(string.Format("SELECT FUCONVEYOR,F_DeviceName FROM ST_CELL,T_Base_Device  WHERE FUCONVEYOR = F_DeviceIndex  and  (FUCODE = {0})", Convert.ToInt32(this.cbDeviceName.SelectedValue))).Tables[0].DefaultView;
                
                            cbEndDev.DataSource = null;
                            cbEndDev.DisplayMember = "F_DeviceName";
                            cbEndDev.ValueMember = "FUCONVEYOR";
                            cbEndDev.DataSource = dv1;
                            txtCellCode.Enabled = false;
                            comboBox1.Enabled = false;
                            
                            plCell.Enabled = false;
                        }


                        break;
                    case 4://RGV
                        comboBox1.Text = "0";
                        //20130510
                        if (Convert.ToInt32(cbDeviceName.SelectedValue) == 23004)
                        {
                            rgvdev = 23002;
                        }
                        else if (Convert.ToInt32(cbDeviceName.SelectedValue) == 13004)
                        {
                            rgvdev = 13002;
                        }
                        else
                        {
                            rgvdev = Convert.ToInt32(cbDeviceName.SelectedValue);
                        }
                        pnGd.Visible = false;
                        dv = dbo.ExceSQL("SELECT F_RGVGateDeviceIndex,F_DeviceName FROM T_Base_Device,T_Base_RGV_Gate WHERE F_DeviceIndex=F_RGVGateDeviceIndex and F_ChannelsIndex =" + rgvdev).Tables[0].DefaultView;//20130510
                        if (dv.Count > 0)
                        {
                            cbEndPosition.DataSource = null;
                            cbEndPosition.DisplayMember = "F_DeviceName";
                            cbEndPosition.ValueMember = "F_RGVGateDeviceIndex";
                            cbEndPosition.DataSource = dv;
                            comboBox1.Enabled = false;
                            
                            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;
            }

            //向调度任务表插入手工任务,管理类型4,
            //管理索引(如果调度任务表没有手工任务,
            //在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(panelType.Visible)
                {
                    if (this.comboBoxType.Text.Trim() == "")
                    {
                        MessageBox.Show("请选择托盘类型!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.comboBoxType.Focus();
                        return;
                    }
                }
                if ((cbDeviceCommand.Text == "复位") || (cbDeviceCommand.Text == "回原点"))
                {
                }
                else
                {
                    if (this.txtCellCode.Enabled == true)
                    {
                        DataView dv0;
                        if (this.cbEndPosition.Text == "立库货位")
                        {

                            int fstack = Convert.ToInt32(cbDeviceName.SelectedValue);
                            deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(fstack));
                            if (deviceInfo.VirtualStack!= 0)
                            {
                                fstack = deviceInfo.VirtualStack;
                            }
                            else
                            {
                                fstack = Convert.ToInt32(cbDeviceName.SelectedValue);
                            }
                            dv0 = dbo.ExceSQL(string.Format("SELECT FCELLCODE FROM ST_CELL WHERE FStack={0} and (FCELLCODE ='{1}')", fstack, txtCellCode.Text)).Tables[0].DefaultView;
                            //20171221
                            //dv0 = dbo.ExceSQL(string.Format("SELECT FCELLCODE FROM ST_CELL WHERE (FCELLCODE ='{0}')", txtCellCode.Text)).Tables[0].DefaultView;
                            if (dv0.Count <= 0)
                            {
                                MessageBox.Show("请输入或选择合适的货位编码!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                this.txtCellCode.Focus();
                                return;
                            }
                        }
                        else
                        {
                            int ep = -1;
                            if (cbEndPosition.SelectedValue == null)
                            {
                                MessageBox.Show("请输入或选择合适的终点位置!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                this.txtCellCode.Focus();
                                return;
                            }
                            int.TryParse(cbEndPosition.SelectedValue.ToString(), out ep);
                            if (ep==-1)
                            {
                                MessageBox.Show("请输入或选择合适的终点位置!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                this.txtCellCode.Focus();
                                return;
                            }
                            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));
                deviceInfo=Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(cbDeviceName.SelectedValue));
                if ((routeIDsub == -1) && (deviceInfo.DeviceKind != 13) && (deviceInfo.DeviceKind != 31) && !cbDeviceCommand.Text.Contains("灯") && Convert.ToInt32(cbDeviceName.SelectedValue)<99999)
                {

                    MessageBox.Show("您选择的设备错误!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                
                //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);

                int beginDeviceIndex = 0; int Inworkbench = 2,forkno=0;
                
                //2	将取,3	将送,4	取货,5	送货
                switch (CStaticClass.GetDeviceKindIdx(Convert.ToInt32(cbDeviceName.SelectedValue)))
                {
                    case 1://堆垛机
                        if ((cbDeviceCommand.SelectedValue.ToString() == "6" || cbDeviceCommand.SelectedValue.ToString() == "5" || deviceInfo.StackReach==2) && (this.cbEndPosition.Text == "立库货位"))
                        {
                            int fstack = 0;
                            if (deviceInfo.VirtualStack>0)
                            {
                                fstack = deviceInfo.VirtualStack;
                            }
                            else
                            {
                                fstack = Convert.ToInt32(cbDeviceName.SelectedValue);
                            }
                            //DataView dv0 = dbo.ExceSQL(string.Format("SELECT FCELLCODE FROM ST_CELL WHERE fstack={0} and (FCELLCODE ='{1}')", fstack, txtCellCode1.Text)).Tables[0].DefaultView;
                            //if (dv0.Count <= 0)
                            //{
                            //    dv0 = dbo.ExceSQL("SELECT F_LaneGateDeviceIndex FROM T_Base_Lane_Gate where  F_ZXY='" + txtCellCode1.Text + "'").Tables[0].DefaultView;
                            //    if (dv0.Count <= 0)
                            //    {
                            //        MessageBox.Show("请输入或选择合适的货位编码!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            //        this.txtCellCode.Focus();
                            //        return;

                            //    }

                            //}

                        }
                        if (this.comboBox1.Text.Trim() == "")
                        {
                            forkno = 0;


                        }
                        else
                        {
                            forkno = Convert.ToInt32(this.comboBox1.Text);
                        }
                        if(panelType.Visible)
                        {
                            if (comboBoxType.SelectedIndex > -1)
                            {
                                forkno = comboBoxType.SelectedIndex + 1;//用来保存单叉堆垛机的托盘类型(1小托盘,2大托盘)
                            }
                        }
                        break;
                    case 2://输送机
                        if (cbEndPosition.Text.Trim().Length==0 )
                        {
                            MessageBox.Show("请选择目标位置!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            this.cbEndPosition.Focus();
                            return;
                        }
                        if (cbDeviceCommand.SelectedValue.ToString() != "6")
                        {
                            cbEndPosition.Text = "0";
                            
                        }

                        int.TryParse(this.comboBox1.Text, out  forkno);
                        if (panelType.Visible)
                        {
                            if (comboBoxType.SelectedIndex > -1)
                            {
                                forkno = comboBoxType.SelectedIndex + 1;//用来保存单叉堆垛机的托盘类型(1小托盘,2大托盘)
                            }
                        }

                        break;
                    case 4://RGV
                        if (cbEndPosition.SelectedValue == null)
                        {
                            MessageBox.Show("请选择目标位置!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            this.cbEndPosition.Focus();
                            return;
                        }
                       
                        break;
                    case 13://高端
                        if (cbEndDev.SelectedValue == null)
                        {
                            MessageBox.Show("请选择目标位置!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            this.cbEndDev.Focus();
                            return;
                        }
                        if (cbBeginDev.SelectedValue == null)
                        {
                            MessageBox.Show("请选择目标位置!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            this.cbBeginDev.Focus();
                            return;
                        }
                        beginDeviceIndex = Convert.ToInt32(cbBeginDev.SelectedValue);
                        if (int.TryParse(cbInWorkBench.Text, out Inworkbench) == false)
                        {
                            MessageBox.Show("请选择是否进工位!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            this.cbInWorkBench.Focus();
                            return;
                        }
                        forkno = Inworkbench;
                        break;
                }
                //调用服务
                string errtext=string.Empty;
                string epp = string.Empty;
                if (cbEndPosition.SelectedValue == null)
                {
                    epp = "0";
                }
                else
                {
                    epp = cbEndPosition.SelectedValue.ToString();
                }
                string cellcode = string.Empty ;
                if (deviceInfo.DeviceKind == 13)
                {
                    epp = cbEndDev.SelectedValue.ToString();
                    cellcode = tbFPALLETBARCODE.Text;
                }
                else
                {
                    cellcode = txtCellCode.Text;
                }
                string cforks = string.Empty;
                if (deviceInfo.IfCorrelDoubleFork=="1")
                {
                    if (fork2.Checked == true)
                    {
                        cforks = ",2";
                    }
                    if (fork3.Checked == true)
                    {
                        cforks = cforks + ",3";
                    }
                    if (cforks.Length >= 2)
                    {
                        cforks = cforks.Substring(1);
                    }
                }
                #region 德玛提升机手动任务
                if (cbDeviceCommand.SelectedValue.ToString() == "8"&& CStaticClass.GetDeviceKindIdx(Convert.ToInt32(cbDeviceName.SelectedValue))==4)
                {
                    txtCellCode1.Text = txtCellCode1.Text;
                }
                #endregion
                Model.HandTask ht=new Model.HandTask(Convert.ToInt32(cbDeviceName.SelectedValue)
                    ,Convert.ToInt32(cbDeviceCommand.SelectedValue),beginDeviceIndex, epp,
                    cellcode, txtCellCode1.Text, forkno, cforks );
                
                CStaticClass.WcfControl.BeginBuildHandTask(ht,new AsyncCallback(BuildHandTaskCallBack ),ht);
                
            }
            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;
            }
        }

        void BuildHandTaskCallBack( IAsyncResult ar)
        {
            string errtext = string.Empty;
            
            if (CStaticClass.WcfControl.EndBuildHandTask(out errtext, ar) == false)
            {
                MessageBox.Show(errtext, "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        string GetWarehouse(int Fstack)
        {
            DataView dv = dbo.ExceSQL(string.Format("SELECT DISTINCT FWAREHOUSE FROM ST_CELL WHERE (Fstack = {0})", Fstack)).Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                return dv[0][0].ToString();
            }
            else
            {
                return "01";
            }
        }
        

        
        

       

       

        



        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() != "")
            {
                int fstack = Convert.ToInt32(cbDeviceName.SelectedValue);
                deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(fstack));

                switch (CStaticClass.GetDeviceKindIdx(Convert.ToInt32(this.cbDeviceName.SelectedValue)))
                {
                    case 1:
                        if (cbDeviceCommand.SelectedValue.ToString() == "1" || cbDeviceCommand.SelectedValue.ToString() == "8" || cbDeviceCommand.SelectedValue.ToString() == "9")
                        {
                            txtCellCode.Text = "00-00-00";
                            txtCellCode.Enabled = true;
                            txtCellCode1.Enabled = false;
                            txtCellCode1.Text = "";
                            cbEndPosition.SelectedValue = 0;
                        }
                        else if (cbDeviceCommand.SelectedValue.ToString() == "6")//20180609
                        {
                            txtCellCode.Enabled = true;
                            txtCellCode.Text = "";
                            txtCellCode1.Enabled = true;
                            txtCellCode1.Text = "";
                            
                        }
                        else if ((cbDeviceCommand.SelectedValue.ToString() == "2") || (cbDeviceCommand.SelectedValue.ToString() == "4"))
                        {
                            txtCellCode.Enabled = true;
                            txtCellCode.Text = "";
                            txtCellCode1.Enabled = false;
                            txtCellCode1.Text = "";
                            cellPanel.ControlCellCode = txtCellCode;
                        }
                        else if ((cbDeviceCommand.SelectedValue.ToString() == "3") || (cbDeviceCommand.SelectedValue.ToString() == "5"))
                        {
                            txtCellCode.Enabled = false;
                            txtCellCode.Text = "";
                            txtCellCode1.Enabled = true;
                            txtCellCode1.Text = "";

                            cellPanel.ControlCellCode = txtCellCode1;
                            

                        }
                        else
                        {
                            txtCellCode1.Enabled = false;
                            txtCellCode1.Text = "";
                            txtCellCode.Text = "";
                        }

                        break;
                    case 2:
                        
                        if (cbDeviceCommand.SelectedValue.ToString() != "6")
                        {
                            
                            cbEndPosition.SelectedValue = 0;
                            cbEndPosition.Text = "0";
                        }
                        else
                        {
                            
                            cbEndPosition.SelectedValue = -1;
                            cbEndPosition.Text = "";
                        }
                        txtCellCode.Text = "";
                        txtCellCode1.Enabled = false;
                        txtCellCode1.Text = "";
                        break;
                    case 31:

                        if (cbDeviceCommand.SelectedValue.ToString() != "6")
                        {

                            cbEndPosition.SelectedValue = 0;
                            cbEndPosition.Text = "0";
                        }
                        else
                        {

                            cbEndPosition.SelectedValue = -1;
                            cbEndPosition.Text = "";
                        }
                        txtCellCode.Text = "";
                        txtCellCode.Enabled = false;
                        txtCellCode1.Enabled = false;
                        txtCellCode1.Text = "";
                        break;
                    case 4:
                        // 德玛提升机增加终点参数
                        if (cbDeviceCommand.SelectedValue.ToString() == "8")
                        {
                            txtCellCode1.Visible = true;
                            txtCellCode1.Enabled = true;
                        }
                        else
                        {
                            txtCellCode1.Visible = false;
                            txtCellCode1.Enabled = false;

                        }
                        break;
                }
            }
        }

       
        

        

        private void cbEndPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            int devic = Convert.ToInt32(cbDeviceName.SelectedValue);
            deviceInfo=Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(devic));
            if (deviceInfo.VirtualStack!=0)
            {
                devic = deviceInfo.VirtualStack;
            }
            else
            {
                devic = Convert.ToInt32(cbDeviceName.SelectedValue);
            }
            if (this.cbEndPosition.SelectedValue == null) return;
            int qq=-1;
            if (int.TryParse(this.cbEndPosition.SelectedValue.ToString(),out  qq) == true)
            {
                DataView dv = dbo.ExceSQL(string.Format("SELECT T_Base_Lane_Gate.F_ZXY FROM T_Base_LaneInfo,T_Base_Lane_Gate WHERE   T_Base_LaneInfo.F_LaneDeviceIndex = T_Base_Lane_Gate.F_LaneIndex and  (T_Base_LaneInfo.F_StackIndex = {0}) AND (T_Base_Lane_Gate.F_LaneGateDeviceIndex = {1})",devic,cbEndPosition.SelectedValue)).Tables[0].DefaultView;
                if (dv.Count > 0)
                {
                    if (txtCellCode.Enabled == true)
                    {
                        txtCellCode.Text = dv[0][0].ToString();
                    }
                    else
                    {
                        txtCellCode1.Text = dv[0][0].ToString();
                    }

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

        private void txtCellCode_TextChanged(object sender, EventArgs e)
        {
            if(this.cbDeviceName.SelectedValue==null)return;
            if (CStaticClass.GetDeviceKindIdx(Convert.ToInt32(this.cbDeviceName.SelectedValue)) != 1) return;
            int fstack = Convert.ToInt32(cbDeviceName.SelectedValue);
            deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(fstack));
            if (deviceInfo.MutexStack != 0)
            {
                fstack = deviceInfo.MutexStack;
            }

            else
            {
                fstack = Convert.ToInt32(cbDeviceName.SelectedValue);
            }
            DataView dv0 = dbo.ExceSQL("SELECT F_LaneGateDeviceIndex FROM T_Base_Lane_Gate WHERE  F_ZXY='" + txtCellCode.Text + "' and F_LaneIndex=" + CStaticClass.GetLaneWay(fstack) + "").Tables[0].DefaultView;
            if (dv0.Count > 0)
            {
                this.cbEndPosition.SelectedValue = dv0[0][0];
            }
            else
            {
                //20171221
                dv0 = dbo.ExceSQL("SELECT FCELLCODE FROM ST_CELL WHERE  (FCELLCODE ='" + txtCellCode.Text + "')").Tables[0].DefaultView;

                //dv0 = dbo.ExceSQL("SELECT FCELLCODE FROM ST_CELL WHERE fstack=" + fstack + " and (FCELLCODE ='" + txtCellCode.Text + "')").Tables[0].DefaultView;
                if (dv0.Count > 0)
                {

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

        private void FrmHandCommand_FormClosing(object sender, FormClosingEventArgs e)
        {
            _formInstance = null;
        }

        private void txtCellCode1_TextChanged(object sender, EventArgs e)
        {
            if (this.cbDeviceName.SelectedValue == null) return;
            if (CStaticClass.GetDeviceKindIdx(Convert.ToInt32(this.cbDeviceName.SelectedValue)) != 1) return;
          
            int fstack = Convert.ToInt32(cbDeviceName.SelectedValue);
            deviceInfo = Model.CGetInfo.GetDeviceInfo(Convert.ToInt32(fstack));
            if (deviceInfo.MutexStack != 0)
            {
                fstack = deviceInfo.MutexStack;
            }

            else
            {
                fstack = Convert.ToInt32(cbDeviceName.SelectedValue);
            }
            DataView dv0 = dbo.ExceSQL("SELECT F_LaneGateDeviceIndex FROM T_Base_Lane_Gate WHERE  F_ZXY='" + txtCellCode1.Text + "' and F_LaneIndex=" + CStaticClass.GetLaneWay(fstack) + "").Tables[0].DefaultView;
            if (dv0.Count > 0)
            {
                this.cbEndPosition.SelectedValue = dv0[0][0];
            }
            else
            {
                dv0 = dbo.ExceSQL("SELECT FCELLCODE FROM ST_CELL WHERE fstack=" + fstack + " and  (FCELLCODE ='" + txtCellCode1.Text + "')").Tables[0].DefaultView;
                if (dv0.Count > 0)
                {

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

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "1")
            {
                fork2.Visible = true;
                //fork3.Visible = true;
            }
            else if (comboBox1.Text == "2")
            {
                fork2.Visible = false;
                //fork3.Visible = true;
            }
            else if (comboBox1.Text == "3")
            {
                fork2.Visible = false;
                //fork3.Visible = false;
            }
        }

        
    }
}