From 5da3a276b7847187a7c155ee069d3cd4c9e58074 Mon Sep 17 00:00:00 2001
From: HuBingJie <3146306518@qq.com>
Date: 星期日, 07 十二月 2025 23:28:14 +0800
Subject: [PATCH] 1

---
 代码管理/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs |  155 +++++++++++++++++++++++++++------------------------
 1 files changed, 81 insertions(+), 74 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs"
index 68e8fae..62cf614 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs"
@@ -1,24 +1,8 @@
-锘縰sing HslCommunication;
-using HslCommunication.Core;
-using Microsoft.AspNetCore.Http;
-using Microsoft.VisualBasic.FileIO;
-using Newtonsoft.Json;
-using Quartz;
-using SixLabors.ImageSharp.PixelFormats;
+锘縰sing Quartz;
 using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Linq;
-using System.Reflection;
-using System.Reflection.Metadata.Ecma335;
-using System.Text;
+using System.IO;
 using System.Threading.Tasks;
 using WIDESEA_Common.Log;
-using WIDESEAWCS_Communicator;
-using WIDESEAWCS_Core.Helper;
-using WIDESEAWCS_QuartzJob;
-using WIDESEAWCS_QuartzJob.DeviceBase;
-using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
 
 namespace WIDESEAWCS_Tasks
 {
@@ -29,19 +13,25 @@
         {
             try
             {
-
                 Task.Run(() =>
                 {
                     try
                     {
                         WriteLog.GetLog("LogJob").Write($"鏃ュ織璁板綍锛歿DateTime.Now}", "LogJob");
-                        Run(10);
-                        Run2(10);
+
+                        // 璁剧疆淇濈暀澶╂暟涓�30澶�
+                        int saveDays = 30;
+
+                        // 娓呯悊Log鐩綍涓嬬殑鎵�鏈夎繃鏈熸枃浠跺す
+                        CleanLogDirectory(saveDays);
+
                         Thread.Sleep(1000 * 10);
                     }
-                    catch { }
+                    catch (Exception ex)
+                    {
+                        WriteLog.GetLog("LogJob").Write($"鏃ュ織娓呯悊鍑洪敊锛歿ex.Message}", "LogJob");
+                    }
                 });
-
             }
             catch (Exception ex)
             {
@@ -50,74 +40,91 @@
 
             return Task.CompletedTask;
         }
-        private static void Run(int saveDays)
+
+        /// <summary>
+        /// 娓呯悊Log鐩綍涓嬬殑鎵�鏈夎繃鏈熸棩蹇楁枃浠跺す
+        /// </summary>
+        /// <param name="saveDays">淇濈暀澶╂暟</param>
+        private static void CleanLogDirectory(int saveDays)
         {
-            var logPath = System.Environment.CurrentDirectory + "/log";
-            if (Directory.Exists(logPath))
+            // 鑾峰彇Log鐩綍璺緞
+            var logBasePath = Path.Combine(Environment.CurrentDirectory, "Log");
+
+            if (!Directory.Exists(logBasePath))
             {
-                var nowTime = DateTime.Now;
-                var dayDir = Directory.GetDirectories(logPath, "20*", System.IO.SearchOption.AllDirectories);
+                WriteLog.GetLog("LogJob").Write($"Log鐩綍涓嶅瓨鍦細{logBasePath}", "LogJob");
+                return;
+            }
 
-                foreach (var dirpath in dayDir)
+            var nowTime = DateTime.Now;
+            var cutoffDate = nowTime.AddDays(-saveDays);
+
+            WriteLog.GetLog("LogJob").Write($"寮�濮嬫竻鐞嗘棩蹇楋紝淇濈暀澶╂暟锛歿saveDays}锛屾埅姝㈡棩鏈燂細{cutoffDate:yyyy-MM-dd}", "LogJob");
+
+            // 鑾峰彇Log鐩綍涓嬬殑鎵�鏈夊瓙鐩綍
+            var allSubDirs = Directory.GetDirectories(logBasePath, "*", SearchOption.AllDirectories);
+
+            foreach (var dirPath in allSubDirs)
+            {
+                try
                 {
-                    //鏂囦欢澶瑰悕
-                    var dirName = Path.GetFileName(dirpath).Replace("_", "-");
+                    // 鑾峰彇鏂囦欢澶瑰悕
+                    var dirName = Path.GetFileName(dirPath);
 
-                    DateTime targetDate = DateTime.ParseExact(dirName, "yyyyMMdd", null);
-                    if ((nowTime - targetDate).TotalDays >= saveDays)
+                    // 灏濊瘯瑙f瀽涓烘棩鏈燂紙鏀寔澶氱鏍煎紡锛�
+                    if (TryParseDateFolder(dirName, out DateTime folderDate))
                     {
-
-                        //鍒犻櫎鏂囦欢鍜屾枃浠跺す
-                        try
+                        // 鍒ゆ柇鏄惁杩囨湡
+                        if (folderDate < cutoffDate)
                         {
-                            var flies = Directory.GetFiles(dirpath);
+                            WriteLog.GetLog("LogJob").Write($"鍒犻櫎杩囨湡鏃ュ織鏂囦欢澶癸細{dirPath}锛屾棩鏈燂細{folderDate:yyyy-MM-dd}", "LogJob");
 
-                            foreach (var item in flies)
-                            {
-                                File.Delete(item);
-                            }
-                            Directory.Delete(dirpath);
+                            // 閫掑綊鍒犻櫎鏂囦欢澶瑰強鍏跺唴瀹�
+                            Directory.Delete(dirPath, true);
                         }
-                        catch { }
+                        else
+                        {
+                            WriteLog.GetLog("LogJob").Write($"淇濈暀鏃ュ織鏂囦欢澶癸細{dirPath}锛屾棩鏈燂細{folderDate:yyyy-MM-dd}", "LogJob");
+                        }
                     }
                 }
+                catch (Exception ex)
+                {
+                    WriteLog.GetLog("LogJob").Write($"澶勭悊鏂囦欢澶� {dirPath} 鏃跺嚭閿欙細{ex.Message}", "LogJob");
+                }
             }
+
+            WriteLog.GetLog("LogJob").Write($"鏃ュ織娓呯悊瀹屾垚", "LogJob");
         }
 
-        private static void Run2(int saveDays)
+        /// <summary>
+        /// 灏濊瘯瑙f瀽鏃ユ湡鏂囦欢澶瑰悕绉�
+        /// </summary>
+        /// <param name="folderName">鏂囦欢澶瑰悕绉�</param>
+        /// <param name="date">瑙f瀽鍑虹殑鏃ユ湡</param>
+        /// <returns>鏄惁鎴愬姛瑙f瀽</returns>
+        private static bool TryParseDateFolder(string folderName, out DateTime date)
         {
-            var logPath = System.Environment.CurrentDirectory + "/txt";
-            if (Directory.Exists(logPath))
+            // 鏀寔鐨勬棩鏈熸牸寮�
+            string[] dateFormats =
             {
-                var nowTime = DateTime.Now;
-                var dayDir = Directory.GetDirectories(logPath, "20*", System.IO.SearchOption.AllDirectories);
+                "yyyy-MM-dd",   // 2025-12-07
+                "yyyyMMdd",     // 20251207
+                "yyyy_MM_dd",   // 2025_12_07
+                "yyyy.MM.dd",   // 2025.12.07
+                "yyyy骞碝M鏈坉d鏃�" // 2025骞�12鏈�07鏃�
+            };
 
-                foreach (var dirpath in dayDir)
-                {
-                    //鏂囦欢澶瑰悕
-                    var dirName = Path.GetFileName(dirpath).Replace("_", "-");
+            // 灏濊瘯瑙f瀽鏃ユ湡
+            bool success = DateTime.TryParseExact(
+                folderName,
+                dateFormats,
+                System.Globalization.CultureInfo.InvariantCulture,
+                System.Globalization.DateTimeStyles.None,
+                out date
+            );
 
-                    DateTime targetDate = DateTime.ParseExact(dirName, "yyyyMMdd", null);
-                    if ((nowTime - targetDate).TotalDays >= saveDays)
-                    {
-
-                        //鍒犻櫎鏂囦欢鍜屾枃浠跺す
-                        try
-                        {
-                            var flies = Directory.GetFiles(dirpath);
-
-                            foreach (var item in flies)
-                            {
-                                File.Delete(item);
-                            }
-                            Directory.Delete(dirpath);
-                        }
-                        catch { }
-                    }
-                }
-            }
+            return success;
         }
     }
-
-
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3