|
- using HKLib.DB.Model.Entity;
- using HKLib.Dto;
- using HKLib.Interfaces;
- using HKLib.Logic;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace HKCardManager.UserPages
- {
- public partial class PersonnelEntryPage : UserControl
- {
- List<OrgTable> orgTables = new List<OrgTable>();
-
- public PersonnelEntryPage()
- {
- InitializeComponent();
- DataGridViewInit();
- EventInit();
- orgTables = ServiceHandler.GetOrgList();
- Global.OrgList.Clear();
- orgTables?.ForEach(item => { Global.OrgList.Add(item.Name); });
- comboBox2.DataSource = Global.OrgList;
- Task.Factory.StartNew(() => { GetUser(); });
- }
-
- private void DataGridViewInit()
- {
- dataGridView1.AllowUserToResizeColumns = false;// 禁止用户改变所有列的列宽
- dataGridView1.AllowUserToResizeRows = false; //禁止用户改变所有行的行高
- dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; // 禁止用户改变列头的高度
- dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; // 禁止用户改变列头的宽度
- dataGridView1.AutoGenerateColumns = false;//禁用自动添加列
- }
-
- //获取所有用户信息
- private void GetUser()
- {
- var res = HKLibHelper.GetUserList("");
- Global.UserListDtos.Clear();
- Global.PagUserListDtos.Clear();
- res?.ToList()?.ForEach(item =>
- {
- string status = "";
- string cardNum = "";
- if (item.Cards != null && item.Cards.Count > 0)
- {
- cardNum = item.Cards.ElementAt(0).CardNum;
- status = ((CarStatus)item.Cards.ElementAt(0).State).ToString();
- }
- Global.UserListDtos.Add(new UserInfoModel()
- {
- UserName = item.Name,
- Phone = item.Phone,
- OrgName = item.OrgInfo?.Count > 0 ? item.OrgInfo.ElementAt(0).Name : "",
- CardNum = cardNum,
- State = status
- });
- });
-
- for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
- {
- if (Global.PagUserListDtos.Count < pageSize)
- Global.PagUserListDtos.Add(Global.UserListDtos.ElementAt(i));
- else
- break;
- }
-
- this.Invoke(() =>
- {
- dataGridView1.DataSource = null; dataGridView1.DataSource = Global.PagUserListDtos;
- totalCount = Global.UserListDtos.Count;
- comboBoxNum();
- showDataGirdView();
- });
-
- }
-
- //获取单页面用户信息
- private void GetPag()
- {
- Global.PagUserListDtos.Clear();
- for (int i = (currentPageIndex - 1) * pageSize; i < Global.UserListDtos.Count; i++)
- {
- if (Global.PagUserListDtos.Count < pageSize && i >= 0)
- Global.PagUserListDtos.Add(Global.UserListDtos.ElementAt(i));
- else
- break;
- }
- dataGridView1.DataSource = null;
- dataGridView1.DataSource = Global.PagUserListDtos;
- }
-
- //添加用户
- private void button2_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(textBox1.Text.Trim()))
- {
- MessageLogNotify.GetInstance.ShowWarning("请输入姓名");
- return;
- }
- if (!string.IsNullOrEmpty(textBox2.Text.Trim()))
- {
- if (!textBox2.Text.Trim().IsMobile())
- {
- MessageLogNotify.GetInstance.ShowWarning("手机号格式不正确");
- return;
- }
- }
- UserDto userDto = new UserDto()
- {
- Name = textBox1.Text.Trim(),
- Phone = textBox2.Text.Trim(),
- OrgId = orgTables.FirstOrDefault(p => p.Name == comboBox2.Text)?.SId
- };
- string OrgName = comboBox2.Text;
- Task.Factory.StartNew(() =>
- {
- this.Invoke(() => { button2.Enabled = false; });
- if (HKLibHelper.AddUser(userDto))
- {
- GetUser();
- this.Invoke(() =>
- {
- textBox1.Text = string.Empty;
- textBox2.Text = string.Empty;
- });
- MessageLogNotify.GetInstance.Show($"用户 【{userDto.Name}】 添加成功");
- }
- else
- MessageLogNotify.GetInstance.ShowError($"用户 【{userDto.Name}】 添加失败,请重试!");
- this.Invoke(() => { button2.Enabled = true; });
- });
-
- }
-
- //数据表列点击事件
- private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- int index = dataGridView1.CurrentRow.Index;
- if (index <= dataGridView1.Rows.Count - 1 && dataGridView1.CurrentCell.Value.ToString() == "删除")
- {
- var res = dataGridView1.Rows[index].Cells[0].Value?.ToString();
- if (string.IsNullOrEmpty(res))
- {
- MessageLogNotify.GetInstance.ShowError("用户名无效");
- return;
- }
- Task.Factory.StartNew(() =>
- {
- if (HKLibHelper.DisableUser(res))
- {
- GetUser();
- MessageLogNotify.GetInstance.Show($"用户:【{res}】 删除成功");
- }
- else
- MessageLogNotify.GetInstance.ShowError($"用户:【{res}】 删除失败");
- });
- }
- }
-
- #region 分页显示
-
- private int totalCount = 0;//总数据个数
- private int pageCount = 0;//总页数
- private int currentPageIndex = 1;//当前页索引
- private int pageSize = 10;//每页分页大小
- private int remainder = 0;//最后一页剩余个数
- private bool isAutoUpdateLabelTip = true;//获取或设置是否自动更新分页标签内容提示,默认为true
-
- //事件初始化
- private void EventInit()
- {
- label_first.Click += label_first_Click; //首页
- label_end.Click += label_end_Click;//末页
- label_up.Click += label_up_Click;//上一页
- label_down.Click += label_down_Click;//下一页
- comboBox_page.DropDownClosed += comboBox_page_DropDownClosed;//下拉列表切换页面
- comboBox_num.DropDownClosed += comboBox_num_DropDownClosed;//设置分页大小
- button_go.Click += button_go_Click;//跳转指定页
- }
-
- //首页
- private void label_first_Click(object? sender, EventArgs e)
- {
- currentPageIndex = 1;
- showDataGirdView();
- }
-
- //末页
- private void label_end_Click(object? sender, EventArgs e)
- {
- currentPageIndex = pageCount;
- showDataGirdView();
- }
-
- //上一页
- private void label_up_Click(object? sender, EventArgs e)
- {
- if (currentPageIndex <= pageCount && currentPageIndex != 1)
- {
- currentPageIndex--;
- showDataGirdView();
- }
- if (isAutoUpdateLabelTip) updateSplitPageLabelTip();
- }
-
- //下一页
- private void label_down_Click(object? sender, EventArgs e)
- {
- if (currentPageIndex < pageCount && currentPageIndex != pageCount)
- {
- currentPageIndex++;
- showDataGirdView();
- }
- if (isAutoUpdateLabelTip) updateSplitPageLabelTip();
- }
-
- //第几页
- private void comboBox_page_DropDownClosed(object? sender, EventArgs e)
- {
- if (int.TryParse(comboBox_page.Text.Trim(), out int index))
- {
- if (index > 0)
- {
- currentPageIndex = index;
- updateSplitPageLabelTip();
- showDataGirdView();
- }
- }
- }
-
- //每页条数
- private void comboBox_num_DropDownClosed(object? sender, EventArgs e)
- {
- if (int.TryParse(comboBox_num.Text.Trim(), out int res))
- {
- pageSize = res;
- currentPageIndex = 1; //初始化页码为1
- updateSplitPageLabelTip();
- showDataGirdView();
- }
- }
-
- //跳转指定页
- private void button_go_Click(object? sender, EventArgs e)
- {
- if (int.TryParse(textBox_num.Text.Trim(), out int num))
- {
- if (num > 0 && num <= pageCount)
- {
- currentPageIndex = num;
- currentPageIndex = currentPageIndex > pageCount ? pageCount : currentPageIndex;
- updateSplitPageLabelTip();
- showDataGirdView();
- }
- else
- {
- currentPageIndex = 1;
- MessageLogNotify.GetInstance.ShowWarning("超出页面范围!");
- }
- }
- else MessageLogNotify.GetInstance.ShowError("请输入指定页面!");
- }
-
- //修改显示数据
- public void updateSplitPageLabelTip()
- {
- pagingShow();
- comboBoxPage();
- comboBoxNum();
- }
-
- //判断显示状状况
- private void pagingShow()
- {
- //共多少页
- pageCount = totalCount / pageSize;
- remainder = totalCount % pageSize;
- pageCount = remainder == 0 ? pageCount : pageCount + 1;
- if (pageCount <= 1)
- {
- label_first.Enabled = false;
- label_end.Enabled = false;
- label_up.Enabled = false;
- label_down.Enabled = false;
- comboBox_page.Enabled = false;
- textBox_num.Enabled = false;
- button_go.Enabled = false;
- }
- else
- {
- label_first.Enabled = true;
- label_end.Enabled = true;
- label_up.Enabled = true;
- label_down.Enabled = true;
- comboBox_page.Enabled = true;
- textBox_num.Enabled = true;
- button_go.Enabled = true;
- }
- //首页关闭首页,上一页
- if (currentPageIndex == 1)
- {
- label_first.Enabled = false;
- label_up.Enabled = false;
- }
- else
- {
- label_first.Enabled = true;
- label_up.Enabled = true;
- }
- //末页关闭末页,下一页
- if (currentPageIndex == pageCount)
- {
- label_end.Enabled = false;
- label_down.Enabled = false;
- }
- else
- {
- label_end.Enabled = true;
- label_down.Enabled = true;
- }
- //共多少条
- label_count.Text = "总" + totalCount + "条/" + pageCount + "页";
- textBox_num.Text = currentPageIndex.ToString();
- }
-
- //多少页
- private void comboBoxPage()
- {
- comboBox_page.Items.Clear();
- for (int i = 1; i <= pageCount; i++) comboBox_page.Items.Add(i.ToString());
- comboBox_page.SelectedIndex = -1;
- comboBox_page.SelectedIndex = pageCount == 1 ? 0 : currentPageIndex - 1; //当为一页,就设置设置为0
- }
-
- //每页多少条
- private void comboBoxNum()
- {
- comboBox_num.Items.Clear();
- comboBox_num.Items.Add("10");
- comboBox_num.Items.Add("20");
- comboBox_num.Items.Add("50");
- comboBox_num.Items.Add("100");
- comboBox_num.Items.Add("500");
- comboBox_num.Items.Add("1000");
- comboBox_num.SelectedIndex = 0;
- int i = 0;
- switch (pageSize)
- {
- case 10:
- i = 0;
- break;
- case 20:
- i = 1;
- break;
- case 50:
- i = 2;
- break;
- case 100:
- i = 3;
- break;
- case 500:
- i = 4;
- break;
- case 1000:
- i = 5;
- break;
- default:
- break;
- }
- comboBox_num.SelectedIndex = i;
- }
-
- //数据显示
- private void showDataGirdView()
- {
- GetPag();
- updateSplitPageLabelTip();
- }
- #endregion
- }
- }
|