You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 regels
4.7 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. public CarMangerPage()
  24. {
  25. InitializeComponent();
  26. this.SizeChanged += CarMangerPage_SizeChanged;
  27. radioButton1.Checked = true;
  28. orgTables = ServiceHandler.GetOrgList();
  29. orgTables?.ForEach(item =>
  30. {
  31. OrgList.Add(item.Name);
  32. });
  33. comboBox1.DataSource = OrgList;
  34. }
  35. private void CarMangerPage_SizeChanged(object? sender, EventArgs e)
  36. {
  37. panel1.Left = (this.Width - panel1.Width) / 2;
  38. panel1.Top = (this.Height - panel1.Height) / 2;
  39. }
  40. //写卡
  41. private void button2_Click(object sender, EventArgs e)
  42. {
  43. if (UHFCardHelper.GetInstance().ComOpen)
  44. {
  45. if (string.IsNullOrEmpty(textBox2.Text.Trim()))
  46. {
  47. MessageLogNotify.GetInstance.Show("请输入姓名");
  48. return;
  49. }
  50. if (!textBox4.Text.Trim().IsMobile())
  51. {
  52. MessageLogNotify.GetInstance.Show("手机号格式不正确");
  53. return;
  54. }
  55. string OrgName = comboBox1.Text;
  56. string worker = new IdWorker(1, 1).NextId().ToString();// ID生成
  57. UserDto userDto = new UserDto()
  58. {
  59. Name = textBox2.Text.Trim(),
  60. Phone = textBox4.Text.Trim(),
  61. OrgId = orgTables.FirstOrDefault(p => p.Name == comboBox1.Text)?.SId,
  62. CardNo = worker
  63. };
  64. Task.Factory.StartNew(() =>
  65. {
  66. //写卡
  67. var res = UHFCardHelper.GetInstance().WriteCard(userDto.CardNo);
  68. if (res != null && res.Res)
  69. {
  70. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 写卡成功");
  71. }
  72. else
  73. {
  74. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 写卡失败,原因:{res?.ResMes}");
  75. }
  76. if (HKLibHelper.AddUserAndBindCard(userDto))
  77. {
  78. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 添加成功");
  79. }
  80. else
  81. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 添加失败,请重试!");
  82. });
  83. }
  84. else
  85. {
  86. MessageLogNotify.GetInstance.Show("设备未连接");
  87. }
  88. }
  89. //读卡
  90. private void button1_Click(object sender, EventArgs e)
  91. {
  92. if (UHFCardHelper.GetInstance().ComOpen)
  93. {
  94. Task.Factory.StartNew(() =>
  95. {
  96. var res = UHFCardHelper.GetInstance().ReadCard();
  97. if (Regex.IsMatch(res, "\\d{19}"))
  98. {
  99. var users = HKLibHelper.GetUserList("")?.FirstOrDefault(p => p.Cards?.FirstOrDefault(s => s.CardNum == res) != null);
  100. if (users != null)
  101. {
  102. this.Invoke(() =>
  103. {
  104. textBox2.Text = users.Name;
  105. textBox4.Text = users.Phone;
  106. if (users.OrgInfo != null && users.OrgInfo.Count > 0)
  107. {
  108. comboBox1.Text = users.OrgInfo.ElementAt(0).Name;
  109. }
  110. });
  111. MessageLogNotify.GetInstance.Show("读卡成功,详细信息请在页面查看");
  112. }
  113. }
  114. else
  115. {
  116. MessageLogNotify.GetInstance.Show("读卡成功,该卡是新卡");
  117. }
  118. });
  119. }
  120. else
  121. {
  122. MessageLogNotify.GetInstance.Show("设备未连接");
  123. }
  124. }
  125. }
  126. }