/**
        * 根据ID获取变损类型
        * @param       ref DataTable dt 存储查询出来信息
        * @return      int 值为Constant.OK:执行成功,值为Constant.ERROR为执行失败
        * @author      Rick
        **/
        public int getTLoseTypeById(string TLoseNo, ref TransformerLose t_lose)
        {
            string strSelect = "select  distinct TransformerLoseNo, TransformerType, standarVolume from TransformerLoseInfo where TransformerLoseNo = " + TLoseNo;
            try
            {
                DataTable dt = SQLUtl.Query(strSelect).Tables[0];
                if (null != dt.Rows[0].ItemArray[0])
                {
                    t_lose.TransformerLoseNo = dt.Rows[0].ItemArray[0].ToString().Trim();
                }
                else
                {
                    t_lose.TransformerLoseNo = "";
                }

                if (null != dt.Rows[0].ItemArray[1])
                {
                    t_lose.TransformerType = dt.Rows[0].ItemArray[1].ToString().Trim();
                }
                else
                {
                    t_lose.TransformerType = "";
                }

                if (null != dt.Rows[0].ItemArray[2])
                {
                    t_lose.StandarVolume = dt.Rows[0].ItemArray[2].ToString().Trim();
                }
                else
                {
                    t_lose.StandarVolume = "";
                }

                return Constant.OK;
            }catch(Exception)
            {
                return Constant.ERROR;
            }
        }
        /**
         * 添加新变损信息
         * @param       string t_no:变损编号
         * @param      ref List<TransformerLose> list:变损信息
         * @return      int 值为Constant.OK:执行成功,值为Constant.ERROR为执行失败
         * @author      Rick
         **/
        public int getTLoseById(string t_lose_no, ref List<TransformerLose> list)
        {
            string strSelect = "select * from TransformerLoseInfo where TransformerLoseNo = " + t_lose_no;

            try
            {
                DataSet ds = SQLUtl.Query(strSelect);
                DataTable dt = ds.Tables[0];
                int count = dt.Rows.Count;
                int i = 0;
                while (count > 0)
                {
                    TransformerLose t_lose = new TransformerLose();

                    //给对象t_lose_no赋值
                    if (null != dt.Rows[i].ItemArray[0])
                    {
                        t_lose.TransformerLoseNo = dt.Rows[i].ItemArray[0].ToString().Trim();
                    }
                    else
                    {
                        t_lose.TransformerLoseNo = "";
                    }

                    if (null != dt.Rows[i].ItemArray[1])
                    {
                        t_lose.TransformerType = dt.Rows[i].ItemArray[1].ToString().Trim();
                    }
                    else
                    {
                        t_lose.TransformerType = "";
                    }

                    if (null != dt.Rows[i].ItemArray[2])
                    {
                        t_lose.StandarVolume = dt.Rows[i].ItemArray[2].ToString().Trim();
                    }
                    else
                    {
                        t_lose.StandarVolume = "";
                    }

                    if (null != dt.Rows[i].ItemArray[3])
                    {
                        t_lose.MonthUsed = dt.Rows[i].ItemArray[3].ToString().Trim();
                    }
                    else
                    {
                        t_lose.MonthUsed = "";
                    }

                    if (null != dt.Rows[i].ItemArray[4])
                    {
                        t_lose.TranformerLose = dt.Rows[i].ItemArray[4].ToString().Trim();
                    }
                    else
                    {
                        t_lose.TranformerLose = "";
                    }

                    if (null != dt.Rows[i].ItemArray[5])
                    {
                        t_lose.LessOrMoreFlag = dt.Rows[i].ItemArray[5].ToString().Trim();
                    }
                    else
                    {
                        t_lose.LessOrMoreFlag = "";
                    }

                    list.Add(t_lose);
                    count = count - 1;
                    i = i + 1;
                }

                return Constant.OK;
            }
            catch (Exception)
            {
                return Constant.ERROR;
            }
        }
        private void Add_Button_Click(object sender, EventArgs e)
        {
            List<TransformerLose> t_lose_list = new List<TransformerLose>();
            if (this.LoseNo_Add.Text.ToString().Trim() == "")
            {
                MessageBox.Show("请填写变损编号!");
                return;
            }
            bool state = false;
            this.t_lose_action.checkTLoseNo(this.LoseNo_Add.Text.ToString().Trim(), ref state);
            if (!state)
            {
                MessageBox.Show("该变损编号已经存在!");
                return;
            }
            if ("" == this.TType_Add.Text.ToString().Trim())
            {
                MessageBox.Show("电压器类型不能为空!");
                return;
            }
            if ("" == this.Limit_Add.Text.ToString().Trim())
            {
                MessageBox.Show("额定容量不能为空!");
                return;
            }

            if ("" == this.Less_Add_11.Text.ToString().Trim() || "" == this.Less_Add_12.Text.ToString().Trim())
            {
                MessageBox.Show("变损信息没有填写!");
                return;
            }
            else
            {
                TransformerLose t_lose_1 = new TransformerLose();
                t_lose_1.TransformerLoseNo = this.LoseNo_Add.Text.ToString().Trim();
                t_lose_1.TransformerType = this.TType_Add.Text.ToString().Trim();
                t_lose_1.StandarVolume = this.Limit_Add.Text.ToString().Trim();
                t_lose_1.MonthUsed = this.Less_Add_11.Text.ToString().Trim();
                t_lose_1.TranformerLose = this.Less_Add_12.Text.ToString().Trim();
                t_lose_1.LessOrMoreFlag = "0";
                t_lose_list.Add(t_lose_1);
            }

            if ("" == this.Less_Add_21.Text.ToString().Trim() || "" == this.Less_Add_22.Text.ToString().Trim())
            {
                MessageBox.Show("变损信息没有填写!");
                return;
            }
            else
            {
                TransformerLose t_lose_2 = new TransformerLose();
                t_lose_2.TransformerLoseNo = this.LoseNo_Add.Text.ToString().Trim();
                t_lose_2.TransformerType = this.TType_Add.Text.ToString().Trim();
                t_lose_2.StandarVolume = this.Limit_Add.Text.ToString().Trim();
                t_lose_2.MonthUsed = this.Less_Add_21.Text.ToString().Trim();
                t_lose_2.TranformerLose = this.Less_Add_22.Text.ToString().Trim();
                t_lose_2.LessOrMoreFlag = "0";
                t_lose_list.Add(t_lose_2);
            }

            if ("" == this.Less_Add_31.Text.ToString().Trim() || "" == this.Less_Add_32.Text.ToString().Trim())
            {
                MessageBox.Show("变损信息没有填写!");
                return;
            }
            else
            {
                TransformerLose t_lose_3 = new TransformerLose();
                t_lose_3.TransformerLoseNo = this.LoseNo_Add.Text.ToString().Trim();
                t_lose_3.TransformerType = this.TType_Add.Text.ToString().Trim();
                t_lose_3.StandarVolume = this.Limit_Add.Text.ToString().Trim();
                t_lose_3.MonthUsed = this.Less_Add_31.Text.ToString().Trim();
                t_lose_3.TranformerLose = this.Less_Add_32.Text.ToString().Trim();
                t_lose_3.LessOrMoreFlag = "0";
                t_lose_list.Add(t_lose_3);
            }

            if ("" == this.More_Add.Text.ToString().Trim())
            {
                MessageBox.Show("变损信息没有填写!");
                return;
            }
            else
            {
                TransformerLose t_lose_4 = new TransformerLose();
                t_lose_4.TransformerLoseNo = this.LoseNo_Add.Text.ToString().Trim();
                t_lose_4.TransformerType = this.TType_Add.Text.ToString().Trim();
                t_lose_4.StandarVolume = this.Limit_Add.Text.ToString().Trim();
                t_lose_4.MonthUsed = this.Less_Add_31.Text.ToString().Trim();
                t_lose_4.TranformerLose = this.More_Add.Text.ToString().Trim();
                t_lose_4.LessOrMoreFlag = "1";
                t_lose_list.Add(t_lose_4);
            }

            if (Constant.OK == this.t_lose_action.addTLose(ref t_lose_list))
            {
                MessageBox.Show("添加成功!");

                this.T_List_init();
                this.LoseNo_Add.Text = "";
                this.Limit_Add.Text = "";
                this.TType_Add.Text = "";
                this.Less_Add_11.Text = "";
                this.Less_Add_12.Text = "";
                this.Less_Add_21.Text = "";
                this.Less_Add_22.Text = "";
                this.Less_Add_31.Text = "";
                this.Less_Add_32.Text = "";
                this.More_Add.Text = "";
                // this.T_List.SelectedIndex = -1;

                this.Refresh();
                return;
            }
            else
            {
                MessageBox.Show("添加失败!");
                return;
            }
        }
        private void Update_Lose_Click(object sender, EventArgs e)
        {
            List<TransformerLose> t_lose_list = new List<TransformerLose>();

            if ("" == this.TType.Text.ToString().Trim())
            {
                MessageBox.Show("电压器类型不能为空!");
                return;
            }
            if ("" == this.Limit.Text.ToString().Trim())
            {
                MessageBox.Show("额定容量不能为空!");
                return;
            }

            if ("" == this.Less_11.Text.ToString().Trim() || "" == this.Less12.Text.ToString().Trim())
            {
                MessageBox.Show("变损信息没有填写!");
                return;
            }
            else
            {
                TransformerLose t_lose_1 = new TransformerLose();
                t_lose_1.TransformerLoseNo = this.LoseNo.Text.ToString().Trim();
                t_lose_1.TransformerType = this.TType.Text.ToString().Trim();
                t_lose_1.StandarVolume = this.Limit.Text.ToString().Trim();
                t_lose_1.MonthUsed = this.Less_11.Text.ToString().Trim();
                t_lose_1.TranformerLose = this.Less12.Text.ToString().Trim();
                t_lose_1.LessOrMoreFlag = "0";
                t_lose_list.Add(t_lose_1);
            }

            if ("" == this.Less22.Text.ToString().Trim() || "" == this.Less21.Text.ToString().Trim())
            {
                MessageBox.Show("变损信息没有填写!");
                return;
            }
            else
            {
                TransformerLose t_lose_2 = new TransformerLose();
                t_lose_2.TransformerLoseNo = this.LoseNo.Text.ToString().Trim();
                t_lose_2.TransformerType = this.TType.Text.ToString().Trim();
                t_lose_2.StandarVolume = this.Limit.Text.ToString().Trim();
                t_lose_2.MonthUsed = this.Less21.Text.ToString().Trim();
                t_lose_2.TranformerLose = this.Less22.Text.ToString().Trim();
                t_lose_2.LessOrMoreFlag = "0";
                t_lose_list.Add(t_lose_2);
            }

            if ("" == this.Less32.Text.ToString().Trim() || "" == this.Less31.Text.ToString().Trim())
            {
                MessageBox.Show("变损信息没有填写!");
                return;
            }
            else
            {
                TransformerLose t_lose_3 = new TransformerLose();
                t_lose_3.TransformerLoseNo = this.LoseNo.Text.ToString().Trim();
                t_lose_3.TransformerType = this.TType.Text.ToString().Trim();
                t_lose_3.StandarVolume = this.Limit.Text.ToString().Trim();
                t_lose_3.MonthUsed = this.Less31.Text.ToString().Trim();
                t_lose_3.TranformerLose = this.Less32.Text.ToString().Trim();
                t_lose_3.LessOrMoreFlag = "0";
                t_lose_list.Add(t_lose_3);
            }

            if ("" == this.More.Text.ToString().Trim())
            {
                MessageBox.Show("变损信息没有填写!");
                return;
            }
            else
            {
                TransformerLose t_lose_4 = new TransformerLose();
                t_lose_4.TransformerLoseNo = this.LoseNo.Text.ToString().Trim();
                t_lose_4.TransformerType = this.TType.Text.ToString().Trim();
                t_lose_4.StandarVolume = this.Limit.Text.ToString().Trim();
                t_lose_4.MonthUsed = this.Less31.Text.ToString().Trim();
                t_lose_4.TranformerLose = this.More.Text.ToString().Trim();
                t_lose_4.LessOrMoreFlag = "1";
                t_lose_list.Add(t_lose_4);
            }

            if (Constant.OK == this.t_lose_action.updateTLose(this.LoseNo.Text.ToString().Trim(), ref t_lose_list))
            {
                MessageBox.Show("更新成功!");
                return;
            }
            else
            {
                MessageBox.Show("更新失败!");
                return;
            }
        }
        private void T_List_SelectedIndexChanged(object sender, EventArgs e)
        {
            //TransformerInfo transfomerInfo = new TransformerInfo();
            TransformerLose transformerLose = new TransformerLose();
            List<TransformerLose> t_lose_list = new List<TransformerLose>();

            if (-1 != this.T_List.SelectedIndex)
            {
                //char c = ;
                string key = this.T_List.SelectedItem.ToString().Trim().Split(' ')[0];
                if (key == "")
                {
                    return;
                }
                if (Constant.OK == this.t_lose_action.getTLoseById(key, ref t_lose_list))
                {

                            //string item = transformerLose.TransformerLoseNo + " " + transformerLose.TransformerType + " " + transformerLose.StandarVolume;
                            //this.T_Lose_Type.SelectedItem = item;

                            this.LoseNo.Text = t_lose_list[0].TransformerLoseNo;
                            this.LoseNo.Enabled = false;

                            this.TType.Text = t_lose_list[0].TransformerType;

                            this.Limit.Text = t_lose_list[0].StandarVolume;

                            int count = t_lose_list.Count;
                            switch(count)
                            {
                                case 1:
                                    {
                                        this.Less_11.Text = t_lose_list[0].MonthUsed;
                                        this.Less12.Text = t_lose_list[0].TranformerLose;
                                        break;
                                    }
                                case 2:
                                    {
                                        this.Less_11.Text = t_lose_list[0].MonthUsed;
                                        this.Less12.Text = t_lose_list[0].TranformerLose;
                                        this.Less21.Text = t_lose_list[1].MonthUsed;
                                        this.Less22.Text = t_lose_list[1].TranformerLose;
                                        break;
                                    }
                                case 3:
                                    {
                                        this.Less_11.Text = t_lose_list[0].MonthUsed;
                                        this.Less12.Text = t_lose_list[0].TranformerLose;
                                        this.Less21.Text = t_lose_list[1].MonthUsed;
                                        this.Less22.Text = t_lose_list[1].TranformerLose;
                                        this.Less31.Text = t_lose_list[2].MonthUsed;
                                        this.Less32.Text = t_lose_list[2].TranformerLose;
                                        break;
                                    }
                                case 4:
                                    {
                                        this.Less_11.Text = t_lose_list[0].MonthUsed;
                                        this.Less12.Text = t_lose_list[0].TranformerLose;
                                        this.Less21.Text = t_lose_list[1].MonthUsed;
                                        this.Less22.Text = t_lose_list[1].TranformerLose;
                                        this.Less31.Text = t_lose_list[2].MonthUsed;
                                        this.Less32.Text = t_lose_list[2].TranformerLose;
                                        this.More.Text = t_lose_list[3].TranformerLose;
                                        break;
                                    }
                                default:
                                    {
                                        break;
                                    }

                            //this.Less_11.Text = t_lose_list[0].TranformerLose;
                            //this.Less12.Text = t_lose_list[0].MonthUsed;

                        }
                }
                else
                {
                        //do nothing
                }

               }
        }
        private void T_List_SelectedIndexChanged(object sender, EventArgs e)
        {
            TransformerInfo transfomerInfo = new TransformerInfo();
            TransformerLose transformerLose = new TransformerLose();

            if (-1 != this.T_List.SelectedIndex)
            {
                if (Constant.OK == this.t_info_action.getTInfoById(this.T_List.SelectedItem.ToString().Trim().Split(' ')[0], ref transfomerInfo))
                {
                    this.T_Code.Text = transfomerInfo.TransformerNo;
                    this.T_Code.Enabled = false;
                    this.T_Name.Text = transfomerInfo.TransformerName;
                    if ("" != transfomerInfo.TransformerLoseNo)
                    {
                        if (Constant.OK == this.t_lose_action.getTLoseTypeById(transfomerInfo.TransformerLoseNo, ref transformerLose))
                        {
                            string item = transformerLose.TransformerLoseNo + " " + transformerLose.TransformerType + " " + transformerLose.StandarVolume;
                            this.T_Lose_Type.SelectedItem = item;
                        }
                    }else
                    {
                        //do nothing
                    }

                }
            }
        }