ExceptionClosureBizHandler.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_CLOSURE"
  8. /// </summary>
  9. public class ExceptionClosureBizHandler : IFlowBizHandler, ITransient
  10. {
  11. public string BizType => "EXCEPTION_CLOSURE";
  12. private readonly SqlSugarRepository<AdoS8Exception> _rep;
  13. private readonly SqlSugarRepository<AdoS8ExceptionTimeline> _timelineRep;
  14. public ExceptionClosureBizHandler(
  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.ActiveFlowInstanceId = instanceId;
  25. e.UpdatedAt = DateTime.Now;
  26. await _rep.UpdateAsync(e);
  27. await InsertTimelineAsync(e.Id, "CLOSURE_START", "发起关闭确认", null, null, instanceId, null);
  28. }
  29. public async Task OnFlowCompleted(long bizId, long instanceId, FlowInstanceStatusEnum finalStatus, long? lastApproverId)
  30. {
  31. var e = await _rep.GetByIdAsync(bizId) ?? throw new S8BizException("异常不存在");
  32. e.ActiveFlowInstanceId = null;
  33. e.UpdatedAt = DateTime.Now;
  34. if (finalStatus == FlowInstanceStatusEnum.Approved)
  35. {
  36. e.Status = "CLOSED";
  37. e.ClosedAt = DateTime.Now;
  38. await _rep.UpdateAsync(e);
  39. await InsertTimelineAsync(e.Id, "CLOSURE_APPROVED", "关闭已确认", "RESOLVED", "CLOSED",
  40. instanceId, lastApproverId);
  41. }
  42. else
  43. {
  44. e.Status = "IN_PROGRESS";
  45. await _rep.UpdateAsync(e);
  46. await InsertTimelineAsync(e.Id, "CLOSURE_REJECTED", "关闭被驳回", "RESOLVED", "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. ["sceneCode"] = e.SceneCode,
  57. };
  58. }
  59. private async Task InsertTimelineAsync(long exceptionId, string code, string label,
  60. string? from, string? to, long? instanceId, long? approverId)
  61. {
  62. // P4-17: 补齐审批人 ID 到 Timeline 留痕
  63. string? remark = null;
  64. if (instanceId.HasValue && approverId.HasValue)
  65. remark = $"审批实例ID: {instanceId},审批人: {approverId}";
  66. else if (instanceId.HasValue)
  67. remark = $"审批实例ID: {instanceId}";
  68. else if (approverId.HasValue)
  69. remark = $"审批人: {approverId}";
  70. await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
  71. {
  72. ExceptionId = exceptionId,
  73. ActionCode = code,
  74. ActionLabel = label,
  75. FromStatus = from,
  76. ToStatus = to,
  77. ActionRemark = remark,
  78. CreatedAt = DateTime.Now
  79. });
  80. }
  81. }