SmartOpsImprovementActionReminderJob.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Admin.NET.Core;
  2. using Admin.NET.Plugin.AiDOP.Entity;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. using Admin.NET.Plugin.AiDOP.SmartOps;
  5. using Furion.Schedule;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Logging;
  8. namespace Admin.NET.Plugin.AiDOP.Job;
  9. [JobDetail("job_smart_ops_action_reminder", Description = "智慧诊断行动项到期/逾期提醒", GroupName = "default", Concurrent = false)]
  10. [Daily(TriggerId = "trigger_smart_ops_action_reminder", Description = "每日扫描改善行动项提醒")]
  11. public class SmartOpsImprovementActionReminderJob : IJob
  12. {
  13. private readonly IServiceScopeFactory _scopeFactory;
  14. private readonly ILogger _logger;
  15. public SmartOpsImprovementActionReminderJob(IServiceScopeFactory scopeFactory, ILoggerFactory loggerFactory)
  16. {
  17. _scopeFactory = scopeFactory;
  18. _logger = loggerFactory.CreateLogger(nameof(SmartOpsImprovementActionReminderJob));
  19. }
  20. public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
  21. {
  22. if (!AidopJobGate.ShouldRun(nameof(SmartOpsImprovementActionReminderJob), _logger)) return;
  23. using var scope = _scopeFactory.CreateScope();
  24. var rootDb = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>();
  25. var today = DateTime.Today;
  26. List<long> tenantIds;
  27. try
  28. {
  29. tenantIds = await rootDb.CopyNew().Queryable<AdoSmartOpsImprovementAction>()
  30. .Where(x => x.TenantId != null
  31. && x.TenantId > 0
  32. && x.TenantId != SmartOpsTrustedScopeRules.PlatformTenantId)
  33. .Select(x => x.TenantId!.Value)
  34. .Distinct()
  35. .ToListAsync(stoppingToken);
  36. }
  37. catch (Exception ex)
  38. {
  39. _logger.LogWarning(ex, "行动项提醒作业读取租户失败");
  40. return;
  41. }
  42. var service = scope.ServiceProvider.GetRequiredService<SmartOpsImprovementActionService>();
  43. foreach (var tenantId in tenantIds)
  44. {
  45. stoppingToken.ThrowIfCancellationRequested();
  46. try
  47. {
  48. var db = rootDb.CopyNew();
  49. var sent = await service.SendDueRemindersAsync(db, tenantId, today);
  50. if (sent > 0)
  51. _logger.LogInformation("行动项提醒 tenant={TenantId} sent={Sent}", tenantId, sent);
  52. }
  53. catch (Exception ex)
  54. {
  55. _logger.LogError(ex, "行动项提醒失败 tenant={TenantId}", tenantId);
  56. }
  57. }
  58. }
  59. }