【新增】资源分类接口
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package com.corewing.app.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ResourceCategoryTreeRequest {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.corewing.app.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.corewing.app.common.base.CommonEntity;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class BizResourceCategory extends CommonEntity {
|
||||
|
||||
@TableId
|
||||
private String id;
|
||||
|
||||
private String categoryName;
|
||||
|
||||
private String parentId;
|
||||
|
||||
private Integer sortCode;
|
||||
|
||||
private String remark;
|
||||
|
||||
private String extJson;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.corewing.app.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.corewing.app.entity.BizResourceCategory;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface BizResourceCategoryMapper extends BaseMapper<BizResourceCategory> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.corewing.app.modules.downloadCenter;
|
||||
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import com.corewing.app.common.Result;
|
||||
import com.corewing.app.dto.ResourceCategoryTreeRequest;
|
||||
import com.corewing.app.service.BizResourceCategoryService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 资源分类接口
|
||||
*/
|
||||
@Api(tags = "资源分类接口")
|
||||
@RestController
|
||||
@RequestMapping("/api/app/resource_category")
|
||||
public class BizResourceCategoryController {
|
||||
|
||||
@Resource
|
||||
private BizResourceCategoryService bizResourceCategoryService;
|
||||
|
||||
/**
|
||||
* 获取分类树形集合
|
||||
* @param resourceCategoryTreeRequest
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation("分类树形集合")
|
||||
@GetMapping("/tree")
|
||||
public Result<List<Tree<String>>> categoryTree(ResourceCategoryTreeRequest resourceCategoryTreeRequest) {
|
||||
return Result.success(bizResourceCategoryService.tree(resourceCategoryTreeRequest));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.corewing.app.service;
|
||||
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.corewing.app.dto.ResourceCategoryTreeRequest;
|
||||
import com.corewing.app.entity.BizResourceCategory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface BizResourceCategoryService extends IService<BizResourceCategory> {
|
||||
|
||||
List<Tree<String>> tree(ResourceCategoryTreeRequest resourceCategoryTreeRequest);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.corewing.app.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.tree.Tree;
|
||||
import cn.hutool.core.lang.tree.TreeNode;
|
||||
import cn.hutool.core.lang.tree.TreeUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.corewing.app.dto.ResourceCategoryTreeRequest;
|
||||
import com.corewing.app.entity.AppModelCategory;
|
||||
import com.corewing.app.entity.BizResourceCategory;
|
||||
import com.corewing.app.mapper.BizResourceCategoryMapper;
|
||||
import com.corewing.app.service.BizResourceCategoryService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class BizResourceCategoryServiceImpl extends ServiceImpl<BizResourceCategoryMapper, BizResourceCategory> implements BizResourceCategoryService {
|
||||
|
||||
|
||||
@Override
|
||||
public List<Tree<String>> tree(ResourceCategoryTreeRequest resourceCategoryTreeRequest) {
|
||||
LambdaQueryWrapper<BizResourceCategory> queryWrapper = new LambdaQueryWrapper<>();
|
||||
List<BizResourceCategory> resourceList = list(queryWrapper);
|
||||
|
||||
// 填充上层的父级菜单
|
||||
this.fillParentCategoryInfo(resourceList);
|
||||
|
||||
List<TreeNode<String>> treeNodeList = resourceList.stream().map(category ->
|
||||
new TreeNode<>(category.getId(), category.getParentId(),
|
||||
category.getCategoryName(), category.getSortCode()).setExtra(JSONUtil.parseObj(category)))
|
||||
.collect(Collectors.toList());
|
||||
return TreeUtil.build(treeNodeList, "0");
|
||||
}
|
||||
|
||||
private void fillParentCategoryInfo(List<BizResourceCategory> categoryList) {
|
||||
if(CollUtil.isNotEmpty(categoryList)){
|
||||
List<BizResourceCategory> parentResourceCategoryList = categoryList.stream().filter(distinctByKey(BizResourceCategory::getParentId)).collect(Collectors.toList());
|
||||
|
||||
List<String> parentIds = null;
|
||||
if(CollUtil.isNotEmpty(parentResourceCategoryList)){
|
||||
parentIds = CollUtil.newArrayList();
|
||||
for(BizResourceCategory parentCategory : categoryList){
|
||||
if(!StrUtil.equals(parentCategory.getParentId(),"0")){
|
||||
parentIds.add(parentCategory.getParentId());
|
||||
}
|
||||
}
|
||||
}
|
||||
if(CollUtil.isNotEmpty(parentIds)){
|
||||
LambdaQueryWrapper<BizResourceCategory> parentCategoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
parentCategoryLambdaQueryWrapper.in(BizResourceCategory::getId,parentIds);
|
||||
List<BizResourceCategory> parentCategoryList = this.list(parentCategoryLambdaQueryWrapper);
|
||||
if(CollUtil.isNotEmpty(parentCategoryList)){
|
||||
this.fillParentCategoryInfo(parentCategoryList);
|
||||
categoryList.addAll(parentCategoryList);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
|
||||
Set<Object> seen = ConcurrentHashMap.newKeySet();
|
||||
return t -> seen.add(keyExtractor.apply(t));
|
||||
}
|
||||
}
|
||||
7
src/main/resources/mapper/BizResourceCategoryMapper.xml
Normal file
7
src/main/resources/mapper/BizResourceCategoryMapper.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.corewing.app.mapper.BizResourceCategoryMapper">
|
||||
|
||||
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user