Newer
Older
EMS_REFACTOR / FrmUserAdd.cs
nn-203 on 26 Jul 2017 9 KB first commit
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace Cyberpipe
{
    public partial class FrmUserAdd : Office2007Form
    {
        int id;
        string username="";
        string pwd="";
        
        Dictionary<string, string> repoCodes = new Dictionary<string, string>();
        Dictionary<string, string> region = new Dictionary<string, string>();
        Dictionary<string, string> role = new Dictionary<string, string>();

        public FrmUserAdd(int _id)
        {
            InitializeComponent();
            id = _id;
        }
        /// <summary>
        /// 添加、保存按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (txtName.Text.Trim() == "")
            {
                MessageBox.Show("请输入用户名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (txtPassWord.Text.Trim() == "")
            {
                MessageBox.Show("请输入用户密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            
            if (comboBoxExRegionRole.SelectedIndex == -1)
            {
                MessageBox.Show("请选择角色!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (appCombo.SelectedIndex == -1) {
                MessageBox.Show("请选择其App权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (id == -1)
            {
                sql = "select * from casic_userinfotest where USERNAME='" + txtName.Text.Trim() + "'";
                try
                {
                    DataTable dt = OledbHelper.QueryTable(sql);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        MessageBox.Show("该用户名已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtName.Clear();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    LogError.PublishError(ex);
                    return;
                }
                string role=comboBoxExRegionRole.SelectedItem.ToString().Trim();
                int roleid = 0;
                if (role == "系统管理员")
                {
                    roleid = 1;
                }
                else if (role == "数据管理员")
                {
                    roleid = 2;
                }
                else if (role == "设计审核员")
                {
                    roleid = 3;
                }
                else if (role == "普通工作人员")
                {
                    roleid = 4;
                }
                
                string approle = appCombo.SelectedItem.ToString().Trim();
                int approleid = 0;
                if (approle == "有审批权限") {
                    approleid = 1;
                }
                
                sql = "insert into casic_userinfotest(USERNAME,PASSWORD,RID,APPR) values('" 
                    + txtName.Text.Trim() + "','" + Utility.MD5Encrypt2(txtPassWord.Text.Trim()) + "',"
                    + roleid + "," + approleid + ")";
            }
            else
            {
                sql = "select * from casic_userinfotest where USERNAME ='" + txtName.Text.Trim() 
                    + "' and ID <> " + id;
                
                try
                {
                    DataTable dt = OledbHelper.QueryTable(sql);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        MessageBox.Show("该用户名已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtName.Clear();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    LogError.PublishError(ex);
                    return;
                }
                string role = comboBoxExRegionRole.SelectedItem.ToString().Trim();
                int roleid = 0;
                if (role == "系统管理员")
                {
                    roleid = 1;
                }
                else if (role == "数据管理员")
                {
                    roleid = 2;
                }
                else if (role == "设计审核员")
                {
                    roleid = 3;
                }
                else if (role == "普通工作人员")
                {
                    roleid = 4;
                }

                string approle = appCombo.SelectedItem.ToString().Trim();
                int approleid = 0;
                if (approle == "有审批权限")
                {
                    approleid = 1;
                }

                if(pwd!=txtPassWord.Text.Trim()){
                    sql = "update casic_userinfotest set USERNAME='" + txtName.Text.Trim() + "',PASSWORD='" 
                    + Utility.MD5Encrypt2(txtPassWord.Text.Trim()) + "' ,rid = "
                    + roleid + ",APPR="+approleid+" where ID =" + id; 
                }else{
                 sql = "update casic_userinfotest set USERNAME='" + txtName.Text.Trim() + "',PASSWORD='" 
                    + (txtPassWord.Text.Trim()) + "' ,rid = "  + roleid + ",APPR="+approleid
                    +" where ID =" + id; 
                }
            }
            try
            {
                OledbHelper.sqlExecuteNonQuery(sql);
                {
                    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DialogResult = DialogResult.OK;
                    
                    Close();
                }
            }
            catch (Exception ex)
            {
                LogError.PublishError(ex);
            }
        }
        /// <summary>
        /// 关闭按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_Click(object sender, EventArgs e)
        {
            Close();
        }
        /// <summary>
        /// 窗体初始化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmUserRepo_Load(object sender, EventArgs e)
        {
            string sql = "";
            DataTable dt = null;
            if (id == -1)//新建用户 
            {
                //初始化两个combobox 
                appCombo.SelectedIndex = 0;
                comboBoxExRegionRole.SelectedIndex = 3;
            }

             if (id != -1)
            {
                Text = "编辑用户信息";
                buttonX1.Text = "保存";
                try
                {
                    //用户名、密码、描述
                    sql = "select * from casic_userinfotest where \"ID\" =" + id;
                    dt = OledbHelper.QueryTable(sql);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        txtName.Text = dt.Rows[0]["USERNAME"].ToString().Trim();
                        //txtDesc.Text = dt.Rows[0]["desc"].ToString().Trim();
                        txtPassWord.Text = dt.Rows[0]["PASSWORD"].ToString().Trim();//MD5解密是不可逆的
                        username = txtName.Text;
                        pwd = txtPassWord.Text;

                        int roleid = int.Parse(dt.Rows[0]["RID"].ToString().Trim());
                        comboBoxExRegionRole.SelectedIndex = (roleid - 1);
                        appCombo.SelectedIndex = int.Parse(dt.Rows[0]["APPR"].ToString().Trim());

                    } 
                    
                }
                catch (Exception ex)
                {
                    LogError.PublishError(ex);
                }
            }
        }
        /// <summary>
        /// 下拉框选中项改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql = "";
            //string sql = "select casic_role.\"name\" from casic_role join casic_regionrole on casic_role.\"id\"=casic_regionrole.\"rid\" where casic_regionrole.\"reid\"=" + region[cbxRegion.SelectedItem.ToString().Trim()];
            DataTable dt = OledbHelper.QueryTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                comboBoxExRegionRole.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    comboBoxExRegionRole.Items.Add(dt.Rows[i][0].ToString().Trim());//部门角色                    
                }
            }
        }
    }
}