| | |
| | | package cn.iocoder.yudao.module.mes.service.pro.feedback; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.feedback.vo.*; |
| | | import cn.iocoder.yudao.module.mes.enums.md.autocode.MesMdAutoCodeRuleCodeEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.pro.MesProFeedbackTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.qc.MesQcTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.service.qc.template.MesQcTemplateItemService; |
| | | import cn.iocoder.yudao.module.mes.enums.pro.MesProWorkOrderTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.service.md.autocode.MesMdAutoCodeRecordService; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.feedback.MesProFeedbackDO; |
| | |
| | | import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderProcessService; |
| | | import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.itemconsume.MesWmItemConsumeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.productissue.MesWmProductIssueService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.productproduce.MesWmProductProduceLineService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.productproduce.MesWmProductProduceService; |
| | | import cn.iocoder.yudao.module.mes.service.mdm.MesMdmItemService; |
| | |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | |
| | | |
| | | @Resource |
| | | private MesProFeedbackMapper feedbackMapper; |
| | | @Resource |
| | | private MesFeedbackParamService feedbackParamService; |
| | | |
| | | @Resource |
| | | private MesProWorkOrderService workOrderService; |
| | |
| | | @Resource |
| | | private MesWmItemConsumeService itemConsumeService; |
| | | @Resource |
| | | private MesWmProductIssueService productIssueService; |
| | | @Resource |
| | | private MesWmProductProduceService productProduceService; |
| | | @Resource |
| | | private MesWmProductProduceLineService produceLineService; |
| | |
| | | private MesMdmItemService mdmItemService; |
| | | @Resource |
| | | private MdmItemBatchConfigApi itemBatchConfigApi; |
| | | @Resource |
| | | private MesQcTemplateItemService templateItemService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createFeedback(MesProFeedbackSaveReqVO createReqVO) { |
| | | // 1. 校验 |
| | | MesProTaskDO task = validateFeedbackData(createReqVO); |
| | | task = feedbackParamService.lockTask(task.getId()); |
| | | validateLockedTaskDimensions(createReqVO, task); |
| | | |
| | | // 2. 插入(自动填充 itemId) |
| | | // 2. 插入(主键由数据库自增,忽略请求中可能残留的 id;自动填充 itemId) |
| | | MesProFeedbackDO feedback = BeanUtils.toBean(createReqVO, MesProFeedbackDO.class) |
| | | .setId(null) |
| | | .setStatus(MesProFeedbackStatusEnum.PREPARE.getStatus()) |
| | | .setItemId(task.getItemId()); |
| | | .setItemId(task.getItemId()) |
| | | .setParamSnapshot(feedbackParamService.buildDraft(task, createReqVO.getParamValues())); |
| | | feedbackMapper.insert(feedback); |
| | | return feedback.getId(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateFeedback(MesProFeedbackSaveReqVO updateReqVO) { |
| | | // 1.1 校验存在 + 草稿状态 |
| | | validateFeedbackStatusPrepare(updateReqVO.getId()); |
| | | // 先锁报工再锁任务,与提交顺序一致。 |
| | | MesProFeedbackDO original = feedbackMapper.selectOne(new LambdaQueryWrapperX<MesProFeedbackDO>() |
| | | .eq(MesProFeedbackDO::getId, updateReqVO.getId()).last("FOR UPDATE")); |
| | | if (original == null) throw exception(PRO_FEEDBACK_NOT_EXISTS); |
| | | if (!Objects.equals(original.getStatus(), MesProFeedbackStatusEnum.PREPARE.getStatus())) |
| | | throw exception(PRO_FEEDBACK_NOT_PREPARE); |
| | | // 1.2 校验业务数据 |
| | | MesProTaskDO task = validateFeedbackData(updateReqVO); |
| | | task = feedbackParamService.lockTask(task.getId()); |
| | | validateLockedTaskDimensions(updateReqVO, task); |
| | | |
| | | // 2. 更新(自动填充 itemId) |
| | | MesProFeedbackDO updateObj = BeanUtils.toBean(updateReqVO, MesProFeedbackDO.class) |
| | | .setItemId(task.getItemId()); |
| | | if (updateReqVO.getParamValues() != null) { |
| | | String snapshot = feedbackParamService.buildDraft(task, updateReqVO.getParamValues()); |
| | | updateObj.setParamSnapshot(snapshot == null ? "" : snapshot); |
| | | } else if (!Objects.equals(original.getTaskId(), task.getId())) { |
| | | updateObj.setParamSnapshot(""); |
| | | } |
| | | feedbackMapper.updateById(updateObj); |
| | | } |
| | | |
| | | @Override |
| | | public void deleteFeedback(Long id) { |
| | | // 1. 校验存在 + 草稿状态 |
| | | validateFeedbackStatusPrepare(id); |
| | | private void validateLockedTaskDimensions(MesProFeedbackSaveReqVO req, MesProTaskDO task) { |
| | | if (!MesProTaskStatusEnum.canFeedback(task.getStatus())) throw exception(PRO_FEEDBACK_CANNOT_CREATE_WHEN_PAUSED); |
| | | if (!Objects.equals(req.getProcessId(), task.getProcessId()) |
| | | || !Objects.equals(req.getWorkOrderId(), task.getWorkOrderId()) |
| | | || !Objects.equals(req.getRouteId(), task.getRouteId())) |
| | | throw exception(PRO_FEEDBACK_PARAM_INVALID, "任务信息已变更,请重新选择任务"); |
| | | } |
| | | |
| | | // 2. 删除 |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesProFeedbackImportRespVO importFeedbackList(List<MesProFeedbackImportExcelVO> importList, |
| | | boolean updateSupport) { |
| | | // 1. 校验非空 |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(MES_PRO_FEEDBACK_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 逐行处理 |
| | | MesProFeedbackImportRespVO respVO = MesProFeedbackImportRespVO.builder() |
| | | .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>()) |
| | | .failureCodes(new LinkedHashMap<>()).build(); |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | importList.forEach(importItem -> { |
| | | int currentIndex = index.getAndIncrement(); |
| | | String key = StrUtil.blankToDefault(importItem.getCode(), "第 " + currentIndex + " 行"); |
| | | if (StrUtil.isBlank(importItem.getCode())) { |
| | | respVO.getFailureCodes().put(key, "报工单编号不能为空"); |
| | | return; |
| | | } |
| | | if (StrUtil.isBlank(importItem.getTaskCode())) { |
| | | respVO.getFailureCodes().put(key, "生产任务编码不能为空"); |
| | | return; |
| | | } |
| | | // 2.1 解析生产任务 |
| | | MesProTaskDO task = taskService.getTaskByCode(importItem.getTaskCode().trim()); |
| | | if (task == null) { |
| | | respVO.getFailureCodes().put(key, "生产任务编码[" + importItem.getTaskCode() + "]不存在"); |
| | | return; |
| | | } |
| | | // 2.2 构建请求并创建/更新(复用 create/update 服务,报工人/审核人取当前登录用户) |
| | | MesProFeedbackSaveReqVO reqVO = BeanUtils.toBean(importItem, MesProFeedbackSaveReqVO.class); |
| | | reqVO.setTaskId(task.getId()); |
| | | Long loginUserId = getLoginUserId(); |
| | | reqVO.setFeedbackUserId(ObjUtil.defaultIfNull(reqVO.getFeedbackUserId(), loginUserId)); |
| | | reqVO.setApproveUserId(ObjUtil.defaultIfNull(reqVO.getApproveUserId(), loginUserId)); |
| | | MesProFeedbackDO existFeedback = feedbackMapper.selectByCode(importItem.getCode()); |
| | | try { |
| | | if (existFeedback == null) { |
| | | reqVO.setId(null); |
| | | createFeedback(reqVO); |
| | | respVO.getCreateCodes().add(importItem.getCode()); |
| | | } else if (updateSupport) { |
| | | reqVO.setId(existFeedback.getId()); |
| | | updateFeedback(reqVO); |
| | | respVO.getUpdateCodes().add(importItem.getCode()); |
| | | } else { |
| | | respVO.getFailureCodes().put(key, "报工单编号已存在"); |
| | | } |
| | | } catch (ServiceException e) { |
| | | respVO.getFailureCodes().put(key, e.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteFeedback(Long id) { |
| | | MesProFeedbackDO feedback = feedbackMapper.selectOne(new LambdaQueryWrapperX<MesProFeedbackDO>() |
| | | .eq(MesProFeedbackDO::getId, id).last("FOR UPDATE")); |
| | | if (feedback == null) throw exception(PRO_FEEDBACK_NOT_EXISTS); |
| | | if (!Objects.equals(feedback.getStatus(), MesProFeedbackStatusEnum.PREPARE.getStatus())) |
| | | throw exception(PRO_FEEDBACK_NOT_PREPARE); |
| | | feedbackMapper.deleteById(id); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitFeedback(Long id) { |
| | | // 1. 校验存在 + 草稿状态 |
| | | validateFeedbackStatusPrepare(id); |
| | | |
| | | // 2. 更新状态为审批中(报工人和报工时间由表单保存时确定,提交不覆盖) |
| | | feedbackMapper.updateById(new MesProFeedbackDO().setId(id) |
| | | .setStatus(MesProFeedbackStatusEnum.APPROVING.getStatus())); |
| | | MesProFeedbackDO feedback = feedbackMapper.selectOne(new LambdaQueryWrapperX<MesProFeedbackDO>() |
| | | .eq(MesProFeedbackDO::getId, id).last("FOR UPDATE")); |
| | | if (feedback == null) throw exception(PRO_FEEDBACK_NOT_EXISTS); |
| | | if (!Objects.equals(feedback.getStatus(), MesProFeedbackStatusEnum.PREPARE.getStatus())) |
| | | throw exception(PRO_FEEDBACK_NOT_PREPARE); |
| | | MesProTaskDO task = feedbackParamService.lockTask(feedback.getTaskId()); |
| | | validateTaskAssignee(task); |
| | | String snapshot = feedbackParamService.complete(task, feedback.getParamSnapshot()); |
| | | MesProFeedbackDO update = new MesProFeedbackDO().setId(id) |
| | | .setStatus(MesProFeedbackStatusEnum.APPROVING.getStatus()); |
| | | if (snapshot != null) update.setParamSnapshot(snapshot); |
| | | feedbackMapper.updateById(update); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void rejectFeedback(Long id) { |
| | | // 1. 校验存在 + 审批中状态 |
| | | validateFeedbackStatusApproving(id); |
| | | MesProFeedbackDO feedback = lockFeedbackStatusApproving(id); |
| | | // 1.1 校验当前登录人是该报工单的指定审核人 |
| | | validateCurrentUserIsApprover(feedback); |
| | | |
| | | // 2. 更新状态为草稿 |
| | | feedbackMapper.updateById(new MesProFeedbackDO().setId(id) |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean approveFeedback(MesProFeedbackApproveReqVO reqVO) { |
| | | // 1.1 校验存在 + 审批中状态 |
| | | MesProFeedbackDO feedback = validateFeedbackStatusApproving(reqVO.getId()); |
| | | MesProFeedbackDO feedback = lockFeedbackStatusApproving(reqVO.getId()); |
| | | // 1.1.1 校验当前登录人是该报工单的指定审核人 |
| | | validateCurrentUserIsApprover(feedback); |
| | | // 1.2 校验报工数量 > 0 |
| | | if (feedback.getFeedbackQuantity() == null |
| | | || feedback.getFeedbackQuantity().compareTo(BigDecimal.ZERO) <= 0) { |
| | |
| | | // 1.3 校验任务未完成 |
| | | taskService.validateTaskNotFinished(feedback.getTaskId()); |
| | | |
| | | // 2.1 查询工序的关键工序标识 + 检验标识(需在 uncheckQuantity 校验之前,因为质检工序允许 uncheckQuantity > 0) |
| | | MesProRouteProcessDO routeProcess = routeProcessService.getRouteProcessByRouteIdAndProcessId( |
| | | feedback.getRouteId(), feedback.getProcessId()); |
| | | boolean keyFlag = routeProcess != null && Boolean.TRUE.equals(routeProcess.getKeyFlag()); |
| | | boolean checkFlag = routeProcess != null && Boolean.TRUE.equals(routeProcess.getCheckFlag()); |
| | | // 2.1 查询工序的检验标识(工单工序快照优先,需在 uncheckQuantity 校验之前,因为质检工序允许 uncheckQuantity > 0) |
| | | ProcessFlags processFlags = resolveProcessFlags(feedback.getRouteId(), feedback.getWorkOrderId(), |
| | | feedback.getProcessId()); |
| | | boolean checkFlag = processFlags.checkFlag(); |
| | | // 2.2 非质检工序:仍有待检数量时不能审批(质检工序的 uncheckQuantity > 0 是正常状态,不做拦截) |
| | | if (!checkFlag |
| | | && feedback.getUncheckQuantity() != null |
| | |
| | | throw exception(PRO_FEEDBACK_UNCHECK_QUANTITY_EXISTS, feedback.getUncheckQuantity()); |
| | | } |
| | | |
| | | if (checkFlag) { |
| | | try { |
| | | templateItemService.getRequiredTemplateByItemIdAndType(feedback.getItemId(), MesQcTypeEnum.IPQC.getType()); |
| | | } catch (ServiceException e) { |
| | | if (!Objects.equals(e.getCode(), QC_NO_TEMPLATE.getCode())) throw e; |
| | | MesMdmItemDO item = mdmItemService.getItem(feedback.getItemId()); |
| | | String name = item != null ? item.getName() + "(" + item.getCode() + ")" : "ID " + feedback.getItemId(); |
| | | throw exception(PRO_FEEDBACK_PARAM_INVALID, "物料【" + name + "】未配置启用的过程检验模板,请先配置后再审批"); |
| | | } |
| | | } else { |
| | | BigDecimal qualified = ObjectUtil.defaultIfNull(feedback.getQualifiedQuantity(), BigDecimal.ZERO); |
| | | BigDecimal unqualified = ObjectUtil.defaultIfNull(feedback.getUnqualifiedQuantity(), BigDecimal.ZERO); |
| | | if (qualified.signum() < 0 || unqualified.signum() < 0 |
| | | || qualified.add(unqualified).compareTo(feedback.getFeedbackQuantity()) != 0) { |
| | | throw exception(PRO_FEEDBACK_PARAM_INVALID, "合格数和不合格数必须非负且合计等于报工数量"); |
| | | } |
| | | } |
| | | // 3. 物料消耗:倒冲工序按 BOM 自动扣料;非倒冲工序跳过(物料走手工领料/投料) |
| | | boolean backflushFlag = routeProcess != null && Boolean.TRUE.equals(routeProcess.getBackflushFlag()); |
| | | boolean backflushFlag = processFlags.backflushFlag(); |
| | | if (backflushFlag) { |
| | | // 3.1 校验倒冲工序必须配置 BOM 消耗,避免静默不扣料 |
| | | List<MesProWorkOrderBomDO> boms = workOrderBomService.getWorkOrderBomListByWorkOrderId( |
| | | feedback.getWorkOrderId()); |
| | | // 3.1 校验当前倒冲工序必须配置投料 BOM,避免静默不扣料 |
| | | List<MesProWorkOrderBomDO> boms = workOrderBomService.getWorkOrderBomListByWorkOrderIdAndProcessId( |
| | | feedback.getWorkOrderId(), feedback.getProcessId()); |
| | | if (CollUtil.isEmpty(boms)) { |
| | | throw exception(PRO_FEEDBACK_BACKFLUSH_BOM_REQUIRED); |
| | | } |
| | |
| | | MesWmItemConsumeDO itemConsume = itemConsumeService.generateItemConsume(feedback); |
| | | if (itemConsume != null) { |
| | | itemConsumeService.finishItemConsume(itemConsume.getId()); |
| | | // 消耗单负责实际扣库;领料单仅作为倒冲业务凭证,避免重复扣减库存 |
| | | productIssueService.generateBackflushProductIssue(feedback, itemConsume); |
| | | } |
| | | } |
| | | |
| | | // 5. 先更新报工状态为已完成(确保 checkAndFinishMps 检查时当前报工已标记完成) |
| | | feedbackMapper.updateById(new MesProFeedbackDO().setId(reqVO.getId()) |
| | | .setStatus(MesProFeedbackStatusEnum.FINISHED.getStatus()) |
| | | .setUncheckQuantity(BigDecimal.ZERO)); |
| | | |
| | | // 6. 根据是否需要检验,决定产出和入库方式 |
| | | // 6.2 调整 只要审批成功直接更新 |
| | | // 关键工序:更新任务 + 工单的已生产数量;非关键工序:只更新任务的已生产数量(工单只看关键工序产出) |
| | | if (keyFlag) { |
| | | updateTaskAndWorkOrderByFeedback(feedback); |
| | | } else { |
| | | taskService.updateProducedQuantity(feedback.getTaskId(), |
| | | feedback.getFeedbackQuantity(), |
| | | ObjectUtil.defaultIfNull(feedback.getQualifiedQuantity(), BigDecimal.ZERO), |
| | | ObjectUtil.defaultIfNull(feedback.getUnqualifiedQuantity(), BigDecimal.ZERO)); |
| | | checkAndPublishProductionFinished(feedback.getWorkOrderId()); |
| | | |
| | | } |
| | | // 4. 先生成产出;进度仅在产出完成且报工状态变为完成后更新。 |
| | | if (checkFlag) { |
| | | // 6.1 需要检验:生成产出单(质量状态=待检验),更新报工状态为待检验,等质检完成回调后再入库 |
| | | // 完成时回调见:MesQcIpqcServiceImpl#finishIpqc → splitPendingAndFinishProduce + updateProFeedbackWhenIpqcFinish |
| | |
| | | // 6.2 无需检验:生成产出单(按合格/不合格拆行),直接完成入库 |
| | | MesWmProductProduceDO produce = productProduceService.generateProductProduce(feedback, false, reqVO); |
| | | productProduceService.finishProductProduce(produce.getId()); |
| | | feedbackMapper.updateById(new MesProFeedbackDO().setId(reqVO.getId()) |
| | | .setStatus(MesProFeedbackStatusEnum.FINISHED.getStatus()).setUncheckQuantity(BigDecimal.ZERO)); |
| | | updateTaskAndWorkOrderByFeedback(feedback); |
| | | |
| | | } |
| | | |
| | |
| | | * @param feedback 报工单 |
| | | */ |
| | | private void updateTaskAndWorkOrderByFeedback(MesProFeedbackDO feedback) { |
| | | // 1. 查询该报工单关联的所有产出单行,按质量状态聚合数量 |
| | | // 1. 查询该报工单关联的产出单行,只按主产出物料(报工物料)聚合合格/不合格数量, |
| | | // 联产出物料单独检验,不计入任务进度 |
| | | BigDecimal qualifiedQty = BigDecimal.ZERO; |
| | | BigDecimal unqualifiedQty = BigDecimal.ZERO; |
| | | List<MesWmProductProduceLineDO> lines = produceLineService.getProductProduceLineListByFeedbackId(feedback.getId()); |
| | | for (MesWmProductProduceLineDO line : lines) { |
| | | if (!ObjUtil.equal(line.getItemId(), feedback.getItemId())) { |
| | | continue; |
| | | } |
| | | if (ObjUtil.equal(line.getQualityStatus(), MesWmQualityStatusEnum.FAIL.getStatus())) { |
| | | unqualifiedQty = unqualifiedQty.add(line.getQuantity()); |
| | | } |
| | |
| | | // 3. 更新订单的已生产数量 |
| | | // 新增逻辑:只有在该笔订单最后一个工序才更新生产订单的已生产数量 |
| | | MesProWorkOrderProcessDO lastProcess = workOrderProcessService.getLastWorkOrderProcessByWorkOrderId(feedback.getWorkOrderId()); |
| | | if (lastProcess != null && ObjUtil.equal(lastProcess.getProcessId(), feedback.getProcessId())) { |
| | | Long lastProcessId; |
| | | if (lastProcess != null) { |
| | | lastProcessId = lastProcess.getProcessId(); |
| | | } else { |
| | | // 旧工单可能没有保存工单工序:与排产一致回退到工艺路线工序。 |
| | | List<MesProRouteProcessDO> routeProcesses = routeProcessService.getRouteProcessListByRouteId(feedback.getRouteId()); |
| | | if (CollUtil.isEmpty(routeProcesses) || routeProcesses.stream().anyMatch(process -> process.getSort() == null)) { |
| | | throw exception(PRO_FEEDBACK_PARAM_INVALID, "无法确定末道工序,请检查工单和工艺路线的工序顺序"); |
| | | } |
| | | int maxSort = routeProcesses.stream().mapToInt(MesProRouteProcessDO::getSort).max().orElseThrow(); |
| | | List<MesProRouteProcessDO> lastProcesses = routeProcesses.stream() |
| | | .filter(process -> process.getSort() == maxSort).toList(); |
| | | if (lastProcesses.size() != 1 || lastProcesses.get(0).getProcessId() == null) { |
| | | throw exception(PRO_FEEDBACK_PARAM_INVALID, "无法唯一确定末道工序,请检查工艺路线的工序顺序"); |
| | | } |
| | | lastProcessId = lastProcesses.get(0).getProcessId(); |
| | | } |
| | | if (ObjUtil.equal(lastProcessId, feedback.getProcessId())) { |
| | | workOrderService.updateProducedQuantity(feedback.getWorkOrderId(), |
| | | feedback.getFeedbackQuantity()); |
| | | // 4. 更新 MPS 的已生产数量(如果是销售订单来源) |
| | |
| | | return feedback; |
| | | } |
| | | |
| | | private MesProFeedbackDO lockFeedbackStatusApproving(Long id) { |
| | | MesProFeedbackDO feedback = feedbackMapper.selectOne(new LambdaQueryWrapperX<MesProFeedbackDO>() |
| | | .eq(MesProFeedbackDO::getId, id).last("FOR UPDATE")); |
| | | if (feedback == null) throw exception(PRO_FEEDBACK_NOT_EXISTS); |
| | | if (ObjUtil.notEqual(feedback.getStatus(), MesProFeedbackStatusEnum.APPROVING.getStatus())) |
| | | throw exception(PRO_FEEDBACK_NOT_APPROVING); |
| | | return feedback; |
| | | } |
| | | private MesProFeedbackDO validateFeedbackStatusApproving(Long id) { |
| | | MesProFeedbackDO feedback = validateFeedbackExists(id); |
| | | if (ObjUtil.notEqual(feedback.getStatus(), MesProFeedbackStatusEnum.APPROVING.getStatus())) { |
| | | throw exception(PRO_FEEDBACK_NOT_APPROVING); |
| | | } |
| | | return feedback; |
| | | } |
| | | |
| | | /** |
| | | * 校验当前登录用户是否为报工单指定的审核人 |
| | | * |
| | | * 报工单创建时必须指定 approveUserId,审批通过/驳回只能由该审核人操作, |
| | | * 避免任意拥有菜单权限的用户越权审批。 |
| | | * |
| | | * @param feedback 报工单 |
| | | */ |
| | | private void validateCurrentUserIsApprover(MesProFeedbackDO feedback) { |
| | | if (feedback.getApproveUserId() == null) { |
| | | throw exception(PRO_FEEDBACK_APPROVER_NOT_CONFIGURED); |
| | | } |
| | | if (!feedback.getApproveUserId().equals(getLoginUserId())) { |
| | | throw exception(PRO_FEEDBACK_NOT_APPROVER); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验当前登录人是否为任务的报工人;任务未配置报工人时无人可报工 |
| | | */ |
| | | private void validateTaskAssignee(MesProTaskDO task) { |
| | | Long loginUserId = getLoginUserId(); |
| | | if (loginUserId == null) { |
| | | throw exception(PRO_FEEDBACK_NOT_ASSIGNED); |
| | | } |
| | | String current = String.valueOf(loginUserId); |
| | | boolean assigned = StrUtil.split(task.getAssigneeUserIds(), ',') |
| | | .stream().map(String::trim).anyMatch(current::equals); |
| | | if (!assigned) { |
| | | throw exception(PRO_FEEDBACK_NOT_ASSIGNED); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | if (!MesProTaskStatusEnum.canFeedback(task.getStatus())) { |
| | | throw exception(PRO_FEEDBACK_CANNOT_CREATE_WHEN_PAUSED); |
| | | } |
| | | // 校验当前登录人是该任务的报工人 |
| | | validateTaskAssignee(task); |
| | | |
| | | // 2. 从任务自动填充关联字段(前端无需传入,后端统一从任务获取) |
| | | reqVO.setWorkOrderId(task.getWorkOrderId()); |
| | |
| | | reqVO.setItemId(task.getItemId()); |
| | | reqVO.setWorkstationId(task.getWorkstationId()); |
| | | |
| | | // 3. 校验工艺路线 + 工序配置有效 |
| | | MesProRouteProcessDO routeProcess = routeProcessService.getRouteProcessByRouteIdAndProcessId( |
| | | task.getRouteId(), task.getProcessId()); |
| | | if (routeProcess == null) { |
| | | throw exception(PRO_FEEDBACK_ROUTE_PROCESS_INVALID); |
| | | } |
| | | // 3. 校验工单工序 / 工艺路线工序配置有效 |
| | | boolean checkFlag = resolveProcessFlags(task.getRouteId(), task.getWorkOrderId(), task.getProcessId()).checkFlag(); |
| | | // 4.1 校验数量 |
| | | boolean checkFlag = Boolean.TRUE.equals(routeProcess.getCheckFlag()); |
| | | if (checkFlag) { |
| | | // 需要检验:只需填报工数量,且必须 > 0 |
| | | if (reqVO.getFeedbackQuantity() == null |
| | |
| | | return task; |
| | | } |
| | | |
| | | /** |
| | | * 解析工序的检验标识与倒冲标识 |
| | | * |
| | | * <p>工单工序快照(mes_pro_work_order_process)是下单时确认的权威配置,优先使用; |
| | | * 工单未维护该工序时再回退到工艺路线模板(mes_pro_route_process)。 |
| | | */ |
| | | private ProcessFlags resolveProcessFlags(Long routeId, Long workOrderId, Long processId) { |
| | | MesProWorkOrderProcessDO workOrderProcess = workOrderId != null && processId != null |
| | | ? workOrderProcessService.getWorkOrderProcessByWorkOrderIdAndProcessId(workOrderId, processId) : null; |
| | | MesProRouteProcessDO routeProcess = routeId != null && processId != null |
| | | ? routeProcessService.getRouteProcessByRouteIdAndProcessId(routeId, processId) : null; |
| | | if (workOrderProcess == null && routeProcess == null) { |
| | | throw exception(PRO_FEEDBACK_ROUTE_PROCESS_INVALID); |
| | | } |
| | | Boolean checkFlag = workOrderProcess != null && workOrderProcess.getCheckFlag() != null |
| | | ? workOrderProcess.getCheckFlag() : routeProcess != null ? routeProcess.getCheckFlag() : null; |
| | | Boolean backflushFlag = workOrderProcess != null && workOrderProcess.getBackflushFlag() != null |
| | | ? workOrderProcess.getBackflushFlag() : routeProcess != null ? routeProcess.getBackflushFlag() : null; |
| | | return new ProcessFlags(Boolean.TRUE.equals(checkFlag), Boolean.TRUE.equals(backflushFlag)); |
| | | } |
| | | |
| | | private record ProcessFlags(boolean checkFlag, boolean backflushFlag) { |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesProFeedbackQuickCreateRespVO quickCreateFeedback(MesProFeedbackQuickCreateReqVO reqVO) { |
| | |
| | | if (!MesProTaskStatusEnum.canFeedback(task.getStatus())) { |
| | | throw exception(PRO_FEEDBACK_CANNOT_CREATE_WHEN_PAUSED); |
| | | } |
| | | // 校验当前登录人是该任务的报工人 |
| | | validateTaskAssignee(task); |
| | | |
| | | // 2. 校验工艺路线工序配置 |
| | | MesProRouteProcessDO routeProcess = routeProcessService.getRouteProcessByRouteIdAndProcessId( |
| | | task.getRouteId(), task.getProcessId()); |
| | | if (routeProcess == null) { |
| | | throw exception(PRO_FEEDBACK_ROUTE_PROCESS_INVALID); |
| | | } |
| | | // 2. 校验工单工序 / 工艺路线工序配置 |
| | | boolean checkFlag = resolveProcessFlags(task.getRouteId(), task.getWorkOrderId(), task.getProcessId()).checkFlag(); |
| | | // 3. 校验数量 |
| | | boolean checkFlag = Boolean.TRUE.equals(routeProcess.getCheckFlag()); |
| | | if (checkFlag) { |
| | | if (reqVO.getFeedbackQuantity() == null |
| | | || reqVO.getFeedbackQuantity().compareTo(BigDecimal.ZERO) <= 0) { |
| | |
| | | .setFeedbackTime(LocalDateTime.now()) |
| | | .setStatus(MesProFeedbackStatusEnum.APPROVING.getStatus()) |
| | | .setRemark(reqVO.getRemark()); |
| | | MesProTaskDO lockedTask = feedbackParamService.lockTask(task.getId()); |
| | | if (!MesProTaskStatusEnum.canFeedback(lockedTask.getStatus())) throw exception(PRO_FEEDBACK_CANNOT_CREATE_WHEN_PAUSED); |
| | | if (!Objects.equals(task.getProcessId(), lockedTask.getProcessId()) |
| | | || !Objects.equals(task.getWorkOrderId(), lockedTask.getWorkOrderId()) |
| | | || !Objects.equals(task.getRouteId(), lockedTask.getRouteId())) |
| | | throw exception(PRO_FEEDBACK_PARAM_INVALID, "任务信息已变更,请重新报工"); |
| | | String draftParam = feedbackParamService.buildDraft(lockedTask, reqVO.getParamValues()); |
| | | feedback.setParamSnapshot(feedbackParamService.complete(lockedTask, draftParam)); |
| | | feedbackMapper.insert(feedback); |
| | | // 5. 返回结果 |
| | | MesProWorkOrderDO workOrder = workOrderService.getWorkOrder(task.getWorkOrderId()); |
| | |
| | | int failCount = 0; |
| | | for (Long taskId : reqVO.getTaskIds()) { |
| | | try { |
| | | MesProTaskDO batchTask = feedbackParamService.lockTask(taskId); |
| | | if (Objects.equals(batchTask.getParamCollectMode(), 1) |
| | | || (Objects.equals(batchTask.getParamCollectMode(), 2) && StrUtil.isBlank(batchTask.getParamSharedSnapshot()))) { |
| | | throw exception(PRO_FEEDBACK_PARAM_INVALID, "该任务需要单独填写参数,请使用单笔报工"); |
| | | } |
| | | MesProFeedbackQuickCreateReqVO singleReq = new MesProFeedbackQuickCreateReqVO(); |
| | | singleReq.setTaskId(taskId); |
| | | singleReq.setFeedbackQuantity(reqVO.getFeedbackQuantity()); |
| | |
| | | BigDecimal qualifiedQty, BigDecimal unqualifiedQty, |
| | | BigDecimal laborScrapQty, BigDecimal materialScrapQty, BigDecimal otherScrapQty) { |
| | | // 1. 校验报工单存在且为待检验状态 |
| | | MesProFeedbackDO feedback = validateFeedbackExists(feedbackId); |
| | | MesProFeedbackDO feedback = feedbackMapper.selectOne(new LambdaQueryWrapperX<MesProFeedbackDO>() |
| | | .eq(MesProFeedbackDO::getId, feedbackId).last("FOR UPDATE")); |
| | | if (feedback == null) throw exception(PRO_FEEDBACK_NOT_EXISTS); |
| | | if (ObjUtil.notEqual(feedback.getStatus(), MesProFeedbackStatusEnum.UNCHECK.getStatus())) { |
| | | throw exception(PRO_FEEDBACK_NOT_UNCHECK); |
| | | } |
| | | |
| | | // 2. 拆分待检产出行(合格/不合格),生成明细,完成产出入库 |
| | | // 2. 拆分当前检验的产出行(合格/不合格),生成明细;全部物料检验完成才完成产出入库 |
| | | productProduceService.splitPendingAndFinishProduce(feedbackId, sourceLineId, qualifiedQty, unqualifiedQty); |
| | | |
| | | // 3. 回写合格/不合格/废品数量,更新状态为已完成 |
| | | feedbackMapper.updateById(new MesProFeedbackDO().setId(feedbackId) |
| | | .setQualifiedQuantity(qualifiedQty) |
| | | .setUnqualifiedQuantity(unqualifiedQty) |
| | | .setUncheckQuantity(BigDecimal.ZERO) |
| | | .setLaborScrapQuantity(laborScrapQty) |
| | | .setMaterialScrapQuantity(materialScrapQty) |
| | | .setOtherScrapQuantity(otherScrapQty) |
| | | .setStatus(MesProFeedbackStatusEnum.FINISHED.getStatus())); |
| | | // 3. 累加本次检验结果;报工单只记录主产出物料的合格/不合格/废品数量, |
| | | // 联产出物料各自检验,不并入报工数量;待全部产出物料检验完成才完成报工 |
| | | List<MesWmProductProduceLineDO> produceLines = produceLineService.getProductProduceLineListByFeedbackId(feedbackId); |
| | | MesWmProductProduceLineDO sourceLine = produceLines.stream() |
| | | .filter(line -> ObjUtil.equal(line.getId(), sourceLineId)).findFirst().orElse(null); |
| | | boolean mainOutput = sourceLine == null || ObjUtil.equal(sourceLine.getItemId(), feedback.getItemId()); |
| | | BigDecimal totalQualified = ObjectUtil.defaultIfNull(feedback.getQualifiedQuantity(), BigDecimal.ZERO) |
| | | .add(mainOutput ? qualifiedQty : BigDecimal.ZERO); |
| | | BigDecimal totalUnqualified = ObjectUtil.defaultIfNull(feedback.getUnqualifiedQuantity(), BigDecimal.ZERO) |
| | | .add(mainOutput ? unqualifiedQty : BigDecimal.ZERO); |
| | | BigDecimal totalLaborScrap = ObjectUtil.defaultIfNull(feedback.getLaborScrapQuantity(), BigDecimal.ZERO) |
| | | .add(mainOutput ? laborScrapQty : BigDecimal.ZERO); |
| | | BigDecimal totalMaterialScrap = ObjectUtil.defaultIfNull(feedback.getMaterialScrapQuantity(), BigDecimal.ZERO) |
| | | .add(mainOutput ? materialScrapQty : BigDecimal.ZERO); |
| | | BigDecimal totalOtherScrap = ObjectUtil.defaultIfNull(feedback.getOtherScrapQuantity(), BigDecimal.ZERO) |
| | | .add(mainOutput ? otherScrapQty : BigDecimal.ZERO); |
| | | boolean allInspected = produceLines.stream() |
| | | .noneMatch(line -> ObjUtil.equal(line.getQualityStatus(), MesWmQualityStatusEnum.PENDING.getStatus())); |
| | | MesProFeedbackDO updateObj = new MesProFeedbackDO().setId(feedbackId) |
| | | .setQualifiedQuantity(totalQualified) |
| | | .setUnqualifiedQuantity(totalUnqualified) |
| | | .setLaborScrapQuantity(totalLaborScrap) |
| | | .setMaterialScrapQuantity(totalMaterialScrap) |
| | | .setOtherScrapQuantity(totalOtherScrap) |
| | | .setStatus(allInspected ? MesProFeedbackStatusEnum.FINISHED.getStatus() |
| | | : MesProFeedbackStatusEnum.UNCHECK.getStatus()); |
| | | if (allInspected) { |
| | | updateObj.setUncheckQuantity(BigDecimal.ZERO); |
| | | } |
| | | feedbackMapper.updateById(updateObj); |
| | | |
| | | // 4. 更新任务/工单的已生产数量 |
| | | feedback.setQualifiedQuantity(qualifiedQty).setUnqualifiedQuantity(unqualifiedQty); |
| | | //5.检查并发布生产完成事件 |
| | | checkAndPublishProductionFinished(feedback.getWorkOrderId()); |
| | | // 4. 全部检验完成后回写任务/工单进度 |
| | | if (allInspected) { |
| | | updateTaskAndWorkOrderByFeedback(feedback); |
| | | } |
| | | } |
| | | |
| | | @Override |