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.

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