| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using Admin.NET.Plugin.AiDOP.Production;
- using Furion.Schedule;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
- using SqlSugar;
- namespace Admin.NET.Plugin.AiDOP.Job;
- /// <summary>
- /// S2 定时自动全量排产。默认关闭,由配置 S2:AutoSchedule:Enabled=true 开启;
- /// 周期默认 60 分钟(可在作业管理中调整触发器)。
- /// 每个有在制工单的租户提交一次异步排程(与手工按钮共用单飞与 run_id)。
- /// </summary>
- [JobDetail("job_s2_auto_schedule", Description = "S2 定时自动全量排产", GroupName = "default", Concurrent = false)]
- [Period(S2AutoScheduleJob.IntervalMs, TriggerId = "trigger_s2_auto_schedule", Description = "S2 自动排产节拍(默认60分钟)")]
- public class S2AutoScheduleJob : IJob
- {
- public const int IntervalMs = 3600000;
- private readonly IServiceScopeFactory _scopeFactory;
- private readonly IConfiguration _configuration;
- private readonly ILogger _logger;
- public S2AutoScheduleJob(
- IServiceScopeFactory scopeFactory,
- IConfiguration configuration,
- ILoggerFactory loggerFactory)
- {
- _scopeFactory = scopeFactory;
- _configuration = configuration;
- _logger = loggerFactory.CreateLogger(nameof(S2AutoScheduleJob));
- }
- public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
- {
- if (!_configuration.GetValue("S2:AutoSchedule:Enabled", false))
- return;
- using var scope = _scopeFactory.CreateScope();
- var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>();
- var action = scope.ServiceProvider.GetRequiredService<ProductionSchedulingActionService>();
- var tenants = await db.Ado.SqlQueryAsync<long>(
- """
- SELECT DISTINCT tenant_id
- FROM WorkOrdMaster
- WHERE IFNULL(IsActive, 0) = 1
- AND IFNULL(Status, '') NOT IN ('c', 'C')
- AND tenant_id IS NOT NULL AND tenant_id > 0
- """);
- foreach (var tenantId in tenants)
- {
- if (stoppingToken.IsCancellationRequested)
- break;
- try
- {
- var domain = tenantId.ToString();
- await action.SubmitGenerateAsync(
- tenantId, domain, "sys-auto-schedule",
- enableCapacityConstraint: false,
- trigger: "auto");
- _logger.LogInformation("S2AutoScheduleJob 已提交租户 {TenantId}", tenantId);
- }
- catch (Exception ex)
- {
- // 单飞冲突或业务拒绝:记日志,继续下一租户
- _logger.LogWarning(ex, "S2AutoScheduleJob 跳过租户 {TenantId}", tenantId);
- }
- }
- }
- }
|