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.

401 lines
14 KiB

  1. using HKCardManager.UserPages;
  2. using HKLib.Dto;
  3. using HKLib.Interfaces;
  4. using HKLib.SQLHelper;
  5. using System.Text.RegularExpressions;
  6. using HKHelper;
  7. using UHFHelper;
  8. using Snowflake.Core;
  9. using HKLib.RabbitMQ.Config;
  10. using HKLib.RabbitMQ.Publisher;
  11. namespace HKCardManager
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. Size = new Size(1400, 850);
  19. DbContext.InitTable();
  20. splitContainer1.SplitterDistance = 240;
  21. DataGridViewInit();
  22. ShowPage(new PersonnelEntryPage());
  23. Task.Factory.StartNew(() =>
  24. {
  25. var res = UHFCardHelper.GetInstance().OpenPort();
  26. if (res != null && res.Res)
  27. MessageLogNotify.GetInstance.Show("制卡设备已连接!");
  28. else MessageLogNotify.GetInstance.ShowError($"制卡设备未连接!");
  29. });
  30. this.SizeChanged += Form1_SizeChanged;
  31. this.button1.SizeChanged += Button1_SizeChanged;
  32. this.splitContainer2.SplitterMoved += SplitContainer1_SizeChanged;
  33. MessageLogNotify.GetInstance.Info = new Action<string, Color>((s, br) =>
  34. {
  35. this.Invoke(() =>
  36. {
  37. dataGridView1.Rows.Insert(0, new string[] { "", $"{DateTime.Now.ToString("HH:mm:ss")} {s}" });
  38. dataGridView1.Rows[0].DefaultCellStyle.ForeColor = br;
  39. dataGridView1.Rows[0].DefaultCellStyle.Padding = new Padding(0, 0, 0, 10);
  40. });
  41. });
  42. }
  43. private void SplitContainer1_SizeChanged(object? sender, EventArgs e)
  44. {
  45. if (tempUserControl != null)
  46. {
  47. tempUserControl.Width = this.panel1.Width;
  48. tempUserControl.Height = this.panel1.Height;
  49. }
  50. }
  51. private void DataGridViewInit()
  52. {
  53. dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
  54. dataGridView1.AutoGenerateColumns = false;//禁止自动添加列
  55. dataGridView1.AllowUserToResizeColumns = false;//是否可以调整列的大小
  56. dataGridView1.AllowUserToResizeRows = false;//是否可以调整行的大小
  57. dataGridView1.BorderStyle = BorderStyle.None;//DataGridView边框样式
  58. dataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
  59. dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
  60. dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
  61. dataGridView1.SelectionChanged += DataGridView1_SelectionChanged;
  62. }
  63. private void DataGridView1_SelectionChanged(object? sender, EventArgs e)
  64. {
  65. dataGridView1.ClearSelection();//禁止选择行
  66. }
  67. private void Write(string msg)
  68. {
  69. //bool条件默认为配置文件里面定义,可以自定义任何保存数据条件
  70. //bool temp = Convert.ToBoolean(iniFile.IniReadValue("设置", "保存运行信息", AppDomain.CurrentDomain.BaseDirectory + "设置.ini"));
  71. //if (temp)
  72. //{
  73. // string logPath = Path.GetDirectoryName(Application.ExecutablePath);
  74. // // System.IO.StreamWriter sw = System.IO.File.AppendText(logPath + "/" + DateTime.Now.ToString("yyMMdd") + ".txt");
  75. // System.IO.StreamWriter sw = System.IO.File.AppendText(logPath + "\\log\\" + DateTime.Now.ToString("yyMMdd") + ".txt");
  76. // sw.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + msg);
  77. // sw.Close();
  78. // sw.Dispose();
  79. //}
  80. }
  81. private void AddListToListBox(ListBox rtbox, string text, Brush foreColor, bool addTime = true)
  82. {
  83. rtbox.DrawMode = DrawMode.OwnerDrawVariable;
  84. rtbox.Font = new Font("楷体", 12);
  85. if (rtbox.ItemHeight != 24)
  86. {
  87. rtbox.ItemHeight = 1;
  88. rtbox.DrawItem += ((sender, e) => //控件重绘事件
  89. {
  90. if (e.Index == -1) return;
  91. e.DrawBackground();
  92. ListBoxItem item = (ListBoxItem)rtbox.Items[e.Index];
  93. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
  94. {
  95. e.DrawFocusRectangle();
  96. Brush b = Brushes.Khaki;//设置选中项颜色
  97. e.Graphics.FillRectangle(b, e.Bounds);
  98. }
  99. if (item != null)
  100. {
  101. Brush brush = item.ForeColor;
  102. Rectangle r = e.Bounds;
  103. r.Height = e.Bounds.Height;
  104. r.Width = e.Bounds.Width + 100;
  105. e.Graphics.DrawString(item.Text, e.Font, brush, r, StringFormat.GenericDefault);
  106. }
  107. else
  108. {
  109. e.Graphics.DrawString(item.Text, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
  110. }
  111. });
  112. }
  113. rtbox.ItemHeight = 25;//设置单行高度
  114. if (rtbox.InvokeRequired)
  115. {
  116. rtbox.Invoke(new Action<ListBox, string, Brush, bool>((rtb, str, color, addtime) => AddListToListBox(rtb, str, color, addtime)), rtbox, text, foreColor, addTime);
  117. return;
  118. }
  119. //text = string.Format("{0}-{1}", rtbox.Items.Count.ToString("000"), text);
  120. if (addTime)
  121. {
  122. //text = string.Format("[{0}]:{1}", DateTime.Now.ToString("HH:mm:ss"), text);
  123. text = $"{DateTime.Now.ToString("HH:mm:ss")}:{text}";
  124. }
  125. if (rtbox.Items.Count > 0)
  126. rtbox.Items.Insert(0, new ListBoxItem(text, foreColor));
  127. else
  128. rtbox.Items.Add(new ListBoxItem(text, foreColor));
  129. try
  130. {
  131. Write(text);
  132. }
  133. catch { }
  134. //清除过多的数据
  135. if (rtbox.Items.Count > 100)
  136. {
  137. int count = rtbox.Items.Count - 100;
  138. List<object> temp = new List<object>();
  139. for (int i = 100; i < rtbox.Items.Count; i++)
  140. {
  141. if (rtbox.Items.Count >= i - 1) temp.Add(rtbox.Items[i]);
  142. }
  143. foreach (object obj in temp)
  144. {
  145. rtbox.Items.Remove(obj);
  146. }
  147. }
  148. //rtbox.SelectedIndex = rtbox.Items.Count - 1;
  149. //if (rtbox.Items.Count > 100)
  150. //{
  151. // int count = rtbox.Items.Count - 100;
  152. // List<object> temp = new List<object>();
  153. // for (int i = 0; i < count; i++)
  154. // {
  155. // temp.Add(rtbox.Items[i]);
  156. // }
  157. // foreach (object obj in temp)
  158. // {
  159. // rtbox.Items.Remove(obj);
  160. // }
  161. //}
  162. //rtbox.SelectedIndex = rtbox.Items.Count - 1;
  163. }
  164. private void Button1_SizeChanged(object? sender, EventArgs e)
  165. {
  166. groupBox1.Width = button1.Width;
  167. groupBox2.Width = button1.Width;
  168. groupBox3.Width = button1.Width;
  169. groupBox4.Width = button1.Width;
  170. button2.Left = 10;
  171. button2.Width = (groupBox1.Width - 20);
  172. button3.Left = 10;
  173. button3.Width = (groupBox1.Width - 20);
  174. button4.Left = 10;
  175. button4.Width = (groupBox2.Width - 20);
  176. button5.Left = 10;
  177. button5.Width = (groupBox2.Width - 20);
  178. button6.Left = 10;
  179. button6.Width = (groupBox2.Width - 20);
  180. button7.Left = 10;
  181. button7.Width = (groupBox2.Width - 20);
  182. button9.Left = 10;
  183. button9.Width = (groupBox3.Width - 20);
  184. button10.Left = 10;
  185. button10.Width = (groupBox3.Width - 20);
  186. button11.Left = 10;
  187. button11.Width = (groupBox3.Width - 20);
  188. button12.Left = 10;
  189. button12.Width = (groupBox4.Width - 20);
  190. }
  191. UserControl tempUserControl;
  192. private void Form1_SizeChanged(object? sender, EventArgs e)
  193. {
  194. if (tempUserControl != null)
  195. {
  196. tempUserControl.Width = this.panel1.Width;
  197. tempUserControl.Height = this.panel1.Height;
  198. }
  199. splitContainer1.SplitterDistance = 240;
  200. if (this.WindowState == FormWindowState.Maximized || this.WindowState == FormWindowState.Normal)
  201. {
  202. Task.Factory.StartNew(() =>
  203. {
  204. Thread.Sleep(100);
  205. this.Invoke(() =>
  206. {
  207. if (tempUserControl != null)
  208. {
  209. tempUserControl.Width = this.panel1.Width;
  210. tempUserControl.Height = this.panel1.Height;
  211. }
  212. splitContainer1.SplitterDistance = 240;
  213. });
  214. });
  215. }
  216. }
  217. //人员录入
  218. private void button2_Click(object sender, EventArgs e)
  219. {
  220. ShowPage(new PersonnelEntryPage());
  221. }
  222. //机构录入
  223. private void button3_Click(object sender, EventArgs e)
  224. {
  225. ShowPage(new InstitutionalEntryPage());
  226. }
  227. //发卡
  228. private void button5_Click(object sender, EventArgs e)
  229. {
  230. ShowPage(new CarMangerPage());
  231. }
  232. //销户,挂失,解挂
  233. private void button4_Click(object sender, EventArgs e)
  234. {
  235. ShowPage(new CancellationPage(), ((Button)sender)?.Tag.ToString());
  236. }
  237. //月餐段汇总表
  238. private void button11_Click(object sender, EventArgs e)
  239. {
  240. ShowPage(new ReportFormPage(), ((Button)sender)?.Tag.ToString());
  241. }
  242. //餐段设置
  243. private void button12_Click(object sender, EventArgs e)
  244. {
  245. ShowPage(new MealSegmentSetPage());
  246. }
  247. private void ShowPage(UserControl userControl, string? name = "")
  248. {
  249. panel1.Controls.Clear();
  250. userControl.Width = panel1.Width;
  251. userControl.Height = panel1.Height;
  252. tempUserControl = userControl;
  253. userControl.Tag = name;
  254. userControl.Show();
  255. panel1.Controls.Add(userControl);
  256. }
  257. private void button8_Click(object sender, EventArgs e)
  258. {
  259. ShowPage(new AdvertisingSetPage());
  260. }
  261. private async void button13_Click(object sender, EventArgs e)
  262. {
  263. if (UHFCardHelper.GetInstance().ComOpen)
  264. {
  265. try
  266. {
  267. var res = UHFCardHelper.GetInstance().ReadCard();
  268. if (string.IsNullOrEmpty(res))
  269. {
  270. MessageBox.Show("提示!!!\n 请放入卡片,在选择销卡...");
  271. MessageLogNotify.GetInstance.Show("请放入卡片");
  272. return;
  273. }
  274. if (res.Equals("666") || res.Equals("888") || res.Equals("999"))
  275. {
  276. MessageBox.Show("超级卡不能被注销");
  277. return;
  278. }
  279. var result = await HKLibHelper.GetUserList("");
  280. UserListDto users = null;
  281. result.ForEach(item =>
  282. {
  283. if (item.Cards.FirstOrDefault(t => t.CardNum.Equals(res)) != null)
  284. {
  285. users = item;
  286. return;
  287. }
  288. });
  289. if (users != null)
  290. {
  291. await HKLibHelper.CardStutasChange(new CardStutasDto()
  292. {
  293. keywrod = users.Name,
  294. Stutas = 3,
  295. });
  296. }
  297. var resz = MessageBox.Show($"提示!!!\n 当前卡号{res},确定销卡?", "提示", MessageBoxButtons.YesNo);
  298. if (resz == DialogResult.Yes)
  299. {
  300. var res1 = UHFCardHelper.GetInstance().WriteCard("000");
  301. MessageBox.Show(res1.Res ? "000为默认新卡卡号,销卡成功" : "销卡失败");
  302. }
  303. }
  304. catch (Exception)
  305. {
  306. MessageBox.Show("销卡成功");
  307. }
  308. }
  309. else
  310. {
  311. MessageBox.Show("提示!!!\n 设备未连接...");
  312. MessageLogNotify.GetInstance.ShowWarning("设备未连接");
  313. }
  314. }
  315. private void button14_Click(object sender, EventArgs e)
  316. {
  317. if (UHFCardHelper.GetInstance().ComOpen)
  318. {
  319. try
  320. {
  321. var res = UHFCardHelper.GetInstance().ReadCard();
  322. if (string.IsNullOrEmpty(res))
  323. {
  324. MessageLogNotify.GetInstance.Show("请放入卡片");
  325. return;
  326. }
  327. UHFCardHelper.GetInstance().WriteCard("000");
  328. string worker = new IdWorker(1, 1).NextId().ToString();// ID生成
  329. var resm = UHFCardHelper.GetInstance().WriteCard(worker);
  330. MessageLogNotify.GetInstance.Show($"卡片重置成功,原卡号【{res}】,新卡号【{worker}】");
  331. MessageBox.Show($"提示!!!\n 卡片重置成功,原卡号【{res}】,新卡号【{worker}】");
  332. }
  333. catch (Exception)
  334. {
  335. MessageBox.Show("重置卡片失败");
  336. }
  337. }
  338. else
  339. {
  340. MessageBox.Show("提示!!!\n 设备未连接...");
  341. MessageLogNotify.GetInstance.ShowWarning("设备未连接");
  342. }
  343. }
  344. }
  345. }