S8TaskFlowService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using Admin.NET.Plugin.AiDOP.Entity.S8;
  2. using Admin.NET.Plugin.AiDOP.Infrastructure.S8;
  3. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  4. public class S8TaskFlowService : ITransient
  5. {
  6. private readonly SqlSugarRepository<AdoS8Exception> _rep;
  7. private readonly SqlSugarRepository<AdoS8ExceptionTimeline> _timelineRep;
  8. public S8TaskFlowService(
  9. SqlSugarRepository<AdoS8Exception> rep,
  10. SqlSugarRepository<AdoS8ExceptionTimeline> timelineRep)
  11. {
  12. _rep = rep;
  13. _timelineRep = timelineRep;
  14. }
  15. public async Task<AdoS8Exception> ClaimAsync(long id, long tenantId, long factoryId, long assigneeId, string? remark)
  16. {
  17. if (assigneeId <= 0) throw new S8BizException("认领需指定处理人 AssigneeId");
  18. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  19. if (!S8StatusRules.IsAllowedTransition(e.Status, "ASSIGNED"))
  20. throw new S8BizException($"状态 {e.Status} 不可认领");
  21. var fromStatus = e.Status;
  22. e.Status = "ASSIGNED";
  23. e.AssigneeId = assigneeId;
  24. e.AssignedAt = DateTime.Now;
  25. e.UpdatedAt = DateTime.Now;
  26. await _rep.AsTenant().UseTranAsync(async () =>
  27. {
  28. await _rep.UpdateAsync(e);
  29. await InsertTimelineAsync(e.Id, "CLAIM", "认领", fromStatus, "ASSIGNED", assigneeId, null, remark);
  30. }, ex => throw ex);
  31. return e;
  32. }
  33. public async Task<AdoS8Exception> TransferAsync(long id, long tenantId, long factoryId, long newAssigneeId, string? remark)
  34. {
  35. if (newAssigneeId <= 0) throw new S8BizException("转派目标无效");
  36. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  37. if (S8StatusRules.IsTerminal(e.Status)) throw new S8BizException("已关闭不可转派");
  38. e.AssigneeId = newAssigneeId;
  39. e.UpdatedAt = DateTime.Now;
  40. await _rep.AsTenant().UseTranAsync(async () =>
  41. {
  42. await _rep.UpdateAsync(e);
  43. await InsertTimelineAsync(e.Id, "TRANSFER", "转派", e.Status, e.Status, newAssigneeId, null, remark);
  44. }, ex => throw ex);
  45. return e;
  46. }
  47. public async Task<AdoS8Exception> UpgradeAsync(long id, long tenantId, long factoryId, string? remark)
  48. {
  49. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  50. if (!S8StatusRules.IsAllowedTransition(e.Status, "ESCALATED"))
  51. throw new S8BizException($"状态 {e.Status} 不可升级");
  52. var from = e.Status;
  53. e.Status = "ESCALATED";
  54. e.UpdatedAt = DateTime.Now;
  55. await _rep.AsTenant().UseTranAsync(async () =>
  56. {
  57. await _rep.UpdateAsync(e);
  58. await InsertTimelineAsync(e.Id, "UPGRADE", "升级", from, "ESCALATED", null, null, remark);
  59. }, ex => throw ex);
  60. return e;
  61. }
  62. public async Task<AdoS8Exception> RejectAsync(long id, long tenantId, long factoryId, string? remark)
  63. {
  64. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  65. if (!S8StatusRules.IsAllowedTransition(e.Status, "REJECTED"))
  66. throw new S8BizException($"状态 {e.Status} 不可驳回");
  67. var from = e.Status;
  68. e.Status = "REJECTED";
  69. e.UpdatedAt = DateTime.Now;
  70. await _rep.AsTenant().UseTranAsync(async () =>
  71. {
  72. await _rep.UpdateAsync(e);
  73. await InsertTimelineAsync(e.Id, "REJECT", "驳回", from, "REJECTED", null, null, remark);
  74. }, ex => throw ex);
  75. return e;
  76. }
  77. public async Task<AdoS8Exception> CloseAsync(long id, long tenantId, long factoryId, string? remark)
  78. {
  79. var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
  80. if (!S8StatusRules.IsAllowedTransition(e.Status, "CLOSED"))
  81. throw new S8BizException($"状态 {e.Status} 不可关闭");
  82. var from = e.Status;
  83. e.Status = "CLOSED";
  84. e.ClosedAt = DateTime.Now;
  85. e.UpdatedAt = DateTime.Now;
  86. await _rep.AsTenant().UseTranAsync(async () =>
  87. {
  88. await _rep.UpdateAsync(e);
  89. await InsertTimelineAsync(e.Id, "CLOSE", "关闭", from, "CLOSED", null, null, remark);
  90. }, ex => throw ex);
  91. return e;
  92. }
  93. public async Task CommentAsync(long id, string? remark)
  94. {
  95. var e = await _rep.GetFirstAsync(x => x.Id == id && !x.IsDeleted)
  96. ?? throw new S8BizException("异常不存在");
  97. await InsertTimelineAsync(e.Id, "COMMENT", "补充说明", e.Status, e.Status, null, null, remark);
  98. }
  99. private Task<AdoS8Exception?> LoadAsync(long id, long tenantId, long factoryId) =>
  100. _rep.GetFirstAsync(x => x.Id == id && x.TenantId == tenantId && x.FactoryId == factoryId && !x.IsDeleted);
  101. private async Task InsertTimelineAsync(long exceptionId, string code, string label, string? from, string? to,
  102. long? operatorId, string? operatorName, string? remark) =>
  103. await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
  104. {
  105. ExceptionId = exceptionId,
  106. ActionCode = code,
  107. ActionLabel = label,
  108. FromStatus = from,
  109. ToStatus = to,
  110. OperatorId = operatorId,
  111. OperatorName = operatorName,
  112. ActionRemark = remark,
  113. CreatedAt = DateTime.Now
  114. });
  115. }