S8TaskFlowService.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using Admin.NET.Plugin.AiDOP.Entity.S8;
  2. using Admin.NET.Plugin.AiDOP.Infrastructure.S8;
  3. using Admin.NET.Plugin.ApprovalFlow.Service;
  4. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  5. public class S8TaskFlowService : ITransient
  6. {
  7. private readonly SqlSugarRepository<AdoS8Exception> _rep;
  8. private readonly SqlSugarRepository<AdoS8ExceptionTimeline> _timelineRep;
  9. private readonly FlowEngineService _flowEngine;
  10. public S8TaskFlowService(
  11. SqlSugarRepository<AdoS8Exception> rep,
  12. SqlSugarRepository<AdoS8ExceptionTimeline> timelineRep,
  13. FlowEngineService flowEngine)
  14. {
  15. _rep = rep;
  16. _timelineRep = timelineRep;
  17. _flowEngine = flowEngine;
  18. }
  19. public async Task<AdoS8Exception> ClaimAsync(long id, long tenantId, long factoryId, long assigneeId, string? remark)
  20. {
  21. if (assigneeId <= 0) throw new S8BizException("认领需指定处理人 AssigneeId");
  22. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  23. if (!S8StatusRules.IsAllowedTransition(e.Status, "ASSIGNED"))
  24. throw new S8BizException($"状态 {e.Status} 不可认领");
  25. var fromStatus = e.Status;
  26. e.Status = "ASSIGNED";
  27. e.AssigneeId = assigneeId;
  28. e.AssignedAt = DateTime.Now;
  29. e.UpdatedAt = DateTime.Now;
  30. await _rep.AsTenant().UseTranAsync(async () =>
  31. {
  32. await _rep.UpdateAsync(e);
  33. await InsertTimelineAsync(e.Id, "CLAIM", "认领", fromStatus, "ASSIGNED", assigneeId, null, remark);
  34. }, ex => throw ex);
  35. return e;
  36. }
  37. public async Task<AdoS8Exception> TransferAsync(long id, long tenantId, long factoryId, long newAssigneeId, string? remark)
  38. {
  39. if (newAssigneeId <= 0) throw new S8BizException("转派目标无效");
  40. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  41. if (S8StatusRules.IsTerminal(e.Status)) throw new S8BizException("已关闭不可转派");
  42. e.AssigneeId = newAssigneeId;
  43. e.UpdatedAt = DateTime.Now;
  44. await _rep.AsTenant().UseTranAsync(async () =>
  45. {
  46. await _rep.UpdateAsync(e);
  47. await InsertTimelineAsync(e.Id, "TRANSFER", "转派", e.Status, e.Status, newAssigneeId, null, remark);
  48. }, ex => throw ex);
  49. return e;
  50. }
  51. public async Task<AdoS8Exception> UpgradeAsync(long id, long tenantId, long factoryId, string? remark)
  52. {
  53. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  54. if (e.ActiveFlowInstanceId.HasValue)
  55. throw new S8BizException("该异常已有进行中的审批流程,请等待审批完成");
  56. if (!S8StatusRules.IsAllowedTransition(e.Status, "ESCALATED"))
  57. throw new S8BizException($"状态 {e.Status} 不可升级");
  58. await _flowEngine.StartFlow(new StartFlowInput
  59. {
  60. BizType = "EXCEPTION_ESCALATION",
  61. BizId = e.Id,
  62. Title = $"异常升级审批 - {e.ExceptionCode}",
  63. Comment = remark,
  64. BizData = new Dictionary<string, object>
  65. {
  66. ["severity"] = e.Severity,
  67. ["sceneCode"] = e.SceneCode,
  68. ["priorityLevel"] = e.PriorityLevel,
  69. }
  70. });
  71. // 状态和时间线由 ExceptionEscalationBizHandler.OnFlowStarted 回调更新
  72. return await LoadAsync(id, tenantId, factoryId) ?? e;
  73. }
  74. public async Task<AdoS8Exception> RejectAsync(long id, long tenantId, long factoryId, string? remark)
  75. {
  76. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  77. if (!S8StatusRules.IsAllowedTransition(e.Status, "REJECTED"))
  78. throw new S8BizException($"状态 {e.Status} 不可驳回");
  79. var from = e.Status;
  80. e.Status = "REJECTED";
  81. e.UpdatedAt = DateTime.Now;
  82. await _rep.AsTenant().UseTranAsync(async () =>
  83. {
  84. await _rep.UpdateAsync(e);
  85. await InsertTimelineAsync(e.Id, "REJECT", "驳回", from, "REJECTED", null, null, remark);
  86. }, ex => throw ex);
  87. return e;
  88. }
  89. public async Task<AdoS8Exception> CloseAsync(long id, long tenantId, long factoryId, string? remark)
  90. {
  91. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  92. if (e.ActiveFlowInstanceId.HasValue)
  93. throw new S8BizException("该异常已有进行中的审批流程,请等待审批完成");
  94. if (!S8StatusRules.IsAllowedTransition(e.Status, "CLOSED"))
  95. throw new S8BizException($"状态 {e.Status} 不可关闭");
  96. await _flowEngine.StartFlow(new StartFlowInput
  97. {
  98. BizType = "EXCEPTION_CLOSURE",
  99. BizId = e.Id,
  100. Title = $"异常关闭确认 - {e.ExceptionCode}",
  101. Comment = remark,
  102. BizData = new Dictionary<string, object>
  103. {
  104. ["sceneCode"] = e.SceneCode,
  105. }
  106. });
  107. // 状态和时间线由 ExceptionClosureBizHandler.OnFlowStarted 回调更新
  108. return await LoadAsync(id, tenantId, factoryId) ?? e;
  109. }
  110. public async Task CommentAsync(long id, string? remark)
  111. {
  112. var e = await _rep.GetFirstAsync(x => x.Id == id && !x.IsDeleted)
  113. ?? throw new S8BizException("异常不存在");
  114. await InsertTimelineAsync(e.Id, "COMMENT", "补充说明", e.Status, e.Status, null, null, remark);
  115. }
  116. private Task<AdoS8Exception?> LoadAsync(long id, long tenantId, long factoryId) =>
  117. _rep.GetFirstAsync(x => x.Id == id && x.TenantId == tenantId && x.FactoryId == factoryId && !x.IsDeleted);
  118. private async Task InsertTimelineAsync(long exceptionId, string code, string label, string? from, string? to,
  119. long? operatorId, string? operatorName, string? remark) =>
  120. await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
  121. {
  122. ExceptionId = exceptionId,
  123. ActionCode = code,
  124. ActionLabel = label,
  125. FromStatus = from,
  126. ToStatus = to,
  127. OperatorId = operatorId,
  128. OperatorName = operatorName,
  129. ActionRemark = remark,
  130. CreatedAt = DateTime.Now
  131. });
  132. }