diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dao/H5ComponentDao.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dao/H5ComponentDao.java index 8f1c8b1a3..33d7ccf3f 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dao/H5ComponentDao.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dao/H5ComponentDao.java @@ -6,6 +6,7 @@ import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; +import java.util.Map; /** * H5 组件 Mapper 接口 @@ -23,4 +24,13 @@ public interface H5ComponentDao extends BaseMapper { * @return 组件列表 */ List selectTopComponentsByCategory(@Param("categoryId") Long categoryId, @Param("pageSize") Integer pageSize); -} + + /** + * 分页查询组件列表 + * @param page 分页对象 + * @param params 查询参数 + * @return 组件列表 + */ + List selectPageList(@Param("page") com.baomidou.mybatisplus.extension.plugins.pagination.Page page, + @Param("params") Map params); +} \ No newline at end of file diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/H5ComponentServiceImpl.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/H5ComponentServiceImpl.java index 02c476889..01d1fc68e 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/H5ComponentServiceImpl.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/H5ComponentServiceImpl.java @@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.luban.api.dao.H5ComponentCategoryDao; import com.luban.api.dao.H5ComponentCategoryRelDao; import com.luban.api.dao.H5ComponentDao; import com.luban.api.dao.H5ComponentTagRelDao; @@ -36,6 +37,7 @@ import java.util.stream.Collectors; public class H5ComponentServiceImpl extends ServiceImpl implements H5ComponentService { private final H5ComponentDao h5ComponentDao; + private final H5ComponentCategoryDao h5ComponentCategoryDao; private final H5ComponentCategoryRelDao h5ComponentCategoryRelDao; private final H5ComponentTagRelDao h5ComponentTagRelDao; @@ -47,40 +49,26 @@ public class H5ComponentServiceImpl extends ServiceImpl getPage(Integer page, Integer pageSize, String type, String keyword, Long categoryId, Long tagId) { - Page mpPage = new Page<>(page, pageSize); - - LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); - wrapper.eq(H5Component::getStatus, 1); // 只查询启用的组件 - - wrapper.eq(StrUtil.isNotBlank(type), H5Component::getType, type); - - if (StrUtil.isNotBlank(keyword)) { - wrapper.and(w -> w.like(H5Component::getName, keyword) - .or().like(H5Component::getRemark, keyword)); - } - - // 按分类筛选 + //将参数封装成对象,方便后续加参数 + Map params = new HashMap<>(); + params.put("type", type); + params.put("keyword", keyword); + params.put("tagId", tagId); + + //categoryId 查询出所有子集一起查询 + List categoryIds = new ArrayList<>(); if (categoryId != null) { - List componentIds = getComponentIdsByCategory(categoryId); - if (componentIds.isEmpty()) { - return new Page(page, pageSize).setRecords(List.of()); - } - wrapper.in(H5Component::getId, componentIds); + categoryIds.add(categoryId); + // 查询所有子分类ID + List childCategoryIds = getChildCategoryIds(categoryId); + categoryIds.addAll(childCategoryIds); } - - // 按标签筛选 - if (tagId != null) { - List componentIds = getComponentIdsByTag(tagId); - if (componentIds.isEmpty()) { - return new Page(page, pageSize).setRecords(List.of()); - } - wrapper.in(H5Component::getId, componentIds); - } - - wrapper.orderByDesc(H5Component::getUsageCount) - .orderByDesc(H5Component::getCreateTime); - - Page resultPage = baseMapper.selectPage(mpPage, wrapper); + params.put("categoryIds", categoryIds); + + //使用xml sql 分页查询 + Page resultPage = new Page<>(page, pageSize); + List componentList = h5ComponentDao.selectPageList(resultPage, params); + resultPage.setRecords(componentList); Page voPage = new Page<>(resultPage.getCurrent(), resultPage.getSize(), resultPage.getTotal()); List voList = resultPage.getRecords().stream() @@ -90,6 +78,48 @@ public class H5ComponentServiceImpl extends ServiceImpl getChildCategoryIds(Long parentId) { + //TODO 还有优化的空间 + // 一次性查询所有分类,只获取需要的字段(id和parentId) + List allCategories = h5ComponentCategoryDao.selectList( + new LambdaQueryWrapper() + .select(H5ComponentCategory::getId, H5ComponentCategory::getParentId) + ); + + // 构建父分类到子分类的映射 + Map> parentChildMap = allCategories.stream() + .collect(Collectors.groupingBy( + H5ComponentCategory::getParentId, + Collectors.mapping(H5ComponentCategory::getId, Collectors.toList()) + )); + + // 收集所有子分类ID + Set allChildIds = new HashSet<>(); + collectChildIds(parentId, parentChildMap, allChildIds); + + return new ArrayList<>(allChildIds); + } + + /** + * 递归收集子分类ID + * @param parentId 父分类ID + * @param parentChildMap 父子关系映射 + * @param result 结果集合 + */ + private void collectChildIds(Long parentId, Map> parentChildMap, Set result) { + List childIds = parentChildMap.getOrDefault(parentId, Collections.emptyList()); + for (Long childId : childIds) { + result.add(childId); + // 递归收集子分类的子分类 + collectChildIds(childId, parentChildMap, result); + } + } @Override @Transactional(rollbackFor = Exception.class) @@ -131,7 +161,7 @@ public class H5ComponentServiceImpl extends ServiceImpl(); } - + Map> resultMap = new HashMap<>(); for (Long categoryId : categoryIds) { List components = h5ComponentDao.selectTopComponentsByCategory(categoryId, pageSize); @@ -226,7 +256,7 @@ public class H5ComponentServiceImpl extends ServiceImpl + + + - + \ No newline at end of file