NewOperatorForm.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Windows.Forms;
  5. using DevExpress.XtraEditors;
  6. using GCAS.Code;
  7. using GCAS.Dto;
  8. using GCAS.Localization;
  9. using GCAS.Model;
  10. namespace GCAS
  11. {
  12. public partial class NewOperatorForm : XtraForm
  13. {
  14. private List<NameValue> L;
  15. public OperatorModel operatorModel;
  16. public OperatorModel currentOperator;
  17. private OperatorRepository operatorRepository;
  18. private readonly ActionsRepository actionsRepository;
  19. public NewOperatorForm()
  20. {
  21. InitializeComponent();
  22. actionsRepository = new ActionsRepository();
  23. L = LocalizationHelper.GetSource(Thread.CurrentThread.CurrentUICulture);
  24. }
  25. private void NewDeviceForm_Load(object sender, EventArgs e)
  26. {
  27. operatorRepository = new OperatorRepository();
  28. InitView();
  29. }
  30. private void InitView()
  31. {
  32. if (operatorModel != null)
  33. {
  34. text_name.Text = operatorModel.Name;
  35. text_truename.Text = operatorModel.TrueName;
  36. cb_role.EditValue = operatorModel.Role == 1 ? L.GetString("User") : L.GetString("Admin");
  37. if (operatorModel.Id == currentOperator.Id)
  38. {
  39. cb_role.Enabled = false;
  40. }
  41. }
  42. }
  43. private async void btn_save_Click(object sender, EventArgs e)
  44. {
  45. if (string.IsNullOrEmpty(text_name.Text))
  46. {
  47. XtraMessageBox.Show(L.GetString("userNameNotNull"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  48. return;
  49. }
  50. if (string.IsNullOrEmpty(text_truename.Text))
  51. {
  52. XtraMessageBox.Show(L.GetString("trueNameNotNull"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  53. return;
  54. }
  55. if (string.IsNullOrEmpty(text_pwd.Text) && operatorModel == null)
  56. {
  57. XtraMessageBox.Show(L.GetString("passwordNotNull"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  58. return;
  59. }
  60. int role = 1;
  61. switch (cb_role.Text)
  62. {
  63. case "管理员":
  64. case "Admin":
  65. role = 2;
  66. break;
  67. case "普通用户":
  68. case "User":
  69. role = 1;
  70. break;
  71. }
  72. if (operatorModel == null)
  73. {
  74. operatorModel = new OperatorModel();
  75. }
  76. operatorModel.Name = text_name.Text;
  77. operatorModel.TrueName = text_truename.Text;
  78. operatorModel.Role = role;
  79. operatorModel.Pwd = operatorModel == null || !string.IsNullOrEmpty(text_pwd.Text) ? SHAHelper.SHAmd5Encrypt(text_pwd.Text) : operatorModel.Pwd;
  80. int result = 0;
  81. if (operatorModel.Id == 0)
  82. {
  83. result = await operatorRepository.Insert(operatorModel);
  84. if (result > 0)
  85. {
  86. await actionsRepository.Insert(new ActionsModel { Info = string.Format(L.GetString("addUserLog"), operatorModel.Name), Operator = currentOperator.Name, RoleName = currentOperator.RoleName, Time = DateTime.Now });
  87. }
  88. }
  89. else
  90. {
  91. result = await operatorRepository.Update(operatorModel);
  92. if (result > 0)
  93. {
  94. await actionsRepository.Insert(new ActionsModel { Info = string.Format(L.GetString("editUserLog"), operatorModel.Name), Operator = currentOperator.Name, RoleName = currentOperator.RoleName, Time = DateTime.Now });
  95. }
  96. }
  97. if (result <= 0)
  98. {
  99. XtraMessageBox.Show(L.GetString("saveFailed"), L.GetString("exclamation"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  100. }
  101. else
  102. {
  103. DialogResult = DialogResult.OK;
  104. Close();
  105. }
  106. }
  107. private void text_name_Click(object sender, EventArgs e)
  108. {
  109. SoftKeyboard.ActiveSoftKeyboard(sender);
  110. }
  111. private void text_truename_Click(object sender, EventArgs e)
  112. {
  113. SoftKeyboard.ActiveSoftKeyboard(sender);
  114. }
  115. private void text_pwd_Click(object sender, EventArgs e)
  116. {
  117. SoftKeyboard.ActiveSoftKeyboard(sender);
  118. }
  119. }
  120. }