Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

264 lignes
10 KiB

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