Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

390 linhas
13 KiB

  1. using HKLib.DB.Model.Entity;
  2. using HKLib.Dto;
  3. using HKLib.Interfaces;
  4. using HKLib.Logic;
  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.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace HKCardManager.UserPages
  15. {
  16. public partial class PersonnelEntryPage : UserControl
  17. {
  18. List<OrgTable> orgTables = new List<OrgTable>();
  19. public PersonnelEntryPage()
  20. {
  21. InitializeComponent();
  22. DataGridViewInit();
  23. EventInit();
  24. orgTables = ServiceHandler.GetOrgList();
  25. Global.OrgList.Clear();
  26. orgTables?.ForEach(item => { Global.OrgList.Add(item.Name); });
  27. comboBox2.DataSource = Global.OrgList;
  28. Task.Factory.StartNew(() => { GetUser(); });
  29. }
  30. private void DataGridViewInit()
  31. {
  32. dataGridView1.AllowUserToResizeColumns = false;// 禁止用户改变所有列的列宽
  33. dataGridView1.AllowUserToResizeRows = false; //禁止用户改变所有行的行高
  34. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; // 禁止用户改变列头的高度
  35. dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; // 禁止用户改变列头的宽度
  36. dataGridView1.AutoGenerateColumns = false;//禁用自动添加列
  37. }
  38. //获取所有用户信息
  39. private void GetUser()
  40. {
  41. var res = HKLibHelper.GetUserList("");
  42. Global.UserListDtos.Clear();
  43. Global.PagUserListDtos.Clear();
  44. res?.ToList()?.ForEach(item =>
  45. {
  46. string status = "";
  47. string cardNum = "";
  48. if (item.Cards != null && item.Cards.Count > 0)
  49. {
  50. cardNum = item.Cards.ElementAt(0).CardNum;
  51. status = ((CarStatus)item.Cards.ElementAt(0).State).ToString();
  52. }
  53. Global.UserListDtos.Add(new UserInfoModel()
  54. {
  55. UserName = item.Name,
  56. Phone = item.Phone,
  57. OrgName = item.OrgInfo?.Count > 0 ? item.OrgInfo.ElementAt(0).Name : "",
  58. CardNum = cardNum,
  59. State = status
  60. });
  61. });
  62. for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
  63. {
  64. if (Global.PagUserListDtos.Count < pageSize)
  65. Global.PagUserListDtos.Add(Global.UserListDtos.ElementAt(i));
  66. else
  67. break;
  68. }
  69. this.Invoke(() =>
  70. {
  71. dataGridView1.DataSource = null; dataGridView1.DataSource = Global.PagUserListDtos;
  72. totalCount = Global.UserListDtos.Count;
  73. comboBoxNum();
  74. showDataGirdView();
  75. });
  76. }
  77. //获取单页面用户信息
  78. private void GetPag()
  79. {
  80. Global.PagUserListDtos.Clear();
  81. for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
  82. {
  83. if (Global.PagUserListDtos.Count < pageSize && i >= 0)
  84. Global.PagUserListDtos.Add(Global.UserListDtos.ElementAt(i));
  85. else
  86. break;
  87. }
  88. dataGridView1.DataSource = null;
  89. dataGridView1.DataSource = Global.PagUserListDtos;
  90. }
  91. //添加用户
  92. private void button2_Click(object sender, EventArgs e)
  93. {
  94. if (string.IsNullOrEmpty(textBox1.Text.Trim()))
  95. {
  96. MessageLogNotify.GetInstance.ShowWarning("请输入姓名");
  97. return;
  98. }
  99. if (!string.IsNullOrEmpty(textBox2.Text.Trim()))
  100. {
  101. if (!textBox2.Text.Trim().IsMobile())
  102. {
  103. MessageLogNotify.GetInstance.ShowWarning("手机号格式不正确");
  104. return;
  105. }
  106. }
  107. UserDto userDto = new UserDto()
  108. {
  109. Name = textBox1.Text.Trim(),
  110. Phone = textBox2.Text.Trim(),
  111. OrgId = orgTables.FirstOrDefault(p => p.Name == comboBox2.Text)?.SId
  112. };
  113. string OrgName = comboBox2.Text;
  114. Task.Factory.StartNew(() =>
  115. {
  116. this.Invoke(() => { button2.Enabled = false; });
  117. if (HKLibHelper.AddUser(userDto))
  118. {
  119. GetUser();
  120. this.Invoke(() =>
  121. {
  122. textBox1.Text = string.Empty;
  123. textBox2.Text = string.Empty;
  124. });
  125. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 添加成功");
  126. }
  127. else
  128. MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 添加失败,请重试!");
  129. this.Invoke(() => { button2.Enabled = true; });
  130. });
  131. }
  132. //数据表列点击事件
  133. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  134. {
  135. int index = dataGridView1.CurrentRow.Index;
  136. if (index <= dataGridView1.Rows.Count - 1 && dataGridView1.CurrentCell.Value.ToString() == "删除")
  137. {
  138. var res = dataGridView1.Rows[index].Cells[0].Value?.ToString();
  139. if (string.IsNullOrEmpty(res))
  140. {
  141. MessageLogNotify.GetInstance.ShowError("用户名无效");
  142. return;
  143. }
  144. Task.Factory.StartNew(() =>
  145. {
  146. if (HKLibHelper.DisableUser(res))
  147. {
  148. GetUser();
  149. MessageLogNotify.GetInstance.Show($"用户:【{res}】 删除成功");
  150. }
  151. else
  152. MessageLogNotify.GetInstance.ShowError($"用户:【{res}】 删除失败");
  153. });
  154. }
  155. }
  156. #region 分页显示
  157. private int totalCount = 0;//总数据个数
  158. private int pageCount = 0;//总页数
  159. private int currentPageIndex = 1;//当前页索引
  160. private int pageSize = 10;//每页分页大小
  161. private int remainder = 0;//最后一页剩余个数
  162. private bool isAutoUpdateLabelTip = true;//获取或设置是否自动更新分页标签内容提示,默认为true
  163. //事件初始化
  164. private void EventInit()
  165. {
  166. label_first.Click += label_first_Click; //首页
  167. label_end.Click += label_end_Click;//末页
  168. label_up.Click += label_up_Click;//上一页
  169. label_down.Click += label_down_Click;//下一页
  170. comboBox_page.DropDownClosed += comboBox_page_DropDownClosed;//下拉列表切换页面
  171. comboBox_num.DropDownClosed += comboBox_num_DropDownClosed;//设置分页大小
  172. button_go.Click += button_go_Click;//跳转指定页
  173. }
  174. //首页
  175. private void label_first_Click(object? sender, EventArgs e)
  176. {
  177. currentPageIndex = 1;
  178. showDataGirdView();
  179. }
  180. //末页
  181. private void label_end_Click(object? sender, EventArgs e)
  182. {
  183. currentPageIndex = pageCount;
  184. showDataGirdView();
  185. }
  186. //上一页
  187. private void label_up_Click(object? sender, EventArgs e)
  188. {
  189. if (currentPageIndex <= pageCount && currentPageIndex != 1)
  190. {
  191. currentPageIndex--;
  192. showDataGirdView();
  193. }
  194. if (isAutoUpdateLabelTip) updateSplitPageLabelTip();
  195. }
  196. //下一页
  197. private void label_down_Click(object? sender, EventArgs e)
  198. {
  199. if (currentPageIndex < pageCount && currentPageIndex != pageCount)
  200. {
  201. currentPageIndex++;
  202. showDataGirdView();
  203. }
  204. if (isAutoUpdateLabelTip) updateSplitPageLabelTip();
  205. }
  206. //第几页
  207. private void comboBox_page_DropDownClosed(object? sender, EventArgs e)
  208. {
  209. if (int.TryParse(comboBox_page.Text.Trim(), out int index))
  210. {
  211. if (index > 0)
  212. {
  213. currentPageIndex = index;
  214. updateSplitPageLabelTip();
  215. showDataGirdView();
  216. }
  217. }
  218. }
  219. //每页条数
  220. private void comboBox_num_DropDownClosed(object? sender, EventArgs e)
  221. {
  222. if (int.TryParse(comboBox_num.Text.Trim(), out int res))
  223. {
  224. pageSize = res;
  225. currentPageIndex = 1; //初始化页码为1
  226. updateSplitPageLabelTip();
  227. showDataGirdView();
  228. }
  229. }
  230. //跳转指定页
  231. private void button_go_Click(object? sender, EventArgs e)
  232. {
  233. if (int.TryParse(textBox_num.Text.Trim(), out int num))
  234. {
  235. if (num > 0 && num <= pageCount)
  236. {
  237. currentPageIndex = num;
  238. currentPageIndex = currentPageIndex > pageCount ? pageCount : currentPageIndex;
  239. updateSplitPageLabelTip();
  240. showDataGirdView();
  241. }
  242. else
  243. {
  244. currentPageIndex = 1;
  245. MessageLogNotify.GetInstance.ShowWarning("超出页面范围!");
  246. }
  247. }
  248. else MessageLogNotify.GetInstance.ShowError("请输入指定页面!");
  249. }
  250. //修改显示数据
  251. public void updateSplitPageLabelTip()
  252. {
  253. pagingShow();
  254. comboBoxPage();
  255. comboBoxNum();
  256. }
  257. //判断显示状状况
  258. private void pagingShow()
  259. {
  260. //共多少页
  261. pageCount = totalCount / pageSize;
  262. remainder = totalCount % pageSize;
  263. pageCount = remainder == 0 ? pageCount : pageCount + 1;
  264. if (pageCount <= 1)
  265. {
  266. label_first.Enabled = false;
  267. label_end.Enabled = false;
  268. label_up.Enabled = false;
  269. label_down.Enabled = false;
  270. comboBox_page.Enabled = false;
  271. textBox_num.Enabled = false;
  272. button_go.Enabled = false;
  273. }
  274. else
  275. {
  276. label_first.Enabled = true;
  277. label_end.Enabled = true;
  278. label_up.Enabled = true;
  279. label_down.Enabled = true;
  280. comboBox_page.Enabled = true;
  281. textBox_num.Enabled = true;
  282. button_go.Enabled = true;
  283. }
  284. //首页关闭首页,上一页
  285. if (currentPageIndex == 1)
  286. {
  287. label_first.Enabled = false;
  288. label_up.Enabled = false;
  289. }
  290. else
  291. {
  292. label_first.Enabled = true;
  293. label_up.Enabled = true;
  294. }
  295. //末页关闭末页,下一页
  296. if (currentPageIndex == pageCount)
  297. {
  298. label_end.Enabled = false;
  299. label_down.Enabled = false;
  300. }
  301. else
  302. {
  303. label_end.Enabled = true;
  304. label_down.Enabled = true;
  305. }
  306. //共多少条
  307. label_count.Text = "总" + totalCount + "条/" + pageCount + "页";
  308. textBox_num.Text = currentPageIndex.ToString();
  309. }
  310. //多少页
  311. private void comboBoxPage()
  312. {
  313. comboBox_page.Items.Clear();
  314. for (int i = 1; i <= pageCount; i++) comboBox_page.Items.Add(i.ToString());
  315. comboBox_page.SelectedIndex = -1;
  316. comboBox_page.SelectedIndex = pageCount == 1 ? 0 : currentPageIndex - 1; //当为一页,就设置设置为0
  317. }
  318. //每页多少条
  319. private void comboBoxNum()
  320. {
  321. comboBox_num.Items.Clear();
  322. comboBox_num.Items.Add("10");
  323. comboBox_num.Items.Add("20");
  324. comboBox_num.Items.Add("50");
  325. comboBox_num.Items.Add("100");
  326. comboBox_num.Items.Add("500");
  327. comboBox_num.Items.Add("1000");
  328. comboBox_num.SelectedIndex = 0;
  329. int i = 0;
  330. switch (pageSize)
  331. {
  332. case 10:
  333. i = 0;
  334. break;
  335. case 20:
  336. i = 1;
  337. break;
  338. case 50:
  339. i = 2;
  340. break;
  341. case 100:
  342. i = 3;
  343. break;
  344. case 500:
  345. i = 4;
  346. break;
  347. case 1000:
  348. i = 5;
  349. break;
  350. default:
  351. break;
  352. }
  353. comboBox_num.SelectedIndex = i;
  354. }
  355. //数据显示
  356. private void showDataGirdView()
  357. {
  358. GetPag();
  359. updateSplitPageLabelTip();
  360. }
  361. #endregion
  362. }
  363. }