Valeo_Line_MES_backend/MDM/Host/StartupTaskService.cs

57 lines
1.8 KiB
C#
Raw Permalink Normal View History

2026-01-10 13:47:54 +08:00
using MDM.Model.Plant;
using MDM.Services.Plant;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SqlSugar.IOC;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MDM.Host
{
/// <summary>
/// 应用程序启动时执行的任务服务
/// </summary>
public class StartupTaskService : IHostedService
{
private readonly ILogger<StartupTaskService> _logger;
private readonly PlcIoPointCacheService _cacheService;
public StartupTaskService(ILogger<StartupTaskService> logger, PlcIoPointCacheService cacheService)
{
_logger = logger;
_cacheService = cacheService;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("应用程序启动,正在执行初始化逻辑...");
// 在这里写你想要启动时立刻执行的逻辑
ExecuteStartupLogic();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("StartupTaskService 已停止");
return Task.CompletedTask;
}
private void ExecuteStartupLogic()
{
// 查询所有启用的点位数据IsActive == 1
var activePoints = DbScoped.SugarScope.CopyNew().Queryable<PlantPlcIoPoint>()
.Where(p => p.IsActive == 1)
.ToList();
_logger.LogInformation($"✅ 从数据库加载到 {activePoints.Count} 条有效的 PlantPlcIoPoint 数据");
// 将数据存入缓存服务
_cacheService.InitializeCache(activePoints);
_logger.LogInformation("📦 PlantPlcIoPoint 数据已成功加载到内存缓存。");
}
}
}