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 FrmZCKIOStation : Form
    {
        DBOperator dbo = CStaticClass.dbo;
        DBOperator dboM = CStaticClass.dboM;
        private static FrmZCKIOStation _formInstance;

        public static FrmZCKIOStation FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmZCKIOStation();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmZCKIOStation()
        {
            InitializeComponent();
            DataView dv = dbo.ExceSQL("select F_DeviceIndex,F_DeviceName from T_Base_Device where F_DeviceKindIndex=2").Tables[0].DefaultView;
            if (dv.Count > 0)
            {
                this.comboBox1.DisplayMember = "F_DeviceName";
                this.comboBox1.ValueMember = "F_DeviceIndex";
                this.comboBox1.DataSource = dv;
            }
            _formInstance = this;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedValue == null) 
            {
                MessageBox.Show("请选择出入库站台!","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                this.comboBox1.Focus();
                return;
            }
            if (this.comboBox2.Text.Trim()=="")
            {
                MessageBox.Show("请选择出入库属性!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.comboBox2.Focus();
                return;
            }
            if ("出库站台入库站台拣选站台".IndexOf(this.comboBox2.Text.Trim()) < 0)
            {
                MessageBox.Show("请选择出入库属性!", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.comboBox2.Focus();
                return;
            }
            string iotype = " (T_Base_Route.F_RouteKind <> 0) ";
            if (this.comboBox2.Text.Trim() == "入库站台")
            {
                iotype = " (T_Base_Route.F_RouteKind = 1) ";
                dbo.ExecuteSql("update t_base_route set F_Status=1,F_AutoUpdate=1  where F_StartDevice=" + this.comboBox1.SelectedValue);
                dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where START_DEVICE=" + this.comboBox1.SelectedValue);
                dbo.ExecuteSql("update t_base_route set F_Status=0,F_AutoUpdate=0  where F_EndDevice=" + this.comboBox1.SelectedValue);
                dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where END_DEVICE=" + this.comboBox1.SelectedValue);
            }
            else if (this.comboBox2.Text.Trim() == "出库站台")
            {
                iotype = " (T_Base_Route.F_RouteKind = 2) ";
                dbo.ExecuteSql("update t_base_route set F_Status=1,F_AutoUpdate=1  where F_EndDevice=" + this.comboBox1.SelectedValue);
                dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where END_DEVICE=" + this.comboBox1.SelectedValue);
                dbo.ExecuteSql("update t_base_route set F_Status=0,F_AutoUpdate=0  where F_StartDevice=" + this.comboBox1.SelectedValue);
                dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where START_DEVICE=" + this.comboBox1.SelectedValue);
            }
            else
            {
                iotype = " (T_Base_Route.F_RouteKind <> 0) ";
                dbo.ExecuteSql("update t_base_route set F_Status=1,F_AutoUpdate=1  where ((F_EndDevice=" + this.comboBox1.SelectedValue +") or (F_StartDevice=" + this.comboBox1.SelectedValue +"))");
                dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where ((End_Device=" + this.comboBox1.SelectedValue + ") or (Start_Device=" + this.comboBox1.SelectedValue + "))");
            }
            DataView dv = dbo.ExceSQL("SELECT DISTINCT T_Base_Route.F_RouteID, T_Base_Route.F_RouteKind,  "+
                "T_Base_Route_Device.F_DeviceIndex FROM T_Base_Route,T_Base_Route_Device where "+
                "T_Base_Route.F_RouteID = T_Base_Route_Device.F_RouteID and "+iotype+
                " AND (T_Base_Route_Device.F_DeviceIndex = "+this.comboBox1.SelectedValue +")").Tables[0].DefaultView;
            DataView dvf;
                for(int i=0;i<dv.Count;i++)
                {
                    dbo.ExecuteSql("update t_base_route set F_Status=1,F_AutoUpdate=1  where f_routeid=" + dv[i]["F_RouteID"]);
                    dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where CONTROL_ROUTE_ID='" + dv[i]["F_RouteID"] + "' and CONTROL_ROUTE_STATUS=0");
                    if (dv[i]["F_RouteKind"].ToString() == "1")
                    {
                        
                        dvf = dbo.ExceSQL("SELECT DISTINCT T_Base_Route.F_RouteID, T_Base_Route.F_RouteKind,  " +
                        "T_Base_Route_Device.F_DeviceIndex FROM T_Base_Route,T_Base_Route_Device where " +
                        "T_Base_Route.F_RouteID = T_Base_Route_Device.F_RouteID and T_Base_Route.F_RouteKind = 2 " +
                        " AND (T_Base_Route_Device.F_DeviceIndex = " + this.comboBox1.SelectedValue + ")").Tables[0].DefaultView;
                        for (int f = 0; f < dvf.Count; f++)
                        {
                            dbo.ExecuteSql("update t_base_route set F_Status=0,F_AutoUpdate=0  where f_routeid=" + dvf[f]["F_RouteID"]);
                            dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where CONTROL_ROUTE_ID='" + dvf[f]["F_RouteID"] + "' and CONTROL_ROUTE_STATUS=1");
                        }
                    }
                    else if (dv[i]["F_RouteKind"].ToString() == "2")
                    {
                        
                        dvf = dbo.ExceSQL("SELECT DISTINCT T_Base_Route.F_RouteID, T_Base_Route.F_RouteKind,  " +
                        "T_Base_Route_Device.F_DeviceIndex FROM T_Base_Route,T_Base_Route_Device where " +
                        "T_Base_Route.F_RouteID = T_Base_Route_Device.F_RouteID and T_Base_Route.F_RouteKind = 1 " +
                        " AND (T_Base_Route_Device.F_DeviceIndex = " + this.comboBox1.SelectedValue + ")").Tables[0].DefaultView;
                        for (int f = 0; f < dvf.Count; f++)
                        {
                            dbo.ExecuteSql("update t_base_route set F_Status=0,F_AutoUpdate=0  where f_routeid=" + dvf[f]["F_RouteID"]);
                            dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where CONTROL_ROUTE_ID='" + dvf[f]["F_RouteID"] + "' and CONTROL_ROUTE_STATUS=1");
                        }
                    }
                    

                }
        }

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


    }
}