S8TaskFlowService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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<AdoS8Exception> SubmitVerificationAsync(
  111. long id, long tenantId, long factoryId,
  112. long currentUserId, long verifierId, string? remark)
  113. {
  114. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  115. if (e.AssigneeId != currentUserId)
  116. throw new S8BizException("只有当前处理人才能提交复检");
  117. if (verifierId <= 0)
  118. throw new S8BizException("请选择检验人");
  119. if (!S8StatusRules.IsAllowedTransition(e.Status, "PENDING_VERIFICATION"))
  120. throw new S8BizException($"状态 {e.Status} 不可提交复检");
  121. var from = e.Status;
  122. e.Status = "PENDING_VERIFICATION";
  123. e.VerifierId = verifierId;
  124. e.VerificationAssignedAt = DateTime.Now;
  125. e.UpdatedAt = DateTime.Now;
  126. await _rep.AsTenant().UseTranAsync(async () =>
  127. {
  128. await _rep.UpdateAsync(e);
  129. await InsertTimelineAsync(e.Id, "VERIFY_SUBMITTED", "提交复检", from, "PENDING_VERIFICATION",
  130. currentUserId, null, remark);
  131. }, ex => throw ex);
  132. return e;
  133. }
  134. public async Task<AdoS8Exception> ApproveVerificationAsync(
  135. long id, long tenantId, long factoryId,
  136. long currentUserId, string? remark)
  137. {
  138. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  139. if (e.VerifierId != currentUserId)
  140. throw new S8BizException("只有指定检验人才能检验通过");
  141. if (!S8StatusRules.IsAllowedTransition(e.Status, "RESOLVED"))
  142. throw new S8BizException($"状态 {e.Status} 不可检验通过");
  143. var from = e.Status;
  144. e.Status = "RESOLVED";
  145. e.VerifiedAt = DateTime.Now;
  146. e.VerificationResult = "APPROVED";
  147. e.VerificationRemark = remark;
  148. e.UpdatedAt = DateTime.Now;
  149. await _rep.AsTenant().UseTranAsync(async () =>
  150. {
  151. await _rep.UpdateAsync(e);
  152. await InsertTimelineAsync(e.Id, "VERIFY_APPROVED", "检验通过", from, "RESOLVED",
  153. currentUserId, null, remark);
  154. }, ex => throw ex);
  155. return e;
  156. }
  157. public async Task<AdoS8Exception> RejectVerificationAsync(
  158. long id, long tenantId, long factoryId,
  159. long currentUserId, string remark)
  160. {
  161. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  162. if (e.VerifierId != currentUserId)
  163. throw new S8BizException("只有指定检验人才能检验退回");
  164. if (!S8StatusRules.IsAllowedTransition(e.Status, "IN_PROGRESS"))
  165. throw new S8BizException($"状态 {e.Status} 不可检验退回");
  166. if (string.IsNullOrWhiteSpace(remark))
  167. throw new S8BizException("检验退回必须填写退回原因");
  168. var from = e.Status;
  169. e.Status = "IN_PROGRESS";
  170. e.VerifiedAt = DateTime.Now;
  171. e.VerificationResult = "REJECTED";
  172. e.VerificationRemark = remark;
  173. e.UpdatedAt = DateTime.Now;
  174. await _rep.AsTenant().UseTranAsync(async () =>
  175. {
  176. await _rep.UpdateAsync(e);
  177. await InsertTimelineAsync(e.Id, "VERIFY_REJECTED", "检验退回", from, "IN_PROGRESS",
  178. currentUserId, null, remark);
  179. }, ex => throw ex);
  180. return e;
  181. }
  182. public async Task CommentAsync(long id, string? remark)
  183. {
  184. var e = await _rep.GetFirstAsync(x => x.Id == id && !x.IsDeleted)
  185. ?? throw new S8BizException("异常不存在");
  186. await InsertTimelineAsync(e.Id, "COMMENT", "补充说明", e.Status, e.Status, null, null, remark);
  187. }
  188. private Task<AdoS8Exception?> LoadAsync(long id, long tenantId, long factoryId) =>
  189. _rep.GetFirstAsync(x => x.Id == id && x.TenantId == tenantId && x.FactoryId == factoryId && !x.IsDeleted);
  190. private async Task InsertTimelineAsync(long exceptionId, string code, string label, string? from, string? to,
  191. long? operatorId, string? operatorName, string? remark) =>
  192. await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
  193. {
  194. ExceptionId = exceptionId,
  195. ActionCode = code,
  196. ActionLabel = label,
  197. FromStatus = from,
  198. ToStatus = to,
  199. OperatorId = operatorId,
  200. OperatorName = operatorName,
  201. ActionRemark = remark,
  202. CreatedAt = DateTime.Now
  203. });
  204. }