| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using Admin.NET.Plugin.ApprovalFlow;
- using Admin.NET.Plugin.ApprovalFlow.Service;
- using Admin.NET.Plugin.AiDOP.Entity.S8;
- namespace Admin.NET.Plugin.AiDOP.Service.S8;
- /// <summary>
- /// 异常关闭确认审批业务回调
- /// BizType = "EXCEPTION_CLOSURE"
- /// </summary>
- public class ExceptionClosureBizHandler : IFlowBizHandler, ITransient
- {
- public string BizType => "EXCEPTION_CLOSURE";
- private readonly SqlSugarRepository<AdoS8Exception> _rep;
- private readonly SqlSugarRepository<AdoS8ExceptionTimeline> _timelineRep;
- public ExceptionClosureBizHandler(
- SqlSugarRepository<AdoS8Exception> rep,
- SqlSugarRepository<AdoS8ExceptionTimeline> timelineRep)
- {
- _rep = rep;
- _timelineRep = timelineRep;
- }
- public async Task OnFlowStarted(long bizId, long instanceId)
- {
- var e = await _rep.GetByIdAsync(bizId) ?? throw new S8BizException("异常不存在");
- e.ActiveFlowInstanceId = instanceId;
- e.ActiveFlowBizType = BizType;
- e.UpdatedAt = DateTime.Now;
- await _rep.UpdateAsync(e);
- await InsertTimelineAsync(e.Id, "CLOSE_START", "发起关闭确认", null, null, instanceId, null);
- }
- public async Task OnFlowCompleted(long bizId, long instanceId, FlowInstanceStatusEnum finalStatus, long? lastApproverId)
- {
- var e = await _rep.GetByIdAsync(bizId) ?? throw new S8BizException("异常不存在");
- e.ActiveFlowInstanceId = null;
- e.ActiveFlowBizType = null;
- e.UpdatedAt = DateTime.Now;
- if (finalStatus == FlowInstanceStatusEnum.Approved)
- {
- e.Status = "CLOSED";
- e.ClosedAt = DateTime.Now;
- await _rep.UpdateAsync(e);
- await InsertTimelineAsync(e.Id, "CLOSE_APPROVED", "关闭已确认", "RESOLVED", "CLOSED",
- instanceId, lastApproverId);
- }
- else if (finalStatus == FlowInstanceStatusEnum.Cancelled)
- {
- // 撤回:状态维持 RESOLVED,不写终态
- await _rep.UpdateAsync(e);
- await InsertTimelineAsync(e.Id, "CLOSE_CANCELLED", "处理人撤回关闭申请", null, null,
- instanceId, lastApproverId);
- }
- else
- {
- e.Status = "IN_PROGRESS";
- await _rep.UpdateAsync(e);
- await InsertTimelineAsync(e.Id, "CLOSE_REJECTED", "关闭被驳回", "RESOLVED", "IN_PROGRESS",
- instanceId, lastApproverId);
- }
- }
- public async Task<Dictionary<string, object>> GetBizData(long bizId)
- {
- var e = await _rep.GetByIdAsync(bizId);
- if (e == null) return new Dictionary<string, object>();
- return new Dictionary<string, object>
- {
- ["sceneCode"] = e.SceneCode ?? string.Empty,
- ["factoryCode"] = e.FactoryId.ToString(),
- };
- }
- private async Task InsertTimelineAsync(long exceptionId, string code, string label,
- string? from, string? to, long? instanceId, long? approverId)
- {
- string? remark = null;
- if (instanceId.HasValue && approverId.HasValue)
- remark = $"审批实例ID: {instanceId},审批人: {approverId}";
- else if (instanceId.HasValue)
- remark = $"审批实例ID: {instanceId}";
- else if (approverId.HasValue)
- remark = $"审批人: {approverId}";
- await _timelineRep.InsertAsync(new AdoS8ExceptionTimeline
- {
- ExceptionId = exceptionId,
- ActionCode = code,
- ActionLabel = label,
- FromStatus = from,
- ToStatus = to,
- OperatorId = approverId,
- ActionRemark = remark,
- CreatedAt = DateTime.Now
- });
- }
- }
|