using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBFactory;
namespace wcfControlMonitorClient
{
    /// <summary>
    /// 20101011新增加出入库站台属性定义
    /// </summary>
    public partial class FrmZCKSetIOStation : Form
    {
        DBOperator dbo = CStaticClass.dbo;
        DBOperator dboM = CStaticClass.dboM;
        private static FrmZCKSetIOStation _formInstance;

        public static FrmZCKSetIOStation FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmZCKSetIOStation();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmZCKSetIOStation()
        {
            InitializeComponent();
            
            _formInstance = this;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //sys_item    
            //Item_ID=54   item_flag=0两台工作;item_flag=1一台工作
            //wh_Cell  
            //device_code     cell_inout(In,Out,InOut,'')
            //22102  //22104
            int item_flag = 0;
            StringBuilder inout22102=new StringBuilder("");
            StringBuilder inout22104=new StringBuilder("");
            if (radioButton1.Checked == true)
            {//不能存在22102出库任务,22104入库任务
                if (IfExitTask(22102, 2) == true) return;
                if (IfExitTask(22104, 1) == true) return;
                item_flag = 0;
                inout22102.Append("In");
                inout22104.Append("Out");
                dbo.ExecuteSql("update t_base_route set F_Status=1 where F_RouteKind=1 and F_InsideAltDevice=22102");
                dbo.ExecuteSql("update t_base_route set F_Status=1 where F_RouteKind=2 and F_InsideAltDevice=22104");
                dbo.ExecuteSql("update t_base_route set F_Status=0 where F_RouteKind=2 and F_InsideAltDevice=22102");
                dbo.ExecuteSql("update t_base_route set F_Status=0 where F_RouteKind=1 and F_InsideAltDevice=22104");
            }
            else if (radioButton2.Checked == true)
            {//不能存在22104出库任务,22102入库任务
                if (IfExitTask(22104, 2) == true) return;
                if (IfExitTask(22102, 1) == true) return;
                item_flag = 0;
                inout22102.Append("Out");
                inout22104.Append("In");
                dbo.ExecuteSql("update t_base_route set F_Status=1 where F_RouteKind=2 and F_InsideAltDevice=22102");
                dbo.ExecuteSql("update t_base_route set F_Status=1 where F_RouteKind=1 and F_InsideAltDevice=22104");
                dbo.ExecuteSql("update t_base_route set F_Status=0 where F_RouteKind=1 and F_InsideAltDevice=22102");
                dbo.ExecuteSql("update t_base_route set F_Status=0 where F_RouteKind=2 and F_InsideAltDevice=22104");
            }
            else if (radioButton3.Checked == true)
            {//不能存在22102出入库任务
                if (IfExitTask(22102, 2) == true) return;
                if (IfExitTask(22102, 1) == true) return;
                item_flag = 0;
                inout22102.Append("");
                inout22104.Append("InOut");
                dbo.ExecuteSql("update t_base_route set F_Status=0 where F_InsideAltDevice=22102");
                dbo.ExecuteSql("update t_base_route set F_Status=1 where F_InsideAltDevice=22104");
            }
            else if (radioButton4.Checked == true)
            {//不能存在22104出入库任务
                if (IfExitTask(22104, 2) == true) return;
                if (IfExitTask(22104, 1) == true) return;
                item_flag = 0;
                inout22102.Append("InOut");
                inout22104.Append("");
                dbo.ExecuteSql("update t_base_route set F_Status=1 where F_InsideAltDevice=22102");
                dbo.ExecuteSql("update t_base_route set F_Status=0 where F_InsideAltDevice=22104");
            }
            dboM.ExecuteSql(string.Format("update sys_item set item_flag={0} where Item_ID=54 ",item_flag));
            dboM.ExecuteSql(string.Format("update wh_Cell set cell_inout='{0}' where device_code=22102 ",inout22102.ToString()));
            dboM.ExecuteSql(string.Format("update wh_Cell set cell_inout='{0}' where device_code=22104 ", inout22104.ToString()));

        }

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

        private void FrmZCKSetIOStation_Load(object sender, EventArgs e)
        {
            string io22102 = string.Empty;
            string io22104 = string.Empty;
            DataView dv = dboM.ExceSQL("select cell_inout from wh_Cell where device_code=22102").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                if (dv[0]["cell_inout"].ToString().ToUpper() == "IN")
                {//下楼
                    io22102 = "IN";
                }
                else if (dv[0]["cell_inout"].ToString().ToUpper() == "OUT")
                {//上楼
                    io22102 = "OUT";
                }
                else if (dv[0]["cell_inout"].ToString().ToUpper() == "INOUT")
                {//上下楼
                    io22102 = "INOUT";
                }
                else
                {//被停用
                    io22102 = "";
                }
            }
            dv = dboM.ExceSQL("select cell_inout from wh_Cell where device_code=22104").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                if (dv[0]["cell_inout"].ToString().ToUpper() == "IN")
                {//下楼
                    io22104 = "IN";
                }
                else if (dv[0]["cell_inout"].ToString().ToUpper() == "OUT")
                {//上楼
                    io22104 = "OUT";
                }
                else if (dv[0]["cell_inout"].ToString().ToUpper() == "INOUT")
                {//上下楼
                    io22104 = "INOUT";
                }
                else
                {//被停用
                    io22104 = "";
                }
            }
            if (io22102 == "IN" && io22104 == "OUT")
            {
                radioButton1.Checked = true;
            }
            else if (io22104 == "IN" && io22102 == "OUT")
            {
                radioButton2.Checked = true;
            }
            else if (io22104 == "INOUT")
            {
                radioButton3.Checked = true;
            }
            else if (io22102 == "INOUT")
            {
                radioButton4.Checked = true;
            }
        }

        bool IfExitTask(int device, int io)
        {
            DataView dv = dbo.ExceSQL(string.Format("SELECT T_Monitor_Task.F_MonitorIndex FROM T_Base_Route,T_Base_Route_Device,T_Monitor_Task WHERE T_Base_Route.F_RouteID = T_Base_Route_Device.F_RouteID and T_Base_Route_Device.F_RouteIDSub = T_Monitor_Task.F_RouteID and (T_Base_Route.F_RouteKind = {0}) AND (T_Base_Route.F_InsideAltDevice = {1})",io,device )).Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

    }
}