ExceptionClosureBizHandler.cs 3.5 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. /// EXCEPTION_CLOSURE 流程的 Biz 回调(双线合一后已退化为复检审批载体)。
  7. /// 启动点:S8 提交复检;终态由 S8 业务侧(ApproveVerification/RejectVerification)决定。
  8. /// 该 handler 只负责维护 ActiveFlowInstanceId 字段,不再改写 e.Status。
  9. /// </summary>
  10. public class ExceptionClosureBizHandler : IFlowBizHandler, ITransient
  11. {
  12. public string BizType => "EXCEPTION_CLOSURE";
  13. private readonly SqlSugarRepository<AdoS8Exception> _rep;
  14. private readonly SqlSugarRepository<AdoS8ExceptionTimeline> _timelineRep;
  15. public ExceptionClosureBizHandler(
  16. SqlSugarRepository<AdoS8Exception> rep,
  17. SqlSugarRepository<AdoS8ExceptionTimeline> timelineRep)
  18. {
  19. _rep = rep;
  20. _timelineRep = timelineRep;
  21. }
  22. public async Task OnFlowStarted(long bizId, long instanceId)
  23. {
  24. var e = await _rep.GetByIdAsync(bizId) ?? throw new S8BizException("异常不存在");
  25. e.ActiveFlowInstanceId = instanceId;
  26. e.ActiveFlowBizType = BizType;
  27. e.UpdatedAt = DateTime.Now;
  28. await _rep.UpdateAsync(e);
  29. await InsertTimelineAsync(e.Id, "VERIFY_FLOW_START", "复检审批流启动", 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. await _rep.UpdateAsync(e);
  38. // 状态由 S8 服务层管控(VERIFY_APPROVED/VERIFY_REJECTED 时间线已写)。
  39. // 这里只补一条流程级审计标记,便于追溯审批实例终态。
  40. var label = finalStatus switch
  41. {
  42. FlowInstanceStatusEnum.Approved => "复检审批流通过",
  43. FlowInstanceStatusEnum.Rejected => "复检审批流拒绝",
  44. FlowInstanceStatusEnum.Cancelled => "复检审批流撤回",
  45. _ => "复检审批流结束",
  46. };
  47. await InsertTimelineAsync(e.Id, "VERIFY_FLOW_END", label, instanceId, lastApproverId);
  48. }
  49. public async Task<Dictionary<string, object>> GetBizData(long bizId)
  50. {
  51. var e = await _rep.GetByIdAsync(bizId);
  52. if (e == null) return new Dictionary<string, object>();
  53. return new Dictionary<string, object>
  54. {
  55. ["sceneCode"] = e.SceneCode ?? string.Empty,
  56. ["factoryCode"] = e.FactoryId.ToString(),
  57. };
  58. }
  59. private async Task InsertTimelineAsync(long exceptionId, string code, string label,
  60. long? instanceId, long? approverId)
  61. {
  62. string? remark = null;
  63. if (instanceId.HasValue && approverId.HasValue)
  64. remark = $"审批实例ID: {instanceId},审批人: {approverId}";
  65. else if (instanceId.HasValue)
  66. remark = $"审批实例ID: {instanceId}";
  67. else if (approverId.HasValue)
  68. remark = $"审批人: {approverId}";
  69. await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
  70. {
  71. ExceptionId = exceptionId,
  72. ActionCode = code,
  73. ActionLabel = label,
  74. FromStatus = null,
  75. ToStatus = null,
  76. OperatorId = approverId,
  77. ActionRemark = remark,
  78. CreatedAt = DateTime.Now
  79. });
  80. }
  81. }