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.

603 lines
24 KiB

  1. using HKLib.Dto;
  2. using HKLib.Interfaces;
  3. using HKLib.RabbitMQ.Config;
  4. using HKLib.RabbitMQ.Publisher;
  5. using HKLib.SQLHelper;
  6. using Snowflake.Core;
  7. using SqlSugar;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. using UHFHelper;
  19. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  20. namespace HKCardManager.UserPages
  21. {
  22. public partial class CarMangerPage : UserControl
  23. {
  24. //List<string> OrgList = new List<string>();
  25. List<OrgDto> orgTables = new List<OrgDto>();
  26. List<string> Personnels = new List<string>();
  27. public CarMangerPage()
  28. {
  29. InitializeComponent();
  30. this.SizeChanged += CarMangerPage_SizeChanged;
  31. radioButton1.CheckedChanged += RadioButton1_CheckedChanged;
  32. radioButton2.CheckedChanged += RadioButton1_CheckedChanged;
  33. comboBox2.MouseDown += ComboBox2_MouseDown;
  34. comboBox2.TextUpdate += ComboBox2_TextUpdate;
  35. comboBox2.SelectedValueChanged += ComboBox2_SelectedValueChanged;
  36. comboBox4.DataSource = HKLib.Configer.Admin;
  37. radioButton1.Checked = true;
  38. comboBox2.Visible = false;
  39. Init();
  40. }
  41. private async void Init()
  42. {
  43. orgTables = await HKLibHelper.GetOrg();
  44. Global.UserListDtos.ForEach(item => { if (!string.IsNullOrEmpty(item.UserName)) Personnels.Add(item.UserName); });
  45. comboBox2.DataSource = Personnels;
  46. comboBox1.DataSource = Global.OrgList;
  47. //新加列表
  48. List<string> datalist = new List<string>();
  49. datalist.Add("全部");
  50. datalist.AddRange(Global.OrgList);
  51. comboBox_jglb.DataSource = datalist;
  52. comboBox_jglb.TextChanged += ComboBox_jglb_TextChanged;
  53. dataGridView1_全部.AllowUserToResizeColumns = false;// 禁止用户改变所有列的列宽
  54. dataGridView1_全部.AllowUserToResizeRows = false; //禁止用户改变所有行的行高
  55. dataGridView1_全部.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; // 禁止用户改变列头的高度
  56. dataGridView1_全部.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; // 禁止用户改变列头的宽度
  57. dataGridView1_全部.AutoGenerateColumns = false;//禁用自动添加列
  58. dataGridView_No.AllowUserToResizeColumns = false;// 禁止用户改变所有列的列宽
  59. dataGridView_No.AllowUserToResizeRows = false; //禁止用户改变所有行的行高
  60. dataGridView_No.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; // 禁止用户改变列头的高度
  61. dataGridView_No.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; // 禁止用户改变列头的宽度
  62. dataGridView_No.AutoGenerateColumns = false;//禁用自动添加列
  63. ComboBox_jglb_TextChanged(null, null);
  64. }
  65. private void ComboBox2_SelectedValueChanged(object? sender, EventArgs e)
  66. {
  67. if (radioButton2.Checked)
  68. {
  69. var user = Global.UserListDtos.FirstOrDefault(p => !string.IsNullOrEmpty(comboBox2.Text.Trim()) && !string.IsNullOrEmpty(p.UserName) && p.UserName == comboBox2.Text.Trim());
  70. if (user != null && !string.IsNullOrEmpty(user.Phone))
  71. {
  72. //textBox4.Text = user.Phone;
  73. if (!string.IsNullOrEmpty(user.OrgName) && Global.OrgList.Contains(user.OrgName))
  74. {
  75. comboBox1.Text = user.OrgName;
  76. }
  77. else
  78. {
  79. comboBox1.SelectedIndex = -1;
  80. }
  81. }
  82. }
  83. }
  84. private void ComboBox2_TextUpdate(object? sender, EventArgs e)
  85. {
  86. string name = comboBox2.Text.Trim();
  87. if (string.IsNullOrEmpty(name))
  88. {
  89. Personnels.Clear();
  90. Global.UserListDtos.ForEach(item => { if (!string.IsNullOrEmpty(item.UserName)) Personnels.Add(item.UserName); });
  91. comboBox2.DataSource = null;
  92. comboBox2.DataSource = Personnels;
  93. comboBox2.DroppedDown = true;
  94. comboBox2.SelectedIndex = -1;
  95. return;
  96. }
  97. comboBox2.DroppedDown = true;
  98. var res = Global.UserListDtos.Where(p => !string.IsNullOrEmpty(p.UserName) && !string.IsNullOrEmpty(comboBox2.Text.Trim()) && p.UserName.Contains(comboBox2.Text.Trim())).ToList();
  99. if (res != null)
  100. {
  101. Personnels.Clear();
  102. res.ForEach(p => Personnels.Add(p.UserName));
  103. comboBox2.DataSource = null;
  104. comboBox2.DataSource = Personnels;
  105. comboBox2.SelectedIndex = -1;
  106. comboBox2.Text = name;
  107. comboBox2.SelectionStart = name.Length;
  108. }
  109. }
  110. private void ComboBox2_MouseDown(object? sender, MouseEventArgs e)
  111. {
  112. if (e.Button == MouseButtons.Left)
  113. {
  114. comboBox2.DroppedDown = true;
  115. }
  116. }
  117. private void RadioButton1_CheckedChanged(object? sender, EventArgs e)
  118. {
  119. var obj = (RadioButton)sender;
  120. if (obj != null)
  121. {
  122. if (obj.Name == "radioButton1")
  123. {
  124. if (obj.Checked)
  125. {
  126. textBox2.Visible = true;
  127. textBox2.Enabled = true;
  128. comboBox2.Visible = false;
  129. comboBox2.Enabled = false;
  130. //textBox4.Enabled = true;
  131. //comboBox1.Enabled = true;
  132. }
  133. }
  134. else if (obj.Name == "radioButton2")//人员已录入进行发卡
  135. {
  136. if (obj.Checked)
  137. {
  138. textBox2.Visible = false;
  139. textBox2.Enabled = false;
  140. comboBox2.Visible = true;
  141. comboBox2.Enabled = true;
  142. //textBox4.Enabled = false;
  143. comboBox1.Enabled = false;
  144. var user = Global.UserListDtos.FirstOrDefault(p => !string.IsNullOrEmpty(comboBox2.Text.Trim()) && !string.IsNullOrEmpty(p.UserName) && p.UserName == comboBox2.Text.Trim());
  145. if (user != null && !string.IsNullOrEmpty(user.Phone))
  146. {
  147. //textBox4.Text = user.Phone;
  148. if (!string.IsNullOrEmpty(user.OrgName) && Global.OrgList.Contains(user.OrgName))
  149. {
  150. comboBox1.Text = user.OrgName;
  151. }
  152. else
  153. {
  154. comboBox1.SelectedIndex = -1;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. private void CarMangerPage_SizeChanged(object? sender, EventArgs e)
  162. {
  163. panel1.Left = (this.Width - panel1.Width) / 2;
  164. panel1.Top = (this.Height - panel1.Height) / 2;
  165. }
  166. //写卡
  167. private async void button2_Click(object sender, EventArgs e)
  168. {
  169. if (UHFCardHelper.GetInstance().ComOpen)
  170. {
  171. string Name = string.Empty;
  172. string Phone=string.Empty;
  173. if (radioButton2.Checked)
  174. {
  175. Name = comboBox2.SelectedItem.ToString();
  176. }
  177. else
  178. {
  179. if (string.IsNullOrEmpty(textBox2.Text.Trim()))
  180. {
  181. MessageBox.Show("提示!!!\n 请输入姓名...");
  182. MessageLogNotify.GetInstance.ShowWarning("请输入姓名");
  183. return;
  184. }
  185. var res1 = await HKLibHelper.GetUserList("");
  186. var ck = res1.FirstOrDefault(t => t.Name.Equals(textBox2.Text.Trim()));
  187. if (ck!=null)
  188. {
  189. MessageBox.Show("提示!!!\n 用户已经存在,请勿重复添加...");
  190. MessageLogNotify.GetInstance.ShowWarning("用户已经存在,请勿重复添加");
  191. return;
  192. }
  193. var ck1 = res1.FirstOrDefault(t => t.Phone.Equals(textBox5.Text.Trim()));
  194. if (ck1 != null)
  195. {
  196. MessageBox.Show("提示!!!\n 用户手机号已经存在,请勿重复添加...");
  197. MessageLogNotify.GetInstance.ShowWarning("用户手机号已经存在,请勿重复添加");
  198. return;
  199. }
  200. Phone=textBox5.Text.Trim();
  201. Name = textBox2.Text.Trim();
  202. }
  203. string OrgName = comboBox1.Text;
  204. string worker = new IdWorker(1, 1).NextId().ToString();// ID生成
  205. UserDto userDto = new UserDto()
  206. {
  207. Name = Name,
  208. OrgId = orgTables.FirstOrDefault(p => p.Name == comboBox1.Text)?.SId,
  209. CardNo = worker,
  210. Phone=Phone,
  211. Num= HKLibHelper.GetUserMaxNumSync()
  212. };
  213. //Task.Factory.StartNew(() =>
  214. //{
  215. //写卡
  216. var res = UHFCardHelper.GetInstance().WriteCard(userDto.CardNo);
  217. if (res != null && res.Res)
  218. {
  219. ServiceQueryPush.QueryPush.PushMQ(new { CardNo = userDto.CardNo, Stutas = 1 }, "NewCardChanged", MQEnum.Push);
  220. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 写卡成功...");
  221. textBox2.Text = String.Empty;
  222. button2.Enabled = false;
  223. }
  224. else
  225. {
  226. MessageBox.Show($"用户 【{userDto.Name}】 写卡失败,原因:{res?.ResMes}");
  227. MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 写卡失败,原因:{res?.ResMes}");
  228. }
  229. var result = await HKLibHelper.AddUserAndBindCard(userDto);
  230. if (result)
  231. {
  232. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 写卡成功,卡号:{userDto.CardNo}");
  233. MessageBox.Show($"用户 【{userDto.Name}】 写卡成功,卡号:{userDto.CardNo}");
  234. }
  235. else
  236. {
  237. MessageBox.Show($"用户 【{userDto.Name}】 添加失败,请重试!");
  238. MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 添加失败,请重试!");
  239. }
  240. //});
  241. }
  242. else
  243. {
  244. MessageBox.Show("提示!!!\n 设备未连接...");
  245. MessageLogNotify.GetInstance.ShowWarning("设备未连接");
  246. }
  247. }
  248. //读卡
  249. private async void button1_Click(object sender, EventArgs e)
  250. {
  251. if (UHFCardHelper.GetInstance().ComOpen)
  252. {
  253. var res = UHFCardHelper.GetInstance().ReadCard();
  254. if (string.IsNullOrEmpty(res))
  255. {
  256. MessageBox.Show("提示!!!\n 请放入卡片...");
  257. MessageLogNotify.GetInstance.Show("请放入卡片");
  258. return;
  259. }
  260. if (Regex.IsMatch(res, "\\d{19}"))
  261. {
  262. var result = await HKLibHelper.GetUserList("");
  263. UserListDto users = null;
  264. result.ForEach(item =>
  265. {
  266. if (item.Cards.FirstOrDefault(t => t.CardNum.Equals(res)) != null)
  267. {
  268. users = item;
  269. return;
  270. }
  271. });
  272. if (users != null)
  273. {
  274. textBox2.Text = users.Name;
  275. if (users.OrgInfo != null && users.OrgInfo.Count > 0)
  276. {
  277. comboBox1.Text = users.OrgInfo.ElementAt(0).Name;
  278. }
  279. textBox5.Text = users.Phone;
  280. MessageLogNotify.GetInstance.Show($"读卡成功,当前卡号【{res}】,当前用户【{users.Name}】,详细信息请在页面查看");
  281. return;
  282. }
  283. }
  284. this.Invoke(() => { button2.Enabled = true; });
  285. MessageLogNotify.GetInstance.Show("读卡成功,该卡是新卡");
  286. }
  287. else
  288. {
  289. MessageBox.Show("提示!!!\n 设备未连接...");
  290. MessageLogNotify.GetInstance.ShowWarning("设备未连接");
  291. }
  292. }
  293. #region 批量操作
  294. /// <summary>
  295. /// 批量读卡
  296. /// </summary>
  297. /// <param name="sender"></param>
  298. /// <param name="e"></param>
  299. private async void button_pl_read_Click(object sender, EventArgs e)
  300. {
  301. if (UHFCardHelper.GetInstance().ComOpen)
  302. {
  303. var res = UHFCardHelper.GetInstance().ReadCard();
  304. if (string.IsNullOrEmpty(res))
  305. {
  306. MessageBox.Show("提示!!!\n 请放入卡片,再读卡...");
  307. MessageLogNotify.GetInstance.Show("请放入卡片");
  308. return;
  309. }
  310. if (res.Equals("000"))
  311. {
  312. MessageBox.Show("提示!!!\n 这是一张新卡...");
  313. }
  314. if (FirstUser == null)
  315. {
  316. MessageBox.Show("提示!!!\n 没有可以制卡的成员...");
  317. MessageLogNotify.GetInstance.ShowWarning("没有可以制卡的成员!");
  318. return;
  319. }
  320. if (Regex.IsMatch(res, "\\d{19}"))
  321. {
  322. var result = await HKLibHelper.GetUserList("");
  323. var users = result.Where(m => m.Cards.Select(x => x.CardNum).Contains(res)).ToList().FirstOrDefault();
  324. if (users != null)
  325. {
  326. MessageLogNotify.GetInstance.Show($"读卡成功,当前卡号【{res}】,当前用户【{users.Name}】,详细信息请在页面查看");
  327. return;
  328. }
  329. }
  330. this.Invoke(() => { button_pl_write.Enabled = true; });
  331. MessageLogNotify.GetInstance.Show("读卡成功,该卡是新卡");
  332. }
  333. else
  334. {
  335. MessageBox.Show("提示!!!\n 设备未连接...");
  336. MessageLogNotify.GetInstance.ShowWarning("设备未连接");
  337. }
  338. }
  339. /// <summary>
  340. /// 批量写卡
  341. /// </summary>
  342. /// <param name="sender"></param>
  343. /// <param name="e"></param>
  344. private async void button_pl_write_Click(object sender, EventArgs e)
  345. {
  346. if (UHFCardHelper.GetInstance().ComOpen)
  347. {
  348. if (FirstUser == null)
  349. {
  350. MessageBox.Show("提示!!!\n 没有可以制卡的成员...");
  351. MessageLogNotify.GetInstance.ShowWarning("没有可以制卡的成员!");
  352. return;
  353. }
  354. string Name = FirstUser.UserName;
  355. string OrgName = comboBox1.Text;
  356. string worker = new IdWorker(1, 1).NextId().ToString();// ID生成
  357. UserDto userDto = new UserDto()
  358. {
  359. Name = Name,
  360. OrgId = orgTables.FirstOrDefault(p => p.Name == FirstUser.OrgName)?.SId,
  361. CardNo = worker,
  362. Phone=FirstUser.Phone
  363. };
  364. var res = UHFCardHelper.GetInstance().WriteCard(userDto.CardNo);
  365. if (res.Res == false)
  366. {
  367. MessageBox.Show("提示!!!\n "+ $"用户 【{userDto.Name}】 写卡失败!");
  368. MessageBox.Show($"用户 【{userDto.Name}】 写卡失败!");
  369. return;
  370. }
  371. var result = await HKLibHelper.AddUserAndBindCard(userDto);
  372. if (result)
  373. {
  374. ServiceQueryPush.QueryPush.PushMQ(new { CardNo = userDto.CardNo, Stutas = 1 }, "NewCardChanged", MQEnum.Push);
  375. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 写卡成功,卡号:{userDto.CardNo}");
  376. MessageBox.Show($"用户 【{userDto.Name}】 写卡成功,卡号:{userDto.CardNo}");
  377. RefreshTheData(comboBox_jglb.Text);
  378. this.Invoke(() => { button_pl_write.Enabled = false; });
  379. }
  380. else {
  381. MessageBox.Show($"用户 【{userDto.Name}】 添加失败,请重试!");
  382. MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 添加失败,请重试!");
  383. }
  384. }
  385. else
  386. {
  387. MessageBox.Show("提示!!!\n 设备未连接...");
  388. MessageLogNotify.GetInstance.ShowWarning("设备未连接");
  389. }
  390. }
  391. /// <summary>
  392. /// 文本该
  393. /// </summary>
  394. /// <param name="sender"></param>
  395. /// <param name="e"></param>
  396. private void ComboBox_jglb_TextChanged(object? sender, EventArgs e)
  397. {
  398. try
  399. {
  400. RefreshTheData(comboBox_jglb.Text);
  401. }
  402. catch (Exception ex)
  403. {
  404. MessageLogNotify.GetInstance.ShowWarning(ex.Message);
  405. }
  406. }
  407. /// <summary>
  408. /// 当前正在插入的用户数据
  409. /// </summary>
  410. public UserInfoModel FirstUser { get; set; }
  411. /// <summary>
  412. /// 刷新数据
  413. /// </summary>
  414. /// <param name="Name"></param>
  415. public void RefreshTheData(string Name)
  416. {
  417. try
  418. {
  419. var res = HKLibHelper.GetUserListSync("");
  420. Global.UserListDtos.Clear();
  421. res?.ToList()?.ForEach(item =>
  422. {
  423. string status = "";
  424. string cardNum = "";
  425. if (item.Cards != null && item.Cards.Count > 0)
  426. {
  427. cardNum = item.Cards.ElementAt(0).CardNum;
  428. status = ((CarStatus)item.Cards.ElementAt(0).State).ToString();
  429. }
  430. Global.UserListDtos.Add(new UserInfoModel()
  431. {
  432. Num = item.Num,
  433. UserName = item.Name,
  434. Phone = item.Phone,
  435. OrgName = item.OrgInfo?.Count > 0 ? item.OrgInfo.ElementAt(0).Name : "",
  436. CardNum = cardNum,
  437. State = status
  438. });
  439. });
  440. List<UserInfoModel> data = Global.UserListDtos?.FindAll(par => string.IsNullOrEmpty(par.CardNum));
  441. List<UserInfoModel> dataYes = Global.UserListDtos?.FindAll(par => !string.IsNullOrEmpty(par.CardNum));
  442. if (Name != "全部")
  443. {
  444. data = Global.UserListDtos?.FindAll(par => string.IsNullOrEmpty(par.CardNum) && par.OrgName == Name);
  445. dataYes = Global.UserListDtos?.FindAll(par => !string.IsNullOrEmpty(par.CardNum) && par.OrgName == Name);
  446. }
  447. dataGridView_No.DataSource = null;
  448. dataGridView_No.DataSource = data;
  449. dataGridView1_全部.DataSource = null;
  450. dataGridView1_全部.DataSource = dataYes;
  451. if (data != null && data.Count > 0)
  452. {
  453. FirstUser = data.FirstOrDefault();
  454. labelName.Text = FirstUser.UserName;
  455. }
  456. else
  457. {
  458. FirstUser = null;
  459. labelName.Text = "";
  460. }
  461. }
  462. catch (Exception ex)
  463. {
  464. MessageLogNotify.GetInstance.ShowWarning(ex.Message);
  465. }
  466. }
  467. #endregion
  468. private async void button6_Click(object sender, EventArgs e)
  469. {
  470. if (SqlLogic.GetAdmin() > 3)
  471. {
  472. MessageBox.Show("管理员卡制作上限");
  473. return;
  474. }
  475. var CardNo = comboBox4.SelectedValue.ToString();
  476. var temp = SqlLogic.GetAdmin(CardNo);
  477. if (temp != null)
  478. {
  479. MessageBox.Show("卡号已被使用,请勿重复制作");
  480. return;
  481. }
  482. if (UHFCardHelper.GetInstance().ComOpen)
  483. {
  484. string sid = string.Empty;
  485. var data = (await HKLibHelper.GetOrg()).Where(t => t.Name.Equals("管理员")).FirstOrDefault();
  486. if (data == null)
  487. sid = await HKLibHelper.SetOrg("管理员");
  488. else
  489. sid = data.SId;
  490. var res = UHFCardHelper.GetInstance().ReadCard();
  491. if (string.IsNullOrEmpty(res))
  492. {
  493. MessageBox.Show("请放入卡片");
  494. MessageLogNotify.GetInstance.Show("请放入卡片");
  495. return;
  496. }
  497. SqlLogic.AddAmin(CardNo);
  498. HKLibHelper.AddUserAndBindCardSync(new UserDto
  499. {
  500. Name = "管理员"+CardNo.FirstOrDefault(),
  501. CardNo = CardNo,
  502. OrgId = sid,
  503. Num = HKLibHelper.GetUserMaxNumSync()
  504. });
  505. var rr= UHFCardHelper.GetInstance().WriteCard(CardNo);
  506. this.Invoke(() =>
  507. {
  508. textBox1.Text = CardNo;
  509. });
  510. }
  511. }
  512. /// <summary>
  513. /// 文本改变事件
  514. /// </summary>
  515. /// <param name="sender"></param>
  516. /// <param name="e"></param>
  517. private void comboBox2_TextChanged(object sender, EventArgs e)
  518. {
  519. try
  520. {
  521. if (radioButton1.Checked)
  522. {
  523. if (!string.IsNullOrEmpty(textBox2.Text))
  524. {
  525. comboBox1.Enabled = true;
  526. }
  527. else
  528. comboBox1.Enabled = false;
  529. }
  530. else
  531. {
  532. comboBox1.Enabled = false;
  533. }
  534. }
  535. catch (Exception ex)
  536. {
  537. }
  538. }
  539. private void button3_Click(object sender, EventArgs e)
  540. {
  541. if (UHFCardHelper.GetInstance().ComOpen)
  542. {
  543. var res = UHFCardHelper.GetInstance().ReadCard();
  544. if (string.IsNullOrEmpty(res))
  545. {
  546. MessageBox.Show("提示!!!\n 请放入卡片,再读卡...");
  547. MessageLogNotify.GetInstance.Show("请放入卡片");
  548. return;
  549. }
  550. textBox3.Text= res;
  551. }
  552. }
  553. private void button4_Click(object sender, EventArgs e)
  554. {
  555. if (UHFCardHelper.GetInstance().ComOpen)
  556. {
  557. if (!string.IsNullOrEmpty(textBox4.Text))
  558. {
  559. var rr = UHFCardHelper.GetInstance().WriteCard(textBox4.Text);
  560. if(rr.Res)
  561. MessageLogNotify.GetInstance.Show($"手动制卡成功,卡号[{textBox4.Text}]");
  562. }
  563. }
  564. }
  565. }
  566. }