From a5f972db14fdf186092183e8a730a38c716dee37 Mon Sep 17 00:00:00 2001 From: yangchaojin <549193112@qq.com> Date: Tue, 20 Jan 2026 17:32:13 +0800 Subject: [PATCH] =?UTF-8?q?BUG712=EF=BC=8C=E7=BB=84=E7=BB=87=E6=9C=BA?= =?UTF-8?q?=E6=9E=84=E7=89=A9=E6=96=99=E4=BF=A1=E6=81=AF=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E6=89=A9=E5=B1=95=E7=89=A9=E6=96=99=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E5=B1=9E=E6=80=A7=E6=9F=A5=E8=AF=A2=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DepartmentMaterialServiceImpl.java | 96 +++++++++++++++++-- 1 file changed, 87 insertions(+), 9 deletions(-) diff --git a/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/service/departmentmaterial/DepartmentMaterialServiceImpl.java b/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/service/departmentmaterial/DepartmentMaterialServiceImpl.java index 36ae60da..e1af3443 100644 --- a/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/service/departmentmaterial/DepartmentMaterialServiceImpl.java +++ b/zt-module-base/zt-module-base-server/src/main/java/com/zt/plat/module/base/service/departmentmaterial/DepartmentMaterialServiceImpl.java @@ -6,6 +6,7 @@ import com.zt.plat.module.base.dal.dao.materialhasproperties.MaterialHasProperti import com.zt.plat.module.base.dal.dao.materialproperties.MaterialPropertiesMapper; 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.service.masterdatasync.support.MasterDataPropertyDefinition; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -13,6 +14,7 @@ import org.springframework.validation.annotation.Validated; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; import com.zt.plat.framework.common.pojo.PageResult; import com.zt.plat.framework.common.util.collection.CollectionUtils; @@ -69,7 +71,7 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService DepartmentMaterialDO departmentMaterial = BeanUtils.toBean(createReqVO, DepartmentMaterialDO.class); departmentMaterialMapper.insert(departmentMaterial); // 构造完整响应,方便前端直接刷新数据 - return CollUtil.getFirst(decorateDepartmentMaterials(Collections.singletonList(departmentMaterial))); + return CollUtil.getFirst(decorateDepartmentMaterials(Collections.singletonList(departmentMaterial))); } @Override @@ -90,12 +92,12 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService } @Override - public void deleteDepartmentMaterialListByIds(List ids) { + public void deleteDepartmentMaterialListByIds(List ids) { // 校验存在 validateDepartmentMaterialExists(ids); // 删除 departmentMaterialMapper.deleteByIds(ids); - } + } private void validateDepartmentMaterialExists(List ids) { List list = departmentMaterialMapper.selectByIds(ids); @@ -122,7 +124,7 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService @Override public List getDepartmentMaterialByIds(List ids) { List data = departmentMaterialMapper.selectByIds(ids); - if (org.apache.commons.collections4.CollectionUtils.isEmpty( data)) { + if (org.apache.commons.collections4.CollectionUtils.isEmpty(data)) { return null; } return decorateDepartmentMaterials(data); @@ -144,8 +146,22 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService pageReqVO.setInfomationIds(matchedInfoIds); } + // 如果搜索物料编码,需要同时搜索中铜编码和中铝编码(作为物料属性存储) + if (StrUtil.isNotBlank(pageReqVO.getMaterialNumber())) { + // 优化:将分类筛选结果传入,在数据库层面就完成筛选,减少数据传输 + List codeMatchedIds = queryMaterialIdsByAttribute(pageReqVO.getMaterialNumber(), null); + if (!CollUtil.isEmpty(codeMatchedIds)) { + // 合并编码集合条件并去重 + List ids = Stream + .concat(pageReqVO.getInfomationIds().stream(), codeMatchedIds.stream()) + .distinct() + .toList(); + pageReqVO.setInfomationIds(ids); + } + } + // 判断部门是否是公司,非公司的部门需要显示所属公司下的所有物料信息 - if(pageReqVO.getIsCompany() != null && !pageReqVO.getIsCompany()) { + if (pageReqVO.getIsCompany() != null && !pageReqVO.getIsCompany()) { List deptList = (deptApi.upFindCompanyNode(pageReqVO.getDeptId())).getData(); if (!CollUtil.isEmpty(deptList)) { pageReqVO.setDeptIds(deptList.stream().map(DeptRespDTO::getId).toList()); // 设置部门查询范围 @@ -178,8 +194,8 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService Map infoMap = infoIds.isEmpty() ? Collections.emptyMap() : materialInfomationMapper.selectBatchIds(infoIds).stream() - .filter(Objects::nonNull) - .collect(Collectors.toMap(MaterialInfomationDO::getId, Function.identity())); + .filter(Objects::nonNull) + .collect(Collectors.toMap(MaterialInfomationDO::getId, Function.identity())); Set classIds = new HashSet<>(); sourceList.forEach(item -> { @@ -215,8 +231,8 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService Map dictionaryMap = dictionaryValues.isEmpty() ? Collections.emptyMap() : businessDictionaryDataMapper.selectListByValues(dictionaryValues).stream() - .filter(Objects::nonNull) - .collect(Collectors.toMap(BusinessDictionaryDataDO::getValue, Function.identity(), (exist, replace) -> exist)); + .filter(Objects::nonNull) + .collect(Collectors.toMap(BusinessDictionaryDataDO::getValue, Function.identity(), (exist, replace) -> exist)); List respList = new ArrayList<>(sourceList.size()); for (DepartmentMaterialDO item : sourceList) { @@ -373,4 +389,66 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService return attributes; } } + + /** + * 获取中铜编码和中铝编码的属性ID(使用本地缓存避免重复查询) + */ + private static volatile List cachedCodePropertyIds = null; + + private List getCachedCodePropertyIds() { + if (cachedCodePropertyIds == null) { + synchronized (DepartmentMaterialServiceImpl.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; + } + + /** + * 根据物料编码或属性编码(中铜编码、中铝编码)查询物料ID列表 + * 优化版本:减少数据库查询次数,提升性能 + * + * @param code 编码关键字(支持模糊查询) + * @param classIdFilter 分类筛选的物料ID列表(可为null) + * @return 匹配的物料ID列表 + */ + private List queryMaterialIdsByAttribute(String code, List classIdFilter) { + Set materialIds = new HashSet<>(); + + // 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); + } } \ No newline at end of file