| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499 |
- 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();
- }
- 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}");
- }
- }
- }
- }
|