@@ -2,6 +2,10 @@ package com.zt.plat.module.base.service.departmentmaterial;
import cn.hutool.core.collection.CollUtil ;
import cn.hutool.core.util.StrUtil ;
import com.zt.plat.module.base.dal.dao.materialhasproperties.MaterialHasPropertiesMapper ;
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 jakarta.annotation.Resource ;
import org.springframework.stereotype.Service ;
import org.springframework.validation.annotation.Validated ;
@@ -50,6 +54,12 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService
@Resource
private BusinessDictionaryDataMapper businessDictionaryDataMapper ;
@Resource
private MaterialHasPropertiesMapper materialHasPropertiesMapper ;
@Resource
private MaterialPropertiesMapper materialPropertiesMapper ;
@Resource
private DeptApi deptApi ;
@@ -106,7 +116,7 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService
if ( data = = null ) {
return null ;
}
return CollUtil . getFirst ( decorateDepartmentMaterials ( Collections . singletonList ( data ) ) ) ;
return CollUtil . getFirst ( buildPropertyAggregates ( decorateDepartmentMaterials( Collections . singletonList ( data ) ) ) ) ;
}
@Override
@@ -134,6 +144,15 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService
pageReqVO . setInfomationIds ( matchedInfoIds ) ;
}
// 判断部门是否是公司,非公司的部门需要显示所属公司下的所有物料信息
if ( pageReqVO . getIsCompany ( ) ! = null & & ! pageReqVO . getIsCompany ( ) ) {
List < DeptRespDTO > deptList = ( deptApi . upFindCompanyNode ( pageReqVO . getDeptId ( ) ) ) . getData ( ) ;
if ( ! CollUtil . isEmpty ( deptList ) ) {
pageReqVO . setDeptIds ( deptList . stream ( ) . map ( DeptRespDTO : : getId ) . toList ( ) ) ; // 设置部门查询范围
pageReqVO . setDeptId ( null ) ; // 置空部门编号
}
}
PageResult < DepartmentMaterialDO > pageResult = departmentMaterialMapper . selectPage ( pageReqVO ) ;
// 重置,避免后续使用该入参出现意外的 in 条件
pageReqVO . setInfomationIds ( null ) ;
@@ -283,4 +302,75 @@ public class DepartmentMaterialServiceImpl implements DepartmentMaterialService
return path ;
}
private List < DepartmentMaterialRespVO > buildPropertyAggregates ( List < DepartmentMaterialRespVO > list ) {
if ( CollUtil . isEmpty ( list ) ) {
return Collections . emptyList ( ) ;
}
List < MaterialHasPropertiesDO > hasPropertiesList = materialHasPropertiesMapper . selectList (
new LambdaQueryWrapperX < MaterialHasPropertiesDO > ( ) . in ( MaterialHasPropertiesDO : : getInfomationId ,
list . stream ( ) . map ( DepartmentMaterialRespVO : : getInfomationId ) . toList ( ) ) ) ;
if ( CollUtil . isEmpty ( hasPropertiesList ) ) {
return list ;
}
Set < Long > propertiesIds = hasPropertiesList . stream ( )
. map ( MaterialHasPropertiesDO : : getPropertiesId )
. filter ( Objects : : nonNull )
. collect ( Collectors . toSet ( ) ) ;
Map < Long , MaterialPropertiesDO > propertiesMap = propertiesIds . isEmpty ( )
? Collections . emptyMap ( )
: materialPropertiesMapper . selectBatchIds ( propertiesIds ) . stream ( )
. filter ( Objects : : nonNull )
. collect ( Collectors . toMap ( MaterialPropertiesDO : : getId , Function . identity ( ) ) ) ;
Map < Long , List < MaterialHasPropertiesDO > > infoPropsMap = hasPropertiesList . stream ( )
. collect ( Collectors . groupingBy ( MaterialHasPropertiesDO : : getInfomationId ) ) ;
Map < Long , MaterialPropertiesAggregate > result = new HashMap < > ( ) ;
for ( Map . Entry < Long , List < MaterialHasPropertiesDO > > entry : infoPropsMap . entrySet ( ) ) {
Long infoId = entry . getKey ( ) ;
List < MaterialHasPropertiesDO > props = entry . getValue ( ) ;
// 按 sort 升序,保持 deterministic, 重复 code 时保留首条
props . sort ( Comparator . comparing ( ( MaterialHasPropertiesDO o ) - > o . getSort ( ) = = null ? Long . MAX_VALUE : o . getSort ( ) )
. thenComparing ( MaterialHasPropertiesDO : : getPropertiesId , Comparator . nullsLast ( Long : : compareTo ) )
. thenComparing ( MaterialHasPropertiesDO : : getId , Comparator . nullsLast ( Long : : compareTo ) ) ) ;
Map < String , Object > flatMap = new LinkedHashMap < > ( ) ;
for ( MaterialHasPropertiesDO item : props ) {
MaterialPropertiesDO property = propertiesMap . get ( item . getPropertiesId ( ) ) ;
String code = property ! = null ? property . getCode ( ) : null ;
if ( StrUtil . isBlank ( code ) ) {
continue ;
}
if ( flatMap . containsKey ( code ) ) {
continue ;
}
flatMap . put ( code , item . getValue ( ) ) ;
}
result . put ( infoId , new MaterialPropertiesAggregate ( flatMap ) ) ;
}
return list . stream ( ) . peek ( item - > {
Map < String , Object > flatMap = new LinkedHashMap < > ( ) ;
MaterialPropertiesAggregate aggregate = result . get ( item . getInfomationId ( ) ) ;
if ( aggregate ! = null & & CollUtil . isNotEmpty ( aggregate . getAttributes ( ) ) ) {
aggregate . getAttributes ( ) . forEach ( ( k , v ) - > {
// 基础字段优先,存在同名则跳过属性值
if ( ! flatMap . containsKey ( k ) ) {
flatMap . put ( k , v ) ;
}
} ) ;
item . setFlatAttributes ( flatMap ) ;
}
} ) . collect ( Collectors . toList ( ) ) ;
}
private record MaterialPropertiesAggregate ( Map < String , Object > attributes ) {
public Map < String , Object > getAttributes ( ) {
return attributes ;
}
}
}