| | |
| | | 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.client.MesMdClientService; |
| | | 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 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.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; |
| | |
| | | private MesQcDefectRecordService defectRecordService; |
| | | @Resource |
| | | @Lazy |
| | | private MesMdClientService clientService; |
| | | @Resource |
| | | @Lazy |
| | | private MesMdItemService itemService; |
| | | @Resource |
| | | @Lazy |
| | | private MesWmProductSalesLineService productSalesLineService; |
| | | @Resource |
| | | @Lazy |
| | |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | | @Resource |
| | | private CrmCustomerApi customerApi; |
| | | |
| | | @Resource |
| | | @Lazy |
| | | private MesQcNcrService ncrService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | private void validateOqcSaveData(MesQcOqcSaveReqVO reqVO) { |
| | | // 校验编号唯一 |
| | | validateOqcCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | // 校验客户、物料、检测人员存在 |
| | | clientService.validateClientExists(reqVO.getClientId()); |
| | | itemService.validateItemExists(reqVO.getItemId()); |
| | | // 校验物料、检测人员存在 |
| | | // 注:物料 ID 来自 MDM,客户 ID 来自 CRM |
| | | mdmItemApi.validateItemExists(reqVO.getItemId()); |
| | | adminUserApi.validateUser(reqVO.getInspectorUserId()); |
| | | } |
| | | |
| | |
| | | .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); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | @Override |
| | | public PageResult<MesQcOqcDO> getOqcPage(MesQcOqcPageReqVO pageReqVO) { |
| | | return oqcMapper.selectPage(pageReqVO); |
| | | // 获取用户有权限的客户 ID 列表 |
| | | List<Long> permittedClientIds = customerApi.getPermittedCustomerIds(); |
| | | return oqcMapper.selectPage(pageReqVO, permittedClientIds); |
| | | } |
| | | |
| | | @Override |
| | |
| | | oqcMapper.updateById(updateOqc); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createOqcFromProductSales(Long salesId, MesWmProductSalesLineDO line, String salesCode, Long clientId) { |
| | | // 1. 校验物料是否配置了 OQC 检验模板 |
| | | MesQcTemplateItemDO templateItem; |
| | | try { |
| | | templateItem = templateItemService.getRequiredTemplateByItemIdAndType( |
| | | line.getItemId(), MesQcTypeEnum.OQC.getType()); |
| | | } catch (Exception e) { |
| | | // 获取物料信息,提供更明确的错误提示 |
| | | String itemInfo = getItemInfo(line.getItemId()); |
| | | throw exception(QC_NO_TEMPLATE_FOR_ITEM, itemInfo); |
| | | } |
| | | Long templateId = templateItem.getTemplateId(); |
| | | |
| | | // 2. 生成检验单编码 |
| | | String code = "OQC" + System.currentTimeMillis(); |
| | | |
| | | // 3. 创建 OQC 主表(待检验状态,初始数量为 0) |
| | | MesQcOqcDO oqc = new MesQcOqcDO(); |
| | | oqc.setCode(code); |
| | | oqc.setName(salesCode + "-出货检验"); |
| | | oqc.setTemplateId(templateId); |
| | | oqc.setSourceDocType(MesBizTypeConstants.WM_PRODUCT_SALES); |
| | | oqc.setSourceDocId(salesId); |
| | | oqc.setSourceLineId(line.getId()); |
| | | oqc.setSourceDocCode(salesCode); |
| | | oqc.setClientId(clientId); |
| | | oqc.setItemId(line.getItemId()); |
| | | oqc.setBatchCode(line.getBatchCode()); |
| | | oqc.setOutQuantity(line.getQuantity()); |
| | | oqc.setCheckQuantity(BigDecimal.ZERO); // 待填写 |
| | | oqc.setQualifiedQuantity(BigDecimal.ZERO); // 待填写 |
| | | oqc.setUnqualifiedQuantity(BigDecimal.ZERO); // 待填写 |
| | | oqc.setOutDate(java.time.LocalDateTime.now()); |
| | | oqc.setStatus(MesQcStatusEnum.DRAFT.getStatus()); |
| | | oqcMapper.insert(oqc); |
| | | |
| | | // 4. 从模板指标自动生成检验行 |
| | | oqcLineService.createLinesFromTemplate(oqc.getId(), templateId); |
| | | |
| | | return oqc.getId(); |
| | | } |
| | | |
| | | /** |
| | | * 获取物料信息(编码+名称) |
| | | */ |
| | | private String getItemInfo(Long itemId) { |
| | | if (itemId == null) { |
| | | return "未知物料"; |
| | | } |
| | | try { |
| | | cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO item = mdmItemApi.getItem(itemId).getCheckedData(); |
| | | if (item != null) { |
| | | return item.getCode() + "-" + item.getName(); |
| | | } |
| | | } catch (Exception e) { |
| | | // 忽略异常 |
| | | } |
| | | 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()); |
| | | } |
| | | |
| | | } |