/**
         *
         * 插入teacher,失败返回0
         *
         */
        public int insert(Teacher teacher)
        {
            String sql = "insert into teachers values('"
                            + teacher.Id + "','"
                            + teacher.Name + "','"
                            + teacher.Sex + "','"
                            + teacher.Organization + "','"
                            + teacher.Address + "','"
                            + teacher.Phone+ "',"
                            + teacher.BaseSalary + ","
                            + teacher.Allowance + ","
                            + teacher.Fund + ","
                            + teacher.Sanitary + ","
                            + teacher.IncomeTax + ")";

            //Console.WriteLine(sql);
            connection.Open();
            cmd = new MySqlCommand(sql, connection);
            int n = cmd.ExecuteNonQuery();

            //关闭连接
            closeConnection();

            return n;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (Util.checkTeacherId(textBox1.Text.ToString().Trim()) == false)
                return;
            else
                mTeacher = new TeacherDB().selectTeacherById(textBox1.Text.ToString().Trim());

            textBox2.Text = (mTeacher.BaseSalary + mTeacher.Allowance).ToString();
            textBox3.Text = (mTeacher.Fund + mTeacher.Sanitary + mTeacher.IncomeTax).ToString();
            textBox4.Text = (Double.Parse(textBox2.Text.ToString().Trim()) - Double.Parse(textBox3.Text.ToString().Trim())).ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //判断教工号【是否符合条件,是否被修改】
            if (!mTeacher.Id.Equals(textBox1.Text.ToString().Trim()))
            {
                if (Util.checkTeacherId(textBox1.Text.ToString().Trim()))
                    mTeacher = new TeacherDB().selectTeacherById(textBox1.Text.ToString().Trim());//如果教工号改变则重新获取mTeacher
                else
                {
                    textBox1.Select();
                    return;
                }
            }

            //如果控件已经存在就删除重建;
            Util.checkView(this);

            //基本工资
            lb2 = new Label();
            lb2.Text = "基本工资:";
            lb2.Top = label1.Top + 40;
            lb2.Left = label1.Left;
            lb2.Width = 59;
            this.Controls.Add(lb2);
            textBox2 = new TextBox();
            textBox2.Top = lb2.Top;
            textBox2.Left = textBox1.Left;
            textBox2.Width = 100;
            textBox2.Height = 21;
            textBox2.Text = mTeacher.BaseSalary.ToString();
            this.Controls.Add(textBox2);

            //津贴
            lb3 = new Label();
            lb3.Text = "津贴:";
            lb3.Top = lb2.Top + 40;
            lb3.Left = lb2.Left;
            lb3.Width = 59;
            this.Controls.Add(lb3);
            textBox3 = new TextBox();
            textBox3.Top = lb3.Top;
            textBox3.Left = textBox1.Left;
            textBox3.Width = 100;
            textBox3.Height = 21;
            textBox3.Text = mTeacher.Allowance.ToString();
            this.Controls.Add(textBox3);

            //公积金
            lb4 = new Label();
            lb4.Text = "公积金:";
            lb4.Top = lb3.Top + 40;
            lb4.Left = lb3.Left;
            lb4.Width = 59;
            this.Controls.Add(lb4);
            textBox4 = new TextBox();
            textBox4.Top = lb4.Top;
            textBox4.Left = textBox1.Left;
            textBox4.Width = 100;
            textBox4.Height = 21;
            textBox4.Text = mTeacher.Fund.ToString();
            this.Controls.Add(textBox4);

            //生活费
            lb5 = new Label();
            lb5.Text = "生活费:";
            lb5.Top = lb4.Top + 40;
            lb5.Left = lb2.Left;
            lb5.Width = 59;
            this.Controls.Add(lb5);
            textBox5 = new TextBox();
            textBox5.Top = lb5.Top;
            textBox5.Left = textBox1.Left;
            textBox5.Width = 100;
            textBox5.Height = 21;
            textBox5.Text = mTeacher.Sanitary.ToString();
            this.Controls.Add(textBox5);

            //所得税
            lb6 = new Label();
            lb6.Text = "所得税:";
            lb6.Top = lb5.Top + 40;
            lb6.Left = lb2.Left;
            lb6.Width = 59;
            this.Controls.Add(lb6);
            textBox6 = new TextBox();
            textBox6.Top = lb6.Top;
            textBox6.Left = textBox1.Left;
            textBox6.Width = 100;
            textBox6.Height = 21;
            textBox6.Text = mTeacher.IncomeTax.ToString();
            this.Controls.Add(textBox6);

            b = true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //判断教工号【是否符合条件,是否被修改】
            if (!mTeacher.Id.Equals(textBox1.Text.ToString().Trim()))
            {
                if (Util.checkTeacherId(textBox1.Text.ToString().Trim()))
                    mTeacher = new TeacherDB().selectTeacherById(textBox1.Text.ToString().Trim());//如果教工号改变则重新获取mTeacher
                else
                {
                    textBox1.Select();
                    return;
                }
            }

            //如果控件已经存在就删除重建;
            Util.checkView(this);

            //姓名
            lb2 = new Label();
            lb2.Name = "lb2";
            lb2.Text = "教师姓名:";
            lb2.Top = label1.Top + 40;
            lb2.Left = label1.Left;
            lb2.Width = 59;
            this.Controls.Add(lb2);
            textBox2 = new TextBox();
            textBox2.Top = lb2.Top;
            textBox2.Left = textBox1.Left;
            textBox2.Width = 100;
            textBox2.Height = 21;
            textBox2.Text = mTeacher.Name.ToString();
            this.Controls.Add(textBox2);

            //性别控件
            lb3 = new Label();
            lb3.Name = "lb3";
            lb3.Text = "性别:";
            lb3.Top = lb2.Top + 40;
            lb3.Left = lb2.Left;
            lb3.Width = 50;
            this.Controls.Add(lb3);
            pl = new Panel();
            pl.Top = lb3.Top;
            pl.Left = textBox2.Left;
            pl.Width=130;
            pl.Height = 28;
            rb1 = new RadioButton();
            rb1.Text = "男";
            rb1.Width = 35;
            rb1.Height = 16;
            rb1.Left = 15;
            rb2 = new RadioButton();
            rb2.Text = "女";
            rb2.Width = 35;
            rb2.Height = 16;
            rb2.Left = rb1.Left+50;
            if (mTeacher.Sex.Equals("男"))
                rb1.Checked = true;
            else
                rb2.Checked = true;
            pl.Controls.Add(rb2);
            pl.Controls.Add(rb1);
            this.Controls.Add(pl);

            //单位名称
            lb4 = new Label();
            lb4.Name = "lb4";
            lb4.Text="单位名称:";
            lb4.Top = lb3.Top + 40;
            lb4.Left = lb3.Left;
            lb4.Width = 59;
            this.Controls.Add(lb4);
            textBox3 = new TextBox();
            textBox3.Top = lb4.Top;
            textBox3.Left = textBox2.Left;
            textBox3.Width = 100;
            textBox3.Height = 21;
            textBox3.Text = mTeacher.Organization.ToString();
            this.Controls.Add(textBox3);

            //家庭地址
            lb5 = new Label();
            lb5.Name = "lb5";
            lb5.Text = "家庭地址:";
            lb5.Top = lb4.Top + 40;
            lb5.Left = lb3.Left;
            lb5.Width = 59;
            this.Controls.Add(lb5);
            textBox4 = new TextBox();
            textBox4.Top = lb5.Top;
            textBox4.Left = textBox2.Left;
            textBox4.Width = 135;
            textBox4.Height = 21;
            textBox4.Text = mTeacher.Address.ToString();
            this.Controls.Add(textBox4);

            //联系电话
            lb6 = new Label();
            lb6.Name = "lb6";
            lb6.Text = "联系电话:";
            lb6.Top = lb5.Top + 40;
            lb6.Left = lb3.Left;
            lb6.Width = 59;
            this.Controls.Add(lb6);
            textBox5 = new TextBox();
            textBox5.Top = lb6.Top;
            textBox5.Left = textBox2.Left;
            textBox5.Width = 135;
            textBox5.Height = 21;
            textBox5.Text = mTeacher.Phone.ToString();
            this.Controls.Add(textBox5);

            b = true;//查询完成
        }
        private void button1_Click(object sender, EventArgs e)
        {
            flag = true;//初始化flag
            //判断教工号格式
            if (!Util.checkInputTeacherId(textBox1.Text.ToString().Trim(), label7,2))
                flag = false;

            //判断手机号码格式
            Regex phoneReg = new Regex("1[3,5,7,8][0,2,3,6,5,7,8][0-9]{8}");
            if (phoneReg.IsMatch(textBox5.Text.Trim()))
            {
                label8.Text = "通过!";
            }
            else
            {
                label8.Text ="手机号输入有误!";
                flag = false;
            }

            //判断姓名是否为空
            if (Util.isEmpty(textBox2.Text))
            {
                label9.Text = "不能为空!";
                flag = false;
            }
            else
            {
                label9.Text = "通过!";
            }

            //判断单位名称是否为空
            if (Util.isEmpty(textBox3.Text))
            {
                label10.Text = "不能为空!";
                flag = false;
            }
            else
            {
                label10.Text = "通过!";
            }

            //判断家庭住址是否为空
            if (Util.isEmpty(textBox4.Text))
            {
                label11.Text = "不能为空!";
                flag = false;
            }
            else
            {
                label11.Text = "通过!";
            }
            if (flag == true)
            {
                Teacher mTeacher = new Teacher();
                mTeacher.Id = textBox1.Text.ToString().Trim();
                mTeacher.Name = textBox2.Text.ToString().Trim();
                if (radioButton1.Checked)
                    mTeacher.Sex = "男";
                else
                    mTeacher.Sex = "女";
                mTeacher.Organization = textBox3.Text.ToString().Trim();
                mTeacher.Address = textBox4.Text.ToString().Trim();
                mTeacher.Phone = textBox5.Text.ToString().Trim();
                TeacherDB mTeacherDB = new TeacherDB();
                mTeacherDB.insert(mTeacher);

                MessageBox.Show("录入成功!");
                TeacherSelect._getInstance().resetData();
                SalarySelect._getInstance().resetData();
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                radioButton1.Checked = true;
                label7.Text = "";
                label8.Text = "";
                label9.Text = "";
                label10.Text = "";
                label11.Text = "";

            }
        }
        /**
         *
         * 根据teacher.id更新teacher,失败返回0
         *
         */
        public int updata(Teacher teacher)
        {
            String sql = "update teachers set"
                            + " teacher_name='" + teacher.Name + "',"
                            + " teacher_sex='" + teacher.Sex + "',"
                            + " teacher_organization='" + teacher.Organization + "',"
                            + " teacher_address='" + teacher.Address + "',"
                            + " teacher_phone='" + teacher.Phone + "',"
                            + " salary_base=" + teacher.BaseSalary + ","
                            + " salary_allowance=" + teacher.Allowance + ","
                            + " salary_fund=" + teacher.Fund + ","
                            + " expenses_sanitary=" + teacher.Sanitary + ","
                            + " expenses_incomeTax=" + teacher.IncomeTax
                            + " where teacher_id='" + teacher.Id + "'";
            //Console.WriteLine(sql);
            connection.Open();
            cmd = new MySqlCommand(sql, connection);
            int n = cmd.ExecuteNonQuery();

            //关闭连接
            closeConnection();

            return n;
        }
        /**
         *
         * 通过teacherid查询teacher
         *
         */
        public Teacher selectTeacherById(String teacherId)
        {
            String sql = "select * from teachers where teacher_id='" + teacherId + "'";
            //Console.WriteLine(sql);
            connection.Open();
            cmd = new MySqlCommand(sql, connection);
            MySqlDataReader dataReader = cmd.ExecuteReader();

            if (dataReader.Read())
            {
                Teacher teacher = new Teacher();
                try
                {
                    teacher.Id = dataReader.GetString(0);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Id = "";
                }
                try
                {
                    teacher.Name = dataReader.GetString(1);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Name = "";
                }
                try
                {
                    teacher.Sex = dataReader.GetString(2);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Sex = "";
                }
                try
                {
                    teacher.Organization = dataReader.GetString(3);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Organization = "";
                }
                try
                {
                    teacher.Address = dataReader.GetString(4);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Address = "";
                }
                try
                {
                    teacher.Phone = dataReader.GetString(5);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Phone = "";
                }
                try
                {
                    teacher.BaseSalary = dataReader.GetFloat(6);
                }
                catch (SqlNullValueException e)
                {
                    teacher.BaseSalary = 0;
                }
                try
                {
                    teacher.Allowance = dataReader.GetFloat(7);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Allowance = 0;
                }
                try
                {
                    teacher.Fund = dataReader.GetFloat(8);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Fund = 0;
                }
                try
                {
                    teacher.Sanitary = dataReader.GetFloat(9);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Sanitary = 0;
                }
                try
                {
                    teacher.IncomeTax = dataReader.GetFloat(10);
                }
                catch (SqlNullValueException e)
                {
                    teacher.IncomeTax = 0;
                }

                //关闭连接
                dataReader.Close();
                closeConnection();

                return teacher;
            }

            //关闭连接
            dataReader.Close();
            closeConnection();
            return null;
        }
        /**
         *
         * 查找全部teacher,返回List<Teacher>;
         *
         */
        public List<Teacher> selectAll()
        {
            List<Teacher> all=new List<Teacher>();

            String sql = "select * from teachers";
            connection.Open();
            cmd = new MySqlCommand(sql, connection);
            MySqlDataReader dataReader= cmd.ExecuteReader();
            while (dataReader.Read())
            {

                Teacher teacher = new Teacher();
                try
                {
                    teacher.Id = dataReader.GetString(0);
                }
                catch(SqlNullValueException e)
                {
                    teacher.Id="";
                }
                try
                {
                    teacher.Name = dataReader.GetString(1);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Name = "";
                }
                try
                {
                    teacher.Sex = dataReader.GetString(2);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Sex = "";
                }
                try
                {
                    teacher.Organization = dataReader.GetString(3);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Organization = "";
                }
                try
                {
                    teacher.Address = dataReader.GetString(4);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Address = "";
                }
                try
                {
                    teacher.Phone = dataReader.GetString(5);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Phone = "";
                }
                try
                {
                    teacher.BaseSalary = dataReader.GetFloat(6);
                }
                catch (SqlNullValueException e)
                {
                    teacher.BaseSalary = 0;
                }
                try
                {
                    teacher.Allowance = dataReader.GetFloat(7);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Allowance = 0;
                }
                try
                {
                    teacher.Fund = dataReader.GetFloat(8);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Fund = 0;
                }
                try
                {
                    teacher.Sanitary = dataReader.GetFloat(9);
                }
                catch (SqlNullValueException e)
                {
                    teacher.Sanitary = 0;
                }
                try
                {
                    teacher.IncomeTax = dataReader.GetFloat(10);
                }
                catch (SqlNullValueException e)
                {
                    teacher.IncomeTax = 0;
                }

                all.Add(teacher);
            }

            //关闭连接
            dataReader.Close();
            closeConnection();

            return all;
        }