Main.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Media;
  10. using System.Security.Principal;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using UAS_Tools_HY;
  15. using UAS_Tools_HY.PublicMethods;
  16. namespace UAS_MES_Tools
  17. {
  18. public partial class Main : Form
  19. {
  20. string _Account, _Password, _UserName;
  21. DataTable dt;
  22. Timer loadTime;
  23. string minTime, maxTime;
  24. string curRule, curRuleLeng;
  25. public Main(string account, string password)
  26. {
  27. _Account = account;
  28. _Password = password;
  29. InitializeComponent();
  30. }
  31. private void Main_Load(object sender, EventArgs e)
  32. {
  33. currUser.Text = "当前账户: " + _Account;
  34. dt = ConnectDB.ExecuteSelect($"select * from employee where em_code = '{_Account}'");
  35. if (dt.Rows.Count > 0)
  36. {
  37. _UserName = dt.Rows[0]["em_name"].ToString();
  38. }
  39. loadTime_Tick(null, null);
  40. loadTime = new Timer();
  41. loadTime.Interval = 1000;
  42. loadTime.Tick += loadTime_Tick;
  43. loadTime.Start();
  44. SN.SelectAll();
  45. SN.Focus();
  46. settingLeftInput1.Text = BaseUtil.GetCacheData("SettingCount").ToString();
  47. settingLeftInput2.Text = BaseUtil.GetCacheData("SettingVolume").ToString();
  48. settingRightInput1.Text = BaseUtil.GetCacheData("SettingNGPath").ToString();
  49. settingRightInput2.Text = BaseUtil.GetCacheData("SettingExportPath").ToString();
  50. QDcheckbox1.Checked = true;
  51. QDdtp1.Value = QDdtp2.Value.AddDays(-1);
  52. rulesQuery_Click(null, null);
  53. }
  54. private void SN_KeyDown(object sender, KeyEventArgs e)
  55. {
  56. if (e.KeyCode != Keys.Enter) return;
  57. if (CheckEnter()) return;
  58. bool checkRes = true;
  59. ShowMsg.Text = "Pass";
  60. ShowMsg.BackColor = Color.LawnGreen;
  61. if (radio1.Checked)
  62. {
  63. dt = ConnectDB.ExecuteSelect($@"SELECT COUNT(1) sum FROM dcrlogexcel WHERE item = '{SN.Text.Trim()}'");
  64. if (dt.Rows[0]["sum"].ToString() == "0")
  65. {
  66. PlaySound("NG");
  67. ShowMsg.Text = "Fail";
  68. ShowMsg.BackColor = Color.Red;
  69. MessageBox.Show($"{SN.Text.Trim()},没有DCR测试数据", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  70. checkRes = false;
  71. return;
  72. }
  73. }
  74. if (FormMethods.IsCheckSNRules(curRule, SN.Text.Trim(), curRuleLeng))
  75. {
  76. PlaySound("NG");
  77. ShowMsg.Text = "Fail";
  78. ShowMsg.BackColor = Color.Red;
  79. MessageBox.Show($"{SN.Text.Trim()},不符合条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  80. checkRes = false;
  81. return;
  82. }
  83. if (Datas.Rows.Count > 0)
  84. {
  85. foreach (DataGridViewRow item in Datas.Rows)
  86. {
  87. if (SN.Text.Trim() == item.Cells[0].Value.ToString())
  88. {
  89. int iCou = Convert.ToInt32(item.Cells[2].Value);
  90. iCou += 1;
  91. item.Cells[3].Value = currTime.Text;
  92. item.Cells[6].Value = checkRes ? "OK" : "NG";
  93. }
  94. }
  95. }
  96. DataGridViewRow row = new DataGridViewRow();
  97. row.CreateCells(Datas);
  98. row.Cells[0].Value = SN.Text.Trim();
  99. row.Cells[1].Value = BoxNo.Text.Trim();
  100. row.Cells[2].Value = 1;
  101. row.Cells[3].Value = currTime.Text;
  102. row.Cells[4].Value = _UserName;
  103. row.Cells[5].Value = radio1.Checked ? "是" : "否";
  104. row.Cells[6].Value = checkRes ? "OK": "NG";
  105. Datas.Rows.Add(row);
  106. PlaySound("OK");
  107. Datas.FirstDisplayedScrollingRowIndex = Datas.Rows.Count - 1;
  108. }
  109. private void BoxNo_KeyDown(object sender, KeyEventArgs e)
  110. {
  111. if (e.KeyCode != Keys.Enter) return;
  112. if (CheckEnter()) return;
  113. if (Datas.Rows.Count > 0)
  114. {
  115. foreach(DataGridViewRow item in Datas.Rows)
  116. {
  117. item.Cells[1].Value = BoxNo.Text.Trim();
  118. }
  119. }
  120. BoxNo.Enabled = false;
  121. SN.SelectAll();
  122. SN.Focus();
  123. }
  124. private bool CheckEnter()
  125. {
  126. if (string.IsNullOrEmpty(settingLeftInput1.Text))
  127. {
  128. MessageBox.Show("请维护列表记录数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  129. return true;
  130. }
  131. if (string.IsNullOrEmpty(settingLeftInput2.Text))
  132. {
  133. MessageBox.Show("请维护箱内容量", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  134. return true;
  135. }
  136. if (string.IsNullOrEmpty(dRules.Text))
  137. {
  138. MessageBox.Show("请选择条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  139. return true;
  140. }
  141. if (dRules.Enabled)
  142. {
  143. MessageBox.Show("请选择确认条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  144. return true;
  145. }
  146. return false;
  147. }
  148. private void settingLeftSubmit_Click(object sender, EventArgs e)
  149. {
  150. if (string.IsNullOrEmpty(settingLeftInput1.Text))
  151. {
  152. MessageBox.Show("请输入列表记录数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  153. return;
  154. }
  155. if (string.IsNullOrEmpty(settingLeftInput2.Text))
  156. {
  157. MessageBox.Show("请输入箱内容量", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  158. return;
  159. }
  160. BaseUtil.SetCacheData("SettingCount", settingLeftInput1.Text);
  161. BaseUtil.SetCacheData("SettingVolume", settingLeftInput2.Text);
  162. MessageBox.Show("已提交扫描设置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  163. }
  164. private void settingRightSubmit_Click(object sender, EventArgs e)
  165. {
  166. if (string.IsNullOrEmpty(settingRightInput2.Text))
  167. {
  168. MessageBox.Show("请选择导出文件目录", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  169. return;
  170. }
  171. if(!string.IsNullOrEmpty(settingRightInput1.Text)) BaseUtil.SetCacheData("SettingNGPath", settingRightInput1.Text);
  172. BaseUtil.SetCacheData("SettingExportPath", settingRightInput2.Text);
  173. MessageBox.Show("已提交文件设置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  174. }
  175. private void QDquery_Click(object sender, EventArgs e)
  176. {
  177. if (string.IsNullOrEmpty(QDInput1.Text) && string.IsNullOrEmpty(QDInput2.Text) && !QDcheckbox1.Checked)
  178. {
  179. MessageBox.Show("请添加筛选条件", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  180. return;
  181. }
  182. if (QDcheckbox1.Checked)
  183. {
  184. }
  185. }
  186. private void QDexport_Click(object sender, EventArgs e)
  187. {
  188. if(QDDatas.Rows.Count == 0)
  189. {
  190. MessageBox.Show("请筛选数据", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  191. return;
  192. }
  193. }
  194. private void rulesAdd_Click(object sender, EventArgs e)
  195. {
  196. StringBuilder filterStr = new StringBuilder();
  197. if (!string.IsNullOrEmpty(rulesInput1.Text))
  198. {
  199. filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
  200. }
  201. if (!string.IsNullOrEmpty(rulesInput2.Text))
  202. {
  203. if(filterStr.Length == 0)
  204. {
  205. filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
  206. }
  207. else
  208. {
  209. filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
  210. }
  211. }
  212. Form rulesList = new RulesList("Add",_UserName);
  213. if (rulesList.ShowDialog() == DialogResult.OK)
  214. {
  215. MessageBox.Show("新增规则成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  216. dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
  217. RULE_LENGTH RulesCol4,to_char(UPDATE_TIME, 'YYYY-MM-DD HH:Mi:SS') RulesCol5,UPDATE_USER RulesCol6 FROM g_packing_rules {filterStr.ToString()} order by UPDATE_TIME desc");
  218. RulesDatas.DataSource = dt;
  219. }
  220. }
  221. private void rulesQuery_Click(object sender, EventArgs e)
  222. {
  223. StringBuilder filterStr = new StringBuilder();
  224. if (!string.IsNullOrEmpty(rulesInput1.Text))
  225. {
  226. filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
  227. }
  228. if (!string.IsNullOrEmpty(rulesInput2.Text))
  229. {
  230. if (filterStr.Length == 0)
  231. {
  232. filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
  233. }
  234. else
  235. {
  236. filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
  237. }
  238. }
  239. dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
  240. RULE_LENGTH RulesCol4,to_char(UPDATE_TIME, 'YYYY-MM-DD HH:Mi:SS') RulesCol5,UPDATE_USER RulesCol6 FROM g_packing_rules {filterStr.ToString()} order by UPDATE_TIME desc");
  241. RulesDatas.DataSource = dt;
  242. }
  243. private void RulesDatas_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
  244. {
  245. if (e.RowIndex >= 0 && !RulesDatas.Rows[e.RowIndex].IsNewRow)
  246. {
  247. DataGridView dgv = sender as DataGridView;
  248. if (dgv != null)
  249. {
  250. DataGridViewRow selectedRow = dgv.Rows[e.RowIndex];
  251. string RULE_ID = selectedRow.Cells[0].Value.ToString();
  252. StringBuilder filterStr = new StringBuilder();
  253. if (!string.IsNullOrEmpty(rulesInput1.Text))
  254. {
  255. filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
  256. }
  257. if (!string.IsNullOrEmpty(rulesInput2.Text))
  258. {
  259. if (filterStr.Length == 0)
  260. {
  261. filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
  262. }
  263. else
  264. {
  265. filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
  266. }
  267. }
  268. Form rulesList = new RulesList("Modify",RULE_ID, _UserName);
  269. if (rulesList.ShowDialog() == DialogResult.OK)
  270. {
  271. MessageBox.Show("修改规则成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  272. dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
  273. RULE_LENGTH RulesCol4,to_char(UPDATE_TIME, 'YYYY-MM-DD HH:Mi:SS') RulesCol5,UPDATE_USER RulesCol6 FROM g_packing_rules {filterStr.ToString()} order by UPDATE_TIME desc");
  274. RulesDatas.DataSource = dt;
  275. }
  276. }
  277. }
  278. }
  279. private void RulesDatas_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  280. {
  281. if (e.RowIndex >= 0 && !RulesDatas.Rows[e.RowIndex].IsNewRow)
  282. {
  283. DataGridView dgv = sender as DataGridView;
  284. if (dgv != null)
  285. {
  286. DataGridViewRow selectedRow = dgv.Rows[e.RowIndex];
  287. if (dRules.Enabled)
  288. {
  289. dRules.Text = selectedRow.Cells[1].Value.ToString();
  290. curRule = selectedRow.Cells[2].Value.ToString();
  291. curRuleLeng = selectedRow.Cells[4].Value.ToString();
  292. }
  293. }
  294. }
  295. }
  296. private void boxBtn_Click(object sender, EventArgs e)
  297. {
  298. BoxNo.Enabled = true;
  299. BoxNo.Focus();
  300. BoxNo.SelectAll();
  301. }
  302. private void settingRightInput1_Click(object sender, EventArgs e)
  303. {
  304. using (OpenFileDialog openFileDialog = new OpenFileDialog())
  305. {
  306. openFileDialog.Title = "请选择告警文件";
  307. openFileDialog.Filter = "可执行文件 (*.wav)|*.wav|所有文件 (*.*)|*.*";
  308. openFileDialog.FilterIndex = 1;
  309. openFileDialog.RestoreDirectory = true;
  310. if (!string.IsNullOrEmpty(settingRightInput1.Text) && System.IO.File.Exists(settingRightInput1.Text))
  311. {
  312. openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(settingRightInput1.Text);
  313. }
  314. else
  315. {
  316. openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
  317. }
  318. if (openFileDialog.ShowDialog() == DialogResult.OK)
  319. {
  320. settingRightInput1.Text = openFileDialog.FileName;
  321. }
  322. }
  323. }
  324. private void settingRightInput2_Click(object sender, EventArgs e)
  325. {
  326. using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
  327. {
  328. folderBrowserDialog.Description = "请选择导出文件夹";
  329. if (!string.IsNullOrEmpty(settingRightInput2.Text) && System.IO.Directory.Exists(settingRightInput2.Text))
  330. {
  331. folderBrowserDialog.SelectedPath = settingRightInput2.Text;
  332. }
  333. else
  334. {
  335. folderBrowserDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // folderBrowserDialog.SelectedPath = "";
  336. }
  337. if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
  338. {
  339. settingRightInput2.Text = folderBrowserDialog.SelectedPath;
  340. }
  341. }
  342. }
  343. private void QDdtp1_ValueChanged(object sender, EventArgs e)
  344. {
  345. if (QDdtp1.Value > QDdtp2.Value)
  346. {
  347. QDdtp1.Value = QDdtp2.Value.AddDays(-1);
  348. MessageBox.Show("时间不得大于最大时间", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  349. return;
  350. }
  351. minTime = QDdtp1.Value.ToString("yyyy-mm-dd hh:mm:ss");
  352. }
  353. private void DRulesBtn_Click(object sender, EventArgs e)
  354. {
  355. if (string.IsNullOrEmpty(dRules.Text))
  356. {
  357. MessageBox.Show("请选择条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  358. return;
  359. }
  360. dRules.Enabled = false;
  361. }
  362. private void DRulesBtn1_Click(object sender, EventArgs e)
  363. {
  364. dRules.Enabled = true;
  365. }
  366. private void QDdtp2_ValueChanged(object sender, EventArgs e)
  367. {
  368. if (QDdtp2.Value < QDdtp1.Value)
  369. {
  370. QDdtp2.Value = QDdtp1.Value.AddDays(1);
  371. MessageBox.Show("时间不得小于最小时间", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  372. return;
  373. }
  374. maxTime = QDdtp2.Value.ToString("yyyy-mm-dd hh:mm:ss");
  375. }
  376. private void PlaySound(string type)
  377. {
  378. Task.Run(() => {
  379. if (type == "OK")
  380. {
  381. using (var stream = UAS_Tools_HY.Properties.Resources.OK)
  382. {
  383. SoundPlayer player = new SoundPlayer(stream);
  384. player.Play();
  385. }
  386. }
  387. else
  388. {
  389. if (string.IsNullOrEmpty(settingRightInput1.Text))
  390. {
  391. using (var stream = UAS_Tools_HY.Properties.Resources.NG)
  392. {
  393. SoundPlayer player = new SoundPlayer(stream);
  394. player.Play();
  395. }
  396. }
  397. else
  398. {
  399. string customPath = settingRightInput1.Text.Trim();
  400. if (File.Exists(customPath))
  401. {
  402. SoundPlayer player = new SoundPlayer(customPath);
  403. player.Play();
  404. }
  405. }
  406. }
  407. });
  408. }
  409. public void loadTime_Tick(object sender, EventArgs e)
  410. {
  411. string currentDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  412. currTime.Text = currentDate;
  413. }
  414. }
  415. }