From 8d558cb173873004cd8a0d6c12f37865f03d7a4f Mon Sep 17 00:00:00 2001
From: huanghongfeng <huanghongfeng@hnkhzn.com>
Date: 星期三, 22 十月 2025 10:32:15 +0800
Subject: [PATCH] 1

---
 代码管理/WCS/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/LogJob.cs |  259 ++++++++++++++++-----------------------------------
 1 files changed, 83 insertions(+), 176 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 684b1e5..68e8fae 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,216 +1,123 @@
-锘縰sing Quartz;
+锘縰sing HslCommunication;
+using HslCommunication.Core;
+using Microsoft.AspNetCore.Http;
+using Microsoft.VisualBasic.FileIO;
+using Newtonsoft.Json;
+using Quartz;
+using SixLabors.ImageSharp.PixelFormats;
 using System;
-using System.IO;
-using System.Threading;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Reflection;
+using System.Reflection.Metadata.Ecma335;
+using System.Text;
 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
     {
-        // 瀹氫箟鍏佽鍒犻櫎鐨勬牴鐩綍
-        private static readonly string LogRootPath = Path.GetFullPath(Environment.CurrentDirectory + "/Log");
-        private static readonly string PlatformInfoPath = Path.GetFullPath(Environment.CurrentDirectory + "/Log/绔欏彴璇诲彇淇℃伅璁板綍");
-
         public Task Execute(IJobExecutionContext context)
         {
             try
             {
+
                 Task.Run(() =>
                 {
                     try
                     {
-                            // 娓呯悊鏃ュ織鏂囦欢
-                            CleanLogFilesOnly(LogRootPath, TimeSpan.FromMinutes(43200));
-                            CleanAllInPlatformInfo(PlatformInfoPath, TimeSpan.FromMinutes(43200));
-
-                            Thread.Sleep(1000 * 10); // 10绉掑悗鍐嶆鎵ц
+                        WriteLog.GetLog("LogJob").Write($"鏃ュ織璁板綍锛歿DateTime.Now}", "LogJob");
+                        Run(10);
+                        Run2(10);
+                        Thread.Sleep(1000 * 10);
                     }
-                    catch (Exception ex)
-                    {
-                        Console.WriteLine($"鏃ュ織浠诲姟鎵ц寮傚父: {ex.Message}");
-                    }
+                    catch { }
                 });
+
             }
             catch (Exception ex)
             {
-                Console.WriteLine($"{nameof(LogJob)} 鍒濆鍖栧紓甯�: {ex.Message}");
+                Console.Out.WriteLine(nameof(LogJob) + ":" + ex.Message);
             }
 
             return Task.CompletedTask;
         }
-
-        /// <summary>
-        /// 妫�鏌ヨ矾寰勬槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
-        /// </summary>
-        /// <param name="path">瑕佹鏌ョ殑璺緞</param>
-        /// <param name="allowedRoot">鍏佽鐨勬牴鐩綍</param>
-        /// <returns>鏄惁鍏佽鍒犻櫎</returns>
-        private static bool IsPathAllowed(string path, string allowedRoot)
+        private static void Run(int saveDays)
         {
-            try
+            var logPath = System.Environment.CurrentDirectory + "/log";
+            if (Directory.Exists(logPath))
             {
-                var fullPath = Path.GetFullPath(path);
-                return fullPath.StartsWith(allowedRoot, StringComparison.OrdinalIgnoreCase);
-            }
-            catch
-            {
-                return false;
-            }
-        }
+                var nowTime = DateTime.Now;
+                var dayDir = Directory.GetDirectories(logPath, "20*", System.IO.SearchOption.AllDirectories);
 
-        /// <summary>
-        /// 鍙竻鐞哃og鐩綍涓嬬殑鏂囦欢
-        /// </summary>
-        /// <param name="directoryPath">瑕佹竻鐞嗙殑鐩綍璺緞</param>
-        /// <param name="maxAge">鏂囦欢鏈�澶т繚鐣欐椂闂�</param>
-        private static void CleanLogFilesOnly(string directoryPath, TimeSpan maxAge)
-        {
-            if (!Directory.Exists(directoryPath))
-            {
-                //Console.WriteLine($"鐩綍涓嶅瓨鍦�: {directoryPath}");
-                return;
-            }
-
-            try
-            {
-                var now = DateTime.Now;
-                // 鍙幏鍙栧綋鍓嶇洰褰曠殑鏂囦欢锛屼笉鍖呭惈瀛愮洰褰�
-                var files = Directory.GetFiles(directoryPath, "*", SearchOption.TopDirectoryOnly);
-                int deletedCount = 0;
-
-                foreach (var file in files)
+                foreach (var dirpath in dayDir)
                 {
-                    try
+                    //鏂囦欢澶瑰悕
+                    var dirName = Path.GetFileName(dirpath).Replace("_", "-");
+
+                    DateTime targetDate = DateTime.ParseExact(dirName, "yyyyMMdd", null);
+                    if ((nowTime - targetDate).TotalDays >= saveDays)
                     {
-                        // 妫�鏌ユ枃浠舵槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
-                        if (!IsPathAllowed(file, LogRootPath))
+
+                        //鍒犻櫎鏂囦欢鍜屾枃浠跺す
+                        try
                         {
-                            Console.WriteLine($"璺宠繃闈濴og鐩綍鏂囦欢: {file}");
-                            continue;
+                            var flies = Directory.GetFiles(dirpath);
+
+                            foreach (var item in flies)
+                            {
+                                File.Delete(item);
+                            }
+                            Directory.Delete(dirpath);
                         }
-
-                        // 鑾峰彇鏂囦欢鍒涘缓鏃堕棿
-                        var creationTime = File.GetCreationTime(file);
-
-                        // 妫�鏌ユ枃浠舵槸鍚﹁秴杩囨寚瀹氭椂闂�
-                        if (now - creationTime > maxAge)
-                        {
-                            File.Delete(file);
-                            deletedCount++;
-                            //Console.WriteLine($"宸插垹闄ゆ枃浠�: {file}");
-                        }
-                    }
-                    catch (Exception ex)
-                    {
-                        Console.WriteLine($"鍒犻櫎鏂囦欢 {file} 鏃跺嚭閿�: {ex.Message}");
-                    }
-                }
-
-                //Console.WriteLine($"鐩綍 {directoryPath} 娓呯悊瀹屾垚锛屽垹闄や簡 {deletedCount} 涓枃浠�");
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine($"娓呯悊鐩綍 {directoryPath} 鏃跺嚭閿�: {ex.Message}");
-            }
-        }
-
-        /// <summary>
-        /// 娓呯悊绔欏彴璇诲彇淇℃伅璁板綍鐩綍涓嬬殑鎵�鏈夊唴瀹瑰拰瀛愮洰褰�
-        /// </summary>
-        /// <param name="directoryPath">瑕佹竻鐞嗙殑鐩綍璺緞</param>
-        /// <param name="maxAge">鏂囦欢鏈�澶т繚鐣欐椂闂�</param>
-        private static void CleanAllInPlatformInfo(string directoryPath, TimeSpan maxAge)
-        {
-            if (!Directory.Exists(directoryPath))
-            {
-                //Console.WriteLine($"鐩綍涓嶅瓨鍦�: {directoryPath}");
-                return;
-            }
-
-            try
-            {
-                var now = DateTime.Now;
-                int deletedCount = 0;
-
-                // 鍒犻櫎鎵�鏈夎繃鏈熺殑鏂囦欢
-                var files = Directory.GetFiles(directoryPath, "*", SearchOption.AllDirectories);
-                foreach (var file in files)
-                {
-                    try
-                    {
-                        // 妫�鏌ユ枃浠舵槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
-                        if (!IsPathAllowed(file, PlatformInfoPath))
-                        {
-                            Console.WriteLine($"璺宠繃闈炵珯鍙颁俊鎭洰褰曟枃浠�: {file}");
-                            continue;
-                        }
-
-                        var creationTime = File.GetCreationTime(file);
-                        if (now - creationTime > maxAge)
-                        {
-                            File.Delete(file);
-                            deletedCount++;
-                            //Console.WriteLine($"宸插垹闄ゆ枃浠�: {file}");
-                        }
-                    }
-                    catch (Exception ex)
-                    {
-                        Console.WriteLine($"鍒犻櫎鏂囦欢 {file} 鏃跺嚭閿�: {ex.Message}");
-                    }
-                }
-
-                // 鍒犻櫎鎵�鏈夌┖鐩綍锛堝寘鎷瓙鐩綍锛�
-                DeleteAllEmptySubdirectories(directoryPath);
-
-                //Console.WriteLine($"鐩綍 {directoryPath} 娓呯悊瀹屾垚锛屽垹闄や簡 {deletedCount} 涓枃浠�");
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine($"娓呯悊鐩綍 {directoryPath} 鏃跺嚭閿�: {ex.Message}");
-            }
-        }
-
-        /// <summary>
-        /// 閫掑綊鍒犻櫎鎵�鏈夌┖瀛愮洰褰�
-        /// </summary>
-        /// <param name="directoryPath">瑕佹竻鐞嗙殑鐩綍璺緞</param>
-        private static void DeleteAllEmptySubdirectories(string directoryPath)
-        {
-            if (!Directory.Exists(directoryPath))
-                return;
-
-            try
-            {
-                // 閫掑綊澶勭悊鎵�鏈夊瓙鐩綍
-                foreach (var subDirectory in Directory.GetDirectories(directoryPath))
-                {
-                    // 妫�鏌ョ洰褰曟槸鍚﹀湪鍏佽鍒犻櫎鐨勮寖鍥村唴
-                    if (!IsPathAllowed(subDirectory, PlatformInfoPath))
-                    {
-                        Console.WriteLine($"璺宠繃闈炵珯鍙颁俊鎭洰褰�: {subDirectory}");
-                        continue;
-                    }
-
-                    DeleteAllEmptySubdirectories(subDirectory);
-
-                    // 妫�鏌ュ瓙鐩綍鏄惁涓虹┖
-                    var files = Directory.GetFiles(subDirectory);
-                    var directories = Directory.GetDirectories(subDirectory);
-
-                    if (files.Length == 0 && directories.Length == 0)
-                    {
-                        Directory.Delete(subDirectory);
-                        //Console.WriteLine($"宸插垹闄ょ┖鐩綍: {subDirectory}");
+                        catch { }
                     }
                 }
             }
-            catch (Exception ex)
+        }
+
+        private static void Run2(int saveDays)
+        {
+            var logPath = System.Environment.CurrentDirectory + "/txt";
+            if (Directory.Exists(logPath))
             {
-                Console.WriteLine($"澶勭悊鐩綍 {directoryPath} 鏃跺嚭閿�: {ex.Message}");
+                var nowTime = DateTime.Now;
+                var dayDir = Directory.GetDirectories(logPath, "20*", System.IO.SearchOption.AllDirectories);
+
+                foreach (var dirpath in dayDir)
+                {
+                    //鏂囦欢澶瑰悕
+                    var dirName = Path.GetFileName(dirpath).Replace("_", "-");
+
+                    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 { }
+                    }
+                }
             }
         }
     }
-}
\ No newline at end of file
+
+
+}

--
Gitblit v1.9.3