diff --git a/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/dal/mysql/base/MaterialInfomationMapper.java b/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/dal/mysql/base/MaterialInfomationMapper.java index 512f86e6..b2a9814c 100644 --- a/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/dal/mysql/base/MaterialInfomationMapper.java +++ b/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/dal/mysql/base/MaterialInfomationMapper.java @@ -24,7 +24,7 @@ public interface MaterialInfomationMapper extends BaseMapperX selectPage(MaterialInfomationPageReqVO reqVO, Collection infomationIds) { return BaseMapperX.super.selectPage(reqVO, new LambdaQueryWrapperX() - .likeIfPresent(MaterialInfomationDO::getCode, reqVO.getCode()) + // code 字段的查询已在 Service 层处理(包含中铜编码、中铝编码),此处不再重复查询 .likeIfPresent(MaterialInfomationDO::getName, reqVO.getName()) .likeIfPresent(MaterialInfomationDO::getRemark, reqVO.getRemark()) .betweenIfPresent(MaterialInfomationDO::getCreateTime, reqVO.getCreateTime()) diff --git a/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/service/base/MaterialInfomationServiceImpl.java b/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/service/base/MaterialInfomationServiceImpl.java index 30beead0..0346ef5d 100644 --- a/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/service/base/MaterialInfomationServiceImpl.java +++ b/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/service/base/MaterialInfomationServiceImpl.java @@ -22,6 +22,7 @@ import com.zt.plat.module.base.dal.dataobject.materialhasclasses.MaterialHasClas import com.zt.plat.module.base.dal.dataobject.materialhasproperties.MaterialHasPropertiesDO; import com.zt.plat.module.base.dal.dataobject.materialproperties.MaterialPropertiesDO; import com.zt.plat.module.base.dal.mysql.base.MaterialInfomationMapper; +import com.zt.plat.module.base.service.masterdatasync.support.MasterDataPropertyDefinition; import com.zt.plat.module.erp.api.ErpExternalApi; import com.zt.plat.module.erp.api.dto.ErpProductiveVersionReqDTO; import jakarta.annotation.Resource; @@ -229,6 +230,18 @@ public class MaterialInfomationServiceImpl implements MaterialInfomationService .collect(Collectors.toList()); } + // 如果搜索物料编码,需要同时搜索中铜编码和中铝编码(作为物料属性存储) + if (StrUtil.isNotBlank(pageReqVO.getCode())) { + // 优化:将分类筛选结果传入,在数据库层面就完成筛选,减少数据传输 + List codeMatchedIds = queryMaterialIdsByCodeOrAttribute(pageReqVO.getCode(), infomationIds); + if (CollUtil.isEmpty(codeMatchedIds)) { + // 按编码查询无结果,直接返回空(无论是否有分类筛选) + return PageResult.empty(); + } + // 直接使用编码查询结果(已包含分类筛选) + infomationIds = codeMatchedIds; + } + PageResult pageResult = materialInfomationMapper.selectPage(pageReqVO, infomationIds); if (CollUtil.isEmpty(pageResult.getList())) { return PageResult.empty(pageResult.getTotal()); @@ -251,6 +264,81 @@ public class MaterialInfomationServiceImpl implements MaterialInfomationService return new PageResult<>(respList, pageResult.getTotal()); } + /** + * 根据物料编码或属性编码(中铜编码、中铝编码)查询物料ID列表 + * 优化版本:减少数据库查询次数,提升性能 + * + * @param code 编码关键字(支持模糊查询) + * @param classIdFilter 分类筛选的物料ID列表(可为null) + * @return 匹配的物料ID列表 + */ + private List queryMaterialIdsByCodeOrAttribute(String code, List classIdFilter) { + Set materialIds = new HashSet<>(); + + // 1. 查询物料表的 code 字段 + List directMatches = materialInfomationMapper.selectList( + new LambdaQueryWrapperX() + .like(MaterialInfomationDO::getCode, code) + .in(Objects.nonNull(classIdFilter), MaterialInfomationDO::getId, classIdFilter) + ); + if (CollUtil.isNotEmpty(directMatches)) { + materialIds.addAll(directMatches.stream() + .map(MaterialInfomationDO::getId) + .filter(Objects::nonNull) + .collect(Collectors.toSet())); + } + + // 2. 查询中铜编码和中铝编码属性(使用缓存的属性ID) + List propertyIds = getCachedCodePropertyIds(); + if (CollUtil.isNotEmpty(propertyIds)) { + // 查询属性值表中匹配的物料 + List hasProperties = materialHasPropertiesMapper.selectList( + new LambdaQueryWrapperX() + .in(MaterialHasPropertiesDO::getPropertiesId, propertyIds) + .like(MaterialHasPropertiesDO::getValue, code) + .in(Objects.nonNull(classIdFilter),MaterialHasPropertiesDO::getInfomationId, classIdFilter) + ); + + if (CollUtil.isNotEmpty(hasProperties)) { + materialIds.addAll(hasProperties.stream() + .map(MaterialHasPropertiesDO::getInfomationId) + .filter(Objects::nonNull) + .collect(Collectors.toSet())); + } + } + + return new ArrayList<>(materialIds); + } + + /** + * 获取中铜编码和中铝编码的属性ID(使用本地缓存避免重复查询) + */ + private static volatile List cachedCodePropertyIds = null; + + private List getCachedCodePropertyIds() { + if (cachedCodePropertyIds == null) { + synchronized (MaterialInfomationServiceImpl.class) { + if (cachedCodePropertyIds == null) { + List properties = materialPropertiesMapper.selectList( + new LambdaQueryWrapperX() + .in(MaterialPropertiesDO::getCode, Arrays.asList( + MasterDataPropertyDefinition.CHALCO_CODE.getCode(), + MasterDataPropertyDefinition.ZHONGTONG_CODE.getCode())) + ); + if (CollUtil.isNotEmpty(properties)) { + cachedCodePropertyIds = properties.stream() + .map(MaterialPropertiesDO::getId) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } else { + cachedCodePropertyIds = Collections.emptyList(); + } + } + } + } + return cachedCodePropertyIds; + } + @Override public String getOneTest() { ErpProductiveVersionReqDTO reqDTO = new ErpProductiveVersionReqDTO();