| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636 |
- 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
- {
- 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";
- 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();
- Application excelApp = null;
- Workbook workbook = null;
- Worksheet worksheet = null;
- Range range = null;
- string tempTemplatePath = null;
- try
- {
- string checkExportModel = Path.Combine(settingRightInput2.Text, BoxNo.Text.Trim() + ".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 = '{BoxNo.Text.Trim()}'");
- if (dt.Rows.Count > 0)
- {
- worksheet.Cells[1, 3] = DateTime.Now.ToString("yyyy-MM-dd");
- worksheet.Cells[1, 7] = BoxNo.Text.Trim();
- 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();
- 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}', '{row.Cells[1].Value}',
- '{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;
- dt = ConnectDB.ExecuteSelect($@"SELECT * FROM g_packing_sncheck a,g_packing_rules b WHERE a.outbox_no = '{BoxNo.Text}' 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($"核对完成,此箱{BoxNo.Text} 已装满{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}");
- }
- }
- }
- }
|