| | |
| | | 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.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.service.wm.productsales.MesWmProductSalesLineService; |
| | | 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.mes.service.wm.productsales.MesWmProductSalesService; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesLineDO; |
| | | import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; |
| | | 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.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.oqc.vo.MesQcOqcImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.qc.oqc.vo.MesQcOqcImportRespVO; |
| | | import cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO; |
| | | import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService; |
| | | 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 出货检验单(OQC) Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | public class MesQcOqcServiceImpl implements MesQcOqcService { |
| | | |
| | | @Resource |
| | | private MesMdItemService itemService; |
| | | |
| | | @Resource |
| | | private MesQcOqcMapper oqcMapper; |
| | |
| | | @Resource |
| | | @Lazy |
| | | private MesQcDefectRecordService defectRecordService; |
| | | @Resource |
| | | @Lazy |
| | | private MesMdItemService itemService; |
| | | @Resource |
| | | @Lazy |
| | | private MesWmProductSalesLineService productSalesLineService; |
| | |
| | | private MdmItemApi mdmItemApi; |
| | | @Resource |
| | | private CrmCustomerApi customerApi; |
| | | |
| | | @Resource |
| | | @Lazy |
| | | private MesQcNcrService ncrService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | .setTemplateId(templateId) |
| | | .setSourceDocCode(sourceDocCode) |
| | | .setStatus(MesQcStatusEnum.DRAFT.getStatus()); |
| | | // 检测人员、检测日期、出货日期未填写时使用默认值,不影响流程 |
| | | if (oqc.getInspectorUserId() == null) { |
| | | oqc.setInspectorUserId(getLoginUserId()); |
| | | } |
| | | if (oqc.getInspectDate() == null) { |
| | | oqc.setInspectDate(LocalDateTime.now()); |
| | | } |
| | | if (oqc.getOutDate() == null) { |
| | | oqc.setOutDate(LocalDateTime.now()); |
| | | } |
| | | oqcMapper.insert(oqc); |
| | | |
| | | // 3. 从模板指标自动生成检验行 |
| | |
| | | // 校验编号唯一 |
| | | validateOqcCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | // 校验物料、检测人员存在 |
| | | // 注:客户 ID 来自 CRM,不需要在 MES 中校验 |
| | | itemService.validateItemExists(reqVO.getItemId()); |
| | | adminUserApi.validateUser(reqVO.getInspectorUserId()); |
| | | // 注:物料 ID 来自 MDM,客户 ID 来自 CRM |
| | | mdmItemApi.validateItemExists(reqVO.getItemId()); |
| | | // 校验检测人员存在(非必填,未填写时由创建逻辑默认当前登录人) |
| | | if (reqVO.getInspectorUserId() != null) { |
| | | adminUserApi.validateUser(reqVO.getInspectorUserId()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | .setId(id).setStatus(MesQcStatusEnum.FINISHED.getStatus()); |
| | | oqcMapper.updateById(updateObj); |
| | | |
| | | // 3. 回写来源单据 |
| | | // 3. 不合格时自动创建 NCR |
| | | generateNcrIfNeeded(oqc); |
| | | |
| | | // 4. 回写来源单据 |
| | | writeBackSourceDoc(oqc); |
| | | } |
| | | |
| | | /** |
| | | * 不合格时自动创建 NCR(不合格品处理单) |
| | | * |
| | | * @param oqc 出货检验单 |
| | | */ |
| | | private void generateNcrIfNeeded(MesQcOqcDO oqc) { |
| | | BigDecimal unqualifiedQty = oqc.getUnqualifiedQuantity(); |
| | | if (unqualifiedQty == null || unqualifiedQty.compareTo(BigDecimal.ZERO) <= 0) { |
| | | return; |
| | | } |
| | | MesQcNcrSaveReqVO saveReqVO = new MesQcNcrSaveReqVO(); |
| | | saveReqVO.setName("不合格品处理单"); |
| | | saveReqVO.setQcType(MesQcTypeEnum.OQC.getType()); |
| | | saveReqVO.setQcId(oqc.getId()); |
| | | saveReqVO.setItemId(oqc.getItemId()); |
| | | saveReqVO.setUnqualifiedQuantity(unqualifiedQty); |
| | | saveReqVO.setDefectDescription("由出货检验(" + oqc.getCode() + ")自动生成"); |
| | | ncrService.createNcr(saveReqVO); |
| | | } |
| | | |
| | | /** |
| | |
| | | return; |
| | | } |
| | | if (oqc.getSourceLineId() == null) { |
| | | throw new IllegalArgumentException( |
| | | "OQC 单[" + oqc.getId() + "] sourceDocType 非空但 sourceLineId 为空"); |
| | | throw exception(QC_OQC_SOURCE_LINE_REQUIRED); |
| | | } |
| | | |
| | | if (Objects.equals(oqc.getSourceDocType(), MesBizTypeConstants.WM_PRODUCT_SALES)) { |
| | | productSalesLineService.updateProductSalesLineWhenOqcFinish( |
| | | oqc.getSourceLineId(), oqc.getId(), oqc.getCheckResult()); |
| | | } else { |
| | | throw new IllegalArgumentException( |
| | | "OQC 单[" + oqc.getId() + "] sourceDocType=" + oqc.getSourceDocType() + " 无法识别,无法回写来源单据"); |
| | | throw exception(QC_OQC_SOURCE_DOC_TYPE_UNKNOWN, oqc.getSourceDocType()); |
| | | } |
| | | } |
| | | |
| | |
| | | return null; |
| | | } |
| | | // 未知来源类型应报错,而不是静默忽略 |
| | | throw exception(QC_OQC_SOURCE_DOC_TYPE_UNKNOWN); |
| | | throw exception(QC_OQC_SOURCE_DOC_TYPE_UNKNOWN, sourceDocType); |
| | | } |
| | | |
| | | @Override |
| | |
| | | try { |
| | | templateItem = templateItemService.getRequiredTemplateByItemIdAndType( |
| | | line.getItemId(), MesQcTypeEnum.OQC.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); |
| | | throw exception(QC_OQC_NO_TEMPLATE_FOR_ITEM, itemInfo); |
| | | } |
| | | Long templateId = templateItem.getTemplateId(); |
| | | |
| | | // 2. 生成检验单编码 |
| | | String code = "OQC" + System.currentTimeMillis(); |
| | | String code = "OQC" + java.util.UUID.randomUUID().toString().replace("-", ""); |
| | | |
| | | // 3. 创建 OQC 主表(待检验状态,初始数量为 0) |
| | | MesQcOqcDO oqc = new MesQcOqcDO(); |
| | |
| | | return "物料ID:" + itemId; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public MesQcOqcImportRespVO importOqcList(List<MesQcOqcImportExcelVO> importList, boolean updateSupport) { |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(QC_OQC_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | MesQcOqcImportRespVO respVO = MesQcOqcImportRespVO.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.getClientName())) { |
| | | respVO.getFailureCodes().put(key, "客户名称不能为空"); |
| | | return; |
| | | } |
| | | if (StrUtil.isBlank(importItem.getItemCode())) { |
| | | respVO.getFailureCodes().put(key, "产品物料编码不能为空"); |
| | | return; |
| | | } |
| | | if (importItem.getOutQuantity() == null || 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; |
| | | } |
| | | if (importItem.getInspectorUserId() == null) { |
| | | respVO.getFailureCodes().put(key, "检测人员不能为空"); |
| | | return; |
| | | } |
| | | if (importItem.getOutDate() == null) { |
| | | respVO.getFailureCodes().put(key, "出货日期不能为空"); |
| | | return; |
| | | } |
| | | if (importItem.getInspectDate() == null) { |
| | | respVO.getFailureCodes().put(key, "检测日期不能为空"); |
| | | return; |
| | | } |
| | | cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO customer = customerApi.getCustomerByName(importItem.getClientName()); |
| | | if (customer == null) { |
| | | respVO.getFailureCodes().put(key, "客户不存在:" + importItem.getClientName()); |
| | | return; |
| | | } |
| | | // 出货检验单的物料以 MDM 为准,导入按 MDM 物料编码解析 |
| | | MdmItemRespDTO item = mdmItemApi.getItemByCode(importItem.getItemCode()).getCheckedData(); |
| | | if (item == null) { |
| | | respVO.getFailureCodes().put(key, "物料不存在:" + importItem.getItemCode()); |
| | | return; |
| | | } |
| | | try { |
| | | MesQcOqcSaveReqVO createReqVO = BeanUtils.toBean(importItem, MesQcOqcSaveReqVO.class); |
| | | createReqVO.setCode(generateImportCode("OQC", index)); |
| | | createReqVO.setName(StrUtil.blankToDefault(importItem.getName(), "出货检验单")); |
| | | createReqVO.setClientId(customer.getId()); |
| | | createReqVO.setItemId(item.getId()); |
| | | Long id = createOqc(createReqVO); |
| | | MesQcOqcDO created = oqcMapper.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()); |
| | | } |
| | | |
| | | } |