Main.cs 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. using Microsoft.Office.Interop.Excel;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.IO.Compression;
  11. using System.Linq;
  12. using System.Media;
  13. using System.Net;
  14. using System.Runtime.InteropServices;
  15. using System.Security.Principal;
  16. using System.Text;
  17. using System.Text.RegularExpressions;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using System.Windows.Forms;
  21. using UAS_Tools_HY;
  22. using UAS_Tools_HY.Properties;
  23. using UAS_Tools_HY.PublicMethods;
  24. using Action = System.Action;
  25. using Application = Microsoft.Office.Interop.Excel.Application;
  26. using DataTable = System.Data.DataTable;
  27. using Timer = System.Windows.Forms.Timer;
  28. namespace UAS_MES_Tools
  29. {
  30. public partial class Main : Form
  31. {
  32. string _Account, _Password, _UserName;
  33. DataTable dt;
  34. Timer loadTime;
  35. string minTime, maxTime;
  36. string curRuleId, curRule, curRuleLeng;
  37. int ishaveInd;
  38. Timer File_timer;
  39. Timer File_timer1;
  40. string saveFiles;
  41. int currentNum = 1;
  42. private CancellationTokenSource _currentOperationCts;
  43. public Main(string account, string password)
  44. {
  45. _Account = account;
  46. _Password = password;
  47. InitializeComponent();
  48. }
  49. private void Main_Load(object sender, EventArgs e)
  50. {
  51. currUser.Text = "当前账户: " + _Account;
  52. dt = ConnectDB.ExecuteSelect($"select * from employee where em_code = '{_Account}'");
  53. if (dt.Rows.Count > 0)
  54. {
  55. _UserName = dt.Rows[0]["em_name"].ToString();
  56. }
  57. loadTime_Tick(null, null);
  58. loadTime = new Timer();
  59. loadTime.Interval = 1000;
  60. loadTime.Tick += loadTime_Tick;
  61. loadTime.Start();
  62. SN.SelectAll();
  63. SN.Focus();
  64. settingLeftInput1.Text = BaseUtil.GetCacheData("SettingCount").ToString();
  65. settingLeftInput2.Text = BaseUtil.GetCacheData("SettingVolume").ToString();
  66. if (string.IsNullOrEmpty(BaseUtil.GetCacheData("SettingVolume").ToString())) dSum.Text = "0";
  67. else dSum.Text = BaseUtil.GetCacheData("SettingVolume").ToString();
  68. settingRightInput1.Text = BaseUtil.GetCacheData("SettingNGPath").ToString();
  69. settingRightInput2.Text = BaseUtil.GetCacheData("SettingExportPath").ToString();
  70. QDcheckbox1.Checked = true;
  71. QDdtp2.Value = QDdtp1.Value.AddDays(1);
  72. rulesQuery_Click(null, null);
  73. QDquery_Click(null, null);
  74. saveFiles = Path.Combine(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).FullName, $"FtpFiles");
  75. if (!Directory.Exists(saveFiles))
  76. {
  77. Directory.CreateDirectory(saveFiles);
  78. }
  79. //saveFiles = Path.Combine(saveFiles, DateTime.Now.ToString("yyyyMMdd"));
  80. //if (!Directory.Exists(saveFiles))
  81. //{
  82. // Directory.CreateDirectory(saveFiles);
  83. //}
  84. File_timer = new Timer();
  85. File_timer.Interval = 30 * 1000;
  86. File_timer.Tick += Timer_Tick;
  87. timerVal.Text = "30";
  88. FileCountVal.Text = "10";
  89. FtpPathVal.Text = "ftp://10.18.13.48:21";
  90. handExeVal.Text = "120";
  91. File_timer1 = new Timer();
  92. File_timer1.Interval = Convert.ToInt32(handExeVal.Text) * 1000;
  93. File_timer1.Tick += FileTimer_Tick;
  94. }
  95. private void SN_KeyDown(object sender, KeyEventArgs e)
  96. {
  97. if (e.KeyCode != Keys.Enter) return;
  98. if(checkPagination.SelectedIndex != 0) checkPagination.SelectedIndex = 0;
  99. if (CheckEnter()) return;
  100. bool checkRes = true;
  101. if (radio1.Checked)
  102. {
  103. dt = ConnectDB.ExecuteSelect($@"SELECT COUNT(1) sum FROM dcrlogexcel WHERE item = '{SN.Text.Trim()}'");
  104. if (dt.Rows[0]["sum"].ToString() == "0")
  105. {
  106. PlaySound("NG");
  107. MessageBox.Show($"{SN.Text.Trim()},没有DCR测试数据", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  108. checkRes = false;
  109. return;
  110. }
  111. }
  112. if (FormMethods.IsCheckSNRules(curRule, SN.Text.Trim(), curRuleLeng))
  113. {
  114. PlaySound("NG");
  115. MessageBox.Show($"{SN.Text.Trim()},不符合条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  116. checkRes = false;
  117. return;
  118. }
  119. dt = ConnectDB.ExecuteSelect($@"select * from g_packing_sncheck where sn = '{SN.Text.Trim()}'");
  120. if (dt.Rows.Count > 0)
  121. {
  122. PlaySound("NG");
  123. MessageBox.Show($"{SN.Text.Trim()},历史记录中已核对", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  124. checkRes = false;
  125. return;
  126. }
  127. ishaveInd = -1;
  128. if (Datas.Rows.Count > 0)
  129. {
  130. foreach (DataGridViewRow item in Datas.Rows)
  131. {
  132. if (SN.Text.Trim() == item.Cells[0].Value.ToString())
  133. {
  134. ishaveInd = Datas.Rows.IndexOf(item);
  135. }
  136. }
  137. }
  138. if (ishaveInd >= 0)
  139. {
  140. PlaySound("NG");
  141. MessageBox.Show($"{SN.Text.Trim()},已核对装箱", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  142. checkRes = false;
  143. return;
  144. /*int iCou = Convert.ToInt32(Datas.Rows[ishaveInd].Cells[2].Value);
  145. iCou += 1;
  146. Datas.Rows[ishaveInd].Cells[3].Value = currTime.Text;
  147. Datas.Rows[ishaveInd].Cells[6].Value = checkRes ? "OK" : "NG";
  148. PlaySound("OK");*/
  149. }
  150. else
  151. {
  152. if (string.IsNullOrEmpty(BoxNo.Text) && string.IsNullOrEmpty(BaseUtil.GetCacheData("VisualOutboxNo").ToString()))
  153. {
  154. BaseUtil.SetCacheData("VisualOutboxNo", DateTime.Now.ToString("yyyyMMddHHmmss"));
  155. BoxNo_KeyDown(null, new KeyEventArgs(Keys.Enter));
  156. }
  157. DataGridViewRow row = new DataGridViewRow();
  158. row.CreateCells(Datas);
  159. row.Cells[0].Value = SN.Text.Trim();
  160. row.Cells[1].Value = BoxNo.Text.Trim();
  161. row.Cells[2].Value = 1;
  162. row.Cells[3].Value = currTime.Text;
  163. row.Cells[4].Value = _UserName;
  164. row.Cells[5].Value = radio1.Checked ? "是" : "否";
  165. row.Cells[6].Value = checkRes ? "OK" : "NG";
  166. row.Cells[7].Value = string.IsNullOrEmpty(BoxNo.Text) ? BaseUtil.GetCacheData("VisualOutboxNo") : "";
  167. Datas.Rows.Add(row);
  168. PlaySound("OK");
  169. Datas.FirstDisplayedScrollingRowIndex = Datas.Rows.Count - 1;
  170. try
  171. {
  172. int typeNum = ChangeDetail(row);
  173. if (typeNum == 1)
  174. {
  175. SN.Focus();
  176. SN.SelectAll();
  177. SN.Text = "";
  178. }
  179. else if (typeNum == 2)
  180. {
  181. SN.Text = "";
  182. dCount.Text = "0";
  183. BoxNo.Enabled = true;
  184. BoxNo.Focus();
  185. BoxNo.SelectAll();
  186. //BoxNo.Text = "";
  187. //Datas.Rows.Clear();
  188. BaseUtil.SetCacheData("VisualOutboxNo", "");
  189. Application excelApp = null;
  190. Workbook workbook = null;
  191. Worksheet worksheet = null;
  192. Range range = null;
  193. string tempTemplatePath = null;
  194. try
  195. {
  196. string visualOutboxNo = string.IsNullOrEmpty(row.Cells[1].Value.ToString()) ? row.Cells[7].Value.ToString() : row.Cells[1].Value.ToString();
  197. string generateFileName = string.IsNullOrEmpty(row.Cells[1].Value.ToString()) ? DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") : row.Cells[1].Value.ToString();
  198. string checkExportModel = Path.Combine(settingRightInput2.Text, generateFileName + ".xlsx");
  199. if (string.IsNullOrEmpty(settingRightInput2.Text) || !Directory.Exists(settingRightInput2.Text))
  200. {
  201. MessageBox.Show($"输出目录不存在: {settingRightInput2.Text}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  202. return;
  203. }
  204. byte[] templateBytes = Resources.checkExportModel;
  205. if (templateBytes == null || templateBytes.Length == 0)
  206. {
  207. MessageBox.Show("未能加载模板文件", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  208. return;
  209. }
  210. tempTemplatePath = Path.GetTempFileName().Replace(".tmp", ".xlsx");
  211. File.WriteAllBytes(tempTemplatePath, templateBytes);
  212. excelApp = new Application();
  213. excelApp.Visible = false;
  214. excelApp.DisplayAlerts = false;
  215. workbook = excelApp.Workbooks.Open(tempTemplatePath);
  216. object sheetIndex = 1;
  217. worksheet = (Worksheet)workbook.Worksheets[sheetIndex];
  218. dt = ConnectDB.ExecuteSelect($@"select * from g_packing_sncheck where outbox_no = '{visualOutboxNo}'");
  219. if (dt.Rows.Count > 0)
  220. {
  221. worksheet.Cells[1, 3] = DateTime.Now.ToString("yyyy-MM-dd");
  222. worksheet.Cells[1, 7] = visualOutboxNo;
  223. int startRow = 3;
  224. int startColumn = 2;
  225. int currInd = 0;
  226. int dataRowsCount = dt.Rows.Count;
  227. foreach (DataRow item in dt.Rows)
  228. {
  229. worksheet.Cells[startRow + currInd, startColumn - 1] = currInd + 1;
  230. worksheet.Cells[startRow + currInd, startColumn] = item["SN"].ToString();
  231. if (currInd > 23)
  232. {
  233. currInd = 0;
  234. startColumn += 2;
  235. }
  236. else
  237. {
  238. currInd += 1;
  239. }
  240. }
  241. int endRow = startRow + 25;
  242. range = worksheet.Range[worksheet.Cells[startRow, 1], worksheet.Cells[endRow, startColumn]];
  243. range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
  244. range.VerticalAlignment = XlVAlign.xlVAlignCenter;
  245. }
  246. worksheet.Columns.AutoFit();
  247. workbook.SaveAs(
  248. Filename: checkExportModel,
  249. FileFormat: XlFileFormat.xlOpenXMLWorkbook,
  250. Password: Type.Missing,
  251. WriteResPassword: Type.Missing,
  252. ReadOnlyRecommended: Type.Missing,
  253. CreateBackup: Type.Missing,
  254. AccessMode: XlSaveAsAccessMode.xlExclusive,
  255. ConflictResolution: Type.Missing,
  256. AddToMru: false,
  257. TextCodepage: Type.Missing,
  258. TextVisualLayout: Type.Missing,
  259. Local: Type.Missing
  260. );
  261. MessageBox.Show($"Excel文件已成功导出到: {checkExportModel}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  262. }
  263. catch (Exception ex)
  264. {
  265. MessageBox.Show($"生成 Excel 文件时出错: {ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  266. }
  267. finally
  268. {
  269. // 正确释放COM对象
  270. if (worksheet != null)
  271. {
  272. try
  273. {
  274. Marshal.ReleaseComObject(worksheet);
  275. }
  276. catch { }
  277. worksheet = null;
  278. }
  279. if (workbook != null)
  280. {
  281. try
  282. {
  283. workbook.Close(false);
  284. Marshal.ReleaseComObject(workbook);
  285. }
  286. catch { }
  287. workbook = null;
  288. }
  289. if (excelApp != null)
  290. {
  291. try
  292. {
  293. excelApp.Quit();
  294. Marshal.ReleaseComObject(excelApp);
  295. }
  296. catch { }
  297. excelApp = null;
  298. }
  299. if (!string.IsNullOrEmpty(tempTemplatePath) && File.Exists(tempTemplatePath))
  300. {
  301. try
  302. {
  303. File.Delete(tempTemplatePath);
  304. }
  305. catch (Exception ex)
  306. {
  307. }
  308. }
  309. GC.Collect();
  310. GC.WaitForPendingFinalizers();
  311. }
  312. }
  313. QDquery_Click(null, null);
  314. if (Datas.Rows.Count == Convert.ToInt32(settingLeftInput1.Text))
  315. {
  316. Datas.Rows.Clear();
  317. }
  318. }
  319. catch (Exception ex)
  320. {
  321. MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  322. return;
  323. }
  324. }
  325. }
  326. private int ChangeDetail(DataGridViewRow row)
  327. {
  328. int inputSum = Convert.ToInt32(dCount.Text);
  329. dCount.Text = (inputSum + 1).ToString();
  330. string visualOutboxNo = string.IsNullOrEmpty(row.Cells[1].Value.ToString()) ? row.Cells[7].Value.ToString() : row.Cells[1].Value.ToString();
  331. int insertCount = ConnectDB.ExecuteInsert($@"INSERT INTO g_packing_sncheck (work_order,part_no,sn,outbox_no,
  332. count,update_time,update_name,check_id,rule_name,rule_value,capacity,ISCHECHK_DCR,rule_id)
  333. VALUES ( '', '', '{row.Cells[0].Value}', '{visualOutboxNo}',
  334. '{row.Cells[2].Value}',sysdate,'{_UserName}',packcheckid_seq.NEXTVAL,'{dRules.Text.Trim()}','{curRule}','{dSum.Text}','{row.Cells[5].Value}','{curRuleId}' )");
  335. if (Convert.ToInt32(dCount.Text) < Convert.ToInt32(dSum.Text))
  336. {
  337. return 1;
  338. }
  339. else
  340. {
  341. return 2;
  342. }
  343. //if(Convert.ToInt32(dCount.Text) == Convert.ToInt32(dSum.Text))
  344. //{
  345. // foreach (DataGridViewRow item in Datas.Rows)
  346. // {
  347. // int insertCount = ConnectDB.ExecuteInsert($@"INSERT INTO g_packing_sncheck (work_order,part_no,sn,outbox_no,
  348. // count,update_time,update_name,check_id,rule_name,rule_value)
  349. // VALUES ( '', '', '{item.Cells[0].Value}', '{item.Cells[1].Value}',
  350. // '{item.Cells[2].Value}',sysdate,'{_UserName}',packcheckid_seq.NEXTVAL,'{dRules.Text.Trim()}','{curRule}' )");
  351. // }
  352. // return true;
  353. //}
  354. //else
  355. //{
  356. // return false;
  357. //}
  358. }
  359. private void BoxNo_Leave(object sender, EventArgs e)
  360. {
  361. if (!string.IsNullOrEmpty(BoxNo.Text))
  362. {
  363. KeyEventArgs KeyEventArg = new KeyEventArgs(Keys.Enter);
  364. BoxNo_KeyDown(null, KeyEventArg);
  365. }
  366. }
  367. private void BoxNo_KeyDown(object sender, KeyEventArgs e)
  368. {
  369. if (e.KeyCode != Keys.Enter) return;
  370. string visualOutboxNo = string.IsNullOrEmpty(BoxNo.Text) ? BaseUtil.GetCacheData("VisualOutboxNo").ToString() : BoxNo.Text.Trim();
  371. dt = ConnectDB.ExecuteSelect($@"SELECT * FROM g_packing_sncheck a,g_packing_rules b WHERE a.outbox_no = '{visualOutboxNo}' AND a.rule_id = b.rule_id");
  372. if (dt.Rows.Count > 0)
  373. {
  374. if (Convert.ToInt32(dt.Rows[0]["CAPACITY"].ToString()) > dt.Rows.Count)
  375. {
  376. dRules.Text = dt.Rows[0]["RULE_NAME"].ToString();
  377. curRule = dt.Rows[0]["RULE_VALUE"].ToString();
  378. curRuleId = dt.Rows[0]["RULE_ID"].ToString();
  379. curRuleLeng = dt.Rows[0]["RULE_LENGTH"].ToString();
  380. dRules.Enabled = false;
  381. radio1.Checked = dt.Rows[0]["ISCHECHK_DCR"].ToString() == "是" ? true : false;
  382. dCount.Text = dt.Rows.Count.ToString();
  383. settingLeftInput2.Text = dt.Rows[0]["CAPACITY"].ToString();
  384. dSum.Text = dt.Rows[0]["CAPACITY"].ToString();
  385. BaseUtil.SetCacheData("SettingVolume", dt.Rows[0]["CAPACITY"].ToString());
  386. Datas.Rows.Clear();
  387. foreach (DataRow item in dt.Rows)
  388. {
  389. DataGridViewRow row = new DataGridViewRow();
  390. row.CreateCells(Datas);
  391. row.Cells[0].Value = item["SN"].ToString();
  392. row.Cells[1].Value = item["OUTBOX_NO"].ToString();
  393. row.Cells[2].Value = item["COUNT"].ToString();
  394. row.Cells[3].Value = item["UPDATE_TIME"].ToString();
  395. row.Cells[4].Value = item["UPDATE_NAME"].ToString();
  396. row.Cells[5].Value = item["ISCHECHK_DCR"].ToString();
  397. row.Cells[6].Value = "OK";
  398. Datas.Rows.Add(row);
  399. }
  400. }
  401. else
  402. {
  403. BoxNo.Focus();
  404. BoxNo.SelectAll();
  405. MessageBox.Show($"核对完成,此箱{visualOutboxNo} 已装满{dt.Rows[0]["CAPACITY"].ToString()} 个,请扫描未核对箱号", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  406. BoxNo.Text = "";
  407. return;
  408. }
  409. }
  410. if (CheckEnter())
  411. {
  412. BoxNo.Text = "";
  413. return;
  414. }
  415. /*if (Datas.Rows.Count > 0)
  416. {
  417. foreach(DataGridViewRow item in Datas.Rows)
  418. {
  419. item.Cells[1].Value = BoxNo.Text.Trim();
  420. }
  421. }*/
  422. BoxNo.Enabled = false;
  423. SN.SelectAll();
  424. SN.Focus();
  425. }
  426. private bool CheckEnter()
  427. {
  428. if (string.IsNullOrEmpty(settingLeftInput1.Text))
  429. {
  430. MessageBox.Show("请维护列表记录数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  431. return true;
  432. }
  433. if (string.IsNullOrEmpty(settingLeftInput2.Text))
  434. {
  435. MessageBox.Show("请维护箱内容量", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  436. return true;
  437. }
  438. if (!Regex.IsMatch(dSum.Text, @"^[1-9]\d*$"))
  439. {
  440. MessageBox.Show("箱内容量维护应为正整数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  441. return true;
  442. }
  443. if (string.IsNullOrEmpty(dRules.Text))
  444. {
  445. MessageBox.Show("请选择条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  446. return true;
  447. }
  448. if (dRules.Enabled)
  449. {
  450. MessageBox.Show("请选择确认条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  451. return true;
  452. }
  453. return false;
  454. }
  455. private void settingLeftSubmit_Click(object sender, EventArgs e)
  456. {
  457. if (string.IsNullOrEmpty(settingLeftInput1.Text))
  458. {
  459. MessageBox.Show("请输入列表记录数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  460. return;
  461. }
  462. if (string.IsNullOrEmpty(settingLeftInput2.Text))
  463. {
  464. MessageBox.Show("请输入箱内容量", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  465. return;
  466. }
  467. if (Convert.ToInt32(dCount.Text) > 0)
  468. {
  469. if(Convert.ToInt32(dSum.Text) > Convert.ToInt32(settingLeftInput2.Text))
  470. {
  471. MessageBox.Show($"已扫描{Convert.ToInt32(dCount.Text)} 个产品,修改箱容量需大于{Convert.ToInt32(settingLeftInput2.Text)} ", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  472. return;
  473. }
  474. }
  475. BaseUtil.SetCacheData("SettingCount", settingLeftInput1.Text);
  476. BaseUtil.SetCacheData("SettingVolume", settingLeftInput2.Text);
  477. dSum.Text = BaseUtil.GetCacheData("SettingVolume").ToString();
  478. MessageBox.Show("已提交扫描设置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  479. }
  480. private void settingRightSubmit_Click(object sender, EventArgs e)
  481. {
  482. if (string.IsNullOrEmpty(settingRightInput2.Text))
  483. {
  484. MessageBox.Show("请选择导出文件目录", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  485. return;
  486. }
  487. if(!string.IsNullOrEmpty(settingRightInput1.Text)) BaseUtil.SetCacheData("SettingNGPath", settingRightInput1.Text);
  488. BaseUtil.SetCacheData("SettingExportPath", settingRightInput2.Text);
  489. MessageBox.Show("已提交文件设置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  490. }
  491. private void QDquery_Click(object sender, EventArgs e)
  492. {
  493. StringBuilder filterStr = new StringBuilder();
  494. if (!string.IsNullOrEmpty(QDInput1.Text))
  495. {
  496. filterStr.Append($"WHERE instr(SN,'{QDInput1.Text}') > 0");
  497. }
  498. if (!string.IsNullOrEmpty(QDInput2.Text))
  499. {
  500. if (filterStr.Length == 0)
  501. {
  502. filterStr.Append($"WHERE instr(OUTBOX_NO,'{QDInput2.Text}') > 0");
  503. }
  504. else
  505. {
  506. filterStr.Append($" and instr(OUTBOX_NO, '{QDInput2.Text}') > 0");
  507. }
  508. }
  509. if (QDcheckbox1.Checked)
  510. {
  511. string dtp1 = QDdtp1.Value.ToString("yyyy-MM-dd 00:00:00");
  512. string dtp2 = QDdtp2.Value.ToString("yyyy-MM-dd 00:00:00");
  513. if (filterStr.Length == 0)
  514. {
  515. filterStr.Append($"WHERE update_time > TO_DATE('{dtp1}','YYYY-MM-DD HH24:MI:SS') AND update_time <= TO_DATE('{dtp2}','YYYY-MM-DD HH24:MI:SS')");
  516. }
  517. else
  518. {
  519. filterStr.Append($" AND update_time > TO_DATE('{dtp1}','YYYY-MM-DD HH24:MI:SS') AND update_time <= TO_DATE('{dtp2}','YYYY-MM-DD HH24:MI:SS')");
  520. }
  521. }
  522. if(sender == null)
  523. {
  524. Task.Run(() =>
  525. {
  526. dt = ConnectDB.ExecuteSelect($@"SELECT sn dqsn,outbox_no dqoutbox_no,count dqcount,
  527. update_time dqupdate_time,update_name dqname,rule_name dqrule_name,rule_value dqrule_value
  528. FROM g_packing_sncheck {filterStr.ToString()} ORDER BY update_time desc");
  529. this.Invoke(new Action(() =>
  530. {
  531. QDDatas.DataSource = dt;
  532. }));
  533. });
  534. }
  535. else
  536. {
  537. Loading LoadingForm = new Loading(filterStr.ToString());
  538. if (LoadingForm.ShowDialog() == DialogResult.OK)
  539. {
  540. DataTable dt = LoadingForm.ResultData;
  541. QDDatas.DataSource = dt;
  542. }
  543. }
  544. }
  545. private void QDexport_Click(object sender, EventArgs e)
  546. {
  547. if(QDDatas.Rows.Count == 0)
  548. {
  549. MessageBox.Show("请先查询出导出数据", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  550. return;
  551. }
  552. if (string.IsNullOrEmpty(settingRightInput2.Text))
  553. {
  554. MessageBox.Show("请先在设置中选择导出路径", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  555. return;
  556. }
  557. Application excelApp = null;
  558. Workbook workbook = null;
  559. Worksheet worksheet = null;
  560. Range range = null;
  561. string tempTemplatePath = null;
  562. try
  563. {
  564. string exportPath = Path.Combine(settingRightInput2.Text, DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".xlsx");
  565. if (string.IsNullOrEmpty(settingRightInput2.Text) || !Directory.Exists(settingRightInput2.Text))
  566. {
  567. MessageBox.Show($"输出目录不存在: {settingRightInput2.Text}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  568. return;
  569. }
  570. byte[] templateBytes = Resources.exportModel; // 假设Resources.exportModel是有效的资源
  571. if (templateBytes == null || templateBytes.Length == 0)
  572. {
  573. MessageBox.Show("未能加载模板文件", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  574. return;
  575. }
  576. tempTemplatePath = Path.GetTempFileName().Replace(".tmp", ".xlsx");
  577. File.WriteAllBytes(tempTemplatePath, templateBytes);
  578. excelApp = new Application();
  579. excelApp.Visible = false;
  580. excelApp.DisplayAlerts = false;
  581. workbook = excelApp.Workbooks.Open(tempTemplatePath);
  582. object sheetIndex = 1;
  583. worksheet = (Worksheet)workbook.Worksheets[sheetIndex];
  584. int startRow = 2;
  585. int dataRowsCount = QDDatas.Rows.Count;
  586. for (int i = 0; i < QDDatas.Rows.Count; i++)
  587. {
  588. worksheet.Cells[startRow + i, 1] = i + 1; // 改为从1开始编号,更符合Excel习惯
  589. worksheet.Cells[startRow + i, 2] = QDDatas.Rows[i].Cells[0].Value?.ToString();
  590. }
  591. int endRow = startRow + dataRowsCount - 1;
  592. range = worksheet.Range[worksheet.Cells[startRow, 1], worksheet.Cells[endRow, 2]];
  593. range.HorizontalAlignment = XlHAlign.xlHAlignCenter; // 水平居中
  594. range.VerticalAlignment = XlVAlign.xlVAlignCenter; // 垂直居中
  595. worksheet.Columns.AutoFit();
  596. // 修复SaveAs方法,使用正确的参数
  597. workbook.SaveAs(
  598. Filename: exportPath,
  599. FileFormat: XlFileFormat.xlOpenXMLWorkbook,
  600. Password: Type.Missing,
  601. WriteResPassword: Type.Missing,
  602. ReadOnlyRecommended: Type.Missing,
  603. CreateBackup: Type.Missing,
  604. AccessMode: XlSaveAsAccessMode.xlExclusive,
  605. ConflictResolution: Type.Missing,
  606. AddToMru: false,
  607. TextCodepage: Type.Missing,
  608. TextVisualLayout: Type.Missing,
  609. Local: Type.Missing
  610. );
  611. MessageBox.Show($"Excel文件已成功导出到: {exportPath}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  612. }
  613. catch (Exception ex)
  614. {
  615. MessageBox.Show($"生成 Excel 文件时出错: {ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  616. }
  617. finally
  618. {
  619. // 正确释放COM对象
  620. if (worksheet != null)
  621. {
  622. try
  623. {
  624. Marshal.ReleaseComObject(worksheet);
  625. }
  626. catch { }
  627. worksheet = null;
  628. }
  629. if (workbook != null)
  630. {
  631. try
  632. {
  633. workbook.Close(false);
  634. Marshal.ReleaseComObject(workbook);
  635. }
  636. catch { }
  637. workbook = null;
  638. }
  639. if (excelApp != null)
  640. {
  641. try
  642. {
  643. excelApp.Quit();
  644. Marshal.ReleaseComObject(excelApp);
  645. }
  646. catch { }
  647. excelApp = null;
  648. }
  649. if (!string.IsNullOrEmpty(tempTemplatePath) && File.Exists(tempTemplatePath))
  650. {
  651. try
  652. {
  653. File.Delete(tempTemplatePath);
  654. }
  655. catch (Exception ex)
  656. {
  657. }
  658. }
  659. GC.Collect();
  660. GC.WaitForPendingFinalizers();
  661. }
  662. }
  663. private void rulesAdd_Click(object sender, EventArgs e)
  664. {
  665. StringBuilder filterStr = new StringBuilder();
  666. if (!string.IsNullOrEmpty(rulesInput1.Text))
  667. {
  668. filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
  669. }
  670. if (!string.IsNullOrEmpty(rulesInput2.Text))
  671. {
  672. if(filterStr.Length == 0)
  673. {
  674. filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
  675. }
  676. else
  677. {
  678. filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
  679. }
  680. }
  681. Form rulesList = new RulesList("Add",_UserName);
  682. if (rulesList.ShowDialog() == DialogResult.OK)
  683. {
  684. MessageBox.Show("新增规则成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  685. dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
  686. 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");
  687. RulesDatas.DataSource = dt;
  688. }
  689. }
  690. private void rulesQuery_Click(object sender, EventArgs e)
  691. {
  692. StringBuilder filterStr = new StringBuilder();
  693. if (!string.IsNullOrEmpty(rulesInput1.Text))
  694. {
  695. filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
  696. }
  697. if (!string.IsNullOrEmpty(rulesInput2.Text))
  698. {
  699. if (filterStr.Length == 0)
  700. {
  701. filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
  702. }
  703. else
  704. {
  705. filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
  706. }
  707. }
  708. dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
  709. 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");
  710. RulesDatas.DataSource = dt;
  711. }
  712. private void RulesDatas_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
  713. {
  714. if (e.RowIndex >= 0 && !RulesDatas.Rows[e.RowIndex].IsNewRow)
  715. {
  716. DataGridView dgv = sender as DataGridView;
  717. if (dgv != null)
  718. {
  719. DataGridViewRow selectedRow = dgv.Rows[e.RowIndex];
  720. string RULE_ID = selectedRow.Cells[0].Value.ToString();
  721. StringBuilder filterStr = new StringBuilder();
  722. if (!string.IsNullOrEmpty(rulesInput1.Text))
  723. {
  724. filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
  725. }
  726. if (!string.IsNullOrEmpty(rulesInput2.Text))
  727. {
  728. if (filterStr.Length == 0)
  729. {
  730. filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
  731. }
  732. else
  733. {
  734. filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
  735. }
  736. }
  737. Form rulesList = new RulesList("Modify",RULE_ID, _UserName);
  738. if (rulesList.ShowDialog() == DialogResult.OK)
  739. {
  740. MessageBox.Show("修改规则成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  741. dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
  742. 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");
  743. RulesDatas.DataSource = dt;
  744. }
  745. }
  746. }
  747. }
  748. private void RulesDatas_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  749. {
  750. if (e.RowIndex >= 0 && !RulesDatas.Rows[e.RowIndex].IsNewRow)
  751. {
  752. DataGridView dgv = sender as DataGridView;
  753. if (dgv != null)
  754. {
  755. DataGridViewRow selectedRow = dgv.Rows[e.RowIndex];
  756. if (dRules.Enabled)
  757. {
  758. dRules.Text = selectedRow.Cells[1].Value.ToString();
  759. curRuleId = selectedRow.Cells[0].Value.ToString();
  760. curRule = selectedRow.Cells[2].Value.ToString();
  761. curRuleLeng = selectedRow.Cells[4].Value.ToString();
  762. }
  763. }
  764. }
  765. }
  766. private void boxBtn_Click(object sender, EventArgs e)
  767. {
  768. BoxNo.Enabled = true;
  769. BoxNo.Focus();
  770. BoxNo.SelectAll();
  771. }
  772. private void settingRightInput1_Click(object sender, EventArgs e)
  773. {
  774. using (OpenFileDialog openFileDialog = new OpenFileDialog())
  775. {
  776. openFileDialog.Title = "请选择告警文件";
  777. openFileDialog.Filter = "可执行文件 (*.wav)|*.wav|所有文件 (*.*)|*.*";
  778. openFileDialog.FilterIndex = 1;
  779. openFileDialog.RestoreDirectory = true;
  780. if (!string.IsNullOrEmpty(settingRightInput1.Text) && System.IO.File.Exists(settingRightInput1.Text))
  781. {
  782. openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(settingRightInput1.Text);
  783. }
  784. else
  785. {
  786. openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
  787. }
  788. if (openFileDialog.ShowDialog() == DialogResult.OK)
  789. {
  790. settingRightInput1.Text = openFileDialog.FileName;
  791. }
  792. }
  793. }
  794. private void settingRightInput2_Click(object sender, EventArgs e)
  795. {
  796. using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
  797. {
  798. folderBrowserDialog.Description = "请选择导出文件夹";
  799. if (!string.IsNullOrEmpty(settingRightInput2.Text) && System.IO.Directory.Exists(settingRightInput2.Text))
  800. {
  801. folderBrowserDialog.SelectedPath = settingRightInput2.Text;
  802. }
  803. else
  804. {
  805. folderBrowserDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // folderBrowserDialog.SelectedPath = "";
  806. }
  807. if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
  808. {
  809. settingRightInput2.Text = folderBrowserDialog.SelectedPath;
  810. }
  811. }
  812. }
  813. private void QDdtp1_ValueChanged(object sender, EventArgs e)
  814. {
  815. if (QDdtp1.Value > QDdtp2.Value)
  816. {
  817. QDdtp1.Value = QDdtp2.Value.AddDays(-1);
  818. MessageBox.Show("时间不得大于最大时间", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  819. return;
  820. }
  821. minTime = QDdtp1.Value.ToString("yyyy-mm-dd hh:mm:ss");
  822. }
  823. private void DRulesBtn_Click(object sender, EventArgs e)
  824. {
  825. if (string.IsNullOrEmpty(dRules.Text))
  826. {
  827. MessageBox.Show("请选择条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  828. return;
  829. }
  830. dRules.Enabled = false;
  831. }
  832. private void DRulesBtn1_Click(object sender, EventArgs e)
  833. {
  834. dRules.Enabled = true;
  835. }
  836. private void QDdtp2_ValueChanged(object sender, EventArgs e)
  837. {
  838. if (QDdtp2.Value < QDdtp1.Value)
  839. {
  840. QDdtp2.Value = QDdtp1.Value.AddDays(1);
  841. MessageBox.Show("时间不得小于最小时间", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  842. return;
  843. }
  844. maxTime = QDdtp2.Value.ToString("yyyy-mm-dd hh:mm:ss");
  845. }
  846. private void PlaySound(string type)
  847. {
  848. if (type == "OK")
  849. {
  850. ShowMsg.Text = "Pass";
  851. ShowMsg.BackColor = Color.LawnGreen;
  852. }
  853. else
  854. {
  855. ShowMsg.Text = "Fail";
  856. ShowMsg.BackColor = Color.Red;
  857. }
  858. Task.Run(() => {
  859. if (type == "OK")
  860. {
  861. using (var stream = UAS_Tools_HY.Properties.Resources.OK)
  862. {
  863. SoundPlayer player = new SoundPlayer(stream);
  864. player.Play();
  865. }
  866. }
  867. else
  868. {
  869. if (string.IsNullOrEmpty(settingRightInput1.Text))
  870. {
  871. using (var stream = UAS_Tools_HY.Properties.Resources.NG)
  872. {
  873. SoundPlayer player = new SoundPlayer(stream);
  874. player.Play();
  875. }
  876. }
  877. else
  878. {
  879. string customPath = settingRightInput1.Text.Trim();
  880. if (File.Exists(customPath))
  881. {
  882. SoundPlayer player = new SoundPlayer(customPath);
  883. player.Play();
  884. }
  885. }
  886. }
  887. });
  888. }
  889. public void loadTime_Tick(object sender, EventArgs e)
  890. {
  891. string currentDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  892. currTime.Text = currentDate;
  893. handTime.Value = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.Hour,0, 0);
  894. }
  895. private async void FtpConnect_Click(object sender, EventArgs e)
  896. {
  897. if(FtpConnect.Text == "开启连接")
  898. {
  899. if (string.IsNullOrEmpty(FtpAccountVal.Text))
  900. {
  901. LogMessage("NG,请填写ftp账户");
  902. return;
  903. }
  904. if (string.IsNullOrEmpty(FtpPwVal.Text))
  905. {
  906. LogMessage("NG,请填写ftp密码");
  907. return;
  908. }
  909. if (string.IsNullOrEmpty(timerVal.Text))
  910. {
  911. LogMessage("NG,请填写间隔抓取文件时间");
  912. return;
  913. }
  914. if (string.IsNullOrEmpty(FtpPathVal.Text))
  915. {
  916. LogMessage("NG,请填写ftp抓取路径地址");
  917. return;
  918. }
  919. if (string.IsNullOrEmpty(FileCountVal.Text))
  920. {
  921. LogMessage("NG,请填写打包文件数");
  922. return;
  923. }
  924. FtpConnect.Enabled = false;
  925. FtpConnect.Text = "连接中...";
  926. LogMessage("开始测试连接...");
  927. bool testConnect = await TestFtpConnection(FtpPathVal.Text.Trim(), FtpAccountVal.Text.Trim(), FtpPwVal.Text.Trim());
  928. if (testConnect)
  929. {
  930. FtpAccountVal.Enabled = false;
  931. FtpPwVal.Enabled = false;
  932. timerVal.Enabled = false;
  933. FtpPathVal.Enabled = false;
  934. FileCountVal.Enabled = false;
  935. FtpConnect.Text = "关闭连接";
  936. FtpListen.Enabled = true;
  937. }
  938. else
  939. {
  940. FtpConnect.Enabled = true;
  941. FtpConnect.Text = "开启连接";
  942. }
  943. }
  944. else if(FtpConnect.Text == "关闭连接")
  945. {
  946. FtpAccountVal.Enabled = true;
  947. FtpPwVal.Enabled = true;
  948. timerVal.Enabled = true;
  949. FtpPathVal.Enabled = true;
  950. FileCountVal.Enabled = true;
  951. FtpConnect.Text = "开启连接";
  952. FtpListen.Enabled = false;
  953. }
  954. }
  955. public async Task<bool> TestFtpConnection(string ftpServer, string username, string password)
  956. {
  957. try
  958. {
  959. var request = (FtpWebRequest)WebRequest.Create(ftpServer);
  960. request.Method = WebRequestMethods.Ftp.ListDirectory;
  961. request.Credentials = new NetworkCredential(username, password);
  962. request.UsePassive = true;
  963. request.UseBinary = true;
  964. request.KeepAlive = false;
  965. request.Timeout = 10000;
  966. using (var response = await request.GetResponseAsync() as FtpWebResponse)
  967. {
  968. await Task.Run(() =>
  969. {
  970. LogMessage($"OK,FTP连接成功: {response.StatusDescription}");
  971. });
  972. return true;
  973. }
  974. }
  975. catch (WebException ex)
  976. {
  977. await Task.Run(() =>
  978. {
  979. if (ex.Response is FtpWebResponse response)
  980. {
  981. LogMessage($"NG,FTP连接失败,错误码: {(int)response.StatusCode} - {response.StatusDescription}");
  982. }
  983. else
  984. {
  985. LogMessage("NG,连接异常: " + ex.Message);
  986. }
  987. });
  988. return false;
  989. }
  990. catch (Exception ex)
  991. {
  992. await Task.Run(() =>
  993. {
  994. LogMessage($"NG,连接失败: " + ex.Message);
  995. });
  996. return false;
  997. }
  998. }
  999. private void FtpListen_Click(object sender, EventArgs e)
  1000. {
  1001. File_timer.Interval = Convert.ToInt32(timerVal.Text.Trim()) * 1000;
  1002. if (FtpListen.Text == "开启监听")
  1003. {
  1004. FtpListen.Text = "关闭监听";
  1005. FtpConnect.Enabled = false;
  1006. File_timer.Start();
  1007. Timer_Tick(null, null);
  1008. }
  1009. else if (FtpListen.Text == "关闭监听")
  1010. {
  1011. FtpListen.Text = "开启监听";
  1012. FtpConnect.Enabled = true;
  1013. File_timer.Stop();
  1014. }
  1015. }
  1016. private void MsgBox_DrawItem(object sender, DrawItemEventArgs e)
  1017. {
  1018. e.DrawBackground();
  1019. Brush mybsh = Brushes.Black;
  1020. string msgStr = MsgBox.Items[e.Index].ToString();
  1021. Console.WriteLine();
  1022. if (MsgBox.Items[e.Index].ToString().Contains("OK"))
  1023. {
  1024. mybsh = Brushes.Green;
  1025. }
  1026. else if (MsgBox.Items[e.Index].ToString().Contains("NG"))
  1027. {
  1028. mybsh = Brushes.Red;
  1029. }
  1030. e.DrawFocusRectangle();
  1031. e.Graphics.DrawString(MsgBox.Items[e.Index].ToString(), e.Font, mybsh, e.Bounds, StringFormat.GenericDefault);
  1032. }
  1033. private void LogMessage(string message)
  1034. {
  1035. if (MsgBox.InvokeRequired)
  1036. {
  1037. MsgBox.Invoke(new Action<string>(LogMessage), message);
  1038. return;
  1039. }
  1040. MsgBox.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
  1041. MsgBox.TopIndex = MsgBox.Items.Count - 1;
  1042. MsgBox.DrawMode = DrawMode.OwnerDrawFixed;
  1043. }
  1044. private void Timer_Tick(object sender, EventArgs e)
  1045. {
  1046. try
  1047. {
  1048. string childSaveFiles = Path.Combine(saveFiles, DateTime.Now.ToString("yyyyMMdd"));
  1049. if (!Directory.Exists(childSaveFiles))
  1050. {
  1051. Directory.CreateDirectory(childSaveFiles);
  1052. currentNum = 1;
  1053. LogMessage($"创建本地文件夹: {childSaveFiles}");
  1054. }
  1055. string ftpServer = FtpPathVal.Text.Trim();
  1056. string remoteFolderPath = "";
  1057. string username = FtpAccountVal.Text.Trim();
  1058. string password = FtpPwVal.Text.Trim();
  1059. List<string> fileList = GetFtpFileList(ftpServer, remoteFolderPath, username, password);
  1060. if (fileList.Count == 0)
  1061. {
  1062. LogMessage($"远程文件夹 '{remoteFolderPath}' 中没有文件");
  1063. return;
  1064. }
  1065. int successCount = 0;
  1066. int failCount = 0;
  1067. LogMessage($"开始下载 {fileList.Count} 个文件...");
  1068. foreach (string remoteFile in fileList)
  1069. {
  1070. if (!Directory.Exists(childSaveFiles))
  1071. {
  1072. Directory.CreateDirectory(childSaveFiles);
  1073. }
  1074. string remoteFilePath = $"{remoteFolderPath.TrimEnd('/')}/{remoteFile}";
  1075. string localFilePath = Path.Combine(childSaveFiles, remoteFile);
  1076. if (DownloadSingleFile(ftpServer, remoteFilePath, localFilePath, username, password))
  1077. {
  1078. successCount++;
  1079. }
  1080. else
  1081. {
  1082. failCount++;
  1083. }
  1084. string[] files = Directory.GetFiles(childSaveFiles);
  1085. string zipFilePath = Path.Combine(Path.GetDirectoryName(childSaveFiles), Path.GetFileName(childSaveFiles) + $"-{currentNum}.zip");
  1086. if (Convert.ToInt32(FileCountVal.Text.Trim()) == files.Length)
  1087. {
  1088. CompressFolder(childSaveFiles, zipFilePath);
  1089. currentNum++;
  1090. }
  1091. }
  1092. LogMessage($"下载完成: 成功 {successCount} 个, 失败 {failCount} 个");
  1093. }
  1094. catch (Exception ex)
  1095. {
  1096. LogMessage($"NG,自动获取文件失败,Error: {ex.Message}");
  1097. }
  1098. }
  1099. private List<string> GetFtpFileList(string ftpServer, string folderPath, string username, string password)
  1100. {
  1101. List<string> fileList = new List<string>();
  1102. try
  1103. {
  1104. string uri = $"{ftpServer.TrimEnd('/')}/{folderPath.TrimStart('/')}";
  1105. var request = (FtpWebRequest)WebRequest.Create(uri);
  1106. request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  1107. request.Credentials = new NetworkCredential(username, password);
  1108. request.UsePassive = true;
  1109. request.UseBinary = true;
  1110. request.KeepAlive = false;
  1111. using (var response = (FtpWebResponse)request.GetResponse())
  1112. using (var responseStream = response.GetResponseStream())
  1113. using (var reader = new StreamReader(responseStream))
  1114. {
  1115. string line;
  1116. while ((line = reader.ReadLine()) != null)
  1117. {
  1118. if (!string.IsNullOrWhiteSpace(line))
  1119. {
  1120. string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  1121. if (parts.Length >= 3)
  1122. {
  1123. //string fileName = parts[8];
  1124. //if (!parts[0].StartsWith("d") && fileName != "." && fileName != "..")
  1125. //{
  1126. // fileList.Add(fileName);
  1127. //}
  1128. string fileName = parts[parts.Length - 1];
  1129. if (fileName.Contains("."))
  1130. {
  1131. fileList.Add(fileName);
  1132. }
  1133. }
  1134. }
  1135. }
  1136. }
  1137. }
  1138. catch (Exception ex)
  1139. {
  1140. LogMessage($"获取文件列表失败: {ex.Message}");
  1141. }
  1142. return fileList;
  1143. }
  1144. private bool DownloadSingleFile(string ftpServer, string remoteFilePath, string localFilePath, string username, string password)
  1145. {
  1146. try
  1147. {
  1148. bool downloadSuccess = false;
  1149. string uri = $"{ftpServer.TrimEnd('/')}/{remoteFilePath.TrimStart('/')}";
  1150. var request = (FtpWebRequest)WebRequest.Create(uri);
  1151. request.Method = WebRequestMethods.Ftp.DownloadFile;
  1152. request.Credentials = new NetworkCredential(username, password);
  1153. request.UsePassive = true;
  1154. request.UseBinary = true;
  1155. request.KeepAlive = false;
  1156. using (var response = (FtpWebResponse)request.GetResponse())
  1157. using (var responseStream = response.GetResponseStream())
  1158. using (var fileStream = File.Create(localFilePath))
  1159. {
  1160. byte[] buffer = new byte[4096];
  1161. int bytesRead;
  1162. while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
  1163. {
  1164. fileStream.Write(buffer, 0, bytesRead);
  1165. }
  1166. downloadSuccess = true;
  1167. LogMessage($"OK,文件下载成功: {remoteFilePath} -> {localFilePath}");
  1168. if (downloadSuccess)
  1169. {
  1170. var deleteRequest = (FtpWebRequest)WebRequest.Create(uri);
  1171. deleteRequest.Method = WebRequestMethods.Ftp.DeleteFile;
  1172. deleteRequest.Credentials = new NetworkCredential(username, password);
  1173. deleteRequest.UsePassive = true;
  1174. deleteRequest.KeepAlive = false;
  1175. using (var deleteResponse = (FtpWebResponse)deleteRequest.GetResponse())
  1176. {
  1177. LogMessage($"OK,服务器文件删除成功: {remoteFilePath}");
  1178. }
  1179. }
  1180. return true;
  1181. }
  1182. }
  1183. catch (WebException ex)
  1184. {
  1185. if (ex.Response is FtpWebResponse response)
  1186. {
  1187. LogMessage($"NG,下载失败 {remoteFilePath}: {(int)response.StatusCode} - {response.StatusDescription}");
  1188. }
  1189. else
  1190. {
  1191. LogMessage($"NG,下载失败 {remoteFilePath}: {ex.Message}");
  1192. }
  1193. return false;
  1194. }
  1195. catch (Exception ex)
  1196. {
  1197. LogMessage($"NG,下载失败 {remoteFilePath}: {ex.Message}");
  1198. return false;
  1199. }
  1200. }
  1201. private string CreateFtpDirectoryIfNotExists(string ftpDirectoryPath, string username, string password)
  1202. {
  1203. try
  1204. {
  1205. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpDirectoryPath);
  1206. request.Method = WebRequestMethods.Ftp.MakeDirectory;
  1207. request.Credentials = new NetworkCredential(username, password);
  1208. request.UsePassive = true;
  1209. request.UseBinary = true;
  1210. request.KeepAlive = false;
  1211. using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
  1212. {
  1213. response.Close();
  1214. return "OK,目录创建成功: " + response.StatusDescription;
  1215. }
  1216. }
  1217. catch (WebException ex)
  1218. {
  1219. if (ex.Response is FtpWebResponse response && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
  1220. {
  1221. return "OK,目录已存在: " + response.StatusDescription;
  1222. }
  1223. else
  1224. {
  1225. return "NG,创建目录时发生错误: " + ex.Message;
  1226. }
  1227. }
  1228. }
  1229. private void CompressFile(string sourceFile, string zipPath)
  1230. {
  1231. try
  1232. {
  1233. using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Create))
  1234. using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
  1235. {
  1236. string fileName = Path.GetFileName(sourceFile);
  1237. ZipArchiveEntry readmeEntry = archive.CreateEntry(fileName);
  1238. using (Stream stream = readmeEntry.Open())
  1239. using (FileStream fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
  1240. {
  1241. fileStream.CopyTo(stream);
  1242. }
  1243. }
  1244. LogMessage($"OK,文件已压缩到:{zipPath}");
  1245. }
  1246. catch (Exception ex)
  1247. {
  1248. LogMessage($"NG,压缩失败:{ex.Message}");
  1249. }
  1250. }
  1251. private void CompressFolder(string sourceFolder, string zipPath)
  1252. {
  1253. try
  1254. {
  1255. if (File.Exists(zipPath))
  1256. {
  1257. try
  1258. {
  1259. File.Delete(zipPath);
  1260. }
  1261. catch (IOException ex)
  1262. {
  1263. LogMessage($"NG,无法删除已存在的文件,可能被其他程序占用:{ex.Message}");
  1264. return;
  1265. }
  1266. catch (UnauthorizedAccessException ex)
  1267. {
  1268. LogMessage($"NG,没有权限删除文件:{ex.Message}");
  1269. return;
  1270. }
  1271. }
  1272. ZipFile.CreateFromDirectory(sourceFolder, zipPath);
  1273. Directory.Delete(sourceFolder, true);
  1274. LogMessage($"OK,文件已压缩到:{zipPath}");
  1275. }
  1276. catch (UnauthorizedAccessException ex)
  1277. {
  1278. LogMessage($"NG,访问被拒绝:{ex.Message}");
  1279. }
  1280. catch (Exception ex)
  1281. {
  1282. LogMessage($"NG,压缩失败:{ex.Message}");
  1283. }
  1284. }
  1285. private void listenVal_Click(object sender, EventArgs e)
  1286. {
  1287. using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
  1288. {
  1289. folderDialog.Description = "请选择监控文件夹";
  1290. folderDialog.SelectedPath = listenVal.Text;
  1291. if (folderDialog.ShowDialog() == DialogResult.OK)
  1292. {
  1293. listenVal.Text = folderDialog.SelectedPath;
  1294. }
  1295. }
  1296. }
  1297. private void handPathVal_Click(object sender, EventArgs e)
  1298. {
  1299. using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
  1300. {
  1301. folderDialog.Description = "请选择导出文件夹";
  1302. folderDialog.SelectedPath = handPathVal.Text;
  1303. if (folderDialog.ShowDialog() == DialogResult.OK)
  1304. {
  1305. handPathVal.Text = folderDialog.SelectedPath;
  1306. }
  1307. }
  1308. }
  1309. private void buildConfirm_Click(object sender, EventArgs e)
  1310. {
  1311. if (buildConfirm.Text == "确定")
  1312. {
  1313. if (string.IsNullOrEmpty(handExeVal.Text))
  1314. {
  1315. LogMessage($"NG,请填写处理间隔时间");
  1316. return;
  1317. }
  1318. if (string.IsNullOrEmpty(listenVal.Text))
  1319. {
  1320. LogMessage($"NG,请填写监控路径");
  1321. return;
  1322. }
  1323. if (string.IsNullOrEmpty(handPathVal.Text))
  1324. {
  1325. LogMessage($"NG,请填写导出路径");
  1326. return;
  1327. }
  1328. handExeVal.Enabled = false;
  1329. handTime.Enabled = false;
  1330. listenVal.Enabled = false;
  1331. handPathVal.Enabled = false;
  1332. buildConfirm.Text = "取消";
  1333. }
  1334. else
  1335. {
  1336. handExeVal.Enabled = true;
  1337. handTime.Enabled = true;
  1338. listenVal.Enabled = true;
  1339. handPathVal.Enabled = true;
  1340. buildConfirm.Text = "确定";
  1341. }
  1342. }
  1343. private void buildStart_Click(object sender, EventArgs e)
  1344. {
  1345. if(buildStart.Text == "开启处理")
  1346. {
  1347. buildStart.Text = "关闭处理";
  1348. File_timer1.Interval = Convert.ToInt32(handExeVal.Text) * 60 * 1000;
  1349. File_timer1.Start();
  1350. FileTimer_Tick(null, null);
  1351. }
  1352. else
  1353. {
  1354. buildStart.Text = "开启处理";
  1355. File_timer.Stop();
  1356. }
  1357. }
  1358. private async void FileTimer_Tick(object sender, EventArgs e)
  1359. {
  1360. _currentOperationCts?.Cancel();
  1361. _currentOperationCts = new CancellationTokenSource();
  1362. try
  1363. {
  1364. await Task.Run(() => ProcessFilesInternal(_currentOperationCts.Token));
  1365. }
  1366. catch (OperationCanceledException)
  1367. {
  1368. LogMessage("文件处理操作被新任务取消。");
  1369. }
  1370. catch (Exception ex)
  1371. {
  1372. LogMessage($"NG,定时器任务启动失败: {ex}");
  1373. }
  1374. /*try
  1375. {
  1376. string[] txtFiles = Directory.GetFiles(listenVal.Text, $"*.json");
  1377. string currentHour = DateTime.Now.ToString("yyyyMMdd_hh");
  1378. string outPath = Path.Combine(handPathVal.Text, currentHour + "_Count_" + txtFiles.Length);
  1379. if (!Directory.Exists(outPath))
  1380. {
  1381. Directory.CreateDirectory(outPath);
  1382. }
  1383. foreach (string file in txtFiles)
  1384. {
  1385. string targetFile = Path.Combine(outPath, Path.GetFileName(file));
  1386. if (File.Exists(targetFile))
  1387. {
  1388. File.Delete(targetFile);
  1389. }
  1390. File.Move(file, targetFile);
  1391. }
  1392. string zipPath = outPath + ".zip";
  1393. if (File.Exists(zipPath))
  1394. {
  1395. File.Delete(zipPath);
  1396. }
  1397. ZipFile.CreateFromDirectory(outPath, zipPath);
  1398. LogMessage($"OK,文件已压缩到:{zipPath}");
  1399. }
  1400. catch(Exception ex)
  1401. {
  1402. LogMessage($"NG,解析文件列表失败: {ex.Message}");
  1403. }*/
  1404. }
  1405. private void ProcessFilesInternal(CancellationToken cancellationToken)
  1406. {
  1407. try
  1408. {
  1409. var txtFiles = Directory.EnumerateFiles(listenVal.Text, "*.json", SearchOption.TopDirectoryOnly).ToList();
  1410. int fileCount = txtFiles.Count;
  1411. if (fileCount == 0)
  1412. {
  1413. LogMessage("没有找到需要处理的 .json 文件。");
  1414. return;
  1415. }
  1416. cancellationToken.ThrowIfCancellationRequested();
  1417. LogMessage($"已找到找到 {fileCount} 个json文件,开始处理...");
  1418. string currentHour = DateTime.Now.ToString("yyyyMMdd_HH");
  1419. string outPath = Path.Combine(handPathVal.Text, $"{currentHour}_{Convert.ToInt32(handExeVal.Text)}_Count_{fileCount}");
  1420. cancellationToken.ThrowIfCancellationRequested();
  1421. if (!Directory.Exists(outPath))
  1422. {
  1423. Directory.CreateDirectory(outPath);
  1424. }
  1425. int movedCount = 0;
  1426. foreach (string file in txtFiles)
  1427. {
  1428. cancellationToken.ThrowIfCancellationRequested();
  1429. string targetFile = Path.Combine(outPath, Path.GetFileName(file));
  1430. if (File.Exists(targetFile))
  1431. {
  1432. File.Delete(targetFile);
  1433. }
  1434. File.Move(file, targetFile);
  1435. movedCount++;
  1436. if (movedCount % 1000 == 0)
  1437. {
  1438. LogMessage($"已移动 {movedCount}/{fileCount} 个文件...");
  1439. }
  1440. }
  1441. LogMessage($"成功移动 {movedCount} 个文件到 '{outPath}'");
  1442. string zipPath = outPath + ".zip";
  1443. cancellationToken.ThrowIfCancellationRequested();
  1444. if (File.Exists(zipPath))
  1445. {
  1446. File.Delete(zipPath);
  1447. }
  1448. ZipFile.CreateFromDirectory(outPath, zipPath);
  1449. //Directory.Delete(outPath, true);
  1450. LogMessage($"OK, {fileCount} 个文件已成功压缩到: {zipPath}");
  1451. }
  1452. catch (OperationCanceledException)
  1453. {
  1454. LogMessage("OK, 文件处理操作被用户取消。");
  1455. }
  1456. catch (UnauthorizedAccessException ex)
  1457. {
  1458. LogMessage($"NG, 访问被拒绝,可能没有足够权限读写文件或目录: {ex.Message}");
  1459. }
  1460. catch (DirectoryNotFoundException ex)
  1461. {
  1462. LogMessage($"NG, 指定的路径不存在: {ex.Message}");
  1463. }
  1464. catch (IOException ex)
  1465. {
  1466. LogMessage($"NG, 文件 I/O 错误: {ex.Message}");
  1467. }
  1468. catch (Exception ex)
  1469. {
  1470. LogMessage($"NG, 文件处理过程中发生未知错误: {ex}");
  1471. }
  1472. }
  1473. }
  1474. }