2026-01-09 14:03:01 +08:00
|
|
|
|
using Quartz;
|
2026-01-19 13:39:59 +08:00
|
|
|
|
using Service.Budget;
|
2026-01-09 14:03:01 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Service.Jobs;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 预算归档定时任务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[DisallowConcurrentExecution]
|
|
|
|
|
|
public class BudgetArchiveJob(
|
|
|
|
|
|
IServiceProvider serviceProvider,
|
|
|
|
|
|
ILogger<BudgetArchiveJob> logger) : IJob
|
|
|
|
|
|
{
|
|
|
|
|
|
public async Task Execute(IJobExecutionContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("开始执行预算归档任务");
|
|
|
|
|
|
|
|
|
|
|
|
// 每个月1号执行,归档上个月的数据
|
2026-01-09 15:42:59 +08:00
|
|
|
|
var targetDate = DateTime.Now.AddMonths(-1);
|
2026-01-09 14:03:01 +08:00
|
|
|
|
var year = targetDate.Year;
|
|
|
|
|
|
var month = targetDate.Month;
|
|
|
|
|
|
|
|
|
|
|
|
using var scope = serviceProvider.CreateScope();
|
|
|
|
|
|
var budgetService = scope.ServiceProvider.GetRequiredService<IBudgetService>();
|
2026-01-12 22:29:39 +08:00
|
|
|
|
|
|
|
|
|
|
// 归档月度数据
|
2026-01-09 14:03:01 +08:00
|
|
|
|
var result = await budgetService.ArchiveBudgetsAsync(year, month);
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(result))
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogInformation("归档 {Year}年{Month}月 预算任务执行成功", year, month);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("归档 {Year}年{Month}月 预算任务提示: {Result}", year, month, result);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError(ex, "预算归档任务执行出错");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|