mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-18 01:25:28 +08:00
在线h5开发
This commit is contained in:
parent
c9f9ca33bf
commit
b862bbbec8
@ -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<H5Component> {
|
||||
* @return 组件列表
|
||||
*/
|
||||
List<H5Component> selectTopComponentsByCategory(@Param("categoryId") Long categoryId, @Param("pageSize") Integer pageSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询组件列表
|
||||
* @param page 分页对象
|
||||
* @param params 查询参数
|
||||
* @return 组件列表
|
||||
*/
|
||||
List<H5Component> selectPageList(@Param("page") com.baomidou.mybatisplus.extension.plugins.pagination.Page<H5Component> page,
|
||||
@Param("params") Map<String, Object> params);
|
||||
}
|
||||
@ -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<H5ComponentDao, H5Component> 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<H5ComponentDao, H5Compon
|
||||
|
||||
@Override
|
||||
public Page<H5ComponentVO> getPage(Integer page, Integer pageSize, String type, String keyword, Long categoryId, Long tagId) {
|
||||
Page<H5Component> mpPage = new Page<>(page, pageSize);
|
||||
|
||||
LambdaQueryWrapper<H5Component> 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<String, Object> params = new HashMap<>();
|
||||
params.put("type", type);
|
||||
params.put("keyword", keyword);
|
||||
params.put("tagId", tagId);
|
||||
|
||||
//categoryId 查询出所有子集一起查询
|
||||
List<Long> categoryIds = new ArrayList<>();
|
||||
if (categoryId != null) {
|
||||
List<Long> componentIds = getComponentIdsByCategory(categoryId);
|
||||
if (componentIds.isEmpty()) {
|
||||
return new Page<H5ComponentVO>(page, pageSize).setRecords(List.of());
|
||||
}
|
||||
wrapper.in(H5Component::getId, componentIds);
|
||||
categoryIds.add(categoryId);
|
||||
// 查询所有子分类ID
|
||||
List<Long> childCategoryIds = getChildCategoryIds(categoryId);
|
||||
categoryIds.addAll(childCategoryIds);
|
||||
}
|
||||
|
||||
// 按标签筛选
|
||||
if (tagId != null) {
|
||||
List<Long> componentIds = getComponentIdsByTag(tagId);
|
||||
if (componentIds.isEmpty()) {
|
||||
return new Page<H5ComponentVO>(page, pageSize).setRecords(List.of());
|
||||
}
|
||||
wrapper.in(H5Component::getId, componentIds);
|
||||
}
|
||||
|
||||
wrapper.orderByDesc(H5Component::getUsageCount)
|
||||
.orderByDesc(H5Component::getCreateTime);
|
||||
|
||||
Page<H5Component> resultPage = baseMapper.selectPage(mpPage, wrapper);
|
||||
params.put("categoryIds", categoryIds);
|
||||
|
||||
//使用xml sql 分页查询
|
||||
Page<H5Component> resultPage = new Page<>(page, pageSize);
|
||||
List<H5Component> componentList = h5ComponentDao.selectPageList(resultPage, params);
|
||||
resultPage.setRecords(componentList);
|
||||
|
||||
Page<H5ComponentVO> voPage = new Page<>(resultPage.getCurrent(), resultPage.getSize(), resultPage.getTotal());
|
||||
List<H5ComponentVO> voList = resultPage.getRecords().stream()
|
||||
@ -90,6 +78,48 @@ public class H5ComponentServiceImpl extends ServiceImpl<H5ComponentDao, H5Compon
|
||||
|
||||
return voPage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定分类的所有子分类ID(优化版本-无缓存)
|
||||
* @param parentId 父分类ID
|
||||
* @return 子分类ID列表
|
||||
*/
|
||||
private List<Long> getChildCategoryIds(Long parentId) {
|
||||
//TODO 还有优化的空间
|
||||
// 一次性查询所有分类,只获取需要的字段(id和parentId)
|
||||
List<H5ComponentCategory> allCategories = h5ComponentCategoryDao.selectList(
|
||||
new LambdaQueryWrapper<H5ComponentCategory>()
|
||||
.select(H5ComponentCategory::getId, H5ComponentCategory::getParentId)
|
||||
);
|
||||
|
||||
// 构建父分类到子分类的映射
|
||||
Map<Long, List<Long>> parentChildMap = allCategories.stream()
|
||||
.collect(Collectors.groupingBy(
|
||||
H5ComponentCategory::getParentId,
|
||||
Collectors.mapping(H5ComponentCategory::getId, Collectors.toList())
|
||||
));
|
||||
|
||||
// 收集所有子分类ID
|
||||
Set<Long> 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<Long, List<Long>> parentChildMap, Set<Long> result) {
|
||||
List<Long> 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<H5ComponentDao, H5Compon
|
||||
// 转换为实体对象
|
||||
H5Component component = H5ComponentConvert.INSTANCE.toEntity(dto);
|
||||
component.setUpdateTime(now);
|
||||
|
||||
|
||||
baseMapper.updateById(component);
|
||||
|
||||
// 更新分类关联
|
||||
@ -217,7 +247,7 @@ public class H5ComponentServiceImpl extends ServiceImpl<H5ComponentDao, H5Compon
|
||||
if (categoryIds == null || categoryIds.isEmpty()) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
|
||||
Map<Long, List<H5ComponentVO>> resultMap = new HashMap<>();
|
||||
for (Long categoryId : categoryIds) {
|
||||
List<H5Component> components = h5ComponentDao.selectTopComponentsByCategory(categoryId, pageSize);
|
||||
@ -226,7 +256,7 @@ public class H5ComponentServiceImpl extends ServiceImpl<H5ComponentDao, H5Compon
|
||||
.toList();
|
||||
resultMap.put(categoryId, vos);
|
||||
}
|
||||
|
||||
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
@ -277,4 +307,4 @@ public class H5ComponentServiceImpl extends ServiceImpl<H5ComponentDao, H5Compon
|
||||
wrapper.eq(H5ComponentTagRel::getComponentId, componentId);
|
||||
h5ComponentTagRelDao.delete(wrapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -14,5 +14,40 @@
|
||||
ORDER BY usage_count DESC, create_time DESC
|
||||
LIMIT #{pageSize}
|
||||
</select>
|
||||
|
||||
<!-- 分页查询组件列表 -->
|
||||
<select id="selectPageList" resultType="com.luban.api.entity.H5Component">
|
||||
SELECT hc.*
|
||||
FROM h5_component hc
|
||||
WHERE hc.status = 1
|
||||
<if test="params.type != null and params.type != ''">
|
||||
AND hc.type = #{params.type}
|
||||
</if>
|
||||
<if test="params.keyword != null and params.keyword != ''">
|
||||
AND (
|
||||
hc.name LIKE CONCAT('%', #{params.keyword}, '%')
|
||||
)
|
||||
</if>
|
||||
<if test="params.categoryIds != null and params.categoryIds.size() > 0">
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM h5_component_category_rel hccr
|
||||
WHERE hccr.component_id = hc.id
|
||||
AND hccr.category_id IN
|
||||
<foreach collection="params.categoryIds" item="categoryId" open="(" separator="," close=")">
|
||||
#{categoryId}
|
||||
</foreach>
|
||||
)
|
||||
</if>
|
||||
<if test="params.tagId != null">
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM h5_component_tag_rel hctr
|
||||
WHERE hctr.component_id = hc.id
|
||||
AND hctr.tag_id = #{params.tagId}
|
||||
)
|
||||
</if>
|
||||
ORDER BY hc.create_time DESC
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
</mapper>
|
||||
Loading…
Reference in New Issue
Block a user