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 ControlSystem
{
    /// <summary>
    /// 20101011新增加出入库站台属性定义
    /// </summary>
    public partial class FrmRelativeIDSet : Form
    {
        DBOperator dbo = CStaticClass.dbo;
        DBOperator dboM = CStaticClass.dboM;
        private static FrmRelativeIDSet _formInstance;

        public static FrmRelativeIDSet FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmRelativeIDSet();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmRelativeIDSet()
        {
            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.comboBoxFar.DisplayMember = "F_DeviceName";
                this.comboBoxFar.ValueMember = "F_DeviceIndex";
                this.comboBoxFar.DataSource = dv;
            }
            _formInstance = this;

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
             if (this.comboBoxFar.SelectedValue == null) 
                {
                    MessageBox.Show("请选择出入库站台！","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    this.comboBoxFar.Focus();
                    return;
                }
                if (this.comboBoxNear.Text.Trim()=="")
                {
                    MessageBox.Show("请选择出入库属性！", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.comboBoxNear.Focus();
                    return;
                }
                if ("出库站台入库站台拣选站台".IndexOf(this.comboBoxNear.Text.Trim()) < 0)
                {
                    MessageBox.Show("请选择出入库属性！", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.comboBoxNear.Focus();
                    return;
                }
                string iotype = " (T_Base_Route.F_RouteKind <> 0) ";
                if (this.comboBoxNear.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.comboBoxFar.SelectedValue);
                    dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where START_DEVICE=" + this.comboBoxFar.SelectedValue);
                    dbo.ExecuteSql("update t_base_route set F_Status=0,F_AutoUpdate=0  where F_EndDevice=" + this.comboBoxFar.SelectedValue);
                    dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where END_DEVICE=" + this.comboBoxFar.SelectedValue);
                }
                else if (this.comboBoxNear.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.comboBoxFar.SelectedValue);
                    dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where END_DEVICE=" + this.comboBoxFar.SelectedValue);
                    dbo.ExecuteSql("update t_base_route set F_Status=0,F_AutoUpdate=0  where F_StartDevice=" + this.comboBoxFar.SelectedValue);
                    dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=0 where START_DEVICE=" + this.comboBoxFar.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.comboBoxFar.SelectedValue +") or (F_StartDevice=" + this.comboBoxFar.SelectedValue +"))");
                    dboM.ExceSQL("update IO_CONTROL_ROUTE set CONTROL_ROUTE_STATUS=1 where ((End_Device=" + this.comboBoxFar.SelectedValue + ") or (Start_Device=" + this.comboBoxFar.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.comboBoxFar.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.comboBoxFar.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.comboBoxFar.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 FrmRelativeIDSet_Load(object sender, EventArgs e)
        {

        }


    }
}