| | |
| | | import cn.iocoder.yudao.module.mes.service.qc.template.MesQcTemplateItemService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.arrivalnotice.MesWmArrivalNoticeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.outsourcereceipt.MesWmOutsourceReceiptService; |
| | | import cn.iocoder.yudao.module.erp.api.purchase.ErpSupplierApi; |
| | | 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.enums.qc.MesQcCheckResultEnum; |
| | | import cn.iocoder.yudao.module.srm.api.supplier.SrmSupplierApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | |
| | | import java.util.*; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; |
| | | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | |
| | | 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.iqc.vo.MesQcIqcImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.qc.iqc.vo.MesQcIqcImportRespVO; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | 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 来料检验单(IQC) Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | |
| | | @Resource |
| | | private MesWmOutsourceReceiptService outsourceReceiptService; |
| | | @Resource |
| | | private ErpSupplierApi supplierApi; |
| | | private SrmSupplierApi srmSupplierApi; |
| | | @Resource |
| | | private MesMdItemService itemService; |
| | | @Resource |
| | |
| | | |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | | |
| | | @Resource |
| | | @Lazy |
| | | private MesQcNcrService ncrService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | .setCheckQuantity(createReqVO.getQualifiedQuantity().add(createReqVO.getUnqualifiedQuantity())) |
| | | .setSourceDocCode(sourceDocCode) |
| | | .setStatus(MesQcStatusEnum.DRAFT.getStatus()); |
| | | // 检测人员、检测日期、来料日期未填写时使用默认值,不影响流程 |
| | | if (iqc.getInspectorUserId() == null) { |
| | | iqc.setInspectorUserId(getLoginUserId()); |
| | | } |
| | | if (iqc.getInspectDate() == null) { |
| | | iqc.setInspectDate(LocalDateTime.now()); |
| | | } |
| | | if (iqc.getReceiveDate() == null) { |
| | | iqc.setReceiveDate(LocalDateTime.now()); |
| | | } |
| | | iqcMapper.insert(iqc); |
| | | |
| | | // 3. 从模板指标自动生成检验行 |
| | |
| | | validateIqcCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | // 校验供应商存在 |
| | | if (reqVO.getVendorId() != null) { |
| | | supplierApi.validateSupplier(reqVO.getVendorId()); |
| | | srmSupplierApi.getSupplier(reqVO.getVendorId()); |
| | | } |
| | | // 校验物料存在(使用 MDM 物料) |
| | | if (reqVO.getItemId() != null) { |
| | | mdmItemApi.getItem(reqVO.getItemId()).getCheckedData(); |
| | | } |
| | | // 校验检测人员存在 |
| | | adminUserApi.validateUser(reqVO.getInspectorUserId()); |
| | | // 校验检测人员存在(非必填,未填写时由创建逻辑默认当前登录人) |
| | | if (reqVO.getInspectorUserId() != null) { |
| | | adminUserApi.validateUser(reqVO.getInspectorUserId()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | .setId(id).setStatus(MesQcStatusEnum.FINISHED.getStatus()); |
| | | iqcMapper.updateById(updateObj); |
| | | |
| | | // 3. 回写来源单据 |
| | | // 3. 不合格时自动创建 NCR |
| | | generateNcrIfNeeded(iqc); |
| | | |
| | | // 4. 回写来源单据 |
| | | writeBackSourceDoc(iqc); |
| | | } |
| | | |
| | | /** |
| | | * 不合格时自动创建 NCR(不合格品处理单) |
| | | * |
| | | * @param iqc 来料检验单 |
| | | */ |
| | | private void generateNcrIfNeeded(MesQcIqcDO iqc) { |
| | | BigDecimal unqualifiedQty = iqc.getUnqualifiedQuantity(); |
| | | if (unqualifiedQty == null || unqualifiedQty.compareTo(BigDecimal.ZERO) <= 0) { |
| | | return; |
| | | } |
| | | MesQcNcrSaveReqVO saveReqVO = new MesQcNcrSaveReqVO(); |
| | | saveReqVO.setName("不合格品处理单"); |
| | | saveReqVO.setQcType(MesQcTypeEnum.IQC.getType()); |
| | | saveReqVO.setQcId(iqc.getId()); |
| | | saveReqVO.setItemId(iqc.getItemId()); |
| | | saveReqVO.setUnqualifiedQuantity(unqualifiedQty); |
| | | saveReqVO.setDefectDescription("由来料检验(" + iqc.getCode() + ")自动生成"); |
| | | ncrService.createNcr(saveReqVO); |
| | | } |
| | | |
| | | /** |
| | |
| | | return; |
| | | } |
| | | if (ObjectUtil.hasNull(iqc.getSourceLineId(), iqc.getSourceDocId())) { |
| | | throw new IllegalArgumentException( |
| | | "IQC 单[" + iqc.getId() + "] sourceDocType 非空但 sourceLineId 或 sourceDocId 为空"); |
| | | throw exception(QC_IQC_SOURCE_DOC_PARAMS_MISSING); |
| | | } |
| | | |
| | | // 根据不同的来源单据类型,回写不同的单据 |
| | |
| | | outsourceReceiptService.updateOutsourceReceiptWhenIqcFinish(iqc.getSourceDocId(), iqc.getSourceLineId(), |
| | | iqc.getId(), iqc.getQualifiedQuantity(), iqc.getUnqualifiedQuantity()); |
| | | } else { |
| | | throw new IllegalArgumentException( |
| | | "IQC 单[" + iqc.getId() + "] sourceDocType=" + iqc.getSourceDocType() + " 无法识别,无法回写来源单据"); |
| | | throw exception(QC_IQC_SOURCE_DOC_TYPE_UNKNOWN, iqc.getSourceDocType()); |
| | | } |
| | | } |
| | | |
| | |
| | | try { |
| | | templateItem = templateItemService.getRequiredTemplateByItemIdAndType( |
| | | line.getItemId(), MesQcTypeEnum.IQC.getType()); |
| | | } catch (Exception e) { |
| | | } catch (cn.iocoder.yudao.framework.common.exception.ServiceException e) { |
| | | if (!java.util.Objects.equals(e.getCode(), QC_NO_TEMPLATE.getCode())) throw e; |
| | | // 获取物料信息,提供更明确的错误提示 |
| | | String itemInfo = getItemInfo(line.getItemId()); |
| | | throw exception(QC_NO_TEMPLATE_FOR_ITEM, itemInfo); |
| | |
| | | Long templateId = templateItem.getTemplateId(); |
| | | |
| | | // 2. 生成检验单编码 |
| | | String code = "IQC" + System.currentTimeMillis(); |
| | | String code = "IQC" + UUID.randomUUID().toString().replace("-", ""); |
| | | |
| | | // 3. 创建 IQC 主表(待检验状态,初始合格数量为 0) |
| | | MesQcIqcDO iqc = new MesQcIqcDO(); |
| | |
| | | return "物料ID:" + itemId; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public MesQcIqcImportRespVO importIqcList(List<MesQcIqcImportExcelVO> importList, boolean updateSupport) { |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(QC_IQC_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | MesQcIqcImportRespVO respVO = MesQcIqcImportRespVO.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.getItemCode())) { |
| | | respVO.getFailureCodes().put(key, "产品物料编码不能为空"); |
| | | return; |
| | | } |
| | | if (importItem.getInspectorUserId() == null) { |
| | | respVO.getFailureCodes().put(key, "检测人员不能为空"); |
| | | return; |
| | | } |
| | | if (importItem.getInspectDate() == null) { |
| | | respVO.getFailureCodes().put(key, "检测日期不能为空"); |
| | | return; |
| | | } |
| | | if (importItem.getReceiveDate() == null) { |
| | | respVO.getFailureCodes().put(key, "来料日期不能为空"); |
| | | return; |
| | | } |
| | | if (importItem.getReceivedQuantity() == null || importItem.getQualifiedQuantity() == null || importItem.getUnqualifiedQuantity() == null) { |
| | | respVO.getFailureCodes().put(key, "本次接收数量、合格品数量、不合格品数量均不能为空"); |
| | | return; |
| | | } |
| | | if (importItem.getQualifiedQuantity().add(importItem.getUnqualifiedQuantity()).compareTo(importItem.getReceivedQuantity()) != 0) { |
| | | respVO.getFailureCodes().put(key, "合格品数量与不合格品数量之和必须等于本次接收数量"); |
| | | return; |
| | | } |
| | | // 来料检验单的物料以 MDM 为准,导入按 MDM 物料编码解析 |
| | | MdmItemRespDTO item = mdmItemApi.getItemByCode(importItem.getItemCode()).getCheckedData(); |
| | | if (item == null) { |
| | | respVO.getFailureCodes().put(key, "物料不存在:" + importItem.getItemCode()); |
| | | return; |
| | | } |
| | | try { |
| | | MesQcIqcSaveReqVO createReqVO = BeanUtils.toBean(importItem, MesQcIqcSaveReqVO.class); |
| | | createReqVO.setCode(generateImportCode("IQC", index)); |
| | | createReqVO.setName(StrUtil.blankToDefault(importItem.getName(), "来料检验单")); |
| | | createReqVO.setItemId(item.getId()); |
| | | Long id = createIqc(createReqVO); |
| | | MesQcIqcDO created = iqcMapper.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()); |
| | | } |
| | | |
| | | } |