using System; using System.Collections.Generic; using System.Threading; using System.Windows.Forms; using DevExpress.XtraEditors; using GCAS.Code; using GCAS.Dto; using GCAS.Localization; using GCAS.Model; namespace GCAS { public partial class NewOperatorForm : XtraForm { private List L; public OperatorModel operatorModel; public OperatorModel currentOperator; private OperatorRepository operatorRepository; private readonly ActionsRepository actionsRepository; public NewOperatorForm() { InitializeComponent(); actionsRepository = new ActionsRepository(); L = LocalizationHelper.GetSource(Thread.CurrentThread.CurrentUICulture); } private void NewDeviceForm_Load(object sender, EventArgs e) { operatorRepository = new OperatorRepository(); InitView(); } private void InitView() { if (operatorModel != null) { text_name.Text = operatorModel.Name; text_truename.Text = operatorModel.TrueName; cb_role.EditValue = operatorModel.Role == 1 ? L.GetString("User") : L.GetString("Admin"); if (operatorModel.Id == currentOperator.Id) { cb_role.Enabled = false; } } } private async void btn_save_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(text_name.Text)) { XtraMessageBox.Show(L.GetString("userNameNotNull"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(text_truename.Text)) { XtraMessageBox.Show(L.GetString("trueNameNotNull"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(text_pwd.Text) && operatorModel == null) { XtraMessageBox.Show(L.GetString("passwordNotNull"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int role = 1; switch (cb_role.Text) { case "管理员": case "Admin": role = 2; break; case "普通用户": case "User": role = 1; break; } if (operatorModel == null) { operatorModel = new OperatorModel(); } operatorModel.Name = text_name.Text; operatorModel.TrueName = text_truename.Text; operatorModel.Role = role; operatorModel.Pwd = operatorModel == null || !string.IsNullOrEmpty(text_pwd.Text) ? SHAHelper.SHAmd5Encrypt(text_pwd.Text) : operatorModel.Pwd; int result = 0; if (operatorModel.Id == 0) { result = await operatorRepository.Insert(operatorModel); if (result > 0) { await actionsRepository.Insert(new ActionsModel { Info = string.Format(L.GetString("addUserLog"), operatorModel.Name), Operator = currentOperator.Name, RoleName = currentOperator.RoleName, Time = DateTime.Now }); } } else { result = await operatorRepository.Update(operatorModel); if (result > 0) { await actionsRepository.Insert(new ActionsModel { Info = string.Format(L.GetString("editUserLog"), operatorModel.Name), Operator = currentOperator.Name, RoleName = currentOperator.RoleName, Time = DateTime.Now }); } } if (result <= 0) { XtraMessageBox.Show(L.GetString("saveFailed"), L.GetString("exclamation"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { DialogResult = DialogResult.OK; Close(); } } private void text_name_Click(object sender, EventArgs e) { SoftKeyboard.ActiveSoftKeyboard(sender); } private void text_truename_Click(object sender, EventArgs e) { SoftKeyboard.ActiveSoftKeyboard(sender); } private void text_pwd_Click(object sender, EventArgs e) { SoftKeyboard.ActiveSoftKeyboard(sender); } } }