ExceptionEscalationBizHandler.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.UpdatedAt = DateTime.Now;
  27. await _rep.UpdateAsync(e);
  28. await InsertTimelineAsync(e.Id, "ESCALATE_START", "发起升级", null, "ESCALATED", instanceId);
  29. }
  30. public async Task OnFlowCompleted(long bizId, FlowInstanceStatusEnum finalStatus)
  31. {
  32. var e = await _rep.GetByIdAsync(bizId) ?? throw new S8BizException("异常不存在");
  33. e.ActiveFlowInstanceId = null;
  34. e.UpdatedAt = DateTime.Now;
  35. if (finalStatus == FlowInstanceStatusEnum.Approved)
  36. {
  37. e.Status = "ASSIGNED";
  38. await _rep.UpdateAsync(e);
  39. await InsertTimelineAsync(e.Id, "ESCALATE_APPROVED", "升级已确认", "ESCALATED", "ASSIGNED", null);
  40. }
  41. else
  42. {
  43. e.Status = "IN_PROGRESS";
  44. await _rep.UpdateAsync(e);
  45. await InsertTimelineAsync(e.Id, "ESCALATE_REJECTED", "升级被驳回", "ESCALATED", "IN_PROGRESS", null);
  46. }
  47. }
  48. public async Task<Dictionary<string, object>> GetBizData(long bizId)
  49. {
  50. var e = await _rep.GetByIdAsync(bizId);
  51. if (e == null) return new Dictionary<string, object>();
  52. return new Dictionary<string, object>
  53. {
  54. ["severity"] = e.Severity,
  55. ["sceneCode"] = e.SceneCode,
  56. ["priorityLevel"] = e.PriorityLevel,
  57. };
  58. }
  59. private async Task InsertTimelineAsync(long exceptionId, string code, string label, string? from, string? to, long? instanceId)
  60. {
  61. await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
  62. {
  63. ExceptionId = exceptionId,
  64. ActionCode = code,
  65. ActionLabel = label,
  66. FromStatus = from,
  67. ToStatus = to,
  68. ActionRemark = instanceId.HasValue ? $"审批实例ID: {instanceId}" : null,
  69. CreatedAt = DateTime.Now
  70. });
  71. }
  72. }