|
|
@@ -0,0 +1,264 @@
|
|
|
+using Admin.NET.Plugin.AiDOP.MaterialWarehouse.Dto;
|
|
|
+using Admin.NET.Plugin.ApprovalFlow;
|
|
|
+using Admin.NET.Plugin.ApprovalFlow.Service;
|
|
|
+
|
|
|
+namespace Admin.NET.Plugin.AiDOP.MaterialWarehouse;
|
|
|
+
|
|
|
+/// <summary>
|
|
|
+/// S5 来料检验单 检验员+主管最小审批流服务(S5-IQC-INSPBILL-INSPECTOR-SUPERVISOR-FLOW-1)。
|
|
|
+///
|
|
|
+/// N1_INSPECT 检验员录入 pd/dhsl/bhgsl/clfs → 提交 → N2_SUP_REVIEW 检验主管审核(通过=CLOSED / 退回=回 N1)。
|
|
|
+/// 复用 ApprovalFlow 引擎扩展点(StartFlow/Approve/ReturnToPrev + IFlowBizHandler),不改 ApprovalFlow 核心。
|
|
|
+/// BizType=S5_IQC_INSPBILL,BizId=qms_qcp_inspbill.id,BizCode=FBILLNO。反查用 ApprovalFlowInstance(BizType,BizId),inspbill 不加列。
|
|
|
+/// submit-result 仅写 qms_qcp_inspbill.pd/dhsl/bhgsl/clfs;approve/reject 不写 qms。不写库存、不入库、不做 SQE/仓储通知。
|
|
|
+/// 正常鉴权(非 AllowAnonymous);权限以当前用户 pending task 归属为准。
|
|
|
+/// </summary>
|
|
|
+[ApiDescriptionSettings(Order = 310, Description = "来料检验单流程")]
|
|
|
+[Route("api/S5IqcInspBillFlow")]
|
|
|
+[NonUnify]
|
|
|
+public class IqcInspBillFlowService : IDynamicApiController, ITransient
|
|
|
+{
|
|
|
+ private readonly ISqlSugarClient _db;
|
|
|
+ private readonly FlowEngineService _flowEngine;
|
|
|
+ private readonly UserManager _userManager;
|
|
|
+
|
|
|
+ public IqcInspBillFlowService(ISqlSugarClient db, FlowEngineService flowEngine, UserManager userManager)
|
|
|
+ {
|
|
|
+ _db = db;
|
|
|
+ _flowEngine = flowEngine;
|
|
|
+ _userManager = userManager;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检验单流程状态(只读,供前端按钮控制)。
|
|
|
+ /// </summary>
|
|
|
+ [DisplayName("来料检验单流程状态")]
|
|
|
+ [HttpGet("state")]
|
|
|
+ public async Task<IqcFlowStateOutput> GetState([FromQuery] long id)
|
|
|
+ {
|
|
|
+ await EnsureBillExistsAsync(id);
|
|
|
+ return await BuildStateAsync(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检验员提交检验结果(写 pd/dhsl/bhgsl/clfs + 推进 N1→N2)。
|
|
|
+ /// </summary>
|
|
|
+ [DisplayName("检验员提交检验结果")]
|
|
|
+ [HttpPost("submit-result")]
|
|
|
+ public async Task<IqcFlowStateOutput> SubmitResult([FromBody] IqcSubmitResultInput input)
|
|
|
+ {
|
|
|
+ var billNo = await EnsureBillExistsAsync(input.Id);
|
|
|
+
|
|
|
+ // 参数校验
|
|
|
+ if (input.Pd != 0 && input.Pd != 1) throw Oops.Oh("判定 pd 只能为 0(合格) 或 1(不合格)");
|
|
|
+ if (input.Dhsl < 0) throw Oops.Oh("合格数量不能为负");
|
|
|
+ if (input.Bhgsl < 0) throw Oops.Oh("不合格数量不能为负");
|
|
|
+ if (input.Pd == 0)
|
|
|
+ {
|
|
|
+ if (input.Bhgsl != 0) throw Oops.Oh("判定合格时不合格数量必须为 0");
|
|
|
+ }
|
|
|
+ else // pd == 1
|
|
|
+ {
|
|
|
+ if (input.Bhgsl <= 0) throw Oops.Oh("判定不合格时不合格数量必须大于 0");
|
|
|
+ if (input.Clfs is not (0 or 1 or 2)) throw Oops.Oh("判定不合格时处理方式 clfs 必填且只能为 0(让步接收)/1(挑选)/2(报废)");
|
|
|
+ }
|
|
|
+
|
|
|
+ var inst = await GetLatestInstanceAsync(input.Id);
|
|
|
+ if (inst != null && inst.Status != FlowInstanceStatusEnum.Running)
|
|
|
+ throw Oops.Oh("该检验单流程已结束,无法再提交检验结果");
|
|
|
+
|
|
|
+ var tran = await _db.AsTenant().UseTranAsync(async () =>
|
|
|
+ {
|
|
|
+ // 无运行中实例 → 起流程(自动生成 N1 任务给检验员角色成员)
|
|
|
+ long instanceId;
|
|
|
+ if (inst == null)
|
|
|
+ {
|
|
|
+ instanceId = await _flowEngine.StartFlow(new StartFlowInput
|
|
|
+ {
|
|
|
+ BizType = IqcInspBillFlowConst.BizType,
|
|
|
+ BizId = input.Id,
|
|
|
+ BizNo = billNo,
|
|
|
+ Title = $"来料检验单 {billNo}",
|
|
|
+ Comment = input.Comment,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ instanceId = inst.Id;
|
|
|
+ }
|
|
|
+
|
|
|
+ var myTask = await GetMyPendingTaskAsync(instanceId);
|
|
|
+ if (myTask == null)
|
|
|
+ throw Oops.Oh("当前用户没有待处理的检验任务(需检验员角色成员)");
|
|
|
+ if (myTask.NodeId != IqcInspBillFlowConst.NodeInspect)
|
|
|
+ throw Oops.Oh("当前不在检验录入节点,无法提交检验结果");
|
|
|
+
|
|
|
+ // 写检验结果(仅 pd/dhsl/bhgsl/clfs)
|
|
|
+ await _db.Ado.ExecuteCommandAsync(
|
|
|
+ "UPDATE qms_qcp_inspbill SET pd=@pd, dhsl=@dhsl, bhgsl=@bhgsl, clfs=@clfs WHERE id=@id",
|
|
|
+ new List<SugarParameter>
|
|
|
+ {
|
|
|
+ new("@pd", input.Pd),
|
|
|
+ new("@dhsl", input.Dhsl),
|
|
|
+ new("@bhgsl", input.Bhgsl),
|
|
|
+ new("@clfs", input.Pd == 1 ? input.Clfs : null),
|
|
|
+ new("@id", input.Id),
|
|
|
+ });
|
|
|
+
|
|
|
+ // 推进 N1 → N2
|
|
|
+ await _flowEngine.Approve(myTask.Id, input.Comment);
|
|
|
+ });
|
|
|
+ if (!tran.IsSuccess) throw tran.ErrorException;
|
|
|
+
|
|
|
+ return await BuildStateAsync(input.Id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检验主管通过(N2 → 完成)。
|
|
|
+ /// </summary>
|
|
|
+ [DisplayName("检验主管通过")]
|
|
|
+ [HttpPost("supervisor-approve")]
|
|
|
+ public async Task<IqcFlowStateOutput> SupervisorApprove([FromBody] IqcSupervisorApproveInput input)
|
|
|
+ {
|
|
|
+ await EnsureBillExistsAsync(input.Id);
|
|
|
+ var (inst, myTask) = await RequireSupervisorTaskAsync(input.Id);
|
|
|
+
|
|
|
+ var tran = await _db.AsTenant().UseTranAsync(async () =>
|
|
|
+ {
|
|
|
+ await _flowEngine.Approve(myTask.Id, input.Comment);
|
|
|
+ });
|
|
|
+ if (!tran.IsSuccess) throw tran.ErrorException;
|
|
|
+
|
|
|
+ return await BuildStateAsync(input.Id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检验主管退回(N2 → 回 N1,意见必填)。
|
|
|
+ /// </summary>
|
|
|
+ [DisplayName("检验主管退回")]
|
|
|
+ [HttpPost("supervisor-reject")]
|
|
|
+ public async Task<IqcFlowStateOutput> SupervisorReject([FromBody] IqcSupervisorRejectInput input)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(input.Comment))
|
|
|
+ throw Oops.Oh("退回必须填写意见");
|
|
|
+
|
|
|
+ await EnsureBillExistsAsync(input.Id);
|
|
|
+ var (inst, myTask) = await RequireSupervisorTaskAsync(input.Id);
|
|
|
+
|
|
|
+ var tran = await _db.AsTenant().UseTranAsync(async () =>
|
|
|
+ {
|
|
|
+ await _flowEngine.ReturnToPrev(myTask.Id, input.Comment);
|
|
|
+ });
|
|
|
+ if (!tran.IsSuccess) throw tran.ErrorException;
|
|
|
+
|
|
|
+ return await BuildStateAsync(input.Id);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ───────────────────────── helpers ─────────────────────────
|
|
|
+
|
|
|
+ /// <summary>校验检验单存在并返回 FBILLNO。</summary>
|
|
|
+ private async Task<string> EnsureBillExistsAsync(long id)
|
|
|
+ {
|
|
|
+ if (id <= 0) throw Oops.Oh("检验单 id 非法");
|
|
|
+ var rows = await _db.Ado.SqlQueryAsync<string>(
|
|
|
+ "SELECT FBILLNO FROM qms_qcp_inspbill WHERE id=@id LIMIT 1",
|
|
|
+ new List<SugarParameter> { new("@id", id) });
|
|
|
+ if (rows.Count == 0) throw Oops.Oh("检验单不存在");
|
|
|
+ return rows[0] ?? id.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>取该检验单最新流程实例(BizType+BizId,跨数据范围)。</summary>
|
|
|
+ private async Task<ApprovalFlowInstance?> GetLatestInstanceAsync(long bizId)
|
|
|
+ {
|
|
|
+ return await _db.Queryable<ApprovalFlowInstance>()
|
|
|
+ .ClearFilter()
|
|
|
+ .Where(x => x.BizType == IqcInspBillFlowConst.BizType && x.BizId == bizId)
|
|
|
+ .OrderByDescending(x => x.Id)
|
|
|
+ .FirstAsync();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>取当前用户在指定实例下的待办任务。</summary>
|
|
|
+ private async Task<ApprovalFlowTask?> GetMyPendingTaskAsync(long instanceId)
|
|
|
+ {
|
|
|
+ var userId = _userManager.UserId;
|
|
|
+ return await _db.Queryable<ApprovalFlowTask>()
|
|
|
+ .ClearFilter()
|
|
|
+ .Where(x => x.InstanceId == instanceId && x.AssigneeId == userId && x.Status == FlowTaskStatusEnum.Pending)
|
|
|
+ .FirstAsync();
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>校验并返回主管 N2 待办任务。</summary>
|
|
|
+ private async Task<(ApprovalFlowInstance inst, ApprovalFlowTask task)> RequireSupervisorTaskAsync(long bizId)
|
|
|
+ {
|
|
|
+ var inst = await GetLatestInstanceAsync(bizId);
|
|
|
+ if (inst == null) throw Oops.Oh("该检验单尚未发起流程");
|
|
|
+ if (inst.Status != FlowInstanceStatusEnum.Running) throw Oops.Oh("该检验单流程已结束");
|
|
|
+ var myTask = await GetMyPendingTaskAsync(inst.Id);
|
|
|
+ if (myTask == null) throw Oops.Oh("当前用户没有待处理的审核任务(需检验主管角色成员)");
|
|
|
+ if (myTask.NodeId != IqcInspBillFlowConst.NodeSupervisor) throw Oops.Oh("当前不在主管审核节点");
|
|
|
+ return (inst, myTask);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>当前用户是否具备指定角色(跨数据范围)。</summary>
|
|
|
+ private async Task<bool> HasRoleAsync(long roleId)
|
|
|
+ {
|
|
|
+ var userId = _userManager.UserId;
|
|
|
+ return await _db.Queryable<SysUserRole>()
|
|
|
+ .ClearFilter()
|
|
|
+ .AnyAsync(x => x.UserId == userId && x.RoleId == roleId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>组装流程状态输出。</summary>
|
|
|
+ private async Task<IqcFlowStateOutput> BuildStateAsync(long bizId)
|
|
|
+ {
|
|
|
+ var output = new IqcFlowStateOutput { BizId = bizId };
|
|
|
+ var inst = await GetLatestInstanceAsync(bizId);
|
|
|
+
|
|
|
+ if (inst == null)
|
|
|
+ {
|
|
|
+ output.FlowStatus = "NotStarted";
|
|
|
+ output.CanSubmitResult = await HasRoleAsync(IqcInspBillFlowConst.RoleInspectorId);
|
|
|
+ output.Message = output.CanSubmitResult ? "待检验员录入结果" : "尚未发起流程";
|
|
|
+ return output;
|
|
|
+ }
|
|
|
+
|
|
|
+ output.InstanceId = inst.Id;
|
|
|
+ output.FlowStatus = inst.Status.ToString();
|
|
|
+ output.UpdatedAt = inst.EndTime ?? inst.StartTime;
|
|
|
+
|
|
|
+ if (inst.Status != FlowInstanceStatusEnum.Running)
|
|
|
+ {
|
|
|
+ output.IsCompleted = true;
|
|
|
+ output.Message = inst.Status == FlowInstanceStatusEnum.Approved ? "流程已完成(主管审核通过)" : $"流程已结束({inst.Status})";
|
|
|
+ return output;
|
|
|
+ }
|
|
|
+
|
|
|
+ output.CurrentNodeCode = inst.CurrentNodeId;
|
|
|
+ var myTask = await GetMyPendingTaskAsync(inst.Id);
|
|
|
+ if (myTask != null)
|
|
|
+ {
|
|
|
+ output.CurrentNodeName = myTask.NodeName;
|
|
|
+ if (myTask.NodeId == IqcInspBillFlowConst.NodeInspect)
|
|
|
+ {
|
|
|
+ output.CurrentAssigneeRole = IqcInspBillFlowConst.RoleInspectorCode;
|
|
|
+ output.CanSubmitResult = true;
|
|
|
+ output.Message = "待当前用户录入检验结果";
|
|
|
+ }
|
|
|
+ else if (myTask.NodeId == IqcInspBillFlowConst.NodeSupervisor)
|
|
|
+ {
|
|
|
+ output.CurrentAssigneeRole = IqcInspBillFlowConst.RoleSupervisorCode;
|
|
|
+ output.CanSupervisorApprove = true;
|
|
|
+ output.CanSupervisorReject = true;
|
|
|
+ output.Message = "待当前用户主管审核";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ output.CurrentAssigneeRole = inst.CurrentNodeId == IqcInspBillFlowConst.NodeInspect
|
|
|
+ ? IqcInspBillFlowConst.RoleInspectorCode
|
|
|
+ : IqcInspBillFlowConst.RoleSupervisorCode;
|
|
|
+ output.Message = "流程进行中(当前用户无待办)";
|
|
|
+ }
|
|
|
+ return output;
|
|
|
+ }
|
|
|
+}
|