Skip to content

Commit

Permalink
refactor(Automatic): 重构自动任务处理逻辑
Browse files Browse the repository at this point in the history
- 合并 AutomaticJob 和 AutomaticCustomJob 类,简化任务处理逻辑
- 优化任务调度和执行流程,提高代码可维护性
- 更新相关 ViewModel 和 View 以适应新的任务处理机制
  • Loading branch information
SaboZhang committed Oct 30, 2024
1 parent cc31064 commit bfff7a0
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 235 deletions.
24 changes: 24 additions & 0 deletions src/EasyTidy.Model/OperationParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyTidy.Model;

public class OperationParameters
{

public OperationMode OperationMode { get; set; }

public string SourcePath { get; set; }

public string TargetPath { get; set; }

public FileOperationType FileOperationType { get; set; }

public List<Func<string, bool>> Funcs { get; set; }

public RuleModel RuleModel { get; set; }

}
6 changes: 3 additions & 3 deletions src/EasyTidy.Service/FileActuator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void ExecuteFileOperation(OperationMode operationMode, string sour
// 处理文件夹操作
ProcessDirectory(sourcePath, targetPath, operationMode, fileOperationType, pathFilters);
}
else if (File.Exists(sourcePath))
else if (File.Exists(sourcePath) || File.Exists(targetPath))
{
// 处理单个文件操作
ProcessFile(sourcePath, targetPath, operationMode, fileOperationType, pathFilters);
Expand Down Expand Up @@ -77,13 +77,13 @@ private static void ProcessFile(string sourcePath, string targetPath, OperationM
CopyFile(sourcePath, targetPath, fileOperationType);
break;
case OperationMode.Delete:
DeleteFile(sourcePath);
DeleteFile(targetPath);
break;
case OperationMode.Rename:
RenameFile(sourcePath, targetPath);
break;
case OperationMode.RecycleBin:
MoveToRecycleBin(sourcePath);
MoveToRecycleBin(targetPath);
break;
default:
throw new NotSupportedException($"Operation mode '{operationMode}' is not supported.");
Expand Down
2 changes: 1 addition & 1 deletion src/EasyTidy.Service/FileEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class FileEventHandler
/// <param name="targetPath"></param>
/// <param name="delaySeconds"></param>
/// <param name="fileOperationType"></param>
public static void MonitorFolder(OperationMode operationMode, string folderPath, string targetPath, int delaySeconds, FileOperationType fileOperationType, RuleModel filter)
public static void MonitorFolder(OperationMode operationMode, string folderPath, string targetPath, int delaySeconds, RuleModel filter, FileOperationType fileOperationType = FileOperationType.Skip)
{
if (_watchers.ContainsKey(folderPath)) return; // 防止重复监控

Expand Down
40 changes: 26 additions & 14 deletions src/EasyTidy.Service/OperationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace EasyTidy.Service;
public static class OperationHandler
{
// 创建委托类型用于操作方法
private delegate void OperationMethod(string parameter);
private delegate void OperationMethod(OperationParameters parameter);

// 使用字典映射操作名称到方法
private static readonly Dictionary<OperationMode, Func<string, Task>> _operations;
private static readonly Dictionary<OperationMode, Func<OperationParameters, Task>> _operations;

static OperationHandler()
{
_operations = new Dictionary<OperationMode, Func<string, Task>>
_operations = new Dictionary<OperationMode, Func<OperationParameters, Task>>
{
{ OperationMode.Move, MoveAsync },
{ OperationMode.Copy, CopyAsync },
Expand All @@ -26,7 +26,7 @@ static OperationHandler()
}

// 执行操作的方法
public static async Task ExecuteOperationAsync(OperationMode operationValue, string parameter)
public static async Task ExecuteOperationAsync(OperationMode operationValue, OperationParameters parameter)
{
if (Enum.IsDefined(typeof(OperationMode), operationValue))
{
Expand All @@ -48,36 +48,48 @@ public static async Task ExecuteOperationAsync(OperationMode operationValue, str
}

// 操作方法示例
private static async Task MoveAsync(string parameter)
private static async Task MoveAsync(OperationParameters parameter)
{
await Task.Run(() =>
{
FileActuator.ExecuteFileOperation(OperationMode.Move, parameter, parameter, FileOperationType.Override);
FileActuator.ExecuteFileOperation(OperationMode.Move, parameter.SourcePath, parameter.TargetPath, FileOperationType.Override);
});
Console.WriteLine("执行移动操作");
}

private static async Task CopyAsync(string parameter)
private static async Task CopyAsync(OperationParameters parameter)
{
await Task.Delay(500);
await Task.Run(() =>
{
FileActuator.ExecuteFileOperation(OperationMode.Copy, parameter.SourcePath, parameter.TargetPath, FileOperationType.Override);
});
Console.WriteLine("执行复制操作");
}

private static async Task DeleteAsync(string parameter)
private static async Task DeleteAsync(OperationParameters parameter)
{
await Task.Delay(500);
await Task.Run(() =>
{
FileActuator.ExecuteFileOperation(OperationMode.Delete, parameter.SourcePath, parameter.TargetPath, FileOperationType.Override);
});
Console.WriteLine("执行删除操作");
}

private static async Task RenameAsync(string parameter)
private static async Task RenameAsync(OperationParameters parameter)
{
await Task.Delay(500);
await Task.Run(() =>
{
FileActuator.ExecuteFileOperation(OperationMode.Rename, parameter.SourcePath, parameter.TargetPath, FileOperationType.Override);
});
Console.WriteLine("重命名逻辑");
}

private static async Task RecycleBinAsync(string parameter)
private static async Task RecycleBinAsync(OperationParameters parameter)
{
await Task.Delay(500);
await Task.Run(() =>
{
FileActuator.ExecuteFileOperation(OperationMode.RecycleBin, parameter.SourcePath, parameter.TargetPath, FileOperationType.Override);
});
Console.WriteLine("回收站逻辑");
}
}
75 changes: 52 additions & 23 deletions src/EasyTidy.Util/CronExpressionUtil.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using EasyTidy.Model;
using Quartz;
using Quartz.Impl.Triggers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyTidy.Util;

Expand Down Expand Up @@ -54,6 +54,45 @@ string ProcessCronField(string field, string defaultValue = "*")
return cronExpression;
}

public static string GenerateCronExpression(string dialogMinutes, string dialogHours, string dialogDayOfMonth, string dialogMonth, string dialogDayOfWeek)
{
string ProcessCronField(string field, string defaultValue = "*")
{
if (string.IsNullOrWhiteSpace(field))
return defaultValue;

// Split by comma and process each value
var values = field.Split(',')
.Select(v => v.Trim())
.Select(v => v.Contains('-') || v.Contains('/') ? v : (v == "" ? "*" : v))
.ToArray();

return string.Join(",", values);
}

// 设置 Cron 表达式的各个部分
string seconds = "0"; // 固定为0秒
string minutes = ProcessCronField(dialogMinutes); // 如果没有定义,使用 "*" 表示每分钟
string hours = ProcessCronField(dialogHours); // 如果没有定义,使用 "*" 表示每小时
string dayOfMonth = ProcessCronField(dialogDayOfMonth); // 没有定义时,表示每天
string month = ProcessCronField(dialogMonth); // 没有定义时,表示每月
string dayOfWeek = dialogDayOfWeek ?? "?"; // 使用 "?" 忽略周几

// 检查是否为每月的特定日
if (string.IsNullOrEmpty(dialogMonth))
{
dayOfWeek = "?"; // 如果有特定月份,则忽略周几
}
else if (string.IsNullOrEmpty(dialogDayOfWeek))
{
dayOfMonth = "?"; // 如果有特定周几,则忽略具体日
}

// 拼接 Cron 表达式
string cronExpression = $"{seconds} {minutes} {hours} {dayOfMonth} {month} {dayOfWeek}";
return cronExpression;
}

/// <summary>
/// 验证 Cron 表达式是否有效
/// </summary>
Expand All @@ -63,27 +102,17 @@ public static (bool IsValid, string Message, List<DateTime> FireTimes) Verificat
{
try
{
// 创建 Cron 表达式对象
ICronExpression cron = CronExpression.Parse(cronExpression);

// 获取当前时间
DateTime now = DateTime.UtcNow;
// 存储最近五次的执行时间
var fireTimes = new List<DateTime>();
// 计算最近五次的执行时间
for (int i = 0; i < 5; i++)
{
DateTime? nextFireTime = cron.GetPreviousFireTimeUtc(now);
if (nextFireTime.HasValue)
{
fireTimes.Add(nextFireTime.Value.ToLocalTime());
now = nextFireTime.Value;
}
else
{
break;
}
}
// 创建 CronTrigger 实例并设置 Cron 表达式
CronTriggerImpl cronTriggerImpl = new();
CronExpression cron = new(cronExpression);
cronTriggerImpl.CronExpression = cron;

// 计算触发时间
var dates = TriggerUtils.ComputeFireTimes(cronTriggerImpl, null, 8);

// 将 DateTimeOffset 转换为 DateTime
List<DateTime> fireTimes = dates.Select(dt => dt.DateTime).ToList();


// 输出结果
if (fireTimes.Count > 0)
Expand Down
1 change: 1 addition & 0 deletions src/EasyTidy.Util/EasyTidy.Util.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240829007" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
<PackageReference Include="CommunityToolkit.WinUI.Extensions" Version="8.1.240916" />
<PackageReference Include="Quartz" Version="3.13.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="WebDAVClient" Version="2.2.1" />
</ItemGroup>
Expand Down
119 changes: 0 additions & 119 deletions src/EasyTidy/Common/Job/AutomaticCustomJob.cs

This file was deleted.

Loading

0 comments on commit bfff7a0

Please sign in to comment.