您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

243 行
9.6 KiB

  1. using HKLib.DB.Model.Entity;
  2. using HKLib.Dto;
  3. using HKLib.Interfaces;
  4. using HKLib.Logic;
  5. using Snowflake.Core;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using UHFHelper;
  17. namespace HKCardManager.UserPages
  18. {
  19. public partial class CarMangerPage : UserControl
  20. {
  21. //List<string> OrgList = new List<string>();
  22. List<OrgTable> orgTables = new List<OrgTable>();
  23. List<string> Personnels = new List<string>();
  24. public CarMangerPage()
  25. {
  26. InitializeComponent();
  27. this.SizeChanged += CarMangerPage_SizeChanged;
  28. radioButton1.CheckedChanged += RadioButton1_CheckedChanged;
  29. radioButton2.CheckedChanged += RadioButton1_CheckedChanged;
  30. comboBox2.MouseDown += ComboBox2_MouseDown;
  31. comboBox2.TextUpdate += ComboBox2_TextUpdate;
  32. comboBox2.SelectedValueChanged += ComboBox2_SelectedValueChanged;
  33. radioButton1.Checked = true;
  34. comboBox2.Visible = false;
  35. orgTables = ServiceHandler.GetOrgList();
  36. Global.UserListDtos.ForEach(item => { if (!string.IsNullOrEmpty(item.UserName)) Personnels.Add(item.UserName); });
  37. comboBox2.DataSource = Personnels;
  38. comboBox1.DataSource = Global.OrgList;
  39. }
  40. private void ComboBox2_SelectedValueChanged(object? sender, EventArgs e)
  41. {
  42. if (radioButton2.Checked)
  43. {
  44. var user = Global.UserListDtos.FirstOrDefault(p => !string.IsNullOrEmpty(comboBox2.Text.Trim()) && !string.IsNullOrEmpty(p.UserName) && p.UserName == comboBox2.Text.Trim());
  45. if (user != null && !string.IsNullOrEmpty(user.Phone))
  46. {
  47. textBox4.Text = user.Phone;
  48. if (!string.IsNullOrEmpty(user.OrgName) && Global.OrgList.Contains(user.OrgName))
  49. {
  50. comboBox1.Text = user.OrgName;
  51. }
  52. else
  53. {
  54. comboBox1.SelectedIndex = -1;
  55. }
  56. }
  57. }
  58. }
  59. private void ComboBox2_TextUpdate(object? sender, EventArgs e)
  60. {
  61. string name = comboBox2.Text.Trim();
  62. if (string.IsNullOrEmpty(name))
  63. {
  64. Personnels.Clear();
  65. Global.UserListDtos.ForEach(item => { if (!string.IsNullOrEmpty(item.UserName)) Personnels.Add(item.UserName); });
  66. comboBox2.DataSource = null;
  67. comboBox2.DataSource = Personnels;
  68. comboBox2.DroppedDown = true;
  69. comboBox2.SelectedIndex = -1;
  70. return;
  71. }
  72. comboBox2.DroppedDown = true;
  73. var res = Global.UserListDtos.Where(p => !string.IsNullOrEmpty(p.UserName) && !string.IsNullOrEmpty(comboBox2.Text.Trim()) && p.UserName.Contains(comboBox2.Text.Trim())).ToList();
  74. if (res != null)
  75. {
  76. Personnels.Clear();
  77. res.ForEach(p => Personnels.Add(p.UserName));
  78. comboBox2.DataSource = null;
  79. comboBox2.DataSource = Personnels;
  80. comboBox2.SelectedIndex = -1;
  81. comboBox2.Text = name;
  82. comboBox2.SelectionStart = name.Length;
  83. }
  84. }
  85. private void ComboBox2_MouseDown(object? sender, MouseEventArgs e)
  86. {
  87. if (e.Button == MouseButtons.Left)
  88. {
  89. comboBox2.DroppedDown = true;
  90. }
  91. }
  92. private void RadioButton1_CheckedChanged(object? sender, EventArgs e)
  93. {
  94. var obj = (RadioButton)sender;
  95. if (obj != null)
  96. {
  97. if (obj.Name == "radioButton1")
  98. {
  99. if (obj.Checked)
  100. {
  101. textBox2.Visible = true;
  102. textBox2.Enabled = true;
  103. comboBox2.Visible = false;
  104. comboBox2.Enabled = false;
  105. //textBox4.Enabled = true;
  106. //comboBox1.Enabled = true;
  107. }
  108. }
  109. else if (obj.Name == "radioButton2")//人员已录入进行发卡
  110. {
  111. if (obj.Checked)
  112. {
  113. textBox2.Visible = false;
  114. textBox2.Enabled = false;
  115. comboBox2.Visible = true;
  116. comboBox2.Enabled = true;
  117. //textBox4.Enabled = false;
  118. //comboBox1.Enabled = false;
  119. var user = Global.UserListDtos.FirstOrDefault(p => !string.IsNullOrEmpty(comboBox2.Text.Trim()) && !string.IsNullOrEmpty(p.UserName) && p.UserName == comboBox2.Text.Trim());
  120. if (user != null && !string.IsNullOrEmpty(user.Phone))
  121. {
  122. textBox4.Text = user.Phone;
  123. if (!string.IsNullOrEmpty(user.OrgName) && Global.OrgList.Contains(user.OrgName))
  124. {
  125. comboBox1.Text = user.OrgName;
  126. }
  127. else
  128. {
  129. comboBox1.SelectedIndex = -1;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. private void CarMangerPage_SizeChanged(object? sender, EventArgs e)
  137. {
  138. panel1.Left = (this.Width - panel1.Width) / 2;
  139. panel1.Top = (this.Height - panel1.Height) / 2;
  140. }
  141. //写卡
  142. private void button2_Click(object sender, EventArgs e)
  143. {
  144. if (UHFCardHelper.GetInstance().ComOpen)
  145. {
  146. if (string.IsNullOrEmpty(textBox2.Text.Trim()))
  147. {
  148. MessageLogNotify.GetInstance.ShowWarning("请输入姓名");
  149. return;
  150. }
  151. if (!textBox4.Text.Trim().IsMobile())
  152. {
  153. MessageLogNotify.GetInstance.ShowWarning("手机号格式不正确");
  154. return;
  155. }
  156. string OrgName = comboBox1.Text;
  157. string worker = new IdWorker(1, 1).NextId().ToString();// ID生成
  158. UserDto userDto = new UserDto()
  159. {
  160. Name = textBox2.Text.Trim(),
  161. Phone = textBox4.Text.Trim(),
  162. OrgId = orgTables.FirstOrDefault(p => p.Name == comboBox1.Text)?.SId,
  163. CardNo = worker
  164. };
  165. Task.Factory.StartNew(() =>
  166. {
  167. this.Invoke(() => { button2.Enabled = false; });
  168. //写卡
  169. var res = UHFCardHelper.GetInstance().WriteCard(userDto.CardNo);
  170. if (res != null && res.Res)
  171. {
  172. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 写卡成功");
  173. }
  174. else
  175. {
  176. MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 写卡失败,原因:{res?.ResMes}");
  177. }
  178. if (HKLibHelper.AddUserAndBindCard(userDto))
  179. {
  180. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 添加成功");
  181. }
  182. else
  183. MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 添加失败,请重试!");
  184. this.Invoke(() => { button2.Enabled = true; });
  185. });
  186. }
  187. else
  188. {
  189. MessageLogNotify.GetInstance.ShowWarning("设备未连接");
  190. }
  191. }
  192. //读卡
  193. private void button1_Click(object sender, EventArgs e)
  194. {
  195. if (UHFCardHelper.GetInstance().ComOpen)
  196. {
  197. Task.Factory.StartNew(() =>
  198. {
  199. this.Invoke(() => { button1.Enabled = false; });
  200. var res = UHFCardHelper.GetInstance().ReadCard();
  201. if (Regex.IsMatch(res, "\\d{19}"))
  202. {
  203. var users = HKLibHelper.GetUserList("")?.FirstOrDefault(p => p.Cards?.FirstOrDefault(s => s.CardNum == res) != null);
  204. if (users != null)
  205. {
  206. this.Invoke(() =>
  207. {
  208. textBox2.Text = users.Name;
  209. textBox4.Text = users.Phone;
  210. if (users.OrgInfo != null && users.OrgInfo.Count > 0)
  211. {
  212. comboBox1.Text = users.OrgInfo.ElementAt(0).Name;
  213. }
  214. });
  215. MessageLogNotify.GetInstance.Show("读卡成功,详细信息请在页面查看");
  216. }
  217. }
  218. else
  219. {
  220. MessageLogNotify.GetInstance.Show("读卡成功,该卡是新卡");
  221. }
  222. this.Invoke(() => { button1.Enabled = true; });
  223. });
  224. }
  225. else
  226. {
  227. MessageLogNotify.GetInstance.ShowWarning("设备未连接");
  228. }
  229. }
  230. }
  231. }