ExceptionEscalationBizHandler.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, null);
  29. }
  30. public async Task OnFlowCompleted(long bizId, long instanceId, FlowInstanceStatusEnum finalStatus, long? lastApproverId)
  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",
  40. instanceId, lastApproverId);
  41. }
  42. else
  43. {
  44. e.Status = "IN_PROGRESS";
  45. await _rep.UpdateAsync(e);
  46. await InsertTimelineAsync(e.Id, "ESCALATE_REJECTED", "升级被驳回", "ESCALATED", "IN_PROGRESS",
  47. instanceId, lastApproverId);
  48. }
  49. }
  50. public async Task<Dictionary<string, object>> GetBizData(long bizId)
  51. {
  52. var e = await _rep.GetByIdAsync(bizId);
  53. if (e == null) return new Dictionary<string, object>();
  54. return new Dictionary<string, object>
  55. {
  56. ["severity"] = e.Severity,
  57. ["sceneCode"] = e.SceneCode,
  58. ["priorityLevel"] = e.PriorityLevel,
  59. };
  60. }
  61. private async Task InsertTimelineAsync(long exceptionId, string code, string label,
  62. string? from, string? to, long? instanceId, long? approverId)
  63. {
  64. // P4-17: 补齐审批人 ID 到 Timeline 留痕
  65. string? remark = null;
  66. if (instanceId.HasValue && approverId.HasValue)
  67. remark = $"审批实例ID: {instanceId},审批人: {approverId}";
  68. else if (instanceId.HasValue)
  69. remark = $"审批实例ID: {instanceId}";
  70. else if (approverId.HasValue)
  71. remark = $"审批人: {approverId}";
  72. await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
  73. {
  74. ExceptionId = exceptionId,
  75. ActionCode = code,
  76. ActionLabel = label,
  77. FromStatus = from,
  78. ToStatus = to,
  79. ActionRemark = remark,
  80. CreatedAt = DateTime.Now
  81. });
  82. }
  83. }