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.

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