Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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