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 FrmHandAssociateCommand : Form
    {
        private static FrmHandAssociateCommand _formInstance;

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


        #endregion
        public FrmHandAssociateCommand()
        {
            InitializeComponent();
            _formInstance = this;
            dbo.Open();
        }
        
        private void FrmHandCommand_Load(object sender, EventArgs e)
        {
            
            DataView dv3 = dbo.ExceSQL("SELECT F_RGVGateDeviceIndex ,F_Address,F_DeviceName  FROM T_Base_RGV_Gate,T_Base_Device where F_RGVGateDeviceIndex=F_DeviceIndex").Tables[0].DefaultView;
            if (dv3.Count > 0)
            {
                this.cbConveyorName.DisplayMember = "F_DeviceName";
                this.cbConveyorName.ValueMember = "F_RGVGateDeviceIndex";
                this.cbConveyorName.DataSource = dv3;
            }

            
            DataView dv5 = dbo.ExceSQL("select F_DeviceCommandName,F_DeviceCommandIndex from T_Base_Device_Command where (F_DeviceCommandKind=1 or F_DeviceCommandKind=2) and (F_DeviceKindIndex=2)").Tables[0].DefaultView;
            if (dv5.Count > 0)
            {
                this.cbConveyorCommand.DisplayMember = "F_DeviceCommandName";
                this.cbConveyorCommand.ValueMember = "F_DeviceCommandIndex";
                this.cbConveyorCommand.DataSource = dv5;
            }
            
        }

        
        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 cbConveyorName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbConveyorName.SelectedValue == null) return;
            DataView dv = dbo.ExceSQL("SELECT F_IFChannelLeft,F_ChannelsIndex,F_RGVGateDeviceIndex ,F_Address,F_DeviceName  FROM T_Base_RGV_Gate,T_Base_Device where F_RGVGateDeviceIndex=F_DeviceIndex and F_DeviceIndex=" + cbConveyorName.SelectedValue + "").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                DataView dv4 = dbo.ExceSQL("select F_DeviceIndex ,F_DeviceName from T_Base_Device where F_DeviceKindIndex=4 and F_DeviceIndex ="+ dv[0]["F_ChannelsIndex"]+"").Tables[0].DefaultView;
                if (dv4.Count > 0)
                {
                    this.cbRGVName.DisplayMember = "F_DeviceName";
                    this.cbRGVName.ValueMember = "F_DeviceIndex";
                    this.cbRGVName.DataSource = dv4;
                }
                string left = "";
                if (dv[0]["F_IFChannelLeft"].ToString() == "1")
                {
                    left = "左";
                }
                else
                {
                    left = "右";
                }
                DataView dv6 = dbo.ExceSQL("select F_DeviceCommandName,F_DeviceCommandIndex from T_Base_Device_Command where (F_DeviceCommandKind=1 or F_DeviceCommandKind=2) and (F_DeviceKindIndex=4) and (F_DeviceCommandName like '"+left +"%')").Tables[0].DefaultView;
                if (dv6.Count > 0)
                {
                    this.cbRGVCommand.DisplayMember = "F_DeviceCommandName";
                    this.cbRGVCommand.ValueMember = "F_DeviceCommandIndex";
                    this.cbRGVCommand.DataSource = dv6;
                }
            }
        }

        private void cbConveyorCommand_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbConveyorName.SelectedValue == null) return;
            DataView dv = dbo.ExceSQL("SELECT F_IFChannelLeft,F_ChannelsIndex,F_RGVGateDeviceIndex ,F_Address,F_DeviceName"+
                "  FROM T_Base_RGV_Gate,T_Base_Device where F_RGVGateDeviceIndex=F_DeviceIndex and F_DeviceIndex=" + cbConveyorName.SelectedValue + "").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                string send = "";
                if (cbConveyorCommand.Text.Contains("接") == true)
                {
                    send = "送";
                }
                else
                {
                    send = "接";
                }
                string left = "";
                if (dv[0]["F_IFChannelLeft"].ToString() == "1")
                {
                    left = "左";
                }
                else
                {
                    left = "右";
                }
                DataView dv6 = dbo.ExceSQL("select F_DeviceCommandName,F_DeviceCommandIndex from T_Base_Device_Command where "+
                    "(F_DeviceCommandKind=1 or F_DeviceCommandKind=2) and (F_DeviceKindIndex=4) and (F_DeviceCommandName like '" + left + "%') and (F_DeviceCommandName like '%" + send + "%')").Tables[0].DefaultView;
                if (dv6.Count > 0)
                {
                    this.cbRGVCommand.DisplayMember = "F_DeviceCommandName";
                    this.cbRGVCommand.ValueMember = "F_DeviceCommandIndex";
                    this.cbRGVCommand.DataSource = dv6;
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要下达对接任务吗?", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }

            //向调度任务表插入手工任务,管理类型4,
            //管理索引(如果调度任务表没有手工任务,
            //在T_Base_Manage_Task_Index_Hand_Task表去找个基数然后加一)
            try
            {
                if (this.cbRGVName.Text.Trim() == "")
                {
                    MessageBox.Show("请选择RGV设备!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbRGVName.Focus();
                    return;
                }
                if (this.cbConveyorName.Text.Trim() == "")
                {
                    MessageBox.Show("请选择输送机设备!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbConveyorName.Focus();
                    return;
                }
                //20090918
                if (this.cbConveyorName.SelectedValue==null)
                {
                    MessageBox.Show("请选择输送机设备!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbConveyorName.Focus();
                    return;
                }
                if (this.cbRGVCommand.Text.Trim() == "")
                {
                    MessageBox.Show("请选择RGV设备命令!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbRGVCommand.Focus();
                    return;
                }

                if (this.cbConveyorCommand.Text.Trim() == "")
                {
                    MessageBox.Show("请选择输送机设备命令!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cbConveyorCommand.Focus();
                    return;
                }
                int routeRGVIDsub = GetRouteIDsub(Convert.ToInt32(cbRGVName.SelectedValue));
                if (routeRGVIDsub == -1)
                {
                    MessageBox.Show("您选择的设备错误!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                int routeIDsub = GetRouteIDsub(Convert.ToInt32(cbConveyorName.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.cbRGVName.SelectedValue + " or F_DeviceIndex=" + this.cbConveyorName.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(cbRGVName.SelectedValue) + " and F_DeviceCommandIndex=" + Convert.ToInt32(this.cbRGVCommand.SelectedValue) + " and F_DeviceCommandIndex <> -1");
                if (dss.Tables[0].DefaultView.Count <= 0)
                {
                    MessageBox.Show("请选择设备和对应的有效命令!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                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(cbConveyorName.SelectedValue) + " and F_DeviceCommandIndex=" + Convert.ToInt32(this.cbConveyorCommand.SelectedValue) + " and F_DeviceCommandIndex <> -1");
                if (dss.Tables[0].DefaultView.Count <= 0)
                {
                    MessageBox.Show("请选择设备和对应的有效命令!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                int hidx = GetManageHandIdx();
                int hmindx = ccf.GetMonitorIndex(hidx, 4);
                int hmindx1 = 0; 
                if (cbRGVCommand.Text.Contains("接") == true)
                {
                    Sql = "insert into T_Monitor_Task(F_RouteID,F_ManageTaskIndex,F_ManageTaskKindIndex,F_MonitorIndex," +
                            "F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1) " +
                            "values(" + routeRGVIDsub + "," + hidx + ",4," + hmindx + "," + Convert.ToInt32(cbRGVName.SelectedValue)
                            + "," + Convert.ToInt32(cbRGVCommand.SelectedValue) + "," + cbConveyorName.SelectedValue + ")";
                    ds = dbo.ExceSQL(Sql);
                    RecordMaxHandTaskFID(hidx);
                    hidx = GetManageHandIdx();
                    hmindx1 = 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_Associate) " +
                            "values(" + routeIDsub + "," + hidx + ",4," + hmindx1 + "," + Convert.ToInt32(cbConveyorName.SelectedValue)
                            + "," + Convert.ToInt32(cbConveyorCommand.SelectedValue) + "," + Convert.ToInt32(cbConveyorName.SelectedValue) + ","+hmindx+")";
                    ds = dbo.ExceSQL(Sql);
                    RecordMaxHandTaskFID(hidx);
                }
                else
                {


                    Sql = "insert into T_Monitor_Task(F_RouteID,F_ManageTaskIndex,F_ManageTaskKindIndex,F_MonitorIndex," +
                            "F_DeviceIndex,F_DeviceCommandIndex,F_NumParam1) " +
                            "values(" + routeIDsub + "," + hidx + ",4," + hmindx + "," + Convert.ToInt32(cbConveyorName.SelectedValue)
                            + "," + Convert.ToInt32(cbConveyorCommand.SelectedValue) + "," + Convert.ToInt32(cbConveyorName.SelectedValue) + ")";
                    ds = dbo.ExceSQL(Sql);
                    RecordMaxHandTaskFID(hidx);

                    hidx = GetManageHandIdx();
                    hmindx1 = 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_Associate) " +
                           "values(" + routeRGVIDsub + "," + hidx + ",4," + hmindx1 + "," + Convert.ToInt32(cbRGVName.SelectedValue)
                           + "," + Convert.ToInt32(cbRGVCommand.SelectedValue) + "," + cbConveyorName.SelectedValue + ","+hmindx+")";
                    ds = dbo.ExceSQL(Sql);
                    RecordMaxHandTaskFID(hidx);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


       
        
    }
}