No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

225 líneas
11 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. {
  37. int index = dataGridView1.CurrentRow.Index;
  38. if (index >= 0 && index < dataGridView1.Rows.Count)
  39. {
  40. dataGridView1.Columns[2].ReadOnly = true;
  41. dataGridView1.Columns[3].ReadOnly = true;
  42. if (dataGridView1.CurrentCell.Value?.ToString() == "删除")
  43. {
  44. var res = Global.OrgTables.FirstOrDefault(p => p.Name == dataGridView1.Rows[index].Cells[0].Value?.ToString())?.SId;
  45. if (string.IsNullOrEmpty(res))
  46. {
  47. MessageBox.Show("错误!!!\n 获取机构ID失败...");
  48. MessageLogNotify.GetInstance.ShowError("获取机构ID失败");
  49. return;
  50. }
  51. try
  52. {
  53. var tempname = dataGridView1.Rows[index].Cells[0].Value?.ToString();
  54. if (MessageBox.Show($"确认是否删除[{tempname}]!", "操作", MessageBoxButtons.YesNo) == DialogResult.Yes)
  55. {
  56. if (await HKLibHelper.DelOrg(res))
  57. {
  58. //Global.OrgTables.Clear();
  59. //Global.OrgTables = HKLibHelper.GetOrg();
  60. var result = await HKLibHelper.GetOrg();
  61. if (result != null)
  62. {
  63. dataGridView1.DataSource = null;
  64. Global.OrgTables.Clear();
  65. Global.OrgTables = result;
  66. dataGridView1.DataSource = Global.OrgTables;
  67. }
  68. MessageBox.Show($"{tempname} 机构删除成功!");
  69. MessageLogNotify.GetInstance.Show($"机构:【{tempname}】 删除成功");
  70. }
  71. else
  72. {
  73. MessageBox.Show($"{tempname} 机构删除失败,请稍后重试...");
  74. MessageLogNotify.GetInstance.ShowError($"机构:【{tempname}】 删除失败");
  75. }
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. MessageBox.Show($"机构删除失败,错误原因:" + ex.Message);
  81. MessageLogNotify.GetInstance.ShowEx(ex.ToString());
  82. }
  83. finally
  84. {
  85. dataGridView1.Columns[2].ReadOnly = false;
  86. dataGridView1.Columns[3].ReadOnly = false;
  87. }
  88. }
  89. else if (dataGridView1.CurrentCell.Value?.ToString() == "修改")
  90. {
  91. var name = dataGridView1.Rows[index].Cells[0].Value?.ToString();//原名称
  92. var changeName = dataGridView1.Rows[index].Cells[1].Value?.ToString();//修改后名称
  93. if (string.IsNullOrEmpty(name))
  94. {
  95. MessageBox.Show("错误!!!\n原机构名称无效...");
  96. MessageLogNotify.GetInstance.ShowError("原机构名称无效");
  97. return;
  98. }
  99. if (string.IsNullOrEmpty(changeName))
  100. {
  101. MessageBox.Show("错误!!!\n请输入新机构名称...");
  102. MessageLogNotify.GetInstance.ShowError("请输入新机构名称");
  103. return;
  104. }
  105. if (name == changeName)
  106. {
  107. MessageBox.Show("错误!!!\n新名称和旧名称一致...");
  108. MessageLogNotify.GetInstance.ShowError("新名称和旧名称一致,无法修改");
  109. return;
  110. }
  111. var id = Global.OrgTables.FirstOrDefault(p => p.Name == name)?.SId;
  112. if (string.IsNullOrEmpty(id))
  113. {
  114. MessageBox.Show("未找到对应ID.请稍后重试...");
  115. MessageLogNotify.GetInstance.ShowError("未找到对应ID");
  116. return;
  117. }
  118. //Task.Factory.StartNew(async () =>
  119. //{
  120. try
  121. {
  122. if (await HKLibHelper.AlterOrg(changeName, id))
  123. {
  124. var result = await HKLibHelper.GetOrg();
  125. if (result != null)
  126. {
  127. dataGridView1.DataSource = null;
  128. Global.OrgTables.Clear();
  129. Global.OrgTables = result;
  130. dataGridView1.DataSource = Global.OrgTables;
  131. }
  132. MessageBox.Show($"{name} 机构修改成功...");
  133. MessageLogNotify.GetInstance.Show($"{name} 机构修改成功");
  134. }
  135. else
  136. {
  137. MessageBox.Show($"{name} 机构修改失败,请稍后重试...");
  138. MessageLogNotify.GetInstance.ShowError($"{name} 机构修改失败");
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. MessageBox.Show($"{name} 机构修改失败,请稍后重试...,错误原因:" + ex.Message);
  144. MessageLogNotify.GetInstance.ShowEx(ex.ToString());
  145. }
  146. finally
  147. {
  148. this.Invoke(() =>
  149. {
  150. dataGridView1.Rows[index].Cells[1].Value = string.Empty;
  151. dataGridView1.Columns[2].ReadOnly = false;
  152. dataGridView1.Columns[3].ReadOnly = false;
  153. });
  154. }
  155. //});
  156. }
  157. }
  158. }
  159. }
  160. catch (Exception ex)
  161. {
  162. MessageLogNotify.GetInstance.ShowEx(ex.ToString());
  163. }
  164. }
  165. private void InstitutionalEntryPage_SizeChanged(object? sender, EventArgs e)
  166. {
  167. this.splitContainer1.SplitterDistance = this.Height - 50;
  168. //this.panel1.Left = (this.Width - this.panel1.Width) / 2;
  169. //this.panel1.Top = (this.Height - this.panel1.Height) / 2;
  170. }
  171. private async void button2_Click(object sender, EventArgs e)
  172. {
  173. string name = this.textBox1.Text.Trim();
  174. if (string.IsNullOrEmpty(name))
  175. {
  176. MessageBox.Show("请输入机构名称,在尝试添加...");
  177. MessageLogNotify.GetInstance.ShowWarning("请输入机构名称");
  178. return;
  179. }
  180. var res = await HKLibHelper.GetOrg();
  181. if (res?.FirstOrDefault(t => t.Name == name) != null)
  182. {
  183. MessageBox.Show("机构已存在请勿重复添加...");
  184. MessageLogNotify.GetInstance.ShowWarning("机构已存在请勿重复添加");
  185. return;
  186. }
  187. this.Invoke(() => { button2.Enabled = false; });
  188. var result = await HKLibHelper.SetOrg(name);
  189. if (!string.IsNullOrEmpty(result))
  190. {
  191. Global.OrgList.Add(name);
  192. Global.OrgTables.Add(new HKLib.Dto.OrgDto()
  193. {
  194. Name = name,
  195. SId = result
  196. });
  197. MessageBox.Show($"【{name}】 机构添加成功...");
  198. MessageLogNotify.GetInstance.Show($"【{name}】 机构添加成功");
  199. this.Invoke(() =>
  200. {
  201. dataGridView1.DataSource = null;
  202. dataGridView1.DataSource = Global.OrgTables;
  203. });
  204. }
  205. else
  206. {
  207. MessageBox.Show($"【{name}】 机构添加失败,请稍后重试...");
  208. MessageLogNotify.GetInstance.ShowError($"【{name}】 机构添加失败,请重试");
  209. }
  210. this.Invoke(() => { button2.Enabled = true; });
  211. }
  212. }
  213. }