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.

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