| | |
| | | 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.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.feedback.vo.*; |
| | |
| | | import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderBomService; |
| | | 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.pro.workorder.MesProWorkOrderParamRecordService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.itemconsume.MesWmItemConsumeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.productproduce.MesWmProductProduceLineService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.productproduce.MesWmProductProduceService; |
| | |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @lombok.extern.slf4j.Slf4j |
| | | public class MesProFeedbackServiceImpl implements MesProFeedbackService { |
| | | |
| | | @Resource |
| | |
| | | |
| | | @Resource |
| | | private MesProWorkOrderService workOrderService; |
| | | @Resource |
| | | @Lazy |
| | | private MesProWorkOrderParamRecordService paramRecordService; |
| | | @Resource |
| | | private MesProRouteProcessService routeProcessService; |
| | | @Resource |
| | |
| | | private MesMdmItemService mdmItemService; |
| | | @Resource |
| | | private MdmItemBatchConfigApi itemBatchConfigApi; |
| | | @Resource |
| | | private cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi processInstanceApi; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createFeedback(MesProFeedbackSaveReqVO createReqVO) { |
| | | // 1. 校验 |
| | | MesProTaskDO task = validateFeedbackData(createReqVO); |
| | |
| | | .setStatus(MesProFeedbackStatusEnum.PREPARE.getStatus()) |
| | | .setItemId(task.getItemId()); |
| | | feedbackMapper.insert(feedback); |
| | | // 3. 保存参数值;必填参数校验失败时整体回滚,避免残留草稿 |
| | | saveParamValues(task.getId(), createReqVO.getParamValues()); |
| | | return feedback.getId(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateFeedback(MesProFeedbackSaveReqVO updateReqVO) { |
| | | // 1.1 校验存在 + 草稿状态 |
| | | validateFeedbackStatusPrepare(updateReqVO.getId()); |
| | |
| | | MesProFeedbackDO updateObj = BeanUtils.toBean(updateReqVO, MesProFeedbackDO.class) |
| | | .setItemId(task.getItemId()); |
| | | feedbackMapper.updateById(updateObj); |
| | | // 3. 保存参数值;必填参数校验失败时整体回滚 |
| | | saveParamValues(task.getId(), updateReqVO.getParamValues()); |
| | | } |
| | | |
| | | private void saveParamValues(Long taskId, List<MesProFeedbackSaveReqVO.ParamValue> paramValues) { |
| | | // 即使参数为空也调用更新,以便触发必填参数校验(任务绑定了参数时必须填写) |
| | | if (CollUtil.isEmpty(paramValues)) { |
| | | paramRecordService.updateTaskParamValues(taskId, null, null); |
| | | return; |
| | | } |
| | | paramRecordService.updateTaskParamValues(taskId, |
| | | paramValues.stream().map(MesProFeedbackSaveReqVO.ParamValue::getDetailId).toList(), |
| | | paramValues.stream().map(MesProFeedbackSaveReqVO.ParamValue::getValue).toList()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void submitFeedback(Long id) { |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitFeedback(MesProFeedbackSubmitReqVO reqVO) { |
| | | Long id = reqVO.getId(); |
| | | // 1. 校验存在 + 草稿状态 |
| | | validateFeedbackStatusPrepare(id); |
| | | MesProFeedbackDO feedback = validateFeedbackStatusPrepare(id); |
| | | |
| | | // 2. 更新状态为审批中(报工人和报工时间由表单保存时确定,提交不覆盖) |
| | | feedbackMapper.updateById(new MesProFeedbackDO().setId(id) |
| | | .setStatus(MesProFeedbackStatusEnum.APPROVING.getStatus())); |
| | | MesProWorkOrderDO workOrder = workOrderService.getWorkOrder(feedback.getWorkOrderId()); |
| | | |
| | | if (workOrder != null && Boolean.TRUE.equals(workOrder.getFeedbackApprove()) && cn.hutool.core.util.StrUtil.isNotBlank(workOrder.getProcessDefinitionKey())) { |
| | | // 2. 更新状态为审批中,并启动流程 |
| | | String processInstanceId = processInstanceApi.createProcessInstance(cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId(), |
| | | new cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(workOrder.getProcessDefinitionKey()) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | feedbackMapper.updateById(new MesProFeedbackDO().setId(id) |
| | | .setStatus(MesProFeedbackStatusEnum.APPROVING.getStatus()) |
| | | .setProcessDefinitionKey(workOrder.getProcessDefinitionKey()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | } else { |
| | | // 2. 判断这个报工需不需要批次号 |
| | | MesProFeedbackBatchCheckRespVO mesProFeedbackBatchCheckRespVO = checkBatchRequirements(id); |
| | | if (mesProFeedbackBatchCheckRespVO.getNeedBatchInput()) { |
| | | if (StrUtil.isBlank(reqVO.getLotNumber())) { |
| | | throw exception(MD_ITEM_BATCH_CONFIG_AT_LEAST_ONE_APPROVE_FLAG); |
| | | } |
| | | } |
| | | |
| | | // 3. 更新状态为审批中,然后直接通过审批 |
| | | feedbackMapper.updateById(new MesProFeedbackDO().setId(id) |
| | | .setStatus(MesProFeedbackStatusEnum.APPROVING.getStatus())); |
| | | MesProFeedbackApproveReqVO approveReqVO = cn.iocoder.yudao.framework.common.util.object.BeanUtils.toBean(reqVO, MesProFeedbackApproveReqVO.class); |
| | | approveFeedback(approveReqVO); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void handleBpmProcessResult(Long id, Integer bpmResult,String reason) { |
| | | // 1. 柔性校验:获取报工单并判断状态 |
| | | MesProFeedbackDO feedback = feedbackMapper.selectById(id); |
| | | if (feedback == null) { |
| | | log.warn("[handleBpmProcessResult] 生产报工({}) 不存在", id); |
| | | return; |
| | | } |
| | | if (!MesProFeedbackStatusEnum.APPROVING.getStatus().equals(feedback.getStatus())) { |
| | | log.warn("[handleBpmProcessResult] 生产报工({}) 不处于审批中状态,当前状态:{}", id, feedback.getStatus()); |
| | | return; |
| | | } |
| | | |
| | | // 2. 根据审批结果进行处理 |
| | | if (cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | MesProFeedbackApproveReqVO reqVO = new MesProFeedbackApproveReqVO(); |
| | | reqVO.setId(id); |
| | | // 判断这个报工需不需要批次号 |
| | | MesProFeedbackBatchCheckRespVO mesProFeedbackBatchCheckRespVO = checkBatchRequirements(id); |
| | | if (mesProFeedbackBatchCheckRespVO.getNeedBatchInput()){ |
| | | if (StrUtil.isBlank(reason)){ |
| | | // 报错 请在审批意见里填写批次号 |
| | | throw exception(MD_ITEM_BATCH_CONFIG_AT_LEAST_ONE_APPROVE_FLAG); |
| | | }else { |
| | | BeanUtils.copyProperties(mesProFeedbackBatchCheckRespVO,reqVO); |
| | | reqVO.setLotNumber(reason); |
| | | } |
| | | } |
| | | approveFeedback(reqVO); |
| | | } else if (cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum.REJECT.getStatus().equals(bpmResult) || |
| | | cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | rejectFeedback(id); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | feedback.getRouteId(), feedback.getProcessId()); |
| | | boolean keyFlag = routeProcess != null && Boolean.TRUE.equals(routeProcess.getKeyFlag()); |
| | | boolean checkFlag = routeProcess != null && Boolean.TRUE.equals(routeProcess.getCheckFlag()); |
| | | // 2.2 非质检工序:仍有待检数量时不能审批(质检工序的 uncheckQuantity > 0 是正常状态,不做拦截) |
| | | // 2.2 非质检工序:合格品/不良品数量由质检确定;本工序不经过质检,报工未填写时默认全部为合格品 |
| | | if (!checkFlag |
| | | && feedback.getQualifiedQuantity() == null |
| | | && feedback.getUnqualifiedQuantity() == null) { |
| | | feedback.setQualifiedQuantity(feedback.getFeedbackQuantity()); |
| | | feedback.setUnqualifiedQuantity(BigDecimal.ZERO); |
| | | feedbackMapper.updateById(new MesProFeedbackDO().setId(feedback.getId()) |
| | | .setQualifiedQuantity(feedback.getFeedbackQuantity()) |
| | | .setUnqualifiedQuantity(BigDecimal.ZERO)); |
| | | } |
| | | // 2.3 非质检工序:仍有待检数量时不能审批(质检工序的 uncheckQuantity > 0 是正常状态,不做拦截) |
| | | if (!checkFlag |
| | | && feedback.getUncheckQuantity() != null |
| | | && feedback.getUncheckQuantity().compareTo(BigDecimal.ZERO) > 0) { |
| | |
| | | if (ObjUtil.equal(line.getQualityStatus(), MesWmQualityStatusEnum.PASS.getStatus())) { |
| | | qualifiedQty = qualifiedQty.add(line.getQuantity()); |
| | | } |
| | | } |
| | | // 1.1 兜底:关键非质检工序审批时产出单行尚未生成,直接采用报工单上的合格/不良数量 |
| | | // (非质检工序默认全部为合格品;质检工序的合格/不良在质检回调后按产出行聚合) |
| | | if (CollUtil.isEmpty(lines)) { |
| | | qualifiedQty = ObjectUtil.defaultIfNull(feedback.getQualifiedQuantity(), BigDecimal.ZERO); |
| | | unqualifiedQty = ObjectUtil.defaultIfNull(feedback.getUnqualifiedQuantity(), BigDecimal.ZERO); |
| | | } |
| | | |
| | | // 2. 更新任务的已生产/合格/不合格数量 |
| | |
| | | if (routeProcess == null) { |
| | | throw exception(PRO_FEEDBACK_ROUTE_PROCESS_INVALID); |
| | | } |
| | | // 4.1 校验数量 |
| | | boolean checkFlag = Boolean.TRUE.equals(routeProcess.getCheckFlag()); |
| | | if (checkFlag) { |
| | | // 需要检验:只需填报工数量,且必须 > 0 |
| | | if (reqVO.getFeedbackQuantity() == null |
| | | || reqVO.getFeedbackQuantity().compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw exception(PRO_FEEDBACK_QUANTITY_MUST_POSITIVE); |
| | | } |
| | | } else { |
| | | // 不需检验:需填合格品 + 不良品数量,合计 > 0 |
| | | BigDecimal qualified = ObjectUtil.defaultIfNull(reqVO.getQualifiedQuantity(), BigDecimal.ZERO); |
| | | BigDecimal unqualified = ObjectUtil.defaultIfNull(reqVO.getUnqualifiedQuantity(), BigDecimal.ZERO); |
| | | if (qualified.add(unqualified).compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw exception(PRO_FEEDBACK_QUALIFIED_UNQUALIFIED_REQUIRED); |
| | | } |
| | | // 4.1 校验数量:无论是否需要检验,报工只需填报工数量,且必须 > 0 |
| | | // 合格品/不良品数量由质检确定(非质检工序在审批时默认全部为合格品) |
| | | if (reqVO.getFeedbackQuantity() == null |
| | | || reqVO.getFeedbackQuantity().compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw exception(PRO_FEEDBACK_QUANTITY_MUST_POSITIVE); |
| | | } |
| | | |
| | | return task; |
| | |
| | | if (routeProcess == null) { |
| | | throw exception(PRO_FEEDBACK_ROUTE_PROCESS_INVALID); |
| | | } |
| | | // 3. 校验数量 |
| | | boolean checkFlag = Boolean.TRUE.equals(routeProcess.getCheckFlag()); |
| | | if (checkFlag) { |
| | | if (reqVO.getFeedbackQuantity() == null |
| | | || reqVO.getFeedbackQuantity().compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw exception(PRO_FEEDBACK_QUANTITY_MUST_POSITIVE); |
| | | } |
| | | } else { |
| | | BigDecimal qualified = ObjectUtil.defaultIfNull(reqVO.getQualifiedQuantity(), BigDecimal.ZERO); |
| | | BigDecimal unqualified = ObjectUtil.defaultIfNull(reqVO.getUnqualifiedQuantity(), BigDecimal.ZERO); |
| | | if (qualified.add(unqualified).compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw exception(PRO_FEEDBACK_QUALIFIED_UNQUALIFIED_REQUIRED); |
| | | } |
| | | // 3. 校验数量:只填报工数量,且必须 > 0(合格品/不良品数量由质检确定) |
| | | if (reqVO.getFeedbackQuantity() == null |
| | | || reqVO.getFeedbackQuantity().compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw exception(PRO_FEEDBACK_QUANTITY_MUST_POSITIVE); |
| | | } |
| | | // 4. 构建报工DO |
| | | MesProFeedbackDO feedback = new MesProFeedbackDO() |
| | |
| | | .setTaskId(reqVO.getTaskId()) |
| | | .setItemId(task.getItemId()) |
| | | .setFeedbackQuantity(reqVO.getFeedbackQuantity()) |
| | | .setQualifiedQuantity(reqVO.getQualifiedQuantity()) |
| | | .setUnqualifiedQuantity(reqVO.getUnqualifiedQuantity()) |
| | | .setScheduledQuantity(task.getQuantity()) |
| | | .setFeedbackTime(LocalDateTime.now()) |
| | | .setStatus(MesProFeedbackStatusEnum.APPROVING.getStatus()) |
| | | .setRemark(reqVO.getRemark()); |
| | | feedbackMapper.insert(feedback); |
| | | saveParamValues(task.getId(), reqVO.getParamValues()); |
| | | // 5. 返回结果 |
| | | MesProWorkOrderDO workOrder = workOrderService.getWorkOrder(task.getWorkOrderId()); |
| | | // 5.1 报工后剩余可报工数量 = 排产 - 已生产 - 在途(含本次报工) |
| | | BigDecimal reportable = task.getQuantity() == null ? BigDecimal.ZERO : task.getQuantity(); |
| | | BigDecimal produced = task.getProducedQuantity() == null ? BigDecimal.ZERO : task.getProducedQuantity(); |
| | | BigDecimal inTransit = BigDecimal.ZERO; |
| | | for (MesProFeedbackDO fb : getFeedbackListByTaskId(task.getId())) { |
| | | if (!ObjUtil.equal(fb.getStatus(), MesProFeedbackStatusEnum.FINISHED.getStatus())) { |
| | | inTransit = inTransit.add(fb.getFeedbackQuantity() == null ? BigDecimal.ZERO : fb.getFeedbackQuantity()); |
| | | } |
| | | } |
| | | BigDecimal reportableQuantity = reportable.subtract(produced).subtract(inTransit).max(BigDecimal.ZERO); |
| | | return new MesProFeedbackQuickCreateRespVO() |
| | | .setFeedbackId(feedback.getId()) |
| | | .setTaskStatus(task.getStatus()) |
| | | .setWorkOrderStatus(workOrder != null ? workOrder.getStatus() : null); |
| | | .setWorkOrderStatus(workOrder != null ? workOrder.getStatus() : null) |
| | | .setReportableQuantity(reportableQuantity); |
| | | } |
| | | |
| | | @Override |
| | |
| | | MesProFeedbackQuickCreateReqVO singleReq = new MesProFeedbackQuickCreateReqVO(); |
| | | singleReq.setTaskId(taskId); |
| | | singleReq.setFeedbackQuantity(reqVO.getFeedbackQuantity()); |
| | | singleReq.setQualifiedQuantity(reqVO.getQualifiedQuantity()); |
| | | singleReq.setUnqualifiedQuantity(reqVO.getUnqualifiedQuantity()); |
| | | singleReq.setRemark(reqVO.getRemark()); |
| | | MesProFeedbackQuickCreateRespVO resp = quickCreateFeedback(singleReq); |
| | | results.add(new MesProFeedbackBatchCreateRespVO.BatchResult() |