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.

554 lines
22 KiB

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