| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using Admin.NET.Plugin.AiDOP.Entity.S8;
- using Admin.NET.Plugin.AiDOP.Infrastructure.S8;
- using Admin.NET.Plugin.ApprovalFlow.Service;
- namespace Admin.NET.Plugin.AiDOP.Service.S8;
- public class S8TaskFlowService : ITransient
- {
- private readonly SqlSugarRepository<AdoS8Exception> _rep;
- private readonly SqlSugarRepository<AdoS8ExceptionTimeline> _timelineRep;
- private readonly FlowEngineService _flowEngine;
- public S8TaskFlowService(
- SqlSugarRepository<AdoS8Exception> rep,
- SqlSugarRepository<AdoS8ExceptionTimeline> timelineRep,
- FlowEngineService flowEngine)
- {
- _rep = rep;
- _timelineRep = timelineRep;
- _flowEngine = flowEngine;
- }
- public async Task<AdoS8Exception> ClaimAsync(long id, long tenantId, long factoryId, long assigneeId, string? remark)
- {
- if (assigneeId <= 0) throw new S8BizException("认领需指定处理人 AssigneeId");
- var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
- if (!S8StatusRules.IsAllowedTransition(e.Status, "ASSIGNED"))
- throw new S8BizException($"状态 {e.Status} 不可认领");
- var fromStatus = e.Status;
- e.Status = "ASSIGNED";
- e.AssigneeId = assigneeId;
- e.AssignedAt = DateTime.Now;
- e.UpdatedAt = DateTime.Now;
- await _rep.AsTenant().UseTranAsync(async () =>
- {
- await _rep.UpdateAsync(e);
- await InsertTimelineAsync(e.Id, "CLAIM", "认领", fromStatus, "ASSIGNED", assigneeId, null, remark);
- }, ex => throw ex);
- return e;
- }
- public async Task<AdoS8Exception> TransferAsync(long id, long tenantId, long factoryId, long newAssigneeId, string? remark)
- {
- if (newAssigneeId <= 0) throw new S8BizException("转派目标无效");
- var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
- if (S8StatusRules.IsTerminal(e.Status)) throw new S8BizException("已关闭不可转派");
- e.AssigneeId = newAssigneeId;
- e.UpdatedAt = DateTime.Now;
- await _rep.AsTenant().UseTranAsync(async () =>
- {
- await _rep.UpdateAsync(e);
- await InsertTimelineAsync(e.Id, "TRANSFER", "转派", e.Status, e.Status, newAssigneeId, null, remark);
- }, ex => throw ex);
- return e;
- }
- public async Task<AdoS8Exception> UpgradeAsync(long id, long tenantId, long factoryId, string? remark)
- {
- var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
- if (e.ActiveFlowInstanceId.HasValue)
- throw new S8BizException("该异常已有进行中的审批流程,请等待审批完成");
- if (!S8StatusRules.IsAllowedTransition(e.Status, "ESCALATED"))
- throw new S8BizException($"状态 {e.Status} 不可升级");
- await _flowEngine.StartFlow(new StartFlowInput
- {
- BizType = "EXCEPTION_ESCALATION",
- BizId = e.Id,
- Title = $"异常升级审批 - {e.ExceptionCode}",
- Comment = remark,
- BizData = new Dictionary<string, object>
- {
- ["severity"] = e.Severity,
- ["sceneCode"] = e.SceneCode,
- ["priorityLevel"] = e.PriorityLevel,
- }
- });
- // 状态和时间线由 ExceptionEscalationBizHandler.OnFlowStarted 回调更新
- return await LoadAsync(id, tenantId, factoryId) ?? e;
- }
- public async Task<AdoS8Exception> RejectAsync(long id, long tenantId, long factoryId, string? remark)
- {
- var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
- if (!S8StatusRules.IsAllowedTransition(e.Status, "REJECTED"))
- throw new S8BizException($"状态 {e.Status} 不可驳回");
- var from = e.Status;
- e.Status = "REJECTED";
- e.UpdatedAt = DateTime.Now;
- await _rep.AsTenant().UseTranAsync(async () =>
- {
- await _rep.UpdateAsync(e);
- await InsertTimelineAsync(e.Id, "REJECT", "驳回", from, "REJECTED", null, null, remark);
- }, ex => throw ex);
- return e;
- }
- public async Task<AdoS8Exception> CloseAsync(long id, long tenantId, long factoryId, string? remark)
- {
- var e = await LoadAsync(id, tenantId, factoryId) ?? throw new S8BizException("异常不存在");
- if (e.ActiveFlowInstanceId.HasValue)
- throw new S8BizException("该异常已有进行中的审批流程,请等待审批完成");
- if (!S8StatusRules.IsAllowedTransition(e.Status, "CLOSED"))
- throw new S8BizException($"状态 {e.Status} 不可关闭");
- await _flowEngine.StartFlow(new StartFlowInput
- {
- BizType = "EXCEPTION_CLOSURE",
- BizId = e.Id,
- Title = $"异常关闭确认 - {e.ExceptionCode}",
- Comment = remark,
- BizData = new Dictionary<string, object>
- {
- ["sceneCode"] = e.SceneCode,
- }
- });
- // 状态和时间线由 ExceptionClosureBizHandler.OnFlowStarted 回调更新
- return await LoadAsync(id, tenantId, factoryId) ?? e;
- }
- public async Task CommentAsync(long id, string? remark)
- {
- var e = await _rep.GetFirstAsync(x => x.Id == id && !x.IsDeleted)
- ?? throw new S8BizException("异常不存在");
- await InsertTimelineAsync(e.Id, "COMMENT", "补充说明", e.Status, e.Status, null, null, remark);
- }
- private Task<AdoS8Exception?> LoadAsync(long id, long tenantId, long factoryId) =>
- _rep.GetFirstAsync(x => x.Id == id && x.TenantId == tenantId && x.FactoryId == factoryId && !x.IsDeleted);
- private async Task InsertTimelineAsync(long exceptionId, string code, string label, string? from, string? to,
- long? operatorId, string? operatorName, string? remark) =>
- await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
- {
- ExceptionId = exceptionId,
- ActionCode = code,
- ActionLabel = label,
- FromStatus = from,
- ToStatus = to,
- OperatorId = operatorId,
- OperatorName = operatorName,
- ActionRemark = remark,
- CreatedAt = DateTime.Now
- });
- }
|