using Sunny.UI; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using System.Windows.Forms; using DH.RBAC.Common; using DH.RBAC.Logic.Sys; using DH.RBAC.Utility.Other; using DH.RBAC.Model.Sys; namespace DH.RBAC.Page.Sys.User { public partial class AddUserForm : UIForm { private SysUserLogic userLogic; private SysUserRoleRelationLogic userRoleRelationLogic; private SysUserLogOnLogic userLogOnLogic; private SysRoleLogic roleLogic; private SysOrganizeLogic organizeLogic; public AddUserForm() { InitializeComponent(); userLogic = new SysUserLogic(); userRoleRelationLogic = new SysUserRoleRelationLogic(); userLogOnLogic = new SysUserLogOnLogic(); roleLogic = new SysRoleLogic(); organizeLogic = new SysOrganizeLogic(); } #region 标题栏 private void btnClose_Click(object sender, EventArgs e) { FormHelper.subForm = null; ParentPage.btnQuery_Click(null, null); this.Close(); } private Point mPoint; private void titlePanel_MouseDown(object sender, MouseEventArgs e) { mPoint = new Point(e.X, e.Y); } private void titlePanel_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y); } } private void btnClose_MouseEnter(object sender, EventArgs e) { btnClose.BackColor = Color.FromArgb(231, 231, 231); } private void btnClose_MouseLeave(object sender, EventArgs e) { btnClose.BackColor = Color.Transparent; } #endregion public UserPage ParentPage { get; set; } public string Id { get; set; } /// /// 画面加载,读取用户信息,显示在界面上 /// /// /// private void AddUserForm_Load(object sender, EventArgs e) { if (StringHelper.IsNullOrEmpty(Id)) { lblTitle.Text = "新增用户"; txtAccount.Enabled = true; } else { lblTitle.Text = "修改用户"; txtAccount.Enabled = false; txtPassword.Enabled = false; } //获取部门下拉列表的值 bool flag = GetDepartmentCombox(); if (!flag) { btnClose_Click(null, null); return; } flag = GetRoleTrans(); if (!flag) { btnClose_Click(null, null); return; } if (StringHelper.IsNullOrEmpty(Id)) { return; } SysUser entity = userLogic.Get(Id); entity.StrBirthday = entity.Birthday.Value.ToString("yyyy-MM-dd"); entity.RoleId = userRoleRelationLogic.GetList(entity.Id).Select(c => c.RoleId).ToList(); if (entity == null) { AntdUI.Message.warn(this, "用户信息不存在!", autoClose: 3); btnClose_Click(null, null); return; } //给文本框赋值 txtAccount.Text = entity.Account; txtNickName.Text = entity.NickName; txtName.Text = entity.RealName; txtBirthday.Text = entity.StrBirthday; if (entity.Gender == "1") rdMale.Checked = true; else rdFemale.Checked = true; txtEmail.Text = entity.Email; comboDept.SelectedValue = entity.DepartmentId; txtTel.Text = entity.MobilePhone; txtAddress.Text = entity.Address; uiIntegerUpDown1.Value = entity.SortCode.Value; //左边删除已存在的,右边新增这些 foreach (string id in entity.RoleId) { string name = roleIdDict[id]; if (uiTransfer1.ItemsLeft.Contains(name)) { uiTransfer1.ItemsLeft.Remove(name); } uiTransfer1.ItemsRight.Add(name); } } Dictionary roleNameDict = new Dictionary(); Dictionary roleIdDict = new Dictionary(); /// /// 获得角色 /// /// private bool GetRoleTrans() { List listRole = roleLogic.GetList(); var listTree = new List(); foreach (var item in listRole) { TreeSelect model = new TreeSelect(); model.id = item.Id; model.text = item.Name; listTree.Add(model); } foreach (TreeSelect select in listTree) { uiTransfer1.ItemsLeft.Add(select.text); roleIdDict.Add(select.id, select.text); roleNameDict.Add(select.text, select.id); } return true; } /// /// 获得部门下拉列表数据 /// /// private bool GetDepartmentCombox() { var data = organizeLogic.GetList(); var treeList = new List(); foreach (SysOrganize item in data) { TreeSelect model = new TreeSelect(); model.id = item.Id; model.text = item.FullName; model.parentId = item.ParentId; treeList.Add(model); } List list2 = treeList.Where(it => it.parentId != "0").ToList(); comboDept.ValueMember = "id"; comboDept.DisplayMember = "text"; comboDept.DataSource = list2; return true; } /// /// 确认按钮点击事件处理 /// /// /// private void btnConfirm_Click(object sender, EventArgs e) { if (StringHelper.IsNullOrEmpty(Id)) { DoAdd(); } else { DoUpdate(); } } /// /// 执行更新操作 /// private void DoUpdate() { bool flag = ChechEmpty(false); if (!flag) { return; } SysUser model = new SysUser(); model.Id = Id; model.Account = txtAccount.Text; model.NickName = txtNickName.Text; model.RealName = txtName.Text; model.StrBirthday = txtBirthday.Text; model.Gender = rdMale.Checked ? "1" : "0"; model.Email = txtEmail.Text; model.DepartmentId = comboDept.SelectedValue.ToString(); model.MobilePhone = txtTel.Text; model.Address = txtAddress.Text; model.SortCode = uiIntegerUpDown1.Value; List ids = new List(); foreach (string item in uiTransfer1.ItemsRight) { ids.Add(roleNameDict[item]); } string roleIds = StringHelper.GetStrArray(ids); model.roleIds = roleIds; model.ModifyUserId = GlobalConfig.CurrentUser.Id; DateTime defaultDt = DateTime.Today; DateTime.TryParse(model.StrBirthday + " 00:00:00", out defaultDt); model.Birthday = defaultDt; if (model.roleIds == "") { AntdUI.Message.warn(this, "请选择角色!", autoClose: 3); return; } //更新用户基本信息。 int row = userLogic.AppUpdateAndSetRole(model, model.roleIds.SplitToList().ToArray(), model.ModifyUserId); //更新用户角色信息。 if (row == 0) { AntdUI.Message.warn(this, "对不起,操作失败!", autoClose: 3); return; } //ParentPage.Init(); btnClose_Click(null, null); } /// /// 数据校验 /// /// /// private bool ChechEmpty(bool checkPassword) { if (StringHelper.IsNullOrEmpty(txtAccount.Text)) { AntdUI.Message.warn(this, "账号不能为空!", autoClose: 3); return false; } if (StringHelper.IsNullOrEmpty(txtNickName.Text)) { AntdUI.Message.warn(this, "昵称不能为空!", autoClose: 3); return false; } if (checkPassword) { if (StringHelper.IsNullOrEmpty(txtPassword.Text)) { AntdUI.Message.warn(this, "初始密码不能为空!", autoClose: 3); return false; } } if (comboDept.SelectedItem == null) { AntdUI.Message.warn(this, "所属部门不能为空!", autoClose: 3); return false; } if (StringHelper.IsNullOrEmpty(comboDept.SelectedItem.ToString())) { AntdUI.Message.warn(this, "所属部门不能为空!", autoClose: 3); return false; } if (StringHelper.IsNullOrEmpty(uiIntegerUpDown1.Text)) { AntdUI.Message.warn(this, "排序号不能为空!", autoClose: 3); return false; } if (comboDept.SelectedItem == null) { AntdUI.Message.warn(this, "所属部门不能为空!", autoClose: 3); return false; } if (StringHelper.IsNullOrEmpty(comboDept.SelectedItem.ToString())) { AntdUI.Message.warn(this, "所属部门不能为空!", autoClose: 3); return false; } if (uiTransfer1.ItemsRight == null) { AntdUI.Message.warn(this, "角色不能为空!", autoClose: 3); return false; } if (uiTransfer1.ItemsRight.Count == 0) { AntdUI.Message.warn(this, "角色不能为空!", autoClose: 3); return false; } return true; } /// /// 执行新增操作 /// private void DoAdd() { bool flag = ChechEmpty(true); if (!flag) return; SysUser model = new SysUser(); model.Account = txtAccount.Text; model.NickName = txtNickName.Text; model.password = txtPassword.Text.MD5Encrypt(); model.RealName = txtName.Text; model.StrBirthday = txtBirthday.Text; model.Gender = rdMale.Checked ? "1" : "0"; model.Email = txtEmail.Text; model.DepartmentId = comboDept.SelectedValue.ToString(); model.MobilePhone = txtTel.Text; model.Address = txtAddress.Text; model.SortCode = uiIntegerUpDown1.Value; model.CreateUserId = GlobalConfig.CurrentUser.Id; List ids = new List(); foreach (string item in uiTransfer1.ItemsRight) { ids.Add(roleNameDict[item]); } string roleIds = StringHelper.GetStrArray(ids); model.roleIds = roleIds; var userEntity = userLogic.GetByUserName(model.Account); if (userEntity != null) { AntdUI.Message.warn(this, "已存在当前用户名,请重新输入!", autoClose: 3); return; } DateTime defaultDt = DateTime.Today; DateTime.TryParse(model.StrBirthday + " 00:00:00", out defaultDt); model.Birthday = defaultDt; if (model.roleIds == "") { AntdUI.Message.warn(this, "请选择角色!", autoClose: 3); return; } int row = userLogic.AppInsert(model, model.password, model.roleIds.SplitToList().ToArray(), model.CreateUserId); if (row == 0) { AntdUI.Message.warn(this, "对不起,操作失败!", autoClose: 3); ; return; } btnClose_Click(null, null); } } }