Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

587 рядки
20 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. //定义委托
  17. public delegate void BindHandle(object sender, EventArgs e);
  18. public partial class PersonnelEntryPage : UserControl
  19. {
  20. //List<string> OrgList = new List<string>();
  21. List<OrgTable> orgTables = new List<OrgTable>();
  22. public PersonnelEntryPage()
  23. {
  24. InitializeComponent();
  25. #region 禁止更改宽高
  26. // 禁止用户改变DataGridView的所有列的列宽
  27. dataGridView1.AllowUserToResizeColumns = false;
  28. //禁止用户改变DataGridView所有行的行高
  29. dataGridView1.AllowUserToResizeRows = false;
  30. // 禁止用户改变列头的高度
  31. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  32. // 禁止用户改变列头的宽度
  33. dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
  34. #endregion
  35. this.Load += PersonnelEntryPage_Load;
  36. dataGridView1.AutoGenerateColumns = false;
  37. //comboBox1.SelectedIndex = 0;
  38. orgTables = ServiceHandler.GetOrgList();
  39. Global.OrgList.Clear();
  40. orgTables?.ForEach(item =>
  41. {
  42. Global.OrgList.Add(item.Name);
  43. });
  44. comboBox2.DataSource = Global.OrgList;
  45. Task.Factory.StartNew(() => { GetUser(); });
  46. }
  47. private void GetUser()
  48. {
  49. var res = HKLibHelper.GetUserList("");
  50. Global.UserListDtos.Clear();
  51. Global.PagUserListDtos.Clear();
  52. res?.ToList()?.ForEach(item =>
  53. {
  54. string status = "";
  55. string cardNum = "";
  56. if (item.Cards != null && item.Cards.Count > 0)
  57. {
  58. cardNum = item.Cards.ElementAt(0).CardNum;
  59. status = ((CarStatus)item.Cards.ElementAt(0).State).ToString();
  60. }
  61. Global.UserListDtos.Add(new UserInfoModel()
  62. {
  63. UserName = item.Name,
  64. Phone = item.Phone,
  65. OrgName = item.OrgInfo?.Count > 0 ? item.OrgInfo.ElementAt(0).Name : "",
  66. CardNum = cardNum,
  67. State = status
  68. });
  69. });
  70. for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
  71. {
  72. if (Global.PagUserListDtos.Count < pageSize)
  73. Global.PagUserListDtos.Add(Global.UserListDtos.ElementAt(i));
  74. else
  75. break;
  76. }
  77. this.Invoke(() =>
  78. {
  79. //dataGridView1.DataSource = null; dataGridView1.DataSource = Global.UserListDtos;
  80. dataGridView1.DataSource = null; dataGridView1.DataSource = Global.PagUserListDtos;
  81. totalCount = Global.UserListDtos.Count;
  82. comboBoxNum();
  83. showDataGirdView();
  84. });
  85. }
  86. private void GetPag()
  87. {
  88. Global.PagUserListDtos.Clear();
  89. for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
  90. {
  91. if (Global.PagUserListDtos.Count < pageSize)
  92. Global.PagUserListDtos.Add(Global.UserListDtos.ElementAt(i));
  93. else
  94. break;
  95. }
  96. dataGridView1.DataSource = null;
  97. dataGridView1.DataSource = Global.PagUserListDtos;
  98. }
  99. private void button2_Click(object sender, EventArgs e)
  100. {
  101. if (string.IsNullOrEmpty(textBox1.Text.Trim()))
  102. {
  103. MessageLogNotify.GetInstance.ShowWarning("请输入姓名");
  104. return;
  105. }
  106. if (!string.IsNullOrEmpty(textBox2.Text.Trim()))
  107. {
  108. if (!textBox2.Text.Trim().IsMobile())
  109. {
  110. MessageLogNotify.GetInstance.ShowWarning("手机号格式不正确");
  111. return;
  112. }
  113. }
  114. UserDto userDto = new UserDto()
  115. {
  116. Name = textBox1.Text.Trim(),
  117. Phone = textBox2.Text.Trim(),
  118. OrgId = orgTables.FirstOrDefault(p => p.Name == comboBox2.Text)?.SId
  119. };
  120. string OrgName = comboBox2.Text;
  121. Task.Factory.StartNew(() =>
  122. {
  123. this.Invoke(() => { button2.Enabled = false; });
  124. if (HKLibHelper.AddUser(userDto))
  125. {
  126. GetUser();
  127. this.Invoke(() =>
  128. {
  129. textBox1.Text = string.Empty;
  130. textBox2.Text = string.Empty;
  131. });
  132. MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 添加成功");
  133. }
  134. else
  135. MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 添加失败,请重试!");
  136. this.Invoke(() => { button2.Enabled = true; });
  137. });
  138. }
  139. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  140. {
  141. int index = dataGridView1.CurrentRow.Index;
  142. if (index <= dataGridView1.Rows.Count - 1 && dataGridView1.CurrentCell.Value.ToString() == "删除")
  143. {
  144. var res = dataGridView1.Rows[index].Cells[0].Value?.ToString();
  145. if (string.IsNullOrEmpty(res))
  146. {
  147. MessageLogNotify.GetInstance.ShowError("用户名无效");
  148. return;
  149. }
  150. Task.Factory.StartNew(() =>
  151. {
  152. if (HKLibHelper.DisableUser(res))
  153. {
  154. GetUser();
  155. MessageLogNotify.GetInstance.Show($"用户:【{res}】 删除成功");
  156. }
  157. else
  158. MessageLogNotify.GetInstance.ShowError($"用户:【{res}】 删除失败");
  159. });
  160. }
  161. }
  162. #region datagridView 分页显示
  163. #region 变量定义
  164. private int totalCount = 0;//总数据个数
  165. private int pageCount = 0;//总页数
  166. private int currentPageIndex = 1;//当前页索引
  167. private int pageSize = 10;//每页分页大小
  168. private int remainder = 0;//最后一页剩余个数
  169. private bool isAutoUpdateLabelTip = true;//获取或设置是否自动更新分页标签内容提示,默认为true
  170. private string sqlAddress = "";//"Data Source=192.168.100.144;Initial Catalog=test;User ID=sa;Password=123456;";//DataGridViewd要绑定的表名
  171. private string tableName = "";//"DataSource";//DataGridViewd要绑定的表名
  172. private string strWhere = "";
  173. private DataSet dsAll = null;//总数据源
  174. #endregion
  175. public event EventHandler CellClick;//datagridview cellclick 触发事件
  176. #region 外调函数
  177. /// <summary>
  178. /// 绑定数据
  179. /// </summary>
  180. public void dataBand()
  181. {
  182. comboBoxNum();
  183. showDataGirdView();
  184. }
  185. ///// <summary>
  186. ///// 返回当前页面数据
  187. ///// </summary>
  188. ///// <returns></returns>
  189. //public DataGridView outputDGV()
  190. //{
  191. // return dataGridView_show;
  192. //}
  193. #endregion
  194. private void PersonnelEntryPage_Load(object? sender, EventArgs e)
  195. {
  196. }
  197. //加载事件
  198. //private void pager1_Load(object sender, EventArgs e)
  199. //{
  200. // comboBoxNum();
  201. // showDataGirdView();
  202. //}
  203. #region 分页操作
  204. //首页
  205. private void label_first_Click(object sender, EventArgs e)
  206. {
  207. currentPageIndex = 1;
  208. showDataGirdView();
  209. }
  210. //末页
  211. private void label_end_Click(object sender, EventArgs e)
  212. {
  213. currentPageIndex = pageCount;
  214. showDataGirdView();
  215. }
  216. //上一页
  217. private void label_up_Click(object sender, EventArgs e)
  218. {
  219. if (currentPageIndex <= pageCount && currentPageIndex != 1)
  220. {
  221. currentPageIndex--;
  222. showDataGirdView();
  223. }
  224. if (isAutoUpdateLabelTip)
  225. {
  226. updateSplitPageLabelTip();
  227. }
  228. }
  229. //下一页
  230. private void label_down_Click(object sender, EventArgs e)
  231. {
  232. if (currentPageIndex < pageCount && currentPageIndex != pageCount)
  233. {
  234. currentPageIndex++;
  235. showDataGirdView();
  236. }
  237. if (isAutoUpdateLabelTip)
  238. {
  239. updateSplitPageLabelTip();
  240. }
  241. }
  242. //第几页
  243. private void comboBox_page_DropDownClosed(object sender, EventArgs e)
  244. {
  245. currentPageIndex = int.Parse(comboBox_page.SelectedItem.ToString());
  246. updateSplitPageLabelTip();
  247. showDataGirdView();
  248. }
  249. //每页条数
  250. private void comboBox_num_DropDownClosed(object sender, EventArgs e)
  251. {
  252. pageSize = int.Parse(comboBox_num.SelectedItem.ToString());
  253. //初始化页码为1
  254. currentPageIndex = 1;
  255. updateSplitPageLabelTip();
  256. showDataGirdView();
  257. }
  258. //跳转指定页
  259. private void button_go_Click(object sender, EventArgs e)
  260. {
  261. try
  262. {
  263. currentPageIndex = int.Parse(textBox_num.Text);
  264. }
  265. catch
  266. {
  267. currentPageIndex = 1;
  268. }
  269. currentPageIndex = currentPageIndex > pageCount ? pageCount : currentPageIndex;
  270. updateSplitPageLabelTip();
  271. showDataGirdView();
  272. }
  273. //修改显示数据
  274. public void updateSplitPageLabelTip()
  275. {
  276. pagingShow();
  277. comboBoxPage();
  278. comboBoxNum();
  279. }
  280. //判断显示状状况
  281. private void pagingShow()
  282. {
  283. //共多少页
  284. pageCount = totalCount / pageSize;
  285. remainder = totalCount % pageSize;
  286. pageCount = remainder == 0 ? pageCount : pageCount + 1;
  287. if (pageCount <= 1)
  288. {
  289. label_first.Enabled = false;
  290. label_end.Enabled = false;
  291. label_up.Enabled = false;
  292. label_down.Enabled = false;
  293. comboBox_page.Enabled = false;
  294. textBox_num.Enabled = false;
  295. button_go.Enabled = false;
  296. }
  297. else
  298. {
  299. label_first.Enabled = true;
  300. label_end.Enabled = true;
  301. label_up.Enabled = true;
  302. label_down.Enabled = true;
  303. comboBox_page.Enabled = true;
  304. textBox_num.Enabled = true;
  305. button_go.Enabled = true;
  306. }
  307. //首页关闭首页,上一页
  308. if (currentPageIndex == 1)
  309. {
  310. label_first.Enabled = false;
  311. label_up.Enabled = false;
  312. }
  313. else
  314. {
  315. label_first.Enabled = true;
  316. label_up.Enabled = true;
  317. }
  318. //末页关闭末页,下一页
  319. if (currentPageIndex == pageCount)
  320. {
  321. label_end.Enabled = false;
  322. label_down.Enabled = false;
  323. }
  324. else
  325. {
  326. label_end.Enabled = true;
  327. label_down.Enabled = true;
  328. }
  329. //共多少条
  330. label_count.Text = "总" + totalCount + "条/" + pageCount + "页";
  331. textBox_num.Text = currentPageIndex.ToString();
  332. }
  333. //多少页
  334. private void comboBoxPage()
  335. {
  336. comboBox_page.Items.Clear();
  337. for (int i = 1; i <= pageCount; i++)
  338. {
  339. comboBox_page.Items.Add(i.ToString());
  340. }
  341. comboBox_page.SelectedIndex = -1;
  342. //当为一页,就设置设置为0
  343. comboBox_page.SelectedIndex = pageCount == 1 ? 0 : currentPageIndex - 1;
  344. }
  345. //每页多少条
  346. private void comboBoxNum()
  347. {
  348. comboBox_num.Items.Clear();
  349. comboBox_num.Items.Add("10");
  350. comboBox_num.Items.Add("20");
  351. comboBox_num.Items.Add("50");
  352. comboBox_num.Items.Add("100");
  353. comboBox_num.Items.Add("500");
  354. comboBox_num.Items.Add("1000");
  355. comboBox_num.SelectedIndex = 0;
  356. int i = 0;
  357. switch (pageSize)
  358. {
  359. case 10:
  360. i = 0;
  361. break;
  362. case 20:
  363. i = 1;
  364. break;
  365. case 50:
  366. i = 2;
  367. break;
  368. case 100:
  369. i = 3;
  370. break;
  371. case 500:
  372. i = 4;
  373. break;
  374. case 1000:
  375. i = 5;
  376. break;
  377. default:
  378. break;
  379. }
  380. comboBox_num.SelectedIndex = i;
  381. }
  382. //获取数据
  383. #endregion
  384. #region 数据处理
  385. //绑定数据
  386. private void showDataGirdView()
  387. {
  388. //DataTable dt = new DataTable();
  389. //if (dsAll != null)
  390. //{
  391. // dt = GetDsAll();//获取传入数据
  392. //}
  393. //else
  394. //{
  395. // dt = GetTable();//获取数据库数据
  396. //}
  397. //try
  398. //{
  399. // //绑定前台控件
  400. // totalCount = int.Parse(dt.Rows[0]["total"].ToString());
  401. //}
  402. //catch
  403. //{
  404. // totalCount = 1;
  405. //}
  406. GetPag();
  407. updateSplitPageLabelTip();
  408. //绑定数据
  409. //dataGridView_show.DataSource = dt;
  410. //dataGridView_show.AllowUserToAddRows = false;
  411. }
  412. /// <summary>
  413. /// 获取数据库分页查询
  414. /// </summary>
  415. /// <param name="sql"></param>
  416. /// <returns></returns>
  417. //private DataTable GetTable(string sql = "")
  418. //{
  419. // DataTable dt = new DataTable();
  420. // try
  421. // {
  422. // MySqlConnection Connection = new MySqlConnection(sqlAddress);
  423. // Connection.Open();
  424. // //分页查询
  425. // if (sql == "")
  426. // {
  427. // sql = " ,(SELECT COUNT(1) FROM " + tableName + ") as total ";
  428. // sql = "SELECT TOP " + pageSize + " * " + sql + " FROM " + tableName + " WHERE ID NOT IN(SELECT TOP " + ((currentPageIndex - 1) * pageSize) + " ID FROM " + tableName + ")";
  429. // }
  430. // MySqlDataAdapter adpater = new MySqlDataAdapter(sql, Connection);
  431. // DataSet ds = new DataSet();
  432. // adpater.Fill(ds);
  433. // adpater.Fill(dt);
  434. // Connection.Close();
  435. // }
  436. // catch
  437. // {
  438. // label_count.Text = "无数据";
  439. // }
  440. // return dt;
  441. //}
  442. /// <summary>
  443. /// 获取数据源分页查询
  444. /// </summary>
  445. /// <returns></returns>
  446. //private DataTable GetDsAll(string sql = "")
  447. //{
  448. // DataTable dt = new DataTable();
  449. // try
  450. // {
  451. // dt = DataSetHelper.SplitDataSet(dsAll, pageSize, currentPageIndex).Tables[0];
  452. // //添加一列返回总数‘total’
  453. // int count = dsAll.Tables[0].Rows.Count;
  454. // dt.Columns.Add("total", typeof(int));
  455. // foreach (DataRow dr in dt.Rows)
  456. // {
  457. // dr["total"] = count;
  458. // }
  459. // }
  460. // catch
  461. // {
  462. // label_count.Text = "无数据";
  463. // }
  464. // return dt;
  465. //}
  466. #region DataSet分页查询
  467. public class DataSetHelper
  468. {
  469. public static DataSet SplitDataSet(DataSet ds, int pageSize, int pageIndex)
  470. {
  471. DataSet vds = new DataSet();
  472. vds = ds.Clone();
  473. if (pageIndex < 1) pageIndex = 1;//如果小于1,取第一页
  474. //if ((ds.Tables[0].Rows.Count + pageSize) <= (pageSize * pageIndex)) pageIndex = 1;
  475. int fromIndex = pageSize * (pageIndex - 1);//开始行
  476. int toIndex = pageSize * pageIndex - 1; //结束行
  477. for (int i = fromIndex; i <= toIndex; i++)
  478. {
  479. if (i >= (ds.Tables[0].Rows.Count)) //到达这一行,退出
  480. break;
  481. vds.Tables[0].ImportRow(ds.Tables[0].Rows[i]);
  482. }
  483. ds.Dispose();
  484. return vds;
  485. }
  486. /// <summary>
  487. /// 根据索引和pagesize返回记录
  488. /// </summary>
  489. /// <param name="dt">记录集 DataTable</param>
  490. /// <param name="PageIndex">当前页</param>
  491. /// <param name="pagesize">一页的记录数</param>
  492. /// <returns></returns>
  493. public static DataTable SplitDataTable(DataTable dt, int PageIndex, int PageSize)
  494. {
  495. if (dt == null)
  496. {
  497. return null;
  498. }
  499. if (PageIndex == 0)
  500. return dt;
  501. DataTable newdt = dt.Clone();
  502. //newdt.Clear();
  503. int rowbegin = (PageIndex - 1) * PageSize;
  504. int rowend = PageIndex * PageSize;
  505. if (rowbegin >= dt.Rows.Count)
  506. return newdt;
  507. if (rowend > dt.Rows.Count)
  508. rowend = dt.Rows.Count;
  509. for (int i = rowbegin; i <= rowend - 1; i++)
  510. {
  511. DataRow newdr = newdt.NewRow();
  512. DataRow dr = dt.Rows[i];
  513. foreach (DataColumn column in dt.Columns)
  514. {
  515. newdr[column.ColumnName] = dr[column.ColumnName];
  516. }
  517. newdt.Rows.Add(newdr);
  518. }
  519. return newdt;
  520. }
  521. public static DataTable ToDataTable(DataRow[] rows)
  522. {
  523. if (rows == null || rows.Length == 0)
  524. {
  525. return null;
  526. }
  527. DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
  528. foreach (DataRow row in rows)
  529. {
  530. tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
  531. }
  532. return tmp;
  533. }
  534. }
  535. private void dataGridView_show_CellClick(object sender, DataGridViewCellEventArgs e)
  536. {
  537. if (this.CellClick != null)
  538. {
  539. //this.CellClick(this.dataGridView_show, new EventArgs());
  540. }
  541. }
  542. #endregion
  543. #endregion
  544. #endregion
  545. }
  546. }