| | |
| | | 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.pro.MesProFeedbackStatusEnum; |
| | | 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.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.mes.service.wm.productproduce.MesWmProductProduceLineService; |
| | | 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 static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | 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 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | @Slf4j |
| | | public class MesQcIpqcServiceImpl implements MesQcIpqcService { |
| | | |
| | | @Resource |
| | | private MesMdWorkstationMapper workstationMapper; |
| | | |
| | | /** |
| | | * IPQC 保存数据校验上下文 |
| | |
| | | @Resource |
| | | @Lazy |
| | | private MesWmProductProduceLineService productProduceLineService; |
| | | |
| | | @Resource |
| | | @Lazy |
| | | private MesQcNcrService ncrService; |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | |
| | | ipqcMapper.updateById(new MesQcIpqcDO() |
| | | .setId(id).setStatus(MesQcStatusEnum.FINISHED.getStatus())); |
| | | |
| | | // 3. 回写来源单据 |
| | | // 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; |
| | | } |
| | | String ncrCode = "NCR" + java.time.LocalDateTime.now() |
| | | .format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")); |
| | | MesQcNcrSaveReqVO saveReqVO = new MesQcNcrSaveReqVO(); |
| | | saveReqVO.setCode(ncrCode); |
| | | 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()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param ipqc 过程检验单 |
| | | */ |
| | | private void writeBackSourceDoc(MesQcIpqcDO ipqc) { |
| | | if (ipqc.getSourceDocType() == null) { |
| | | return; |
| | | } |
| | | if (ipqc.getSourceDocId() == null) { |
| | | throw new IllegalArgumentException( |
| | | "IPQC 单[" + ipqc.getId() + "] sourceDocType 非空但 sourceDocId 为空"); |
| | | // 1. 确定 sourceDocType/sourceDocId |
| | | Long sourceDocId = ipqc.getSourceDocId(); |
| | | Long sourceLineId = ipqc.getSourceLineId(); |
| | | |
| | | // 如果 IPQC 单没有设置来源,尝试通过 taskId 查找关联的待检验报工 |
| | | if (sourceDocId == null && ipqc.getTaskId() != null) { |
| | | MesProFeedbackDO feedback = feedbackService.getFeedbackListByTaskId(ipqc.getTaskId()).stream() |
| | | .filter(f -> MesProFeedbackStatusEnum.UNCHECK.getStatus().equals(f.getStatus())) |
| | | .findFirst().orElse(null); |
| | | if (feedback != null) { |
| | | sourceDocId = feedback.getId(); |
| | | log.info("[writeBackSourceDoc] IPQC({}) 未设置来源单据,通过 taskId({}) 找到待检验报工({})", |
| | | ipqc.getId(), ipqc.getTaskId(), feedback.getId()); |
| | | } |
| | | } |
| | | |
| | | if (Objects.equals(ipqc.getSourceDocType(), MesBizTypeConstants.PRO_FEEDBACK)) { |
| | | feedbackService.updateProFeedbackWhenIpqcFinish(ipqc.getSourceDocId(), ipqc.getSourceLineId(), |
| | | 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)); |
| | | } else { |
| | | throw new IllegalArgumentException( |
| | | "IPQC 单[" + ipqc.getId() + "] sourceDocType=" + ipqc.getSourceDocType() + " 无法识别,无法回写来源单据"); |
| | | if (sourceDocId == null) { |
| | | log.warn("[writeBackSourceDoc] IPQC({}) 无法确定来源报工,跳过回写", ipqc.getId()); |
| | | return; |
| | | } |
| | | |
| | | // 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 |
| | |
| | | ipqcMapper.updateById(updateIpqc); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public MesQcIpqcImportRespVO importIpqcList(List<MesQcIpqcImportExcelVO> 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()); |
| | | } |
| | | |
| | | } |