Newer
Older
GHFX_REFACTOR / Backup / FrmUserAdd.cs
wxn on 2 Nov 2016 11 KB 提交
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Collections;
namespace Cyberpipe
{
    public partial class FrmUserAdd : Office2007Form
    {
        int id;
        
        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 (comboBoxExUserRepo.Text.Trim() == "")
            {                
                MessageBox.Show("请选择仓库名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (cbxRegion.SelectedIndex == -1)
            {
                MessageBox.Show("请选择部门!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (comboBoxExRegionRole.SelectedIndex == -1)
            {
                MessageBox.Show("请选择部门角色!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (id == -1)
            {
                sql = "select * from casic_userstatus 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;
                }
                sql = "insert into casic_userstatus(\"username\",\"password\",\"repocode\",\"desc\",\"reid\",\"rid\") values('" + txtName.Text.Trim() + "','" + Utility.MD5Encrypt(txtPassWord.Text.Trim()) + "'," + repoCodes[comboBoxExUserRepo.SelectedItem.ToString().Trim()] + ",'" + txtDesc.Text.Trim() + "'," + region[cbxRegion.SelectedItem.ToString().Trim()] + "," + role[comboBoxExRegionRole.SelectedItem.ToString().Trim()] + ")";
            }
            else
            {
                sql = "select * from casic_userstatus where \"username\"='" + txtName.Text.Trim() + "' and \"id\" <> " + id.ToString();
                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;
                }
                sql = "update casic_userstatus set \"username\"='" + txtName.Text.Trim() + "' ,\"password\"='" + Utility.MD5Encrypt(txtPassWord.Text.Trim()) + "' ,\"repocode\"=" + repoCodes[comboBoxExUserRepo.Text.Trim()] + ",'desc'='" + txtDesc.Text.Trim() + "',reid = " + region[cbxRegion.SelectedItem.ToString().Trim()] + ",rid = " + role[comboBoxExRegionRole.SelectedItem.ToString().Trim()] + " where id =" + id.ToString();
            }
            try
            {
                OledbHelper.sqlExecuteNonQuery(sql);
                {
                    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DialogResult = DialogResult.OK;
                    this.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;
            
            //用户仓库
            sql = "select \"name\",\"id\" from casic_userrepo";
            dt = OledbHelper.QueryTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                repoCodes.Clear();
                comboBoxExUserRepo.Items.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                { 
                    repoCodes.Add(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString());
                    comboBoxExUserRepo.Items.Add(dt.Rows[i][0].ToString().Trim());
                }
            }

            //部门
            sql = "select \"id\",\"name\" from casic_region";
            dt = OledbHelper.QueryTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                region.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    region.Add(dt.Rows[i][1].ToString(), dt.Rows[i][0].ToString());
                    cbxRegion.Items.Add(dt.Rows[i][1].ToString());
                }
            }
            //角色
            sql = "select \"id\",\"name\" from casic_role";
            dt = OledbHelper.QueryTable(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                role.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    role.Add(dt.Rows[i][1].ToString(), dt.Rows[i][0].ToString());
                    comboBoxExRegionRole.Items.Add(dt.Rows[i][1].ToString());
                }
            }

            if (id != -1)
            {
                this.Text = "编辑用户信息";
                buttonX1.Text = "保存";
                try
                {
                    //用户名、密码、描述
                    sql = "select * from casic_userstatus where \"id\" =" + id.ToString();
                    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解密是不可逆的
                    }

                    //用户仓库
                    sql = "select \"name\" from casic_userrepo where \"id\"=" + dt.Rows[0]["repocode"].ToString().Trim();
                    DataTable dtRepo = OledbHelper.QueryTable(sql);
                    if (dtRepo != null && dtRepo.Rows.Count > 0)
                    {
                        for (int i = 0; i < comboBoxExUserRepo.Items.Count; i++)
                        {
                            if (comboBoxExUserRepo.Items[i].ToString().Trim() == dtRepo.Rows[0][0].ToString().Trim())
                            {
                                comboBoxExUserRepo.SelectedIndex = i;
                            }
                        }
                    }

                    
                    //部门 注册index改变事件
                    cbxRegion.SelectedIndexChanged += new EventHandler(cbxRegion_SelectedIndexChanged);
                    //部门 绑定数据
                    sql = "select \"name\" from casic_region where \"id\"=" + dt.Rows[0]["reid"].ToString().Trim();
                    DataTable dtRegion = OledbHelper.QueryTable(sql);
                    if (dtRegion != null && dtRegion.Rows.Count > 0)
                    {
                        for (int i = 0; i < cbxRegion.Items.Count; i++)
                        {
                            if (cbxRegion.Items[i].ToString().Trim() == dtRegion.Rows[0][0].ToString().Trim())
                            {
                                cbxRegion.SelectedIndex = i;
                            }
                        }
                    }
                    //部门角色
                    sql = "select \"name\" from casic_role where \"id\"=" + dt.Rows[0]["rid"].ToString().Trim();
                    DataTable dtRole = OledbHelper.QueryTable(sql);
                    if (dtRole != null && dtRole.Rows.Count > 0)
                    {
                        for (int i = 0; i < comboBoxExRegionRole.Items.Count; i++)
                        {
                            if (comboBoxExRegionRole.Items[i].ToString().Trim() == dtRole.Rows[0][0].ToString().Trim())
                            {
                                comboBoxExRegionRole.SelectedIndex = i;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogError.PublishError(ex);
                }
            }
            else
            {
                //部门 注册index改变事件
                cbxRegion.SelectedIndexChanged += new EventHandler(cbxRegion_SelectedIndexChanged);
            }
        }
        /// <summary>
        /// 下拉框选中项改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbxRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            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());//部门角色                    
                }
            }
        }
    }
}