using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //using static Google.Protobuf.Collections.MapField; using static Org.BouncyCastle.Math.EC.ECCurve; namespace GeBoShi.UI.User { public partial class NewUserFrm : Form { UserService service = new UserService(); User _User = new User(); User _FatherUser; bool _isNewUser = false; public NewUserFrm(User fatherUser,User m = null ) { InitializeComponent(); this.uiTitel1.ShowContrlBox(false, false, true); this.uiTitel1.FatherForm = this; if (m != null) { _FatherUser = fatherUser; _User = m; this.txtCode.Enabled = false; if (_User.Code == "admin") this.txtName.Enabled = cobRoleId.Enabled = false; this.tbPassword.Enabled = false; this.tbRePassword.Enabled = false; _isNewUser = false; } else { _isNewUser = true; } _FatherUser = fatherUser; } private void InitDataView() { this.cobRoleId.DisplayMember = "Name"; this.cobRoleId.ValueMember = "Id"; this.cobRoleId.DataSource = service.GetRoleItems(); // this.txtCode.Text = _User.Code; this.txtName.Text = _User.Name; this.txtNote.Text = _User.Note; this.cobRoleId.SelectedValue = _User.RoleId; if (!_isNewUser) this.tbPassword.Text = _User.Password; else this.tbPassword.Text = ""; this.tbRePassword.Text = ""; } private void NewUserFrm_Load(object sender, EventArgs e) { InitDataView(); } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void btnSave_Click(object sender, EventArgs e) { try { if((_isNewUser)&&(this.tbRePassword.Text != this.tbPassword.Text)) { throw new Exception("密码和重复密码不同!"); } string szCode = this.txtCode.Text.Trim(); string szName = this.txtName.Text.Trim(); string szNote = this.txtNote.Text.Trim(); string szPw = this.tbPassword.Text.Trim(); int roleId = (int)this.cobRoleId.SelectedValue; if (szCode == "" || szName == "") throw new Exception("请填写帐号和名称!"); _User.Code = szCode; _User.Name = szName; _User.RoleId = roleId; _User.Note = szNote; _User.ModifyUserCode = _FatherUser.Code; bool result; if (_User.Id == 0) { _User.Password = szPw; _User.CreateUserCode = _FatherUser.Code; result = service.Insert(_User); } else result = service.Update(_User); if (!result) throw new Exception("保存失败!"); MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.DialogResult = DialogResult.OK; this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }