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; /// /// 异常升级审批业务回调 /// BizType = "EXCEPTION_ESCALATION" /// public class ExceptionEscalationBizHandler : IFlowBizHandler, ITransient { public string BizType => "EXCEPTION_ESCALATION"; private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository _timelineRep; public ExceptionEscalationBizHandler( SqlSugarRepository rep, SqlSugarRepository timelineRep) { _rep = rep; _timelineRep = timelineRep; } public async Task OnFlowStarted(long bizId, long instanceId) { var e = await _rep.GetByIdAsync(bizId) ?? throw new S8BizException("异常不存在"); e.Status = "ESCALATED"; e.ActiveFlowInstanceId = instanceId; e.ActiveFlowBizType = BizType; e.UpdatedAt = DateTime.Now; await _rep.UpdateAsync(e); await InsertTimelineAsync(e.Id, "ESCALATE_START", "发起升级", null, "ESCALATED", 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 = "ASSIGNED"; // S8-SYSUSER-ONLY-1:lastApproverId 与 AssigneeUserId 现在是同一个 ID 空间,直接写。 // // 原实现要先经 EmployeeMaster.SysUserId 反查出 employee.RecID 才能落库, // 反查不到就"保留原值 + 在 timeline 备注里说明"。真库 1105 名员工只有 8 个绑了账号, // 也就是说升级审批通过后处理人几乎总是不变 —— 而页面上看到的是「升级已确认」。 // 桥没了,这条静默失败路径也就没了。 string? assigneeNote = null; if (lastApproverId is > 0) e.AssigneeUserId = lastApproverId.Value; else assigneeNote = "未提供审批人信息,处理人保持原值"; await _rep.UpdateAsync(e); await InsertTimelineAsync(e.Id, "ESCALATE_APPROVED", assigneeNote == null ? "升级已确认" : $"升级已确认({assigneeNote})", "ESCALATED", "ASSIGNED", instanceId, lastApproverId); } else if (finalStatus == FlowInstanceStatusEnum.Cancelled) { e.Status = "IN_PROGRESS"; await _rep.UpdateAsync(e); await InsertTimelineAsync(e.Id, "ESCALATE_CANCELLED", "发起人撤回升级申请", "ESCALATED", "IN_PROGRESS", instanceId, lastApproverId); } else { e.Status = "IN_PROGRESS"; await _rep.UpdateAsync(e); await InsertTimelineAsync(e.Id, "ESCALATE_REJECTED", "升级被驳回", "ESCALATED", "IN_PROGRESS", instanceId, lastApproverId); } } public async Task> GetBizData(long bizId) { var e = await _rep.GetByIdAsync(bizId); if (e == null) return new Dictionary(); return new Dictionary { ["severity"] = e.Severity ?? string.Empty, ["sceneCode"] = e.SceneCode ?? string.Empty, ["priorityLevel"] = e.PriorityLevel ?? 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, OperatorUserId = approverId, ActionRemark = remark, CreatedAt = DateTime.Now }); } }