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 |  139 ++++++++++++++++++++++++++++++++--------------
 1 files changed, 97 insertions(+), 42 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 ebb0563..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,50 +1,37 @@
-锘縰sing HslCommunication;
-using HslCommunication.Core;
-using HslCommunication.WebSocket;
-using Microsoft.AspNetCore.Http;
-using Microsoft.VisualBasic.FileIO;
-using Newtonsoft.Json;
-using Quartz;
-using Quartz.Util;
-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
 {
     [DisallowConcurrentExecution]
     public class LogJob : IJob
     {
-        static object sendData = null;
         public Task Execute(IJobExecutionContext context)
         {
             try
             {
-               
                 Task.Run(() =>
                 {
                     try
                     {
-                        while (true)
-                        {
-                            Run(5);
-                        }
-                    }
-                    catch { }
-                });
+                        WriteLog.GetLog("LogJob").Write($"鏃ュ織璁板綍锛歿DateTime.Now}", "LogJob");
 
+                        // 璁剧疆淇濈暀澶╂暟涓�30澶�
+                        int saveDays = 30;
+
+                        // 娓呯悊Log鐩綍涓嬬殑鎵�鏈夎繃鏈熸枃浠跺す
+                        CleanLogDirectory(saveDays);
+
+                        Thread.Sleep(1000 * 10);
+                    }
+                    catch (Exception ex)
+                    {
+                        WriteLog.GetLog("LogJob").Write($"鏃ュ織娓呯悊鍑洪敊锛歿ex.Message}", "LogJob");
+                    }
+                });
             }
             catch (Exception ex)
             {
@@ -53,23 +40,91 @@
 
             return Task.CompletedTask;
         }
-        private static void Run(int saveDays)
+
+        /// <summary>
+        /// 娓呯悊Log鐩綍涓嬬殑鎵�鏈夎繃鏈熸棩蹇楁枃浠跺す
+        /// </summary>
+        /// <param name="saveDays">淇濈暀澶╂暟</param>
+        private static void CleanLogDirectory(int saveDays)
         {
-            sendData = new { code = "1002", Messagest = $"杞﹁疆SN鍙凤細123123123" };
-            CacheData.WebSocket.PublishAllClientPayload(sendData.Serialize());
+            // 鑾峰彇Log鐩綍璺緞
+            var logBasePath = Path.Combine(Environment.CurrentDirectory, "Log");
+
+            if (!Directory.Exists(logBasePath))
+            {
+                WriteLog.GetLog("LogJob").Write($"Log鐩綍涓嶅瓨鍦細{logBasePath}", "LogJob");
+                return;
+            }
+
+            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);
+
+                    // 灏濊瘯瑙f瀽涓烘棩鏈燂紙鏀寔澶氱鏍煎紡锛�
+                    if (TryParseDateFolder(dirName, out DateTime folderDate))
+                    {
+                        // 鍒ゆ柇鏄惁杩囨湡
+                        if (folderDate < cutoffDate)
+                        {
+                            WriteLog.GetLog("LogJob").Write($"鍒犻櫎杩囨湡鏃ュ織鏂囦欢澶癸細{dirPath}锛屾棩鏈燂細{folderDate:yyyy-MM-dd}", "LogJob");
+
+                            // 閫掑綊鍒犻櫎鏂囦欢澶瑰強鍏跺唴瀹�
+                            Directory.Delete(dirPath, true);
+                        }
+                        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");
         }
 
-        public class CacheData
+        /// <summary>
+        /// 灏濊瘯瑙f瀽鏃ユ湡鏂囦欢澶瑰悕绉�
+        /// </summary>
+        /// <param name="folderName">鏂囦欢澶瑰悕绉�</param>
+        /// <param name="date">瑙f瀽鍑虹殑鏃ユ湡</param>
+        /// <returns>鏄惁鎴愬姛瑙f瀽</returns>
+        private static bool TryParseDateFolder(string folderName, out DateTime date)
         {
-            public static WebSocketServer WebSocket = new WebSocketServer();
-
-            public static void StartServer()
+            // 鏀寔鐨勬棩鏈熸牸寮�
+            string[] dateFormats =
             {
-                WebSocket = new WebSocketServer();
-                WebSocket.ServerStart(1880);
-            }
+                "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鏃�
+            };
+
+            // 灏濊瘯瑙f瀽鏃ユ湡
+            bool success = DateTime.TryParseExact(
+                folderName,
+                dateFormats,
+                System.Globalization.CultureInfo.InvariantCulture,
+                System.Globalization.DateTimeStyles.None,
+                out date
+            );
+
+            return success;
         }
     }
-
-
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3