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.

691 lines
26 KiB

  1. using HKCardManager.Message;
  2. using HKCardManager.Servers;
  3. using HKHelper;
  4. using HKLib.Dto;
  5. using HKLib.Interfaces;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace HKCardManager.UserPages
  17. {
  18. public partial class PersonnelEntryPage : UserControl
  19. {
  20. //List<OrgDto> orgTables = new List<OrgDto>();
  21. public PersonnelEntryPage()
  22. {
  23. InitializeComponent();
  24. DataGridViewInit();
  25. EventInit();
  26. this.SizeChanged += PersonnelEntryPage_SizeChanged;
  27. Init();
  28. }
  29. private async void Init()
  30. {
  31. try
  32. {
  33. Global.OrgTables.Clear();
  34. Global.OrgTables = await HKLibHelper.GetOrg();
  35. Global.OrgList.Clear();
  36. Global.OrgTables?.ForEach(item => { Global.OrgList.Add(item.Name); });
  37. comboBox2.DataSource = Global.OrgList;
  38. GetUser();
  39. }
  40. catch (Exception)
  41. {
  42. }
  43. //Task.Factory.StartNew(() => { GetUser(); });
  44. }
  45. private void PersonnelEntryPage_SizeChanged(object? sender, EventArgs e)
  46. {
  47. this.splitContainer1.SplitterDistance = this.Height - 100;
  48. }
  49. private void DataGridViewInit()
  50. {
  51. dataGridView1.AllowUserToResizeColumns = false;// 禁止用户改变所有列的列宽
  52. dataGridView1.AllowUserToResizeRows = false; //禁止用户改变所有行的行高
  53. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; // 禁止用户改变列头的高度
  54. dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; // 禁止用户改变列头的宽度
  55. dataGridView1.AutoGenerateColumns = false;//禁用自动添加列
  56. }
  57. //获取所有用户信息
  58. private async void GetUser()
  59. {
  60. var res = await HKLibHelper.GetUserList("");
  61. Global.UserListDtos.Clear();
  62. var userinfo = new List<UserInfoModel>();
  63. //Global.PagUserListDtos.Clear();
  64. res?.ToList()?.ForEach(item =>
  65. {
  66. string status = "";
  67. string cardNum = "";
  68. if (item.Cards != null && item.Cards.Count > 0)
  69. {
  70. cardNum = item.Cards.ElementAt(0).CardNum;
  71. status = ((CarStatus)item.Cards.ElementAt(0).State).ToString();
  72. }
  73. Global.UserListDtos.Add(new UserInfoModel()
  74. {
  75. Num = item.Num,
  76. UserName = item.Name,
  77. Phone = item.Phone,
  78. OrgName = item.OrgInfo?.Count > 0 ? item.OrgInfo.ElementAt(0).Name : "",
  79. CardNum = cardNum,
  80. State = status
  81. });
  82. });
  83. for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
  84. {
  85. if (userinfo.Count < pageSize)
  86. userinfo.Add(Global.UserListDtos.ElementAt(i));
  87. else
  88. break;
  89. }
  90. this.Invoke(() =>
  91. {
  92. dataGridView1.DataSource = null;
  93. Global.PagUserListDtos.Clear();
  94. Global.PagUserListDtos = userinfo;
  95. dataGridView1.DataSource = Global.PagUserListDtos;
  96. totalCount = Global.UserListDtos.Count;
  97. comboBoxNum();
  98. showDataGirdView();
  99. });
  100. }
  101. //获取单页面用户信息
  102. private void GetPag()
  103. {
  104. Global.PagUserListDtos.Clear();
  105. for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
  106. {
  107. if (Global.PagUserListDtos.Count < pageSize && i >= 0)
  108. Global.PagUserListDtos.Add(Global.UserListDtos.ElementAt(i));
  109. else
  110. break;
  111. }
  112. dataGridView1.DataSource = null;
  113. dataGridView1.DataSource = Global.PagUserListDtos;
  114. }
  115. //添加用户
  116. private async void button2_Click(object sender, EventArgs e)
  117. {
  118. if (string.IsNullOrEmpty(textBox1.Text.Trim()))
  119. {
  120. MessageBox.Show("请输入姓名,在尝试添加...");
  121. MessageLogNotify.GetInstance.ShowWarning("请输入姓名");
  122. return;
  123. }
  124. if (Global.UserListDtos.FirstOrDefault(p => p.UserName == textBox1.Text.Trim()) != null)
  125. {
  126. MessageBox.Show("用户已经存在,请勿重复添加!");
  127. MessageLogNotify.GetInstance.ShowWarning("用户已经存在,请勿重复添加");
  128. return;
  129. }
  130. if (Global.UserListDtos.FirstOrDefault(p => p.UserName == textBox2.Text.Trim()) != null)
  131. {
  132. MessageBox.Show("用户手机号已存在,请勿重复添加!");
  133. MessageLogNotify.GetInstance.ShowWarning("用户手机号已存在,请勿重复添加");
  134. return;
  135. }
  136. //if (!string.IsNullOrEmpty(textBox2.Text.Trim()))
  137. //{
  138. // if (!textBox2.Text.Trim().IsMobile())
  139. // {
  140. // MessageLogNotify.GetInstance.ShowWarning("手机号格式不正确");
  141. // return;
  142. // }
  143. //}
  144. UserDto userDto = new UserDto()
  145. {
  146. Num = await HKLibHelper.GetUserMaxNum(),
  147. Name = textBox1.Text.Trim(),
  148. Phone = textBox2.Text.Trim(),
  149. OrgId = Global.OrgTables.FirstOrDefault(p => p.Name == comboBox2.Text)?.SId
  150. };
  151. string OrgName = comboBox2.Text;
  152. this.Invoke(() => { button2.Enabled = false; });
  153. var res = await HKLibHelper.AddUser(userDto);
  154. if (res)
  155. {
  156. GetUser();
  157. //this.Invoke(() =>
  158. //{
  159. textBox1.Text = string.Empty;
  160. //textBox2.Text = string.Empty;
  161. //});
  162. MessageBox.Show($"用户 【{userDto.Name}】 添加成功");
  163. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 添加成功");
  164. }
  165. else
  166. {
  167. MessageBox.Show($"用户 【{userDto.Name}】 添加失败,请重试!");
  168. MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 添加失败,请重试!");
  169. }
  170. //this.Invoke(() => { button2.Enabled = true; });
  171. button2.Enabled = true;
  172. }
  173. //数据表列点击事件
  174. private async void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  175. {
  176. try
  177. {
  178. {
  179. int index = dataGridView1.CurrentRow.Index;
  180. if (index >= 0 && index < dataGridView1.Rows.Count)
  181. {
  182. if (dataGridView1.CurrentCell.Value?.ToString() == "删除")
  183. {
  184. var res = dataGridView1.Rows[index].Cells[1].Value?.ToString();
  185. if (string.IsNullOrEmpty(res))
  186. {
  187. MessageBox.Show("用户名无效.请重试!");
  188. MessageLogNotify.GetInstance.ShowError("用户名无效");
  189. return;
  190. }
  191. if (MessageBox.Show($"确认是否删除[{res}]!", "操作", MessageBoxButtons.YesNo) == DialogResult.Yes)
  192. {
  193. var result = await HKLibHelper.DisableUser(res);
  194. if (result)
  195. {
  196. GetUser();
  197. MessageBox.Show($"用户:【{res}】 删除成功");
  198. MessageLogNotify.GetInstance.Show($"用户:【{res}】 删除成功");
  199. }
  200. else
  201. {
  202. MessageBox.Show($"用户:【{res}】 删除失败");
  203. MessageLogNotify.GetInstance.ShowError($"用户:【{res}】 删除失败");
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. catch (Exception ex)
  211. {
  212. MessageBox.Show($"用户删除失败,错误原因:" + ex.Message);
  213. MessageLogNotify.GetInstance.ShowEx(ex.ToString());
  214. }
  215. }
  216. #region 分页显示
  217. private int totalCount = 0;//总数据个数
  218. private int pageCount = 0;//总页数
  219. private int currentPageIndex = 1;//当前页索引
  220. private int pageSize = 10;//每页分页大小
  221. private int remainder = 0;//最后一页剩余个数
  222. private bool isAutoUpdateLabelTip = true;//获取或设置是否自动更新分页标签内容提示,默认为true
  223. //事件初始化
  224. private void EventInit()
  225. {
  226. label_first.Click += label_first_Click; //首页
  227. label_end.Click += label_end_Click;//末页
  228. label_up.Click += label_up_Click;//上一页
  229. label_down.Click += label_down_Click;//下一页
  230. comboBox_page.DropDownClosed += comboBox_page_DropDownClosed;//下拉列表切换页面
  231. comboBox_num.DropDownClosed += comboBox_num_DropDownClosed;//设置分页大小
  232. button_go.Click += button_go_Click;//跳转指定页
  233. }
  234. //首页
  235. private void label_first_Click(object? sender, EventArgs e)
  236. {
  237. currentPageIndex = 1;
  238. showDataGirdView();
  239. }
  240. //末页
  241. private void label_end_Click(object? sender, EventArgs e)
  242. {
  243. currentPageIndex = pageCount;
  244. showDataGirdView();
  245. }
  246. //上一页
  247. private void label_up_Click(object? sender, EventArgs e)
  248. {
  249. if (currentPageIndex <= pageCount && currentPageIndex != 1)
  250. {
  251. currentPageIndex--;
  252. showDataGirdView();
  253. }
  254. if (isAutoUpdateLabelTip) updateSplitPageLabelTip();
  255. }
  256. //下一页
  257. private void label_down_Click(object? sender, EventArgs e)
  258. {
  259. if (currentPageIndex < pageCount && currentPageIndex != pageCount)
  260. {
  261. currentPageIndex++;
  262. showDataGirdView();
  263. }
  264. if (isAutoUpdateLabelTip) updateSplitPageLabelTip();
  265. }
  266. //第几页
  267. private void comboBox_page_DropDownClosed(object? sender, EventArgs e)
  268. {
  269. if (int.TryParse(comboBox_page.Text.Trim(), out int index))
  270. {
  271. if (index > 0)
  272. {
  273. currentPageIndex = index;
  274. updateSplitPageLabelTip();
  275. showDataGirdView();
  276. }
  277. }
  278. }
  279. //每页条数
  280. private void comboBox_num_DropDownClosed(object? sender, EventArgs e)
  281. {
  282. if (int.TryParse(comboBox_num.Text.Trim(), out int res))
  283. {
  284. pageSize = res;
  285. currentPageIndex = 1; //初始化页码为1
  286. updateSplitPageLabelTip();
  287. showDataGirdView();
  288. }
  289. }
  290. //跳转指定页
  291. private void button_go_Click(object? sender, EventArgs e)
  292. {
  293. if (int.TryParse(textBox_num.Text.Trim(), out int num))
  294. {
  295. if (num > 0 && num <= pageCount)
  296. {
  297. currentPageIndex = num;
  298. currentPageIndex = currentPageIndex > pageCount ? pageCount : currentPageIndex;
  299. updateSplitPageLabelTip();
  300. showDataGirdView();
  301. }
  302. else
  303. {
  304. currentPageIndex = 1;
  305. MessageLogNotify.GetInstance.ShowWarning("超出页面范围!");
  306. }
  307. }
  308. else MessageLogNotify.GetInstance.ShowError("请输入指定页面!");
  309. }
  310. //修改显示数据
  311. public void updateSplitPageLabelTip()
  312. {
  313. try
  314. {
  315. pagingShow();
  316. comboBoxPage();
  317. comboBoxNum();
  318. }
  319. catch
  320. {
  321. }
  322. }
  323. //判断显示状状况
  324. private void pagingShow()
  325. {
  326. //共多少页
  327. pageCount = totalCount / pageSize;
  328. remainder = totalCount % pageSize;
  329. pageCount = remainder == 0 ? pageCount : pageCount + 1;
  330. if (pageCount <= 1)
  331. {
  332. label_first.Enabled = false;
  333. label_end.Enabled = false;
  334. label_up.Enabled = false;
  335. label_down.Enabled = false;
  336. comboBox_page.Enabled = false;
  337. textBox_num.Enabled = false;
  338. button_go.Enabled = false;
  339. }
  340. else
  341. {
  342. label_first.Enabled = true;
  343. label_end.Enabled = true;
  344. label_up.Enabled = true;
  345. label_down.Enabled = true;
  346. comboBox_page.Enabled = true;
  347. textBox_num.Enabled = true;
  348. button_go.Enabled = true;
  349. }
  350. //首页关闭首页,上一页
  351. if (currentPageIndex == 1)
  352. {
  353. label_first.Enabled = false;
  354. label_up.Enabled = false;
  355. }
  356. else
  357. {
  358. label_first.Enabled = true;
  359. label_up.Enabled = true;
  360. }
  361. //末页关闭末页,下一页
  362. if (currentPageIndex == pageCount)
  363. {
  364. label_end.Enabled = false;
  365. label_down.Enabled = false;
  366. }
  367. else
  368. {
  369. label_end.Enabled = true;
  370. label_down.Enabled = true;
  371. }
  372. //共多少条
  373. label_count.Text = "总" + totalCount + "条/" + pageCount + "页";
  374. textBox_num.Text = currentPageIndex.ToString();
  375. }
  376. //多少页
  377. private void comboBoxPage()
  378. {
  379. comboBox_page.Items.Clear();
  380. for (int i = 1; i <= pageCount; i++) comboBox_page.Items.Add(i.ToString());
  381. comboBox_page.SelectedIndex = -1;
  382. comboBox_page.SelectedIndex = pageCount == 1 ? 0 : currentPageIndex - 1; //当为一页,就设置设置为0
  383. }
  384. //每页多少条
  385. private void comboBoxNum()
  386. {
  387. comboBox_num.Items.Clear();
  388. comboBox_num.Items.Add("10");
  389. comboBox_num.Items.Add("20");
  390. comboBox_num.Items.Add("50");
  391. comboBox_num.Items.Add("100");
  392. comboBox_num.Items.Add("500");
  393. comboBox_num.Items.Add("1000");
  394. comboBox_num.SelectedIndex = 0;
  395. int i = 0;
  396. switch (pageSize)
  397. {
  398. case 10:
  399. i = 0;
  400. break;
  401. case 20:
  402. i = 1;
  403. break;
  404. case 50:
  405. i = 2;
  406. break;
  407. case 100:
  408. i = 3;
  409. break;
  410. case 500:
  411. i = 4;
  412. break;
  413. case 1000:
  414. i = 5;
  415. break;
  416. default:
  417. break;
  418. }
  419. comboBox_num.SelectedIndex = i;
  420. }
  421. //数据显示
  422. private void showDataGirdView()
  423. {
  424. GetPag();
  425. updateSplitPageLabelTip();
  426. }
  427. #endregion
  428. #region Excel
  429. OpaqueCommand cmd = new OpaqueCommand();
  430. /// <summary>
  431. /// 导入Excel
  432. /// </summary>
  433. /// <param name="sender"></param>
  434. /// <param name="e"></param>
  435. private async void 导入Excel_Click(object sender, EventArgs e)
  436. {
  437. OpenFileDialog openFile = new OpenFileDialog();
  438. openFile.Filter = "人员录入|*.xls";
  439. if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  440. {
  441. cmd.ShowOpaqueLayer(dataGridView1, 125, true);
  442. var importer = new ExcelImporter();
  443. IEnumerable<UserDtoModel> result = null;
  444. //1.添加所有机构名称
  445. List<string> jg = new List<string>();
  446. try
  447. {
  448. result = importer.ExcelToObject<UserDtoModel>(openFile.FileName);
  449. result?.ToList().ForEach(x =>
  450. {
  451. if (!string.IsNullOrEmpty(x.OrgId) && !jg.Contains(x.OrgId))
  452. {
  453. var find = Global.OrgTables.Find(p => p.Name == x.OrgId);
  454. if (find == null)
  455. jg.Add(x.OrgId);
  456. }
  457. });
  458. }
  459. catch (Exception ex)
  460. {
  461. MessageBox.Show($"导入失败,请先关闭Excel!");
  462. cmd.HideOpaqueLayer();
  463. return;
  464. }
  465. try
  466. {
  467. jg?.ForEach(x =>
  468. {
  469. HKLibHelper.SetOrgSync(x);
  470. });
  471. Global.OrgTables = await HKLibHelper.GetOrg();
  472. //2.添加用户
  473. Dictionary<string, List<UserDtoModel>> RrrorUser = new Dictionary<string, List<UserDtoModel>>();
  474. RrrorUser["名字重复"] = new List<UserDtoModel>();
  475. RrrorUser["机构不存在"] = new List<UserDtoModel>();
  476. RrrorUser["手机号重复"] = new List<UserDtoModel>();
  477. RrrorUser["添加出错"] = new List<UserDtoModel>();
  478. RrrorUser["成功"] = new List<UserDtoModel>();
  479. List<string> name = Global.UserListDtos.Select(o => o.UserName).Distinct().ToList();
  480. var phone = Global.UserListDtos.Select(x => x.Phone).Distinct().ToList();
  481. result?.ToList().ForEach(x =>
  482. {
  483. if (name.Contains(x.Name))
  484. {
  485. RrrorUser["名字重复"].Add(x);
  486. }
  487. if (phone.Contains(x.Phone))
  488. {
  489. RrrorUser["手机号重复"].Add(x);
  490. }
  491. else
  492. {
  493. var find = Global.OrgTables.Find(p => !string.IsNullOrEmpty(x.OrgId) && p.Name == x.OrgId);
  494. if (find == null)
  495. RrrorUser["机构不存在"].Add(x);
  496. else
  497. {
  498. var res = HKLibHelper.AddUserSync(new UserDto
  499. {
  500. Num = HKLibHelper.GetUserMaxNumSync(),
  501. Name = x.Name,
  502. OrgId = find.SId,
  503. Phone=x.Phone
  504. });
  505. if (!res)
  506. {
  507. RrrorUser["添加出错"].Add(x);
  508. }
  509. else
  510. {
  511. RrrorUser["成功"].Add(x);
  512. name.Add(x.Name);
  513. }
  514. }
  515. }
  516. });
  517. GetUser();
  518. string message = string.Empty;
  519. foreach (var item in RrrorUser)
  520. {
  521. if (item.Value.Count > 0)
  522. {
  523. message += "\n" + item.Key + "列表:\n";
  524. (item.Value as List<UserDtoModel>)?.ForEach(par =>
  525. {
  526. message += par.Name + "[" + par.OrgId + "] ";
  527. });
  528. }
  529. }
  530. MessageLogNotify.GetInstance.Show($"批量录入成功!\n成功{RrrorUser["成功"].Count}条\n失败{(RrrorUser["名字重复"].Count + RrrorUser["机构不存在"].Count + RrrorUser["添加出错"].Count)}条\n");
  531. MessageLogNotify.GetInstance.Show($"{message}\n");
  532. cmd.HideOpaqueLayer();
  533. MessageShow messageShow = new MessageShow(RrrorUser);
  534. messageShow.Show();
  535. }
  536. catch (Exception ex)
  537. {
  538. cmd.HideOpaqueLayer();
  539. MessageBox.Show($"导入失败,请重试!" + ex.Message);
  540. MessageLogNotify.GetInstance.ShowError($"导入失败,请重试!" + ex.Message);
  541. }
  542. }
  543. }
  544. /// <summary>
  545. /// 生成Excel
  546. /// </summary>
  547. /// <param name="sender"></param>
  548. /// <param name="e"></param>
  549. private void 生成Excel_Click(object sender, EventArgs e)
  550. {
  551. try
  552. {
  553. var models = new List<UserDtoModel>
  554. {
  555. new UserDtoModel{OrgId="组织机构1",Name="张三",Phone="123456789"},
  556. new UserDtoModel{OrgId="组织机构2",Name="李四",Phone="123456789"},
  557. new UserDtoModel{OrgId="组织机构2",Name="王麻子", Phone = "123456789"}
  558. };
  559. SaveFileDialog savefile = new SaveFileDialog();
  560. savefile.Filter = "人员录入|*.xls";
  561. string path = string.Empty;
  562. if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  563. {
  564. path = savefile.FileName;
  565. savefile.Dispose();
  566. if (!File.Exists(path))
  567. File.Create(path).Close();
  568. var exporter = new ExcelExporter();
  569. var bytes = exporter.ObjectToExcelBytes(models);
  570. File.WriteAllBytes(path, bytes);
  571. MessageLogNotify.GetInstance.Show("模板生成成功,路径:" + path);
  572. MessageLogNotify.GetInstance.Show("尝试打开文件...");
  573. //打开文件
  574. ProcessStartInfo processStartInfo = new ProcessStartInfo(path);
  575. Process process = new Process();
  576. process.StartInfo = processStartInfo;
  577. process.StartInfo.UseShellExecute = true;
  578. process.Start();
  579. MessageLogNotify.GetInstance.Show("模板生成成功并打开,请编辑后导入系统!");
  580. }
  581. }
  582. catch (Exception ex)
  583. {
  584. MessageBox.Show($"模板生成失败,请重试!" + ex.Message);
  585. MessageLogNotify.GetInstance.ShowError($"模板生成失败,请重试!" + ex.Message);
  586. }
  587. }
  588. #endregion
  589. /// <summary>
  590. /// 查询
  591. /// </summary>
  592. /// <param name="sender"></param>
  593. /// <param name="e"></param>
  594. private void button1_Click(object sender, EventArgs e)
  595. {
  596. try
  597. {
  598. var res = HKLibHelper.GetUserListSync(textBox1.Text);
  599. Global.UserListDtos.Clear();
  600. var userinfo = new List<UserInfoModel>();
  601. res?.ToList()?.ForEach(item =>
  602. {
  603. string status = "";
  604. string cardNum = "";
  605. if (item.Cards != null && item.Cards.Count > 0)
  606. {
  607. cardNum = item.Cards.FirstOrDefault(t=>t.State==1).CardNum;
  608. status = ((CarStatus)item.Cards.FirstOrDefault(t => t.State == 1).State).ToString();
  609. }
  610. Global.UserListDtos.Add(new UserInfoModel()
  611. {
  612. Num = item.Num,
  613. UserName = item.Name,
  614. Phone = item.Phone,
  615. OrgName = item.OrgInfo?.Count > 0 ? item.OrgInfo.ElementAt(0).Name : "",
  616. CardNum = cardNum,
  617. State = status
  618. });
  619. });
  620. for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
  621. {
  622. if (userinfo.Count < pageSize)
  623. userinfo.Add(Global.UserListDtos.ElementAt(i));
  624. else
  625. break;
  626. }
  627. dataGridView1.DataSource = userinfo;
  628. }
  629. catch (Exception ex)
  630. {
  631. }
  632. }
  633. }
  634. }