SyncWokerflowLogJob.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using Admin.NET.Plugin.DingTalk;
  7. using Furion.Schedule;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Logging;
  10. namespace Admin.NET.Plugin.Job;
  11. /// <summary>
  12. /// 同步钉钉角色job,自动同步触发器请在web页面按需求设置
  13. /// </summary>
  14. [JobDetail(
  15. "SyncWokerflowLogJob",
  16. Description = "同步钉钉审批状态",
  17. GroupName = "default",
  18. Concurrent = false
  19. )]
  20. [Daily(TriggerId = "SyncWokerflowLogTrigger", Description = "同步钉钉审批状态")]
  21. public class SyncWokerflowLogJob : IJob
  22. {
  23. private readonly IServiceScopeFactory _scopeFactory;
  24. private readonly IDingTalkApi _dingTalkApi;
  25. private readonly ILogger _logger;
  26. private readonly SqlSugarRepository<DingTalkDept> _dingTalkDeptRep;
  27. private readonly SqlSugarRepository<DingTalkWokerflowLog> _dingTalkWokerflowLogRep;
  28. public SyncWokerflowLogJob(
  29. IServiceScopeFactory scopeFactory,
  30. IDingTalkApi dingTalkApi,
  31. SqlSugarRepository<DingTalkDept> dingTalkDeptRep,
  32. SqlSugarRepository<DingTalkWokerflowLog> dingTalkWokerflowLogRep,
  33. ILoggerFactory loggerFactory
  34. )
  35. {
  36. _scopeFactory = scopeFactory;
  37. _dingTalkApi = dingTalkApi;
  38. _dingTalkDeptRep = dingTalkDeptRep;
  39. _dingTalkWokerflowLogRep = dingTalkWokerflowLogRep;
  40. _logger = loggerFactory.CreateLogger(CommonConst.SysLogCategoryName);
  41. }
  42. public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
  43. {
  44. using var serviceScope = _scopeFactory.CreateScope();
  45. var _dingTalkOptions = serviceScope.ServiceProvider.GetRequiredService<
  46. IOptions<DingTalkOptions>
  47. >();
  48. // 获取Token
  49. var tokenRes = await _dingTalkApi.GetDingTalkToken(
  50. _dingTalkOptions.Value.ClientId,
  51. _dingTalkOptions.Value.ClientSecret
  52. );
  53. if (tokenRes.ErrCode != 0)
  54. throw Oops.Oh(tokenRes.ErrMsg);
  55. var dingTalkDeptList = new List<DingTalkDept>();
  56. // 获取未完成审批列表
  57. List<DingTalkWokerflowLog> flow_list = await _dingTalkWokerflowLogRep.GetListAsync(t =>
  58. t.Status == "RUNNING"
  59. );
  60. List<DingTalkWokerflowLog> update_list = new List<DingTalkWokerflowLog>();
  61. if (flow_list?.Count > 0)
  62. {
  63. foreach (var item in flow_list)
  64. {
  65. var flow = await _dingTalkApi.GetProcessInstances(
  66. tokenRes.AccessToken,
  67. item.instanceId
  68. );
  69. if (flow.Result.Status != item.Status)
  70. {
  71. item.Status = flow.Result.Status;
  72. item.UpdateTime = DateTime.Now;
  73. item.WorkflowId = flow.Result.BusinessId;
  74. item.taskId = flow
  75. .Result.Tasks.FirstOrDefault(t => t.Status == "RUNNING")
  76. ?.TaskId;
  77. update_list.Add(item);
  78. }
  79. }
  80. if (update_list.Count > 0)
  81. {
  82. await _dingTalkWokerflowLogRep.UpdateRangeAsync(update_list);
  83. }
  84. var originColor = Console.ForegroundColor;
  85. Console.ForegroundColor = ConsoleColor.Blue;
  86. Console.WriteLine("【" + DateTime.Now + "】同步钉钉审批记录状态");
  87. Console.ForegroundColor = originColor;
  88. }
  89. }
  90. }