package cn.iocoder.yudao.module.mes.service.qc.ipqc; import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productproduce.MesWmProductProduceLineDO; import cn.iocoder.yudao.module.mes.enums.wm.MesWmQualityStatusEnum; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.mes.controller.admin.qc.ipqc.vo.MesQcIpqcPageReqVO; import cn.iocoder.yudao.module.mes.controller.admin.qc.ipqc.vo.MesQcIpqcSaveReqVO; import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO; import cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkstationDO; import cn.iocoder.yudao.module.mes.dal.dataobject.pro.route.MesProRouteProductDO; import cn.iocoder.yudao.module.mes.dal.dataobject.pro.task.MesProTaskDO; import cn.iocoder.yudao.module.mes.dal.dataobject.pro.workorder.MesProWorkOrderDO; import cn.iocoder.yudao.module.mes.dal.dataobject.pro.feedback.MesProFeedbackDO; import cn.iocoder.yudao.module.mes.dal.dataobject.qc.defectrecord.MesQcDefectRecordDO; import cn.iocoder.yudao.module.mes.dal.dataobject.qc.ipqc.MesQcIpqcDO; import cn.iocoder.yudao.module.mes.dal.dataobject.qc.template.MesQcTemplateItemDO; import cn.iocoder.yudao.module.mes.dal.mysql.qc.ipqc.MesQcIpqcMapper; import cn.iocoder.yudao.module.mes.dal.mysql.pro.feedback.MesProFeedbackMapper; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.module.mes.dal.mysql.wm.productproduce.MesWmProductProduceLineMapper; import cn.iocoder.yudao.module.mes.enums.qc.MesQcDefectLevelEnum; import cn.iocoder.yudao.module.mes.enums.qc.MesQcStatusEnum; import cn.iocoder.yudao.module.mes.enums.qc.MesQcTypeEnum; import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService; import cn.iocoder.yudao.module.mes.service.md.workstation.MesMdWorkstationService; import cn.iocoder.yudao.module.mes.service.pro.feedback.MesProFeedbackService; import cn.iocoder.yudao.module.mes.service.pro.route.MesProRouteProcessService; import cn.iocoder.yudao.module.mes.service.pro.route.MesProRouteProductService; import cn.iocoder.yudao.module.mes.service.pro.task.MesProTaskService; import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderService; import cn.iocoder.yudao.module.mes.service.qc.defectrecord.MesQcDefectRecordService; import cn.iocoder.yudao.module.mes.service.qc.indicatorresult.MesQcIndicatorResultService; import cn.iocoder.yudao.module.mes.service.qc.template.MesQcTemplateItemService; import cn.iocoder.yudao.module.mes.controller.admin.qc.ncr.vo.MesQcNcrSaveReqVO; import cn.iocoder.yudao.module.mes.service.qc.ncr.MesQcNcrService; import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.List; import java.util.Objects; 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.*; import cn.iocoder.yudao.module.mes.controller.admin.qc.ipqc.vo.MesQcIpqcImportExcelVO; import cn.iocoder.yudao.module.mes.controller.admin.qc.ipqc.vo.MesQcIpqcImportRespVO; import cn.iocoder.yudao.module.mes.dal.mysql.md.workstation.MesMdWorkstationMapper; import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.core.date.DateUtil; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.concurrent.atomic.AtomicInteger; /** * MES 过程检验单(IPQC) Service 实现类 * * @author 超级管理员 */ @Service @Validated @Slf4j public class MesQcIpqcServiceImpl implements MesQcIpqcService { @Resource private MesMdWorkstationMapper workstationMapper; /** * IPQC 保存数据校验上下文 * * @param workstation 工位 * @param task 生产任务(可选,指定 taskId 时非空) */ private record SaveDataContext(MesMdWorkstationDO workstation, MesProTaskDO task) {} @Resource private MesQcIpqcMapper ipqcMapper; @Resource private MesWmProductProduceLineMapper produceLineMapper; @Resource private MesProFeedbackMapper feedbackMapper; @Resource private MesQcTemplateItemService templateItemService; @Resource private MesQcIpqcLineService ipqcLineService; @Resource @Lazy private MesProWorkOrderService workOrderService; @Resource @Lazy private MesMdWorkstationService workstationService; @Resource @Lazy private MesMdItemService itemService; @Resource @Lazy private MesQcDefectRecordService defectRecordService; @Resource @Lazy private MesProRouteProductService routeProductService; @Resource @Lazy private MesProRouteProcessService routeProcessService; @Resource @Lazy private MesProTaskService taskService; @Resource @Lazy private MesProFeedbackService feedbackService; @Resource @Lazy private MesQcIndicatorResultService indicatorResultService; @Resource @Lazy private MesQcNcrService ncrService; @Resource private AdminUserApi adminUserApi; @Override @Transactional(rollbackFor = Exception.class) public Long createIpqc(MesQcIpqcSaveReqVO createReqVO) { // 1.1 校验数据 SaveDataContext ctx = validateIpqcSaveData(createReqVO); // 1.2 校验工单存在 MesProWorkOrderDO workOrder = workOrderService.validateWorkOrderExists(createReqVO.getWorkOrderId()); // 1.3 获取来源单据编号,并确定本次检验的物料与工序 // 报工来源:一个报工可能产出多个物料,每个产出行各自检验,物料取产出行、工序取报工工序 // 其它来源:有任务时取任务的物料,无任务时取工单的产品 SourceDocInfo sourceDoc = validateAndGetSourceDoc( createReqVO.getSourceDocType(), createReqVO.getSourceDocId(), createReqVO.getSourceLineId()); Long itemId; Long processId; if (Objects.equals(createReqVO.getSourceDocType(), MesBizTypeConstants.PRO_FEEDBACK)) { MesProFeedbackDO feedback = feedbackService.validateFeedbackExists(createReqVO.getSourceDocId()); if (!Objects.equals(feedback.getWorkOrderId(), createReqVO.getWorkOrderId()) || !Objects.equals(feedback.getTaskId(), createReqVO.getTaskId())) { throw exception(QC_IPQC_SOURCE_LINE_NOT_BELONG); } if (!ipqcMapper.selectBySourceLineForUpdate(MesBizTypeConstants.PRO_FEEDBACK, createReqVO.getSourceLineId()).isEmpty()) { throw exception(QC_IPQC_SOURCE_ALREADY_LINKED); } if (sourceDoc.line() == null || sourceDoc.line().getItemId() == null) { throw exception(QC_IPQC_SOURCE_LINE_NOT_BELONG); } itemId = sourceDoc.line().getItemId(); processId = feedback.getProcessId(); } else { itemId = (ctx.task() != null) ? ctx.task().getItemId() : workOrder.getProductId(); processId = getProcessId(itemId, ctx.workstation().getProcessId()); } // 1.4 根据物料 + 检验类型自动匹配模板(缺失时给出物料维度的明确提示) MesQcTemplateItemDO templateItem; try { templateItem = templateItemService.getRequiredTemplateByItemIdAndType( itemId, MesQcTypeEnum.IPQC.getType()); } catch (ServiceException e) { if (!Objects.equals(e.getCode(), QC_NO_TEMPLATE.getCode())) { throw e; } throw exception(QC_IPQC_NO_TEMPLATE_FOR_ITEM, getItemInfo(itemId)); } Long templateId = templateItem.getTemplateId(); String sourceDocCode = sourceDoc.code(); // 2. 插入主表 MesQcIpqcDO ipqc = BeanUtils.toBean(createReqVO, MesQcIpqcDO.class) .setItemId(itemId) .setProcessId(processId) .setTemplateId(templateId) .setSourceDocCode(sourceDocCode) .setStatus(MesQcStatusEnum.DRAFT.getStatus()); // 工废/料废/其他废品、检测人员、检测日期未填写时使用默认值,不影响流程 if (ipqc.getLaborScrapQuantity() == null) { ipqc.setLaborScrapQuantity(BigDecimal.ZERO); } if (ipqc.getMaterialScrapQuantity() == null) { ipqc.setMaterialScrapQuantity(BigDecimal.ZERO); } if (ipqc.getOtherScrapQuantity() == null) { ipqc.setOtherScrapQuantity(BigDecimal.ZERO); } if (ipqc.getInspectorUserId() == null) { ipqc.setInspectorUserId(getLoginUserId()); } if (ipqc.getInspectDate() == null) { ipqc.setInspectDate(LocalDateTime.now()); } ipqcMapper.insert(ipqc); // 3. 从模板指标自动生成检验行 ipqcLineService.createLinesFromTemplate(ipqc.getId(), templateId); return ipqc.getId(); } @Override @Transactional(rollbackFor = Exception.class) public void updateIpqc(MesQcIpqcSaveReqVO updateReqVO) { // 1.1 校验存在 + 草稿状态 validateIpqcStatusPrepareForUpdate(updateReqVO.getId()); // 1.2 校验数据 SaveDataContext ctx = validateIpqcSaveData(updateReqVO); // 2. 更新主表 MesQcIpqcDO existIpqc = validateIpqcExists(updateReqVO.getId()); if (Objects.equals(existIpqc.getSourceDocType(), MesBizTypeConstants.PRO_FEEDBACK) && (!Objects.equals(existIpqc.getTaskId(), updateReqVO.getTaskId()) || !Objects.equals(existIpqc.getWorkOrderId(), updateReqVO.getWorkOrderId()) || !Objects.equals(existIpqc.getWorkstationId(), updateReqVO.getWorkstationId()))) { throw exception(QC_IPQC_SOURCE_LINE_NOT_BELONG); } MesQcIpqcDO updateObj = BeanUtils.toBean(updateReqVO, MesQcIpqcDO.class); updateObj.setSourceDocType(null).setSourceDocId(null).setSourceLineId(null); // 不允许修改来源单据 updateObj.setTemplateId(null); // 不允许修改模板 updateObj.setItemId(null); // 不允许修改物料 updateObj.setWorkOrderId(null); // 不允许修改工单 // 报工来源的工序已在创建时按报工工序确定,更新时保持不变,避免被路线推导成空值 if (Objects.equals(existIpqc.getSourceDocType(), MesBizTypeConstants.PRO_FEEDBACK)) { updateObj.setProcessId(existIpqc.getProcessId()); } else { updateObj.setProcessId(getProcessId(existIpqc.getItemId(), ctx.workstation().getProcessId())); } ipqcMapper.updateById(updateObj); } private SaveDataContext validateIpqcSaveData(MesQcIpqcSaveReqVO reqVO) { // 校验编号唯一 validateIpqcCodeUnique(reqVO.getId(), reqVO.getCode()); // 校验工位、检测人员存在 MesMdWorkstationDO workstation = workstationService.validateWorkstationExists(reqVO.getWorkstationId()); // 校验检测人员存在(非必填,未填写时由创建逻辑默认当前登录人) if (reqVO.getInspectorUserId() != null) { adminUserApi.validateUser(reqVO.getInspectorUserId()); } // 校验工单存在 MesProWorkOrderDO workOrder = workOrderService.validateWorkOrderConfirmed(reqVO.getWorkOrderId()); if (reqVO.getProcessId() != null && ObjUtil.notEqual(workstation.getProcessId(), reqVO.getProcessId())) { throw exception(PRO_WORKSTATION_PROCESS_MISMATCH); } // 校验任务关系(如果指定了任务) MesProTaskDO task = null; if (reqVO.getTaskId() != null) { task = taskService.validateTaskNotFinished(reqVO.getTaskId()); validateTaskRelation(task, workstation, workOrder, reqVO); } else { // 无任务时,itemId 仍需与工单产品一致(报工来源的联产出物料以产出明细行为准,不在此校验) if (!Objects.equals(reqVO.getSourceDocType(), MesBizTypeConstants.PRO_FEEDBACK) && reqVO.getItemId() != null && ObjUtil.notEqual(reqVO.getItemId(), workOrder.getProductId())) { throw exception(PRO_WORK_ORDER_PRODUCT_MISMATCH); } } return new SaveDataContext(workstation, task); } private void validateTaskRelation(MesProTaskDO task, MesMdWorkstationDO workstation, MesProWorkOrderDO workOrder, MesQcIpqcSaveReqVO reqVO) { if (ObjUtil.notEqual(task.getWorkOrderId(), workOrder.getId())) { throw exception(PRO_TASK_WORK_ORDER_MISMATCH); } if (ObjUtil.notEqual(task.getWorkstationId(), workstation.getId())) { throw exception(PRO_TASK_WORKSTATION_MISMATCH); } Long expectedProcessId = reqVO.getProcessId() != null ? reqVO.getProcessId() : workstation.getProcessId(); if (expectedProcessId != null && ObjUtil.notEqual(task.getProcessId(), expectedProcessId)) { throw exception(PRO_TASK_ROUTE_PROCESS_MISMATCH); } // 校验检验物料与任务物料一致(有任务时,IPQC 检验的物料应与任务加工的物料一致) // 报工来源允许检验联产出物料,物料以产出明细行为准,此处不参与校验 if (!Objects.equals(reqVO.getSourceDocType(), MesBizTypeConstants.PRO_FEEDBACK) && reqVO.getItemId() != null && ObjUtil.notEqual(reqVO.getItemId(), task.getItemId())) { throw exception(PRO_TASK_ITEM_MISMATCH); } } @Override @Transactional(rollbackFor = Exception.class) public void finishIpqc(Long id) { // 1.1 校验存在 + 草稿状态 MesQcIpqcDO ipqc = validateIpqcStatusPrepareForUpdate(id); // 1.2 校验检测结论必填 if (ipqc.getCheckResult() == null) { throw exception(QC_IPQC_CHECK_RESULT_EMPTY); } // 1.3 校验至少存在一条检测结果 indicatorResultService.validateIndicatorResultExistsByQcIdAndType(id, MesQcTypeEnum.IPQC.getType()); BigDecimal check = ipqc.getCheckQuantity(); BigDecimal qualified = ipqc.getQualifiedQuantity(); BigDecimal unqualified = ipqc.getUnqualifiedQuantity(); if (check == null || qualified == null || unqualified == null || check.signum() <= 0 || qualified.signum() < 0 || unqualified.signum() < 0 || check.compareTo(qualified.add(unqualified)) != 0) { throw exception(QC_IPQC_QUANTITY_INVALID); } BigDecimal labor = ObjectUtil.defaultIfNull(ipqc.getLaborScrapQuantity(), BigDecimal.ZERO); BigDecimal material = ObjectUtil.defaultIfNull(ipqc.getMaterialScrapQuantity(), BigDecimal.ZERO); BigDecimal other = ObjectUtil.defaultIfNull(ipqc.getOtherScrapQuantity(), BigDecimal.ZERO); if (labor.signum() < 0 || material.signum() < 0 || other.signum() < 0 || labor.add(material).add(other).compareTo(unqualified) > 0) { throw exception(QC_IPQC_SCRAP_QUANTITY_INVALID); } if (Objects.equals(ipqc.getSourceDocType(), MesBizTypeConstants.PRO_FEEDBACK)) { if (ipqc.getSourceDocId() == null || ipqc.getSourceLineId() == null) throw exception(QC_IPQC_SOURCE_INCOMPLETE); MesWmProductProduceLineDO line = produceLineMapper.selectByIdForUpdate(ipqc.getSourceLineId()); if (line == null || !Objects.equals(line.getFeedbackId(), ipqc.getSourceDocId())) throw exception(QC_IPQC_SOURCE_LINE_NOT_BELONG); if (!Objects.equals(line.getQualityStatus(), MesWmQualityStatusEnum.PENDING.getStatus())) throw exception(QC_IPQC_SOURCE_LINE_NOT_PENDING); if (line.getQuantity() == null || check.compareTo(line.getQuantity()) != 0) throw exception(QC_IPQC_SOURCE_QUANTITY_INVALID); } // 2. 更新状态为已完成 ipqcMapper.updateById(new MesQcIpqcDO() .setId(id).setStatus(MesQcStatusEnum.FINISHED.getStatus())); // 3. 不合格时自动创建 NCR generateNcrIfNeeded(ipqc); // 4. 回写来源单据 writeBackSourceDoc(ipqc); } /** * 不合格时自动创建 NCR(不合格品处理单) * * @param ipqc 过程检验单 */ private void generateNcrIfNeeded(MesQcIpqcDO ipqc) { BigDecimal unqualifiedQty = ipqc.getUnqualifiedQuantity(); if (unqualifiedQty == null || unqualifiedQty.compareTo(BigDecimal.ZERO) <= 0) { return; } MesQcNcrSaveReqVO saveReqVO = new MesQcNcrSaveReqVO(); saveReqVO.setName("不合格品处理单"); saveReqVO.setQcType(MesQcTypeEnum.IPQC.getType()); saveReqVO.setQcId(ipqc.getId()); saveReqVO.setItemId(ipqc.getItemId()); saveReqVO.setUnqualifiedQuantity(unqualifiedQty); saveReqVO.setDefectDescription("由过程检验(" + ipqc.getCode() + ")自动生成"); ncrService.createNcr(saveReqVO); log.info("[generateNcrIfNeeded] IPQC({}) 不合格,自动创建 NCR", ipqc.getId()); } /** * 回写来源单据(IPQC 完成后) * *

当来源为报工时,拆分待检产出行、完成产出入库、回写报工状态和任务/工单进度 * * @param ipqc 过程检验单 */ private void writeBackSourceDoc(MesQcIpqcDO ipqc) { // 1. 确定 sourceDocType/sourceDocId Long sourceDocId = ipqc.getSourceDocId(); Long sourceLineId = ipqc.getSourceLineId(); if (ipqc.getSourceDocType() == null && sourceDocId == null && sourceLineId == null) { return; } if (!Objects.equals(ipqc.getSourceDocType(), MesBizTypeConstants.PRO_FEEDBACK) || sourceDocId == null || sourceLineId == null) throw exception(QC_IPQC_SOURCE_INCOMPLETE); // 2. 回写报工 feedbackService.updateProFeedbackWhenIpqcFinish(sourceDocId, sourceLineId, ObjectUtil.defaultIfNull(ipqc.getQualifiedQuantity(), BigDecimal.ZERO), ObjectUtil.defaultIfNull(ipqc.getUnqualifiedQuantity(), BigDecimal.ZERO), ObjectUtil.defaultIfNull(ipqc.getLaborScrapQuantity(), BigDecimal.ZERO), ObjectUtil.defaultIfNull(ipqc.getMaterialScrapQuantity(), BigDecimal.ZERO), ObjectUtil.defaultIfNull(ipqc.getOtherScrapQuantity(), BigDecimal.ZERO)); } @Override @Transactional(rollbackFor = Exception.class) public void deleteIpqc(Long id) { // 1. 校验存在 + 草稿状态 validateIpqcStatusPrepareForUpdate(id); // 2.1 删除主表 ipqcMapper.deleteById(id); // 2.2 级联删除行 ipqcLineService.deleteListByIpqcId(id); // 2.3 级联删除缺陷记录 defectRecordService.deleteListByQcTypeAndQcId(MesQcTypeEnum.IPQC.getType(), id); } @Override public MesQcIpqcDO validateIpqcExists(Long id) { MesQcIpqcDO ipqc = ipqcMapper.selectById(id); if (ipqc == null) { throw exception(QC_IPQC_NOT_EXISTS); } return ipqc; } private MesQcIpqcDO validateIpqcStatusPrepareForUpdate(Long id) { // 与报工回调一致:报工 → 产出行 → 检验单,避免相互等待。 MesQcIpqcDO snapshot = validateIpqcExists(id); if (Objects.equals(snapshot.getSourceDocType(), MesBizTypeConstants.PRO_FEEDBACK) && snapshot.getSourceDocId() != null && snapshot.getSourceLineId() != null) { lockFeedback(snapshot.getSourceDocId()); produceLineMapper.selectByIdForUpdate(snapshot.getSourceLineId()); } MesQcIpqcDO ipqc = ipqcMapper.selectByIdForUpdate(id); if (ipqc == null) throw exception(QC_IPQC_NOT_EXISTS); if (ObjUtil.notEqual(ipqc.getStatus(), MesQcStatusEnum.DRAFT.getStatus())) throw exception(QC_IPQC_NOT_PREPARE); return ipqc; } private MesProFeedbackDO lockFeedback(Long id) { MesProFeedbackDO feedback = feedbackMapper.selectOne(new LambdaQueryWrapperX() .eq(MesProFeedbackDO::getId, id).last("FOR UPDATE")); if (feedback == null) throw exception(PRO_FEEDBACK_NOT_EXISTS); return feedback; } private void validateIpqcCodeUnique(Long id, String code) { MesQcIpqcDO ipqc = ipqcMapper.selectByCode(code); if (ipqc == null) { return; } if (ObjUtil.notEqual(ipqc.getId(), id)) { throw exception(QC_IPQC_CODE_DUPLICATE); } } /** * 来源单据校验上下文 * * @param code 来源单据编号 * @param line 报工来源时对应的产出明细行,其它来源为 null */ private record SourceDocInfo(String code, MesWmProductProduceLineDO line) {} private SourceDocInfo validateAndGetSourceDoc(Integer sourceDocType, Long sourceDocId, Long sourceLineId) { if (sourceDocType == null && sourceDocId == null && sourceLineId == null) { return new SourceDocInfo(null, null); } if (sourceDocType == null || sourceDocId == null || sourceLineId == null) throw exception(QC_IPQC_SOURCE_INCOMPLETE); if (Objects.equals(sourceDocType, MesBizTypeConstants.PRO_FEEDBACK)) { MesProFeedbackDO feedback = lockFeedback(sourceDocId); // 校验 sourceLineId 必填 if (sourceLineId == null) { throw exception(QC_IPQC_SOURCE_LINE_REQUIRED); } // 校验 sourceLineId 存在,且属于该报工的产出行 MesWmProductProduceLineDO targetLine = produceLineMapper.selectByIdForUpdate(sourceLineId); if (targetLine == null) throw exception(QC_IPQC_SOURCE_LINE_NOT_BELONG); if (ObjUtil.notEqual(targetLine.getFeedbackId(), sourceDocId)) { throw exception(QC_IPQC_SOURCE_LINE_NOT_BELONG); } // 校验该产出行为待检验状态 if (ObjUtil.notEqual(targetLine.getQualityStatus(), MesWmQualityStatusEnum.PENDING.getStatus())) { throw exception(QC_IPQC_SOURCE_LINE_NOT_PENDING); } return new SourceDocInfo(feedback.getCode(), targetLine); } // 未知来源类型应报错,而不是静默忽略 throw exception(QC_IPQC_SOURCE_DOC_TYPE_UNKNOWN); } /** * 推导工序 ID * *

通过产品 → 工艺路线产品 → 工艺路线 → 校验工位工序是否在该路线中 *

如果工位的工序在路线中,则返回该 processId;否则返回 null * * @param productId 产品 ID(工单产品) * @param workstationProcessId 工位关联的工序 ID * @return 推导出的工序 ID,无法推导时返回 null */ private Long getProcessId(Long productId, Long workstationProcessId) { if (productId == null || workstationProcessId == null) { return null; } // 1. 产品 → 工艺路线 MesProRouteProductDO routeProduct = routeProductService.getRouteProductByItemId(productId); if (routeProduct == null) { return null; } // 2. 校验工位的工序是否在该路线中 if (routeProcessService.getRouteProcessByRouteIdAndProcessId( routeProduct.getRouteId(), workstationProcessId) == null) { return null; } return workstationProcessId; } @Override public MesQcIpqcDO getIpqc(Long id) { return ipqcMapper.selectById(id); } @Override public PageResult getIpqcPage(MesQcIpqcPageReqVO pageReqVO) { return ipqcMapper.selectPage(pageReqVO); } @Override public void recalculateDefectStats(Long ipqcId, List records) { MesQcIpqcDO ipqc = validateIpqcExists(ipqcId); // 1. 行级缺陷统计 ipqcLineService.recalculateLineDefectStats(ipqcId, records); // 2.1 汇总主表的缺陷数量 int totalCritical = 0; int totalMajor = 0; int totalMinor = 0; for (MesQcDefectRecordDO record : records) { int quantity = ObjUtil.defaultIfNull(record.getQuantity(), 1); if (Objects.equals(record.getLevel(), MesQcDefectLevelEnum.CRITICAL.getType())) { totalCritical += quantity; } else if (Objects.equals(record.getLevel(), MesQcDefectLevelEnum.MAJOR.getType())) { totalMajor += quantity; } else if (Objects.equals(record.getLevel(), MesQcDefectLevelEnum.MINOR.getType())) { totalMinor += quantity; } else { throw exception(QC_DEFECT_RECORD_LEVEL_UNKNOWN); } } // 2.2 计算缺陷率 BigDecimal criticalRate = BigDecimal.ZERO; BigDecimal majorRate = BigDecimal.ZERO; BigDecimal minorRate = BigDecimal.ZERO; if (ipqc.getCheckQuantity() != null && ipqc.getCheckQuantity().compareTo(BigDecimal.ZERO) > 0) { BigDecimal checkQty = ipqc.getCheckQuantity(); criticalRate = BigDecimal.valueOf(totalCritical).multiply(BigDecimal.valueOf(100)) .divide(checkQty, 2, RoundingMode.HALF_UP); majorRate = BigDecimal.valueOf(totalMajor).multiply(BigDecimal.valueOf(100)) .divide(checkQty, 2, RoundingMode.HALF_UP); minorRate = BigDecimal.valueOf(totalMinor).multiply(BigDecimal.valueOf(100)) .divide(checkQty, 2, RoundingMode.HALF_UP); } // 2.3 更新主表 MesQcIpqcDO updateIpqc = new MesQcIpqcDO().setId(ipqcId) .setCriticalQuantity(totalCritical).setMajorQuantity(totalMajor).setMinorQuantity(totalMinor) .setCriticalRate(criticalRate).setMajorRate(majorRate).setMinorRate(minorRate); ipqcMapper.updateById(updateIpqc); } @Override public MesQcIpqcImportRespVO importIpqcList(List importList, boolean updateSupport) { if (CollUtil.isEmpty(importList)) { throw exception(QC_IPQC_IMPORT_LIST_IS_EMPTY); } MesQcIpqcImportRespVO respVO = MesQcIpqcImportRespVO.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 = "第 " + currentIndex + " 行"; if (StrUtil.isBlank(importItem.getWorkOrderCode())) { respVO.getFailureCodes().put(key, "生产工单编码不能为空"); return; } if (StrUtil.isBlank(importItem.getWorkstationCode())) { respVO.getFailureCodes().put(key, "工位编码不能为空"); return; } MesProWorkOrderDO workOrder = workOrderService.getWorkOrder(importItem.getWorkOrderCode()); if (workOrder == null) { respVO.getFailureCodes().put(key, "生产工单不存在:" + importItem.getWorkOrderCode()); return; } MesMdWorkstationDO workstation = workstationMapper.selectByCode(importItem.getWorkstationCode()); if (workstation == null) { respVO.getFailureCodes().put(key, "工位不存在:" + importItem.getWorkstationCode()); return; } if (importItem.getCheckQuantity() == null || importItem.getQualifiedQuantity() == null || importItem.getUnqualifiedQuantity() == null) { respVO.getFailureCodes().put(key, "检测数量、合格品数量、不合格品数量均不能为空"); return; } if (importItem.getCheckQuantity().compareTo(importItem.getQualifiedQuantity().add(importItem.getUnqualifiedQuantity())) != 0) { respVO.getFailureCodes().put(key, "检测数量必须等于合格品数量与不合格品数量之和"); return; } BigDecimal scrapSum = ObjUtil.defaultIfNull(importItem.getLaborScrapQuantity(), BigDecimal.ZERO) .add(ObjUtil.defaultIfNull(importItem.getMaterialScrapQuantity(), BigDecimal.ZERO)) .add(ObjUtil.defaultIfNull(importItem.getOtherScrapQuantity(), BigDecimal.ZERO)); if (scrapSum.compareTo(importItem.getUnqualifiedQuantity()) > 0) { respVO.getFailureCodes().put(key, "工废、料废、其他废品数量之和不能超过不合格品数量"); return; } try { MesQcIpqcSaveReqVO createReqVO = BeanUtils.toBean(importItem, MesQcIpqcSaveReqVO.class); createReqVO.setCode(generateImportCode("IPQC", index)); createReqVO.setName(StrUtil.blankToDefault(importItem.getName(), "过程检验单")); createReqVO.setWorkOrderId(workOrder.getId()); createReqVO.setWorkstationId(workstation.getId()); createReqVO.setType(ObjUtil.defaultIfNull(importItem.getType(), 1)); createReqVO.setLaborScrapQuantity(ObjUtil.defaultIfNull(importItem.getLaborScrapQuantity(), BigDecimal.ZERO)); createReqVO.setMaterialScrapQuantity(ObjUtil.defaultIfNull(importItem.getMaterialScrapQuantity(), BigDecimal.ZERO)); createReqVO.setOtherScrapQuantity(ObjUtil.defaultIfNull(importItem.getOtherScrapQuantity(), BigDecimal.ZERO)); Long id = createIpqc(createReqVO); MesQcIpqcDO created = ipqcMapper.selectById(id); respVO.getCreateCodes().add(created != null && StrUtil.isNotBlank(created.getCode()) ? created.getCode() : key); } catch (ServiceException e) { respVO.getFailureCodes().put(key, e.getMessage()); } }); return respVO; } private String generateImportCode(String prefix, AtomicInteger seq) { return prefix + DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmss") + String.format("%03d", seq.incrementAndGet()); } private String getItemInfo(Long itemId) { if (itemId == null) { return "未知物料"; } MesMdItemDO item = itemService.getItem(itemId); if (item != null) { return item.getCode() + "-" + item.getName(); } return "物料ID:" + itemId; } }