ExceptionEscalationBizHandler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Admin.NET.Plugin.ApprovalFlow;
  2. using Admin.NET.Plugin.ApprovalFlow.Service;
  3. using Admin.NET.Plugin.AiDOP.Entity.S8;
  4. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  5. /// <summary>
  6. /// 异常升级审批业务回调
  7. /// BizType = "EXCEPTION_ESCALATION"
  8. /// </summary>
  9. public class ExceptionEscalationBizHandler : IFlowBizHandler, ITransient
  10. {
  11. public string BizType => "EXCEPTION_ESCALATION";
  12. private readonly SqlSugarRepository<AdoS8Exception> _rep;
  13. private readonly SqlSugarRepository<AdoS8ExceptionTimeline> _timelineRep;
  14. public ExceptionEscalationBizHandler(
  15. SqlSugarRepository<AdoS8Exception> rep,
  16. SqlSugarRepository<AdoS8ExceptionTimeline> timelineRep)
  17. {
  18. _rep = rep;
  19. _timelineRep = timelineRep;
  20. }
  21. public async Task OnFlowStarted(long bizId, long instanceId)
  22. {
  23. var e = await _rep.GetByIdAsync(bizId) ?? throw new S8BizException("异常不存在");
  24. e.Status = "ESCALATED";
  25. e.ActiveFlowInstanceId = instanceId;
  26. e.ActiveFlowBizType = BizType;
  27. e.UpdatedAt = DateTime.Now;
  28. await _rep.UpdateAsync(e);
  29. await InsertTimelineAsync(e.Id, "ESCALATE_START", "发起升级", null, "ESCALATED", instanceId, null);
  30. }
  31. public async Task OnFlowCompleted(long bizId, long instanceId, FlowInstanceStatusEnum finalStatus, long? lastApproverId)
  32. {
  33. var e = await _rep.GetByIdAsync(bizId) ?? throw new S8BizException("异常不存在");
  34. e.ActiveFlowInstanceId = null;
  35. e.ActiveFlowBizType = null;
  36. e.UpdatedAt = DateTime.Now;
  37. if (finalStatus == FlowInstanceStatusEnum.Approved)
  38. {
  39. e.Status = "ASSIGNED";
  40. if (lastApproverId.HasValue)
  41. e.AssigneeId = lastApproverId.Value;
  42. await _rep.UpdateAsync(e);
  43. await InsertTimelineAsync(e.Id, "ESCALATE_APPROVED", "升级已确认", "ESCALATED", "ASSIGNED",
  44. instanceId, lastApproverId);
  45. }
  46. else if (finalStatus == FlowInstanceStatusEnum.Cancelled)
  47. {
  48. e.Status = "IN_PROGRESS";
  49. await _rep.UpdateAsync(e);
  50. await InsertTimelineAsync(e.Id, "ESCALATE_CANCELLED", "发起人撤回升级申请", "ESCALATED", "IN_PROGRESS",
  51. instanceId, lastApproverId);
  52. }
  53. else
  54. {
  55. e.Status = "IN_PROGRESS";
  56. await _rep.UpdateAsync(e);
  57. await InsertTimelineAsync(e.Id, "ESCALATE_REJECTED", "升级被驳回", "ESCALATED", "IN_PROGRESS",
  58. instanceId, lastApproverId);
  59. }
  60. }
  61. public async Task<Dictionary<string, object>> GetBizData(long bizId)
  62. {
  63. var e = await _rep.GetByIdAsync(bizId);
  64. if (e == null) return new Dictionary<string, object>();
  65. return new Dictionary<string, object>
  66. {
  67. ["severity"] = e.Severity ?? string.Empty,
  68. ["sceneCode"] = e.SceneCode ?? string.Empty,
  69. ["priorityLevel"] = e.PriorityLevel ?? string.Empty,
  70. ["factoryCode"] = e.FactoryId.ToString(),
  71. };
  72. }
  73. private async Task InsertTimelineAsync(long exceptionId, string code, string label,
  74. string? from, string? to, long? instanceId, long? approverId)
  75. {
  76. string? remark = null;
  77. if (instanceId.HasValue && approverId.HasValue)
  78. remark = $"审批实例ID: {instanceId},审批人: {approverId}";
  79. else if (instanceId.HasValue)
  80. remark = $"审批实例ID: {instanceId}";
  81. else if (approverId.HasValue)
  82. remark = $"审批人: {approverId}";
  83. await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
  84. {
  85. ExceptionId = exceptionId,
  86. ActionCode = code,
  87. ActionLabel = label,
  88. FromStatus = from,
  89. ToStatus = to,
  90. OperatorId = approverId,
  91. ActionRemark = remark,
  92. CreatedAt = DateTime.Now
  93. });
  94. }
  95. }