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.

213 line
9.2 KiB

  1. using HKLib.Interfaces;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using XExten.Advance.LinqFramework;
  12. namespace HKCardManager.UserPages
  13. {
  14. public partial class InstitutionalEntryPage : UserControl
  15. {
  16. public InstitutionalEntryPage()
  17. {
  18. InitializeComponent();
  19. DataGridViewInit();
  20. dataGridView1.DataSource = Global.OrgTables;
  21. this.SizeChanged += InstitutionalEntryPage_SizeChanged;
  22. }
  23. private void DataGridViewInit()
  24. {
  25. dataGridView1.AllowUserToResizeColumns = false;// 禁止用户改变所有列的列宽
  26. dataGridView1.AllowUserToResizeRows = false; //禁止用户改变所有行的行高
  27. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; // 禁止用户改变列头的高度
  28. dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; // 禁止用户改变列头的宽度
  29. dataGridView1.AutoGenerateColumns = false;//禁用自动添加列
  30. dataGridView1.CellContentClick += DataGridView1_CellContentClick;
  31. }
  32. private async void DataGridView1_CellContentClick(object? sender, DataGridViewCellEventArgs e)
  33. {
  34. try
  35. {
  36. int index = dataGridView1.CurrentRow.Index;
  37. if (index >= 0 && index < dataGridView1.Rows.Count)
  38. {
  39. dataGridView1.Columns[2].ReadOnly = true;
  40. dataGridView1.Columns[3].ReadOnly = true;
  41. if (dataGridView1.CurrentCell.Value?.ToString() == "删除")
  42. {
  43. var res = Global.OrgTables.FirstOrDefault(p => p.Name == dataGridView1.Rows[index].Cells[0].Value?.ToString())?.SId;
  44. if (string.IsNullOrEmpty(res))
  45. {
  46. MessageLogNotify.GetInstance.ShowError("获取机构ID失败");
  47. return;
  48. }
  49. //Task.Factory.StartNew(() =>
  50. //{
  51. try
  52. {
  53. if (await HKLibHelper.DelOrg(res))
  54. {
  55. //Global.OrgTables.Clear();
  56. //Global.OrgTables = HKLibHelper.GetOrg();
  57. var result = await HKLibHelper.GetOrg();
  58. if (result != null)
  59. {
  60. //this.Invoke(() =>
  61. //{
  62. dataGridView1.DataSource = null;
  63. Global.OrgTables.Clear();
  64. Global.OrgTables = result;
  65. dataGridView1.DataSource = Global.OrgTables;
  66. //});
  67. }
  68. MessageLogNotify.GetInstance.Show($"机构:【{dataGridView1.Rows[index].Cells[0].Value?.ToString()}】 删除成功");
  69. }
  70. else
  71. MessageLogNotify.GetInstance.ShowError($"机构:【{dataGridView1.Rows[index].Cells[0].Value?.ToString()}】 删除失败");
  72. }
  73. catch (Exception ex)
  74. {
  75. MessageLogNotify.GetInstance.ShowEx(ex.ToString());
  76. }
  77. finally
  78. {
  79. //this.Invoke(() =>
  80. //{
  81. dataGridView1.Columns[2].ReadOnly = false;
  82. dataGridView1.Columns[3].ReadOnly = false;
  83. //});
  84. }
  85. //});
  86. }
  87. else if (dataGridView1.CurrentCell.Value?.ToString() == "修改")
  88. {
  89. var name = dataGridView1.Rows[index].Cells[0].Value?.ToString();//原名称
  90. var changeName = dataGridView1.Rows[index].Cells[1].Value?.ToString();//修改后名称
  91. if (string.IsNullOrEmpty(name))
  92. {
  93. MessageLogNotify.GetInstance.ShowError("原机构名称无效");
  94. return;
  95. }
  96. if (string.IsNullOrEmpty(changeName))
  97. {
  98. MessageLogNotify.GetInstance.ShowError("请输入新机构名称");
  99. return;
  100. }
  101. if (name == changeName)
  102. {
  103. MessageLogNotify.GetInstance.ShowError("新名称和旧名称一致,无法修改");
  104. return;
  105. }
  106. var id = Global.OrgTables.FirstOrDefault(p => p.Name == name)?.SId;
  107. if (string.IsNullOrEmpty(id))
  108. {
  109. MessageLogNotify.GetInstance.ShowError("未找到对应ID");
  110. return;
  111. }
  112. //Task.Factory.StartNew(async () =>
  113. //{
  114. try
  115. {
  116. if (await HKLibHelper.AlterOrg(changeName, id))
  117. {
  118. var result = await HKLibHelper.GetOrg();
  119. if (result != null)
  120. {
  121. //this.Invoke(() =>
  122. //{
  123. dataGridView1.DataSource = null;
  124. Global.OrgTables.Clear();
  125. Global.OrgTables = result;
  126. dataGridView1.DataSource = Global.OrgTables;
  127. //});
  128. }
  129. MessageLogNotify.GetInstance.Show($"{name} 机构修改成功");
  130. }
  131. else
  132. MessageLogNotify.GetInstance.ShowError($"{name} 机构修改失败");
  133. }
  134. catch (Exception ex)
  135. {
  136. MessageLogNotify.GetInstance.ShowEx(ex.ToString());
  137. }
  138. finally
  139. {
  140. this.Invoke(() =>
  141. {
  142. dataGridView1.Rows[index].Cells[1].Value = string.Empty;
  143. dataGridView1.Columns[2].ReadOnly = false;
  144. dataGridView1.Columns[3].ReadOnly = false;
  145. });
  146. }
  147. //});
  148. }
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. MessageLogNotify.GetInstance.ShowEx(ex.ToString());
  154. }
  155. }
  156. private void InstitutionalEntryPage_SizeChanged(object? sender, EventArgs e)
  157. {
  158. this.splitContainer1.SplitterDistance = this.Height - 50;
  159. //this.panel1.Left = (this.Width - this.panel1.Width) / 2;
  160. //this.panel1.Top = (this.Height - this.panel1.Height) / 2;
  161. }
  162. private async void button2_Click(object sender, EventArgs e)
  163. {
  164. string name = this.textBox1.Text.Trim();
  165. if (string.IsNullOrEmpty(name))
  166. {
  167. MessageLogNotify.GetInstance.ShowWarning("请输入机构名称");
  168. return;
  169. }
  170. var res = await HKLibHelper.GetOrg();
  171. if (res?.FirstOrDefault(t => t.Name == name) != null)
  172. {
  173. MessageLogNotify.GetInstance.ShowWarning("机构已存在请勿重复添加");
  174. return;
  175. }
  176. //Task.Factory.StartNew(() =>
  177. //{
  178. this.Invoke(() => { button2.Enabled = false; });
  179. var result = await HKLibHelper.SetOrg(name);
  180. if (!string.IsNullOrEmpty(result))
  181. {
  182. Global.OrgList.Add(name);
  183. Global.OrgTables.Add(new HKLib.Dto.OrgDto()
  184. {
  185. Name = name,
  186. SId = result
  187. });
  188. MessageLogNotify.GetInstance.Show($"【{name}】 机构添加成功");
  189. this.Invoke(() =>
  190. {
  191. dataGridView1.DataSource = null;
  192. dataGridView1.DataSource = Global.OrgTables;
  193. });
  194. }
  195. else
  196. MessageLogNotify.GetInstance.ShowError($"【{name}】 机构添加失败,请重试");
  197. this.Invoke(() => { button2.Enabled = true; });
  198. //});
  199. }
  200. }
  201. }