using Furion.Schedule;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;

namespace WCS.Application;
/// <summary>
/// PLC作业任务
/// </summary>
[JobDetail("job_plc", Description = "PLC作业任务", GroupName = "default", Concurrent = false)]
[Daily(TriggerId = "trigger_plc", Description = "PLC作业任务")]
public class PLCJob : IJob
{
    private readonly IServiceScopeFactory _scopeFactory;
    private readonly ILogger _logger;

    public PLCJob(IServiceScopeFactory scopeFactory, ILoggerFactory loggerFactory)
    {
        _scopeFactory = scopeFactory;
        _logger = loggerFactory.CreateLogger(CommonConst.SysLogCategoryName);
    }

    public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {
        //using var serviceScope = _scopeFactory.CreateScope();

        //var rep = serviceScope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysOnlineUser>>();
        //await rep.CopyNew().AsDeleteable().ExecuteCommandAsync(stoppingToken);
        
        PLCTaskAction.Init();


        string msg = $"【{DateTime.Now}】PLC服务启动...";
        var originColor = Console.ForegroundColor;
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(msg);
        Console.ForegroundColor = originColor;

        // 自定义日志
        _logger.LogInformation(msg);
    }
}