| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647 |
- using Microsoft.Office.Interop.Excel;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Globalization;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Media;
- using System.Net;
- using System.Runtime.InteropServices;
- using System.Security.Principal;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using UAS_Tools_HY;
- using UAS_Tools_HY.Properties;
- using UAS_Tools_HY.PublicMethods;
- using Action = System.Action;
- using Application = Microsoft.Office.Interop.Excel.Application;
- using DataTable = System.Data.DataTable;
- using Timer = System.Windows.Forms.Timer;
- namespace UAS_MES_Tools
- {
- public partial class Main : Form
- {
- string _Account, _Password, _UserName;
- DataTable dt;
- Timer loadTime;
- string minTime, maxTime;
- string curRuleId, curRule, curRuleLeng;
- int ishaveInd;
- Timer File_timer;
- Timer File_timer1;
- string saveFiles;
- int currentNum = 1;
- private CancellationTokenSource _currentOperationCts;
- public Main(string account, string password)
- {
- _Account = account;
- _Password = password;
- InitializeComponent();
- }
- private void Main_Load(object sender, EventArgs e)
- {
- currUser.Text = "当前账户: " + _Account;
- dt = ConnectDB.ExecuteSelect($"select * from employee where em_code = '{_Account}'");
- if (dt.Rows.Count > 0)
- {
- _UserName = dt.Rows[0]["em_name"].ToString();
- }
- loadTime_Tick(null, null);
- loadTime = new Timer();
- loadTime.Interval = 1000;
- loadTime.Tick += loadTime_Tick;
- loadTime.Start();
- SN.SelectAll();
- SN.Focus();
- settingLeftInput1.Text = BaseUtil.GetCacheData("SettingCount").ToString();
- settingLeftInput2.Text = BaseUtil.GetCacheData("SettingVolume").ToString();
- if (string.IsNullOrEmpty(BaseUtil.GetCacheData("SettingVolume").ToString())) dSum.Text = "0";
- else dSum.Text = BaseUtil.GetCacheData("SettingVolume").ToString();
- settingRightInput1.Text = BaseUtil.GetCacheData("SettingNGPath").ToString();
- settingRightInput2.Text = BaseUtil.GetCacheData("SettingExportPath").ToString();
-
- QDcheckbox1.Checked = true;
- QDdtp2.Value = QDdtp1.Value.AddDays(1);
- rulesQuery_Click(null, null);
- QDquery_Click(null, null);
- saveFiles = Path.Combine(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).FullName, $"FtpFiles");
- if (!Directory.Exists(saveFiles))
- {
- Directory.CreateDirectory(saveFiles);
- }
- //saveFiles = Path.Combine(saveFiles, DateTime.Now.ToString("yyyyMMdd"));
- //if (!Directory.Exists(saveFiles))
- //{
- // Directory.CreateDirectory(saveFiles);
- //}
- File_timer = new Timer();
- File_timer.Interval = 30 * 1000;
- File_timer.Tick += Timer_Tick;
- timerVal.Text = "30";
- FileCountVal.Text = "10";
- FtpPathVal.Text = "ftp://10.18.13.48:21";
- handExeVal.Text = "120";
- File_timer1 = new Timer();
- File_timer1.Interval = Convert.ToInt32(handExeVal.Text) * 1000;
- File_timer1.Tick += FileTimer_Tick;
- }
- private void SN_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode != Keys.Enter) return;
- if(checkPagination.SelectedIndex != 0) checkPagination.SelectedIndex = 0;
- if (CheckEnter()) return;
- bool checkRes = true;
- if (radio1.Checked)
- {
- dt = ConnectDB.ExecuteSelect($@"SELECT COUNT(1) sum FROM dcrlogexcel WHERE item = '{SN.Text.Trim()}'");
- if (dt.Rows[0]["sum"].ToString() == "0")
- {
- PlaySound("NG");
- MessageBox.Show($"{SN.Text.Trim()},没有DCR测试数据", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- checkRes = false;
- return;
- }
- }
- if (FormMethods.IsCheckSNRules(curRule, SN.Text.Trim(), curRuleLeng))
- {
- PlaySound("NG");
- MessageBox.Show($"{SN.Text.Trim()},不符合条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- checkRes = false;
- return;
- }
- dt = ConnectDB.ExecuteSelect($@"select * from g_packing_sncheck where sn = '{SN.Text.Trim()}'");
- if (dt.Rows.Count > 0)
- {
- PlaySound("NG");
- MessageBox.Show($"{SN.Text.Trim()},历史记录中已核对", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- checkRes = false;
- return;
- }
- ishaveInd = -1;
- if (Datas.Rows.Count > 0)
- {
- foreach (DataGridViewRow item in Datas.Rows)
- {
- if (SN.Text.Trim() == item.Cells[0].Value.ToString())
- {
- ishaveInd = Datas.Rows.IndexOf(item);
- }
- }
- }
- if (ishaveInd >= 0)
- {
- PlaySound("NG");
- MessageBox.Show($"{SN.Text.Trim()},已核对装箱", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- checkRes = false;
- return;
- /*int iCou = Convert.ToInt32(Datas.Rows[ishaveInd].Cells[2].Value);
- iCou += 1;
- Datas.Rows[ishaveInd].Cells[3].Value = currTime.Text;
- Datas.Rows[ishaveInd].Cells[6].Value = checkRes ? "OK" : "NG";
- PlaySound("OK");*/
- }
- else
- {
- if (string.IsNullOrEmpty(BoxNo.Text) && string.IsNullOrEmpty(BaseUtil.GetCacheData("VisualOutboxNo").ToString()))
- {
- BaseUtil.SetCacheData("VisualOutboxNo", DateTime.Now.ToString("yyyyMMddHHmmss"));
- BoxNo_KeyDown(null, new KeyEventArgs(Keys.Enter));
- }
- DataGridViewRow row = new DataGridViewRow();
- row.CreateCells(Datas);
- row.Cells[0].Value = SN.Text.Trim();
- row.Cells[1].Value = BoxNo.Text.Trim();
- row.Cells[2].Value = 1;
- row.Cells[3].Value = currTime.Text;
- row.Cells[4].Value = _UserName;
- row.Cells[5].Value = radio1.Checked ? "是" : "否";
- row.Cells[6].Value = checkRes ? "OK" : "NG";
- row.Cells[7].Value = string.IsNullOrEmpty(BoxNo.Text) ? BaseUtil.GetCacheData("VisualOutboxNo") : "";
- Datas.Rows.Add(row);
- PlaySound("OK");
- Datas.FirstDisplayedScrollingRowIndex = Datas.Rows.Count - 1;
- try
- {
- int typeNum = ChangeDetail(row);
- if (typeNum == 1)
- {
- SN.Focus();
- SN.SelectAll();
- SN.Text = "";
- }
- else if (typeNum == 2)
- {
- SN.Text = "";
- dCount.Text = "0";
- BoxNo.Enabled = true;
- BoxNo.Focus();
- BoxNo.SelectAll();
- //BoxNo.Text = "";
- //Datas.Rows.Clear();
- BaseUtil.SetCacheData("VisualOutboxNo", "");
- Application excelApp = null;
- Workbook workbook = null;
- Worksheet worksheet = null;
- Range range = null;
- string tempTemplatePath = null;
- try
- {
- string visualOutboxNo = string.IsNullOrEmpty(row.Cells[1].Value.ToString()) ? row.Cells[7].Value.ToString() : row.Cells[1].Value.ToString();
- string generateFileName = string.IsNullOrEmpty(row.Cells[1].Value.ToString()) ? DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") : row.Cells[1].Value.ToString();
- string checkExportModel = Path.Combine(settingRightInput2.Text, generateFileName + ".xlsx");
- if (string.IsNullOrEmpty(settingRightInput2.Text) || !Directory.Exists(settingRightInput2.Text))
- {
- MessageBox.Show($"输出目录不存在: {settingRightInput2.Text}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- byte[] templateBytes = Resources.checkExportModel;
- if (templateBytes == null || templateBytes.Length == 0)
- {
- MessageBox.Show("未能加载模板文件", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- tempTemplatePath = Path.GetTempFileName().Replace(".tmp", ".xlsx");
- File.WriteAllBytes(tempTemplatePath, templateBytes);
- excelApp = new Application();
- excelApp.Visible = false;
- excelApp.DisplayAlerts = false;
- workbook = excelApp.Workbooks.Open(tempTemplatePath);
- object sheetIndex = 1;
- worksheet = (Worksheet)workbook.Worksheets[sheetIndex];
- dt = ConnectDB.ExecuteSelect($@"select * from g_packing_sncheck where outbox_no = '{visualOutboxNo}'");
- if (dt.Rows.Count > 0)
- {
- worksheet.Cells[1, 3] = DateTime.Now.ToString("yyyy-MM-dd");
- worksheet.Cells[1, 7] = visualOutboxNo;
- int startRow = 3;
- int startColumn = 2;
- int currInd = 0;
- int dataRowsCount = dt.Rows.Count;
- foreach (DataRow item in dt.Rows)
- {
- worksheet.Cells[startRow + currInd, startColumn - 1] = currInd + 1;
- worksheet.Cells[startRow + currInd, startColumn] = item["SN"].ToString();
- if (currInd > 23)
- {
- currInd = 0;
- startColumn += 2;
- }
- else
- {
- currInd += 1;
- }
- }
- int endRow = startRow + 25;
- range = worksheet.Range[worksheet.Cells[startRow, 1], worksheet.Cells[endRow, startColumn]];
- range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
- range.VerticalAlignment = XlVAlign.xlVAlignCenter;
- }
- worksheet.Columns.AutoFit();
- workbook.SaveAs(
- Filename: checkExportModel,
- FileFormat: XlFileFormat.xlOpenXMLWorkbook,
- Password: Type.Missing,
- WriteResPassword: Type.Missing,
- ReadOnlyRecommended: Type.Missing,
- CreateBackup: Type.Missing,
- AccessMode: XlSaveAsAccessMode.xlExclusive,
- ConflictResolution: Type.Missing,
- AddToMru: false,
- TextCodepage: Type.Missing,
- TextVisualLayout: Type.Missing,
- Local: Type.Missing
- );
- MessageBox.Show($"Excel文件已成功导出到: {checkExportModel}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception ex)
- {
- MessageBox.Show($"生成 Excel 文件时出错: {ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- finally
- {
- // 正确释放COM对象
- if (worksheet != null)
- {
- try
- {
- Marshal.ReleaseComObject(worksheet);
- }
- catch { }
- worksheet = null;
- }
- if (workbook != null)
- {
- try
- {
- workbook.Close(false);
- Marshal.ReleaseComObject(workbook);
- }
- catch { }
- workbook = null;
- }
- if (excelApp != null)
- {
- try
- {
- excelApp.Quit();
- Marshal.ReleaseComObject(excelApp);
- }
- catch { }
- excelApp = null;
- }
- if (!string.IsNullOrEmpty(tempTemplatePath) && File.Exists(tempTemplatePath))
- {
- try
- {
- File.Delete(tempTemplatePath);
- }
- catch (Exception ex)
- {
- }
- }
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
- }
- QDquery_Click(null, null);
- if (Datas.Rows.Count == Convert.ToInt32(settingLeftInput1.Text))
- {
- Datas.Rows.Clear();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- }
- }
- private int ChangeDetail(DataGridViewRow row)
- {
- int inputSum = Convert.ToInt32(dCount.Text);
- dCount.Text = (inputSum + 1).ToString();
- string visualOutboxNo = string.IsNullOrEmpty(row.Cells[1].Value.ToString()) ? row.Cells[7].Value.ToString() : row.Cells[1].Value.ToString();
- int insertCount = ConnectDB.ExecuteInsert($@"INSERT INTO g_packing_sncheck (work_order,part_no,sn,outbox_no,
- count,update_time,update_name,check_id,rule_name,rule_value,capacity,ISCHECHK_DCR,rule_id)
- VALUES ( '', '', '{row.Cells[0].Value}', '{visualOutboxNo}',
- '{row.Cells[2].Value}',sysdate,'{_UserName}',packcheckid_seq.NEXTVAL,'{dRules.Text.Trim()}','{curRule}','{dSum.Text}','{row.Cells[5].Value}','{curRuleId}' )");
- if (Convert.ToInt32(dCount.Text) < Convert.ToInt32(dSum.Text))
- {
- return 1;
- }
- else
- {
- return 2;
- }
-
- //if(Convert.ToInt32(dCount.Text) == Convert.ToInt32(dSum.Text))
- //{
- // foreach (DataGridViewRow item in Datas.Rows)
- // {
- // int insertCount = ConnectDB.ExecuteInsert($@"INSERT INTO g_packing_sncheck (work_order,part_no,sn,outbox_no,
- // count,update_time,update_name,check_id,rule_name,rule_value)
- // VALUES ( '', '', '{item.Cells[0].Value}', '{item.Cells[1].Value}',
- // '{item.Cells[2].Value}',sysdate,'{_UserName}',packcheckid_seq.NEXTVAL,'{dRules.Text.Trim()}','{curRule}' )");
- // }
- // return true;
- //}
- //else
- //{
- // return false;
- //}
- }
- private void BoxNo_Leave(object sender, EventArgs e)
- {
- if (!string.IsNullOrEmpty(BoxNo.Text))
- {
- KeyEventArgs KeyEventArg = new KeyEventArgs(Keys.Enter);
- BoxNo_KeyDown(null, KeyEventArg);
- }
- }
- private void BoxNo_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode != Keys.Enter) return;
-
- string visualOutboxNo = string.IsNullOrEmpty(BoxNo.Text) ? BaseUtil.GetCacheData("VisualOutboxNo").ToString() : BoxNo.Text.Trim();
- 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");
- if (dt.Rows.Count > 0)
- {
- if (Convert.ToInt32(dt.Rows[0]["CAPACITY"].ToString()) > dt.Rows.Count)
- {
- dRules.Text = dt.Rows[0]["RULE_NAME"].ToString();
- curRule = dt.Rows[0]["RULE_VALUE"].ToString();
- curRuleId = dt.Rows[0]["RULE_ID"].ToString();
- curRuleLeng = dt.Rows[0]["RULE_LENGTH"].ToString();
- dRules.Enabled = false;
- radio1.Checked = dt.Rows[0]["ISCHECHK_DCR"].ToString() == "是" ? true : false;
- dCount.Text = dt.Rows.Count.ToString();
- settingLeftInput2.Text = dt.Rows[0]["CAPACITY"].ToString();
- dSum.Text = dt.Rows[0]["CAPACITY"].ToString();
- BaseUtil.SetCacheData("SettingVolume", dt.Rows[0]["CAPACITY"].ToString());
- Datas.Rows.Clear();
- foreach (DataRow item in dt.Rows)
- {
- DataGridViewRow row = new DataGridViewRow();
- row.CreateCells(Datas);
- row.Cells[0].Value = item["SN"].ToString();
- row.Cells[1].Value = item["OUTBOX_NO"].ToString();
- row.Cells[2].Value = item["COUNT"].ToString();
- row.Cells[3].Value = item["UPDATE_TIME"].ToString();
- row.Cells[4].Value = item["UPDATE_NAME"].ToString();
- row.Cells[5].Value = item["ISCHECHK_DCR"].ToString();
- row.Cells[6].Value = "OK";
- Datas.Rows.Add(row);
- }
- }
- else
- {
- BoxNo.Focus();
- BoxNo.SelectAll();
- MessageBox.Show($"核对完成,此箱{visualOutboxNo} 已装满{dt.Rows[0]["CAPACITY"].ToString()} 个,请扫描未核对箱号", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- BoxNo.Text = "";
- return;
- }
- }
- if (CheckEnter())
- {
- BoxNo.Text = "";
- return;
- }
- /*if (Datas.Rows.Count > 0)
- {
- foreach(DataGridViewRow item in Datas.Rows)
- {
- item.Cells[1].Value = BoxNo.Text.Trim();
- }
- }*/
- BoxNo.Enabled = false;
- SN.SelectAll();
- SN.Focus();
- }
- private bool CheckEnter()
- {
- if (string.IsNullOrEmpty(settingLeftInput1.Text))
- {
- MessageBox.Show("请维护列表记录数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return true;
- }
- if (string.IsNullOrEmpty(settingLeftInput2.Text))
- {
- MessageBox.Show("请维护箱内容量", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return true;
- }
- if (!Regex.IsMatch(dSum.Text, @"^[1-9]\d*$"))
- {
- MessageBox.Show("箱内容量维护应为正整数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return true;
- }
- if (string.IsNullOrEmpty(dRules.Text))
- {
- MessageBox.Show("请选择条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return true;
- }
- if (dRules.Enabled)
- {
- MessageBox.Show("请选择确认条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return true;
- }
- return false;
- }
-
- private void settingLeftSubmit_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(settingLeftInput1.Text))
- {
- MessageBox.Show("请输入列表记录数", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if (string.IsNullOrEmpty(settingLeftInput2.Text))
- {
- MessageBox.Show("请输入箱内容量", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if (Convert.ToInt32(dCount.Text) > 0)
- {
- if(Convert.ToInt32(dSum.Text) > Convert.ToInt32(settingLeftInput2.Text))
- {
- MessageBox.Show($"已扫描{Convert.ToInt32(dCount.Text)} 个产品,修改箱容量需大于{Convert.ToInt32(settingLeftInput2.Text)} ", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- }
- BaseUtil.SetCacheData("SettingCount", settingLeftInput1.Text);
- BaseUtil.SetCacheData("SettingVolume", settingLeftInput2.Text);
- dSum.Text = BaseUtil.GetCacheData("SettingVolume").ToString();
- MessageBox.Show("已提交扫描设置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- private void settingRightSubmit_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(settingRightInput2.Text))
- {
- MessageBox.Show("请选择导出文件目录", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if(!string.IsNullOrEmpty(settingRightInput1.Text)) BaseUtil.SetCacheData("SettingNGPath", settingRightInput1.Text);
- BaseUtil.SetCacheData("SettingExportPath", settingRightInput2.Text);
- MessageBox.Show("已提交文件设置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- private void QDquery_Click(object sender, EventArgs e)
- {
- StringBuilder filterStr = new StringBuilder();
- if (!string.IsNullOrEmpty(QDInput1.Text))
- {
- filterStr.Append($"WHERE instr(SN,'{QDInput1.Text}') > 0");
- }
- if (!string.IsNullOrEmpty(QDInput2.Text))
- {
- if (filterStr.Length == 0)
- {
- filterStr.Append($"WHERE instr(OUTBOX_NO,'{QDInput2.Text}') > 0");
- }
- else
- {
- filterStr.Append($" and instr(OUTBOX_NO, '{QDInput2.Text}') > 0");
- }
- }
- if (QDcheckbox1.Checked)
- {
- string dtp1 = QDdtp1.Value.ToString("yyyy-MM-dd 00:00:00");
- string dtp2 = QDdtp2.Value.ToString("yyyy-MM-dd 00:00:00");
- if (filterStr.Length == 0)
- {
- 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')");
- }
- else
- {
- 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')");
- }
- }
- if(sender == null)
- {
- Task.Run(() =>
- {
- dt = ConnectDB.ExecuteSelect($@"SELECT sn dqsn,outbox_no dqoutbox_no,count dqcount,
- update_time dqupdate_time,update_name dqname,rule_name dqrule_name,rule_value dqrule_value
- FROM g_packing_sncheck {filterStr.ToString()} ORDER BY update_time desc");
- this.Invoke(new Action(() =>
- {
- QDDatas.DataSource = dt;
- }));
- });
- }
- else
- {
- Loading LoadingForm = new Loading(filterStr.ToString());
- if (LoadingForm.ShowDialog() == DialogResult.OK)
- {
- DataTable dt = LoadingForm.ResultData;
- QDDatas.DataSource = dt;
- }
- }
- }
- private void QDexport_Click(object sender, EventArgs e)
- {
- if(QDDatas.Rows.Count == 0)
- {
- MessageBox.Show("请先查询出导出数据", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if (string.IsNullOrEmpty(settingRightInput2.Text))
- {
- MessageBox.Show("请先在设置中选择导出路径", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- Application excelApp = null;
- Workbook workbook = null;
- Worksheet worksheet = null;
- Range range = null;
- string tempTemplatePath = null;
- try
- {
- string exportPath = Path.Combine(settingRightInput2.Text, DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".xlsx");
- if (string.IsNullOrEmpty(settingRightInput2.Text) || !Directory.Exists(settingRightInput2.Text))
- {
- MessageBox.Show($"输出目录不存在: {settingRightInput2.Text}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- byte[] templateBytes = Resources.exportModel; // 假设Resources.exportModel是有效的资源
- if (templateBytes == null || templateBytes.Length == 0)
- {
- MessageBox.Show("未能加载模板文件", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- tempTemplatePath = Path.GetTempFileName().Replace(".tmp", ".xlsx");
- File.WriteAllBytes(tempTemplatePath, templateBytes);
- excelApp = new Application();
- excelApp.Visible = false;
- excelApp.DisplayAlerts = false;
- workbook = excelApp.Workbooks.Open(tempTemplatePath);
- object sheetIndex = 1;
- worksheet = (Worksheet)workbook.Worksheets[sheetIndex];
- int startRow = 2;
- int dataRowsCount = QDDatas.Rows.Count;
- for (int i = 0; i < QDDatas.Rows.Count; i++)
- {
- worksheet.Cells[startRow + i, 1] = i + 1; // 改为从1开始编号,更符合Excel习惯
- worksheet.Cells[startRow + i, 2] = QDDatas.Rows[i].Cells[0].Value?.ToString();
- }
- int endRow = startRow + dataRowsCount - 1;
- range = worksheet.Range[worksheet.Cells[startRow, 1], worksheet.Cells[endRow, 2]];
- range.HorizontalAlignment = XlHAlign.xlHAlignCenter; // 水平居中
- range.VerticalAlignment = XlVAlign.xlVAlignCenter; // 垂直居中
- worksheet.Columns.AutoFit();
- // 修复SaveAs方法,使用正确的参数
- workbook.SaveAs(
- Filename: exportPath,
- FileFormat: XlFileFormat.xlOpenXMLWorkbook,
- Password: Type.Missing,
- WriteResPassword: Type.Missing,
- ReadOnlyRecommended: Type.Missing,
- CreateBackup: Type.Missing,
- AccessMode: XlSaveAsAccessMode.xlExclusive,
- ConflictResolution: Type.Missing,
- AddToMru: false,
- TextCodepage: Type.Missing,
- TextVisualLayout: Type.Missing,
- Local: Type.Missing
- );
- MessageBox.Show($"Excel文件已成功导出到: {exportPath}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception ex)
- {
- MessageBox.Show($"生成 Excel 文件时出错: {ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- finally
- {
- // 正确释放COM对象
- if (worksheet != null)
- {
- try
- {
- Marshal.ReleaseComObject(worksheet);
- }
- catch { }
- worksheet = null;
- }
- if (workbook != null)
- {
- try
- {
- workbook.Close(false);
- Marshal.ReleaseComObject(workbook);
- }
- catch { }
- workbook = null;
- }
- if (excelApp != null)
- {
- try
- {
- excelApp.Quit();
- Marshal.ReleaseComObject(excelApp);
- }
- catch { }
- excelApp = null;
- }
- if (!string.IsNullOrEmpty(tempTemplatePath) && File.Exists(tempTemplatePath))
- {
- try
- {
- File.Delete(tempTemplatePath);
- }
- catch (Exception ex)
- {
- }
- }
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
- }
- private void rulesAdd_Click(object sender, EventArgs e)
- {
- StringBuilder filterStr = new StringBuilder();
- if (!string.IsNullOrEmpty(rulesInput1.Text))
- {
- filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
- }
- if (!string.IsNullOrEmpty(rulesInput2.Text))
- {
- if(filterStr.Length == 0)
- {
- filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
- }
- else
- {
- filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
- }
- }
- Form rulesList = new RulesList("Add",_UserName);
- if (rulesList.ShowDialog() == DialogResult.OK)
- {
- MessageBox.Show("新增规则成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
- 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");
- RulesDatas.DataSource = dt;
- }
- }
- private void rulesQuery_Click(object sender, EventArgs e)
- {
- StringBuilder filterStr = new StringBuilder();
- if (!string.IsNullOrEmpty(rulesInput1.Text))
- {
- filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
- }
- if (!string.IsNullOrEmpty(rulesInput2.Text))
- {
- if (filterStr.Length == 0)
- {
- filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
- }
- else
- {
- filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
- }
- }
- dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
- 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");
- RulesDatas.DataSource = dt;
- }
- private void RulesDatas_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
- {
- if (e.RowIndex >= 0 && !RulesDatas.Rows[e.RowIndex].IsNewRow)
- {
- DataGridView dgv = sender as DataGridView;
- if (dgv != null)
- {
- DataGridViewRow selectedRow = dgv.Rows[e.RowIndex];
- string RULE_ID = selectedRow.Cells[0].Value.ToString();
- StringBuilder filterStr = new StringBuilder();
- if (!string.IsNullOrEmpty(rulesInput1.Text))
- {
- filterStr.Append($"WHERE instr(RULE_NAME,'{rulesInput1.Text}') > 0");
- }
- if (!string.IsNullOrEmpty(rulesInput2.Text))
- {
- if (filterStr.Length == 0)
- {
- filterStr.Append($"WHERE instr(RULE_VALUE,'{rulesInput2.Text}') > 0");
- }
- else
- {
- filterStr.Append($" and instr(RULE_VALUE, '{rulesInput2.Text}') > 0");
- }
- }
- Form rulesList = new RulesList("Modify",RULE_ID, _UserName);
- if (rulesList.ShowDialog() == DialogResult.OK)
- {
- MessageBox.Show("修改规则成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- dt = ConnectDB.ExecuteSelect($@"SELECT RULE_ID ruleId,RULE_NAME RulesCol1, RULE_VALUE RulesCol2, RELE_REMARK RulesCol3,
- 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");
- RulesDatas.DataSource = dt;
- }
- }
- }
- }
- private void RulesDatas_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
- {
- if (e.RowIndex >= 0 && !RulesDatas.Rows[e.RowIndex].IsNewRow)
- {
- DataGridView dgv = sender as DataGridView;
- if (dgv != null)
- {
- DataGridViewRow selectedRow = dgv.Rows[e.RowIndex];
- if (dRules.Enabled)
- {
- dRules.Text = selectedRow.Cells[1].Value.ToString();
- curRuleId = selectedRow.Cells[0].Value.ToString();
- curRule = selectedRow.Cells[2].Value.ToString();
- curRuleLeng = selectedRow.Cells[4].Value.ToString();
- }
- }
- }
- }
- private void boxBtn_Click(object sender, EventArgs e)
- {
- BoxNo.Enabled = true;
- BoxNo.Focus();
- BoxNo.SelectAll();
- }
- private void settingRightInput1_Click(object sender, EventArgs e)
- {
- using (OpenFileDialog openFileDialog = new OpenFileDialog())
- {
- openFileDialog.Title = "请选择告警文件";
- openFileDialog.Filter = "可执行文件 (*.wav)|*.wav|所有文件 (*.*)|*.*";
- openFileDialog.FilterIndex = 1;
- openFileDialog.RestoreDirectory = true;
- if (!string.IsNullOrEmpty(settingRightInput1.Text) && System.IO.File.Exists(settingRightInput1.Text))
- {
- openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(settingRightInput1.Text);
- }
- else
- {
- openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
- }
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- settingRightInput1.Text = openFileDialog.FileName;
- }
- }
- }
- private void settingRightInput2_Click(object sender, EventArgs e)
- {
- using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
- {
- folderBrowserDialog.Description = "请选择导出文件夹";
- if (!string.IsNullOrEmpty(settingRightInput2.Text) && System.IO.Directory.Exists(settingRightInput2.Text))
- {
- folderBrowserDialog.SelectedPath = settingRightInput2.Text;
- }
- else
- {
- folderBrowserDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // folderBrowserDialog.SelectedPath = "";
- }
- if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
- {
- settingRightInput2.Text = folderBrowserDialog.SelectedPath;
- }
- }
- }
- private void QDdtp1_ValueChanged(object sender, EventArgs e)
- {
- if (QDdtp1.Value > QDdtp2.Value)
- {
- QDdtp1.Value = QDdtp2.Value.AddDays(-1);
- MessageBox.Show("时间不得大于最大时间", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- minTime = QDdtp1.Value.ToString("yyyy-mm-dd hh:mm:ss");
- }
- private void DRulesBtn_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(dRules.Text))
- {
- MessageBox.Show("请选择条码规则", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- dRules.Enabled = false;
- }
- private void DRulesBtn1_Click(object sender, EventArgs e)
- {
- dRules.Enabled = true;
- }
- private void QDdtp2_ValueChanged(object sender, EventArgs e)
- {
- if (QDdtp2.Value < QDdtp1.Value)
- {
- QDdtp2.Value = QDdtp1.Value.AddDays(1);
- MessageBox.Show("时间不得小于最小时间", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- maxTime = QDdtp2.Value.ToString("yyyy-mm-dd hh:mm:ss");
- }
- private void PlaySound(string type)
- {
- if (type == "OK")
- {
- ShowMsg.Text = "Pass";
- ShowMsg.BackColor = Color.LawnGreen;
- }
- else
- {
- ShowMsg.Text = "Fail";
- ShowMsg.BackColor = Color.Red;
- }
- Task.Run(() => {
- if (type == "OK")
- {
- using (var stream = UAS_Tools_HY.Properties.Resources.OK)
- {
- SoundPlayer player = new SoundPlayer(stream);
- player.Play();
- }
- }
- else
- {
- if (string.IsNullOrEmpty(settingRightInput1.Text))
- {
- using (var stream = UAS_Tools_HY.Properties.Resources.NG)
- {
- SoundPlayer player = new SoundPlayer(stream);
- player.Play();
- }
- }
- else
- {
- string customPath = settingRightInput1.Text.Trim();
- if (File.Exists(customPath))
- {
- SoundPlayer player = new SoundPlayer(customPath);
- player.Play();
- }
- }
- }
- });
- }
- public void loadTime_Tick(object sender, EventArgs e)
- {
- string currentDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
- currTime.Text = currentDate;
- handTime.Value = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.Hour,0, 0);
- }
- private async void FtpConnect_Click(object sender, EventArgs e)
- {
- if(FtpConnect.Text == "开启连接")
- {
- if (string.IsNullOrEmpty(FtpAccountVal.Text))
- {
- LogMessage("NG,请填写ftp账户");
- return;
- }
- if (string.IsNullOrEmpty(FtpPwVal.Text))
- {
- LogMessage("NG,请填写ftp密码");
- return;
- }
- if (string.IsNullOrEmpty(timerVal.Text))
- {
- LogMessage("NG,请填写间隔抓取文件时间");
- return;
- }
- if (string.IsNullOrEmpty(FtpPathVal.Text))
- {
- LogMessage("NG,请填写ftp抓取路径地址");
- return;
- }
- if (string.IsNullOrEmpty(FileCountVal.Text))
- {
- LogMessage("NG,请填写打包文件数");
- return;
- }
- FtpConnect.Enabled = false;
- FtpConnect.Text = "连接中...";
- LogMessage("开始测试连接...");
- bool testConnect = await TestFtpConnection(FtpPathVal.Text.Trim(), FtpAccountVal.Text.Trim(), FtpPwVal.Text.Trim());
- if (testConnect)
- {
- FtpAccountVal.Enabled = false;
- FtpPwVal.Enabled = false;
- timerVal.Enabled = false;
- FtpPathVal.Enabled = false;
- FileCountVal.Enabled = false;
- FtpConnect.Text = "关闭连接";
- FtpListen.Enabled = true;
- }
- else
- {
- FtpConnect.Enabled = true;
- FtpConnect.Text = "开启连接";
- }
- }
- else if(FtpConnect.Text == "关闭连接")
- {
- FtpAccountVal.Enabled = true;
- FtpPwVal.Enabled = true;
- timerVal.Enabled = true;
- FtpPathVal.Enabled = true;
- FileCountVal.Enabled = true;
- FtpConnect.Text = "开启连接";
- FtpListen.Enabled = false;
- }
- }
- public async Task<bool> TestFtpConnection(string ftpServer, string username, string password)
- {
- try
- {
- var request = (FtpWebRequest)WebRequest.Create(ftpServer);
- request.Method = WebRequestMethods.Ftp.ListDirectory;
- request.Credentials = new NetworkCredential(username, password);
- request.UsePassive = true;
- request.UseBinary = true;
- request.KeepAlive = false;
- request.Timeout = 10000;
- using (var response = await request.GetResponseAsync() as FtpWebResponse)
- {
- await Task.Run(() =>
- {
- LogMessage($"OK,FTP连接成功: {response.StatusDescription}");
- });
- return true;
- }
- }
- catch (WebException ex)
- {
- await Task.Run(() =>
- {
- if (ex.Response is FtpWebResponse response)
- {
- LogMessage($"NG,FTP连接失败,错误码: {(int)response.StatusCode} - {response.StatusDescription}");
- }
- else
- {
- LogMessage("NG,连接异常: " + ex.Message);
- }
- });
- return false;
- }
- catch (Exception ex)
- {
- await Task.Run(() =>
- {
- LogMessage($"NG,连接失败: " + ex.Message);
- });
- return false;
- }
- }
- private void FtpListen_Click(object sender, EventArgs e)
- {
- File_timer.Interval = Convert.ToInt32(timerVal.Text.Trim()) * 1000;
- if (FtpListen.Text == "开启监听")
- {
- FtpListen.Text = "关闭监听";
- FtpConnect.Enabled = false;
- File_timer.Start();
- Timer_Tick(null, null);
- }
- else if (FtpListen.Text == "关闭监听")
- {
- FtpListen.Text = "开启监听";
- FtpConnect.Enabled = true;
- File_timer.Stop();
- }
- }
- private void MsgBox_DrawItem(object sender, DrawItemEventArgs e)
- {
- e.DrawBackground();
- Brush mybsh = Brushes.Black;
- string msgStr = MsgBox.Items[e.Index].ToString();
- Console.WriteLine();
- if (MsgBox.Items[e.Index].ToString().Contains("OK"))
- {
- mybsh = Brushes.Green;
- }
- else if (MsgBox.Items[e.Index].ToString().Contains("NG"))
- {
- mybsh = Brushes.Red;
- }
- e.DrawFocusRectangle();
- e.Graphics.DrawString(MsgBox.Items[e.Index].ToString(), e.Font, mybsh, e.Bounds, StringFormat.GenericDefault);
- }
- private void LogMessage(string message)
- {
- if (MsgBox.InvokeRequired)
- {
- MsgBox.Invoke(new Action<string>(LogMessage), message);
- return;
- }
- MsgBox.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
- MsgBox.TopIndex = MsgBox.Items.Count - 1;
- MsgBox.DrawMode = DrawMode.OwnerDrawFixed;
- }
- private void Timer_Tick(object sender, EventArgs e)
- {
- try
- {
- string childSaveFiles = Path.Combine(saveFiles, DateTime.Now.ToString("yyyyMMdd"));
- if (!Directory.Exists(childSaveFiles))
- {
- Directory.CreateDirectory(childSaveFiles);
- currentNum = 1;
- LogMessage($"创建本地文件夹: {childSaveFiles}");
- }
- string ftpServer = FtpPathVal.Text.Trim();
- string remoteFolderPath = "";
- string username = FtpAccountVal.Text.Trim();
- string password = FtpPwVal.Text.Trim();
- List<string> fileList = GetFtpFileList(ftpServer, remoteFolderPath, username, password);
- if (fileList.Count == 0)
- {
- LogMessage($"远程文件夹 '{remoteFolderPath}' 中没有文件");
- return;
- }
- int successCount = 0;
- int failCount = 0;
- LogMessage($"开始下载 {fileList.Count} 个文件...");
- foreach (string remoteFile in fileList)
- {
- if (!Directory.Exists(childSaveFiles))
- {
- Directory.CreateDirectory(childSaveFiles);
- }
- string remoteFilePath = $"{remoteFolderPath.TrimEnd('/')}/{remoteFile}";
- string localFilePath = Path.Combine(childSaveFiles, remoteFile);
- if (DownloadSingleFile(ftpServer, remoteFilePath, localFilePath, username, password))
- {
- successCount++;
- }
- else
- {
- failCount++;
- }
- string[] files = Directory.GetFiles(childSaveFiles);
- string zipFilePath = Path.Combine(Path.GetDirectoryName(childSaveFiles), Path.GetFileName(childSaveFiles) + $"-{currentNum}.zip");
- if (Convert.ToInt32(FileCountVal.Text.Trim()) == files.Length)
- {
- CompressFolder(childSaveFiles, zipFilePath);
- currentNum++;
- }
- }
- LogMessage($"下载完成: 成功 {successCount} 个, 失败 {failCount} 个");
- }
- catch (Exception ex)
- {
- LogMessage($"NG,自动获取文件失败,Error: {ex.Message}");
- }
- }
- private List<string> GetFtpFileList(string ftpServer, string folderPath, string username, string password)
- {
- List<string> fileList = new List<string>();
- try
- {
- string uri = $"{ftpServer.TrimEnd('/')}/{folderPath.TrimStart('/')}";
- var request = (FtpWebRequest)WebRequest.Create(uri);
- request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
- request.Credentials = new NetworkCredential(username, password);
- request.UsePassive = true;
- request.UseBinary = true;
- request.KeepAlive = false;
- using (var response = (FtpWebResponse)request.GetResponse())
- using (var responseStream = response.GetResponseStream())
- using (var reader = new StreamReader(responseStream))
- {
- string line;
- while ((line = reader.ReadLine()) != null)
- {
- if (!string.IsNullOrWhiteSpace(line))
- {
- string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- if (parts.Length >= 3)
- {
- //string fileName = parts[8];
- //if (!parts[0].StartsWith("d") && fileName != "." && fileName != "..")
- //{
- // fileList.Add(fileName);
- //}
- string fileName = parts[parts.Length - 1];
- if (fileName.Contains("."))
- {
- fileList.Add(fileName);
- }
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- LogMessage($"获取文件列表失败: {ex.Message}");
- }
- return fileList;
- }
- private bool DownloadSingleFile(string ftpServer, string remoteFilePath, string localFilePath, string username, string password)
- {
- try
- {
- bool downloadSuccess = false;
- string uri = $"{ftpServer.TrimEnd('/')}/{remoteFilePath.TrimStart('/')}";
- var request = (FtpWebRequest)WebRequest.Create(uri);
- request.Method = WebRequestMethods.Ftp.DownloadFile;
- request.Credentials = new NetworkCredential(username, password);
- request.UsePassive = true;
- request.UseBinary = true;
- request.KeepAlive = false;
- using (var response = (FtpWebResponse)request.GetResponse())
- using (var responseStream = response.GetResponseStream())
- using (var fileStream = File.Create(localFilePath))
- {
- byte[] buffer = new byte[4096];
- int bytesRead;
- while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
- {
- fileStream.Write(buffer, 0, bytesRead);
- }
- downloadSuccess = true;
- LogMessage($"OK,文件下载成功: {remoteFilePath} -> {localFilePath}");
- if (downloadSuccess)
- {
- var deleteRequest = (FtpWebRequest)WebRequest.Create(uri);
- deleteRequest.Method = WebRequestMethods.Ftp.DeleteFile;
- deleteRequest.Credentials = new NetworkCredential(username, password);
- deleteRequest.UsePassive = true;
- deleteRequest.KeepAlive = false;
- using (var deleteResponse = (FtpWebResponse)deleteRequest.GetResponse())
- {
- LogMessage($"OK,服务器文件删除成功: {remoteFilePath}");
- }
- }
- return true;
- }
- }
- catch (WebException ex)
- {
- if (ex.Response is FtpWebResponse response)
- {
- LogMessage($"NG,下载失败 {remoteFilePath}: {(int)response.StatusCode} - {response.StatusDescription}");
- }
- else
- {
- LogMessage($"NG,下载失败 {remoteFilePath}: {ex.Message}");
- }
- return false;
- }
- catch (Exception ex)
- {
- LogMessage($"NG,下载失败 {remoteFilePath}: {ex.Message}");
- return false;
- }
- }
- private string CreateFtpDirectoryIfNotExists(string ftpDirectoryPath, string username, string password)
- {
- try
- {
- FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpDirectoryPath);
- request.Method = WebRequestMethods.Ftp.MakeDirectory;
- request.Credentials = new NetworkCredential(username, password);
- request.UsePassive = true;
- request.UseBinary = true;
- request.KeepAlive = false;
- using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
- {
- response.Close();
- return "OK,目录创建成功: " + response.StatusDescription;
- }
- }
- catch (WebException ex)
- {
- if (ex.Response is FtpWebResponse response && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
- {
- return "OK,目录已存在: " + response.StatusDescription;
- }
- else
- {
- return "NG,创建目录时发生错误: " + ex.Message;
- }
- }
- }
- private void CompressFile(string sourceFile, string zipPath)
- {
- try
- {
- using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Create))
- using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
- {
- string fileName = Path.GetFileName(sourceFile);
- ZipArchiveEntry readmeEntry = archive.CreateEntry(fileName);
- using (Stream stream = readmeEntry.Open())
- using (FileStream fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
- {
- fileStream.CopyTo(stream);
- }
- }
- LogMessage($"OK,文件已压缩到:{zipPath}");
- }
- catch (Exception ex)
- {
- LogMessage($"NG,压缩失败:{ex.Message}");
- }
- }
- private void CompressFolder(string sourceFolder, string zipPath)
- {
- try
- {
- if (File.Exists(zipPath))
- {
- try
- {
- File.Delete(zipPath);
- }
- catch (IOException ex)
- {
- LogMessage($"NG,无法删除已存在的文件,可能被其他程序占用:{ex.Message}");
- return;
- }
- catch (UnauthorizedAccessException ex)
- {
- LogMessage($"NG,没有权限删除文件:{ex.Message}");
- return;
- }
- }
- ZipFile.CreateFromDirectory(sourceFolder, zipPath);
- Directory.Delete(sourceFolder, true);
- LogMessage($"OK,文件已压缩到:{zipPath}");
- }
- catch (UnauthorizedAccessException ex)
- {
- LogMessage($"NG,访问被拒绝:{ex.Message}");
- }
- catch (Exception ex)
- {
- LogMessage($"NG,压缩失败:{ex.Message}");
- }
- }
- private void listenVal_Click(object sender, EventArgs e)
- {
- using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
- {
- folderDialog.Description = "请选择监控文件夹";
- folderDialog.SelectedPath = listenVal.Text;
- if (folderDialog.ShowDialog() == DialogResult.OK)
- {
- listenVal.Text = folderDialog.SelectedPath;
- }
- }
- }
- private void handPathVal_Click(object sender, EventArgs e)
- {
- using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
- {
- folderDialog.Description = "请选择导出文件夹";
- folderDialog.SelectedPath = handPathVal.Text;
- if (folderDialog.ShowDialog() == DialogResult.OK)
- {
- handPathVal.Text = folderDialog.SelectedPath;
- }
- }
- }
- private void buildConfirm_Click(object sender, EventArgs e)
- {
- if (buildConfirm.Text == "确定")
- {
- if (string.IsNullOrEmpty(handExeVal.Text))
- {
- LogMessage($"NG,请填写处理间隔时间");
- return;
- }
- if (string.IsNullOrEmpty(listenVal.Text))
- {
- LogMessage($"NG,请填写监控路径");
- return;
- }
- if (string.IsNullOrEmpty(handPathVal.Text))
- {
- LogMessage($"NG,请填写导出路径");
- return;
- }
- handExeVal.Enabled = false;
- handTime.Enabled = false;
- listenVal.Enabled = false;
- handPathVal.Enabled = false;
- buildConfirm.Text = "取消";
- }
- else
- {
- handExeVal.Enabled = true;
- handTime.Enabled = true;
- listenVal.Enabled = true;
- handPathVal.Enabled = true;
- buildConfirm.Text = "确定";
- }
- }
- private void buildStart_Click(object sender, EventArgs e)
- {
- if(buildStart.Text == "开启处理")
- {
- buildStart.Text = "关闭处理";
- File_timer1.Interval = Convert.ToInt32(handExeVal.Text) * 60 * 1000;
- File_timer1.Start();
- FileTimer_Tick(null, null);
- }
- else
- {
- buildStart.Text = "开启处理";
- File_timer.Stop();
- }
- }
- private async void FileTimer_Tick(object sender, EventArgs e)
- {
- _currentOperationCts?.Cancel();
- _currentOperationCts = new CancellationTokenSource();
- try
- {
- await Task.Run(() => ProcessFilesInternal(_currentOperationCts.Token));
- }
- catch (OperationCanceledException)
- {
- LogMessage("文件处理操作被新任务取消。");
- }
- catch (Exception ex)
- {
- LogMessage($"NG,定时器任务启动失败: {ex}");
- }
- /*try
- {
- string[] txtFiles = Directory.GetFiles(listenVal.Text, $"*.json");
- string currentHour = DateTime.Now.ToString("yyyyMMdd_hh");
- string outPath = Path.Combine(handPathVal.Text, currentHour + "_Count_" + txtFiles.Length);
- if (!Directory.Exists(outPath))
- {
- Directory.CreateDirectory(outPath);
- }
- foreach (string file in txtFiles)
- {
- string targetFile = Path.Combine(outPath, Path.GetFileName(file));
- if (File.Exists(targetFile))
- {
- File.Delete(targetFile);
- }
- File.Move(file, targetFile);
- }
- string zipPath = outPath + ".zip";
- if (File.Exists(zipPath))
- {
- File.Delete(zipPath);
- }
- ZipFile.CreateFromDirectory(outPath, zipPath);
- LogMessage($"OK,文件已压缩到:{zipPath}");
- }
- catch(Exception ex)
- {
- LogMessage($"NG,解析文件列表失败: {ex.Message}");
- }*/
- }
- private void ProcessFilesInternal(CancellationToken cancellationToken)
- {
- try
- {
- var txtFiles = Directory.EnumerateFiles(listenVal.Text, "*.json", SearchOption.TopDirectoryOnly).ToList();
- int fileCount = txtFiles.Count;
- if (fileCount == 0)
- {
- LogMessage("没有找到需要处理的 .json 文件。");
- return;
- }
- cancellationToken.ThrowIfCancellationRequested();
- LogMessage($"已找到找到 {fileCount} 个json文件,开始处理...");
- string currentHour = DateTime.Now.ToString("yyyyMMdd_HH");
- string outPath = Path.Combine(handPathVal.Text, $"{currentHour}_{Convert.ToInt32(handExeVal.Text)}_Count_{fileCount}");
- cancellationToken.ThrowIfCancellationRequested();
- if (!Directory.Exists(outPath))
- {
- Directory.CreateDirectory(outPath);
- }
- int movedCount = 0;
- foreach (string file in txtFiles)
- {
- cancellationToken.ThrowIfCancellationRequested();
- string targetFile = Path.Combine(outPath, Path.GetFileName(file));
- if (File.Exists(targetFile))
- {
- File.Delete(targetFile);
- }
- File.Move(file, targetFile);
- movedCount++;
- if (movedCount % 1000 == 0)
- {
- LogMessage($"已移动 {movedCount}/{fileCount} 个文件...");
- }
- }
- LogMessage($"成功移动 {movedCount} 个文件到 '{outPath}'");
- string zipPath = outPath + ".zip";
- cancellationToken.ThrowIfCancellationRequested();
- if (File.Exists(zipPath))
- {
- File.Delete(zipPath);
- }
- ZipFile.CreateFromDirectory(outPath, zipPath);
- //Directory.Delete(outPath, true);
- LogMessage($"OK, {fileCount} 个文件已成功压缩到: {zipPath}");
- }
- catch (OperationCanceledException)
- {
- LogMessage("OK, 文件处理操作被用户取消。");
- }
- catch (UnauthorizedAccessException ex)
- {
- LogMessage($"NG, 访问被拒绝,可能没有足够权限读写文件或目录: {ex.Message}");
- }
- catch (DirectoryNotFoundException ex)
- {
- LogMessage($"NG, 指定的路径不存在: {ex.Message}");
- }
- catch (IOException ex)
- {
- LogMessage($"NG, 文件 I/O 错误: {ex.Message}");
- }
- catch (Exception ex)
- {
- LogMessage($"NG, 文件处理过程中发生未知错误: {ex}");
- }
- }
- }
- }
|