ExceptionClosureBizHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.ActiveFlowBizType = BizType;
  26. e.UpdatedAt = DateTime.Now;
  27. await _rep.UpdateAsync(e);
  28. await InsertTimelineAsync(e.Id, "CLOSE_START", "发起关闭确认", null, null, 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.ActiveFlowBizType = null;
  35. e.UpdatedAt = DateTime.Now;
  36. if (finalStatus == FlowInstanceStatusEnum.Approved)
  37. {
  38. e.Status = "CLOSED";
  39. e.ClosedAt = DateTime.Now;
  40. await _rep.UpdateAsync(e);
  41. await InsertTimelineAsync(e.Id, "CLOSE_APPROVED", "关闭已确认", "RESOLVED", "CLOSED",
  42. instanceId, lastApproverId);
  43. }
  44. else if (finalStatus == FlowInstanceStatusEnum.Cancelled)
  45. {
  46. // 撤回:状态维持 RESOLVED,不写终态
  47. await _rep.UpdateAsync(e);
  48. await InsertTimelineAsync(e.Id, "CLOSE_CANCELLED", "处理人撤回关闭申请", null, null,
  49. instanceId, lastApproverId);
  50. }
  51. else
  52. {
  53. e.Status = "IN_PROGRESS";
  54. await _rep.UpdateAsync(e);
  55. await InsertTimelineAsync(e.Id, "CLOSE_REJECTED", "关闭被驳回", "RESOLVED", "IN_PROGRESS",
  56. instanceId, lastApproverId);
  57. }
  58. }
  59. public async Task<Dictionary<string, object>> GetBizData(long bizId)
  60. {
  61. var e = await _rep.GetByIdAsync(bizId);
  62. if (e == null) return new Dictionary<string, object>();
  63. return new Dictionary<string, object>
  64. {
  65. ["sceneCode"] = e.SceneCode ?? string.Empty,
  66. ["factoryCode"] = e.FactoryId.ToString(),
  67. };
  68. }
  69. private async Task InsertTimelineAsync(long exceptionId, string code, string label,
  70. string? from, string? to, long? instanceId, long? approverId)
  71. {
  72. string? remark = null;
  73. if (instanceId.HasValue && approverId.HasValue)
  74. remark = $"审批实例ID: {instanceId},审批人: {approverId}";
  75. else if (instanceId.HasValue)
  76. remark = $"审批实例ID: {instanceId}";
  77. else if (approverId.HasValue)
  78. remark = $"审批人: {approverId}";
  79. await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
  80. {
  81. ExceptionId = exceptionId,
  82. ActionCode = code,
  83. ActionLabel = label,
  84. FromStatus = from,
  85. ToStatus = to,
  86. OperatorId = approverId,
  87. ActionRemark = remark,
  88. CreatedAt = DateTime.Now
  89. });
  90. }
  91. }