|
|
@@ -0,0 +1,298 @@
|
|
|
+using Admin.NET.Plugin.AiDOP.Entity;
|
|
|
+using Admin.NET.Plugin.AiDOP.FinishedWarehouse.Dto;
|
|
|
+using Admin.NET.Plugin.ApprovalFlow;
|
|
|
+using Admin.NET.Plugin.ApprovalFlow.Service;
|
|
|
+
|
|
|
+namespace Admin.NET.Plugin.AiDOP.FinishedWarehouse;
|
|
|
+
|
|
|
+/// <summary>
|
|
|
+/// S7 FQC 成品检验单 检验员+主管最小审批流服务(S7-FQC-INSPBILL-INSPECTOR-SUPERVISOR-FLOW-1)。
|
|
|
+///
|
|
|
+/// N1_INSPECT 检验员录入 pd/hgsl/bhgsl/clfs → 提交 → N2_SUP_REVIEW 检验主管审核(通过=Approved / 退回=回 N1)。
|
|
|
+/// 复用 ApprovalFlow 引擎扩展点(StartFlow/Approve/ReturnToPrev + IFlowBizHandler),不改 ApprovalFlow 核心。
|
|
|
+/// BizType=S7_FQC_INSPBILL,BizId=qms_qcpp_inspbill.id,BizCode=FBILLNO。反查用 ApprovalFlowInstance(BizType,BizId),inspbill 不加列。
|
|
|
+///
|
|
|
+/// 状态归属(严格分离):
|
|
|
+/// - 检验业务结果 → qms_qcpp_inspbill.pd/hgsl/bhgsl/clfs(仅 submit-result 写)
|
|
|
+/// - 检验任务执行进度 → qms_fqcbj.jyfzr/jykssj/jywcsj/FINSPECTSTATUS(经 lydjbh=FBILLNO 关联;仅执行状态未检验/检验中/检验完成)
|
|
|
+/// - 审批节点/待办/审核状态 → ApprovalFlowInstance(绝不写入 FINSPECTSTATUS)
|
|
|
+/// 不写 FBILLSTATUS/库存/仓储/生产状态;clfs 仅保存不触发任何后续动作。正常鉴权(非匿名)。
|
|
|
+/// </summary>
|
|
|
+[ApiDescriptionSettings(Order = 331, Description = "FQC检验单流程")]
|
|
|
+[Route("api/S7FqcInspBillFlow")]
|
|
|
+[NonUnify]
|
|
|
+public class FqcInspBillFlowService : IDynamicApiController, ITransient
|
|
|
+{
|
|
|
+ private readonly ISqlSugarClient _db;
|
|
|
+ private readonly FlowEngineService _flowEngine;
|
|
|
+ private readonly UserManager _userManager;
|
|
|
+
|
|
|
+ public FqcInspBillFlowService(ISqlSugarClient db, FlowEngineService flowEngine, UserManager userManager)
|
|
|
+ {
|
|
|
+ _db = db;
|
|
|
+ _flowEngine = flowEngine;
|
|
|
+ _userManager = userManager;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>检验单流程状态(只读,供前端按钮控制)。</summary>
|
|
|
+ [DisplayName("FQC检验单流程状态")]
|
|
|
+ [HttpGet("state")]
|
|
|
+ public async Task<FqcFlowStateOutput> GetState([FromQuery] long id)
|
|
|
+ {
|
|
|
+ await EnsureBillExistsAsync(id);
|
|
|
+ return await BuildStateAsync(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 检验员提交检验结果(写 qms_qcpp_inspbill.pd/hgsl/bhgsl/clfs + qms_fqcbj 任务进度 + 推进 N1→N2)。
|
|
|
+ /// </summary>
|
|
|
+ [DisplayName("检验员提交检验结果")]
|
|
|
+ [HttpPost("submit-result")]
|
|
|
+ public async Task<FqcFlowStateOutput> SubmitResult([FromBody] FqcSubmitResultInput input)
|
|
|
+ {
|
|
|
+ var (billNo, sourceBillNo) = await EnsureBillAsync(input.Id);
|
|
|
+
|
|
|
+ // 参数校验
|
|
|
+ if (input.Pd != 0 && input.Pd != 1) throw Oops.Oh("判定 pd 只能为 0(合格) 或 1(不合格)");
|
|
|
+ if (input.Hgsl < 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 inspectorName = _userManager.RealName ?? _userManager.Account;
|
|
|
+ var now = DateTime.Now;
|
|
|
+
|
|
|
+ var tran = await _db.AsTenant().UseTranAsync(async () =>
|
|
|
+ {
|
|
|
+ // 无运行中实例 → 起流程(自动生成 N1 任务给检验员角色成员)
|
|
|
+ long instanceId = inst?.Id ?? await _flowEngine.StartFlow(new StartFlowInput
|
|
|
+ {
|
|
|
+ BizType = FqcInspBillFlowConst.BizType,
|
|
|
+ BizId = input.Id,
|
|
|
+ BizNo = billNo,
|
|
|
+ Title = $"FQC检验单 {billNo}",
|
|
|
+ Comment = input.Comment,
|
|
|
+ });
|
|
|
+
|
|
|
+ var myTask = await GetMyPendingTaskAsync(instanceId);
|
|
|
+ if (myTask == null)
|
|
|
+ throw Oops.Oh("当前用户没有待处理的检验任务(需检验员角色成员)");
|
|
|
+ if (myTask.NodeId != FqcInspBillFlowConst.NodeInspect)
|
|
|
+ throw Oops.Oh("当前不在检验录入节点,无法提交检验结果");
|
|
|
+
|
|
|
+ // ① 检验业务结果 → qms_qcpp_inspbill(仅 pd/hgsl/bhgsl/clfs,不覆盖继承字段)
|
|
|
+ await _db.Ado.ExecuteCommandAsync(
|
|
|
+ "UPDATE qms_qcpp_inspbill SET pd=@pd, hgsl=@hgsl, bhgsl=@bhgsl, clfs=@clfs WHERE id=@id",
|
|
|
+ new List<SugarParameter>
|
|
|
+ {
|
|
|
+ new("@pd", input.Pd),
|
|
|
+ new("@hgsl", input.Hgsl),
|
|
|
+ new("@bhgsl", input.Bhgsl),
|
|
|
+ new("@clfs", input.Pd == 1 ? input.Clfs : null),
|
|
|
+ new("@id", input.Id),
|
|
|
+ });
|
|
|
+
|
|
|
+ // ② 检验任务执行进度 → qms_fqcbj(经 lydjbh=FBILLNO 关联;lydjbh 为空则跳过,防误伤)
|
|
|
+ // 安全约束(§二):先查关联数量 —— 0 条跳过、1 条更新、>1 条视为数据异常抛错整体回滚,
|
|
|
+ // 杜绝无保护批量 UPDATE 一次误更新多条任务(即使未来真实数据出现重复 FBILLNO 也安全)。
|
|
|
+ // jykssj 若为空则补首次开始时间(IFNULL 保留既有);jywcsj=本次提交时间;FINSPECTSTATUS=检验完成
|
|
|
+ if (!string.IsNullOrWhiteSpace(sourceBillNo))
|
|
|
+ {
|
|
|
+ var taskCount = await _db.Ado.GetIntAsync(
|
|
|
+ "SELECT COUNT(1) FROM qms_fqcbj WHERE FBILLNO=@bjbh",
|
|
|
+ new List<SugarParameter> { new("@bjbh", sourceBillNo) });
|
|
|
+ if (taskCount > 1)
|
|
|
+ throw Oops.Oh($"检验任务数据异常:来源单号 {sourceBillNo} 匹配到 {taskCount} 条报检任务(qms_fqcbj),无法安全更新,已整体回滚");
|
|
|
+ if (taskCount == 1)
|
|
|
+ {
|
|
|
+ await _db.Ado.ExecuteCommandAsync(
|
|
|
+ "UPDATE qms_fqcbj SET jyfzr=@jyfzr, jykssj=IFNULL(jykssj,@now), jywcsj=@now, FINSPECTSTATUS=@status WHERE FBILLNO=@bjbh",
|
|
|
+ new List<SugarParameter>
|
|
|
+ {
|
|
|
+ new("@jyfzr", inspectorName),
|
|
|
+ new("@now", now),
|
|
|
+ new("@status", FqcInspBillFlowConst.InspectStatusDone),
|
|
|
+ new("@bjbh", sourceBillNo),
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ③ 推进 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<FqcFlowStateOutput> SupervisorApprove([FromBody] FqcSupervisorApproveInput input)
|
|
|
+ {
|
|
|
+ await EnsureBillAsync(input.Id);
|
|
|
+ var (_, 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<FqcFlowStateOutput> SupervisorReject([FromBody] FqcSupervisorRejectInput input)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(input.Comment))
|
|
|
+ throw Oops.Oh("退回必须填写意见");
|
|
|
+
|
|
|
+ await EnsureBillAsync(input.Id);
|
|
|
+ var (_, myTask) = await RequireSupervisorTaskAsync(input.Id);
|
|
|
+
|
|
|
+ var tran = await _db.AsTenant().UseTranAsync(async () =>
|
|
|
+ {
|
|
|
+ // ReturnToPrev 回 N1;不清 pd/hgsl/bhgsl/clfs、不清 FINSPECTSTATUS/jywcsj,检验员改后重提
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+ var (billNo, _) = await EnsureBillAsync(id);
|
|
|
+ return billNo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>校验检验单存在并返回 (FBILLNO, lydjbh来源单号)。</summary>
|
|
|
+ private async Task<(string billNo, string? sourceBillNo)> EnsureBillAsync(long id)
|
|
|
+ {
|
|
|
+ if (id <= 0) throw Oops.Oh("检验单 id 非法");
|
|
|
+ var rows = await _db.Ado.SqlQueryAsync<FqcBillKey>(
|
|
|
+ "SELECT FBILLNO AS BillNo, lydjbh AS SourceBillNo FROM qms_qcpp_inspbill WHERE id=@id LIMIT 1",
|
|
|
+ new List<SugarParameter> { new("@id", id) });
|
|
|
+ var r = rows.FirstOrDefault();
|
|
|
+ if (r == null) throw Oops.Oh("检验单不存在");
|
|
|
+ return (r.BillNo ?? id.ToString(), r.SourceBillNo);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>取该检验单最新流程实例(BizType+BizId,跨数据范围)。</summary>
|
|
|
+ private async Task<ApprovalFlowInstance?> GetLatestInstanceAsync(long bizId)
|
|
|
+ {
|
|
|
+ return await _db.Queryable<ApprovalFlowInstance>()
|
|
|
+ .ClearFilter()
|
|
|
+ .Where(x => x.BizType == FqcInspBillFlowConst.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 != FqcInspBillFlowConst.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<FqcFlowStateOutput> BuildStateAsync(long bizId)
|
|
|
+ {
|
|
|
+ var output = new FqcFlowStateOutput { BizId = bizId };
|
|
|
+ var inst = await GetLatestInstanceAsync(bizId);
|
|
|
+
|
|
|
+ if (inst == null)
|
|
|
+ {
|
|
|
+ output.FlowStatus = "NotStarted";
|
|
|
+ output.CanSubmitResult = await HasRoleAsync(FqcInspBillFlowConst.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 == FqcInspBillFlowConst.NodeInspect)
|
|
|
+ {
|
|
|
+ output.CurrentAssigneeRole = FqcInspBillFlowConst.RoleInspectorCode;
|
|
|
+ output.CanSubmitResult = true;
|
|
|
+ output.Message = "待当前用户录入检验结果";
|
|
|
+ }
|
|
|
+ else if (myTask.NodeId == FqcInspBillFlowConst.NodeSupervisor)
|
|
|
+ {
|
|
|
+ output.CurrentAssigneeRole = FqcInspBillFlowConst.RoleSupervisorCode;
|
|
|
+ output.CanSupervisorApprove = true;
|
|
|
+ output.CanSupervisorReject = true;
|
|
|
+ output.Message = "待当前用户主管审核";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ output.CurrentAssigneeRole = inst.CurrentNodeId == FqcInspBillFlowConst.NodeInspect
|
|
|
+ ? FqcInspBillFlowConst.RoleInspectorCode
|
|
|
+ : FqcInspBillFlowConst.RoleSupervisorCode;
|
|
|
+ output.Message = "流程进行中(当前用户无待办)";
|
|
|
+ }
|
|
|
+ return output;
|
|
|
+ }
|
|
|
+
|
|
|
+ private class FqcBillKey
|
|
|
+ {
|
|
|
+ public string? BillNo { get; set; }
|
|
|
+ public string? SourceBillNo { get; set; }
|
|
|
+ }
|
|
|
+}
|