品类管理查询
This commit is contained in:
@ -15,6 +15,26 @@ import java.util.List;
|
||||
*/
|
||||
public interface CoscoCategoryMaintenanceMapper extends IBaseMapper<CoscoCategoryMaintenance> {
|
||||
|
||||
|
||||
/**
|
||||
* 模糊查询品类名称匹配的节点
|
||||
*/
|
||||
List<CoscoCategoryMaintenanceVO> findByCategoryNameLike(@Param("categoryName") String categoryName,
|
||||
@Param("versionId") Long versionId,
|
||||
@Param("code") String code);
|
||||
|
||||
|
||||
/**
|
||||
* 根据父ID查询节点(用于递归找父级)
|
||||
*/
|
||||
CoscoCategoryMaintenanceVO findByParentId(@Param("parentId") Long parentId, @Param("versionId") Long versionId);
|
||||
|
||||
/**
|
||||
* 批量查询节点(用于加载收集到的所有节点)
|
||||
*/
|
||||
List<CoscoCategoryMaintenanceVO> findAllByIds(@Param("ids") List<Long> ids, @Param("versionId") Long versionId);
|
||||
|
||||
|
||||
List<CoscoCategoryMaintenanceVO> findAllByParentId(@Param("parentId") Long parentId,@Param("versionId") Long versionId);
|
||||
|
||||
void saveData(@Param("categoryMaintenance") CoscoCategoryMaintenance categoryMaintenance);
|
||||
|
@ -83,9 +83,9 @@
|
||||
|
||||
<insert id="batchSaveCategory">
|
||||
INSERT INTO cosco_category_maintenance
|
||||
(version_id, parent_id,category_name,code,status) VALUES
|
||||
(id,version_id, parent_id,category_name,code,status) VALUES
|
||||
<foreach collection="list" item="item" separator=",">
|
||||
(#{item.versionId}, #{item.parentId},#{item.categoryName},#{item.code},#{item.status})
|
||||
(#{item.id},#{item.versionId}, #{item.parentId},#{item.categoryName},#{item.code},#{item.status})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.coscoshipping.ebtp.system.category.maintenance.service.impl;
|
||||
|
||||
|
||||
import com.alibaba.nacos.common.utils.CollectionUtils;
|
||||
import com.chinaunicom.mall.ebtp.common.base.entity.BasePageRequest;
|
||||
import com.chinaunicom.mall.ebtp.common.base.service.impl.BaseServiceImpl;
|
||||
import com.coscoshipping.ebtp.system.category.maintenance.dao.CoscoCategoryMaintenanceMapper;
|
||||
@ -12,10 +13,12 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 对数据表 cosco_category_maintenance 操作的 serviceImpl
|
||||
@ -63,9 +66,92 @@ public class CoscoCategoryMaintenanceServiceImpl extends BaseServiceImpl<CoscoCa
|
||||
|
||||
@Override
|
||||
public List<CoscoCategoryMaintenanceVO> getList(CoscoCategoryMaintenanceVO coscoCategoryMaintenanceVO) {
|
||||
List<CoscoCategoryMaintenanceVO> roots = coscoCategoryMaintenanceMapper.findAllByParentId(0L,coscoCategoryMaintenanceVO.getVersionId());
|
||||
roots.forEach(this::loadChildren);
|
||||
return roots;
|
||||
String categoryName = coscoCategoryMaintenanceVO.getCategoryName();
|
||||
String code = coscoCategoryMaintenanceVO.getCode();
|
||||
Long versionId = coscoCategoryMaintenanceVO.getVersionId();
|
||||
List<CoscoCategoryMaintenanceVO> result = new ArrayList<>();
|
||||
|
||||
if (StringUtils.isNotBlank(categoryName) || StringUtils.isNotBlank(code)) {
|
||||
// 存储已匹配的节点 ID(用于去重)
|
||||
Set<Long> matchedIds = new HashSet<>();
|
||||
List<CoscoCategoryMaintenanceVO> matchedNodes = new ArrayList<>();
|
||||
|
||||
// 1. 品类名称模糊查询
|
||||
if (StringUtils.isNotBlank(categoryName) || StringUtils.isNotBlank(code)) {
|
||||
List<CoscoCategoryMaintenanceVO> nameMatches = coscoCategoryMaintenanceMapper.findByCategoryNameLike(categoryName, versionId,code);
|
||||
for (CoscoCategoryMaintenanceVO node : nameMatches) {
|
||||
if (!matchedIds.contains(node.getId())) {
|
||||
matchedIds.add(node.getId());
|
||||
matchedNodes.add(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (CollectionUtils.isEmpty(matchedNodes)) {
|
||||
return result; // 无匹配结果,直接返回空列表
|
||||
}
|
||||
|
||||
// 3. 递归收集所有匹配节点的父级(包括自身)
|
||||
Set<Long> collectedIds = new HashSet<>();
|
||||
for (CoscoCategoryMaintenanceVO node : matchedNodes) {
|
||||
collectAncestors(node, collectedIds, versionId);
|
||||
}
|
||||
|
||||
// 4. 批量查询所有收集到的节点(避免重复查询)
|
||||
List<CoscoCategoryMaintenanceVO> allNodes = coscoCategoryMaintenanceMapper.findAllByIds(
|
||||
new ArrayList<>(collectedIds), versionId
|
||||
);
|
||||
if (CollectionUtils.isEmpty(allNodes)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 5. 构建树形结构(按 parentId 分组子节点)
|
||||
Map<Long, List<CoscoCategoryMaintenanceVO>> parentMap = allNodes.stream()
|
||||
.filter(n -> n.getParentId() != null) // 过滤空父ID
|
||||
.collect(Collectors.groupingBy(CoscoCategoryMaintenanceVO::getParentId));
|
||||
|
||||
// 6. 找到根节点(parentId = 0 或 null)并递归加载子节点
|
||||
List<CoscoCategoryMaintenanceVO> roots = allNodes.stream()
|
||||
.filter(n -> n.getParentId() == null || n.getParentId() == 0)
|
||||
.collect(Collectors.toList());
|
||||
for (CoscoCategoryMaintenanceVO root : roots) {
|
||||
loadChildrenForFiltered(root, parentMap); // 递归加载子节点
|
||||
}
|
||||
result = roots;
|
||||
} else {
|
||||
// 无搜索条件时,加载所有根节点并递归子节点(原逻辑)
|
||||
List<CoscoCategoryMaintenanceVO> roots = coscoCategoryMaintenanceMapper.findAllByParentId(0L, versionId);
|
||||
roots.forEach(this::loadChildren);
|
||||
result = roots;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归收集节点的所有父级(包括自身)
|
||||
*/
|
||||
private void collectAncestors(CoscoCategoryMaintenanceVO node, Set<Long> collectedIds, Long versionId) {
|
||||
if (node == null || collectedIds.contains(node.getId())) {
|
||||
return;
|
||||
}
|
||||
collectedIds.add(node.getId()); // 收集当前节点
|
||||
// 递归查询父节点
|
||||
CoscoCategoryMaintenanceVO parent = coscoCategoryMaintenanceMapper.findByParentId(node.getParentId(), versionId);
|
||||
if (parent != null) {
|
||||
collectAncestors(parent, collectedIds, versionId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 为过滤后的节点递归加载子节点
|
||||
*/
|
||||
private void loadChildrenForFiltered(CoscoCategoryMaintenanceVO vo, Map<Long, List<CoscoCategoryMaintenanceVO>> parentMap) {
|
||||
List<CoscoCategoryMaintenanceVO> children = parentMap.getOrDefault(vo.getId(), Collections.emptyList());
|
||||
vo.setChildren(children);
|
||||
// 递归加载子节点的子节点
|
||||
for (CoscoCategoryMaintenanceVO child : children) {
|
||||
loadChildrenForFiltered(child, parentMap);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadChildren(CoscoCategoryMaintenanceVO vo) {
|
||||
|
@ -31,6 +31,13 @@
|
||||
<select id="selectPageList"
|
||||
resultType="com.coscoshipping.ebtp.system.category.version.entity.CoscoCategoryVersionVO">
|
||||
SELECT * FROM cosco_category_version
|
||||
where 1=1
|
||||
<if test="name!=null and name!=''">
|
||||
and name like concat('%',#{name},'%')
|
||||
</if>
|
||||
<if test="version!=null and version!=''">
|
||||
and version =#{version}
|
||||
</if>
|
||||
</select>
|
||||
<select id="selectById"
|
||||
resultType="com.coscoshipping.ebtp.system.category.version.entity.CoscoCategoryVersion">
|
||||
|
Reference in New Issue
Block a user