|
|
@ -7,6 +7,7 @@ import com.project.base.domain.result.Result; |
|
|
import com.project.base.domain.utils.TreeUtils; |
|
|
import com.project.base.domain.utils.TreeUtils; |
|
|
import com.project.classicpaper.application.ClassicCategoryApplicationService; |
|
|
import com.project.classicpaper.application.ClassicCategoryApplicationService; |
|
|
import com.project.classicpaper.domain.dto.ClassicCategoryDTO; |
|
|
import com.project.classicpaper.domain.dto.ClassicCategoryDTO; |
|
|
|
|
|
import com.project.classicpaper.domain.dto.ClassicPaperSetDTO; |
|
|
import com.project.classicpaper.domain.entity.ClassicCategoryEntity; |
|
|
import com.project.classicpaper.domain.entity.ClassicCategoryEntity; |
|
|
import com.project.classicpaper.domain.entity.ClassicPaperSetEntity; |
|
|
import com.project.classicpaper.domain.entity.ClassicPaperSetEntity; |
|
|
import com.project.classicpaper.domain.service.ClassicCategoryBaseService; |
|
|
import com.project.classicpaper.domain.service.ClassicCategoryBaseService; |
|
|
@ -14,10 +15,7 @@ import com.project.classicpaper.domain.service.ClassicPaperSetBaseService; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
import java.util.*; |
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.Objects; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
@Service |
|
|
@Service |
|
|
@ -30,7 +28,7 @@ public class ClassicCategoryApplicationServiceImpl implements ClassicCategoryApp |
|
|
private ClassicPaperSetBaseService classicPaperSetBaseService; |
|
|
private ClassicPaperSetBaseService classicPaperSetBaseService; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public Result<List<ClassicCategoryDTO>> treeList() { |
|
|
public Result<List<ClassicCategoryDTO>> treeList(Boolean withPaperSet) { |
|
|
// 1. 查所有分类 → DTO
|
|
|
// 1. 查所有分类 → DTO
|
|
|
List<ClassicCategoryDTO> list = classicCategoryBaseService.list( |
|
|
List<ClassicCategoryDTO> list = classicCategoryBaseService.list( |
|
|
new LambdaQueryWrapper<ClassicCategoryEntity>().orderByAsc(ClassicCategoryEntity::getSort) |
|
|
new LambdaQueryWrapper<ClassicCategoryEntity>().orderByAsc(ClassicCategoryEntity::getSort) |
|
|
@ -43,12 +41,13 @@ public class ClassicCategoryApplicationServiceImpl implements ClassicCategoryApp |
|
|
ClassicCategoryDTO::getParentId, |
|
|
ClassicCategoryDTO::getParentId, |
|
|
ClassicCategoryDTO::setChildrenList); |
|
|
ClassicCategoryDTO::setChildrenList); |
|
|
|
|
|
|
|
|
// 3. 统计各分类下的套题组数量
|
|
|
// 3. 查询所有套题组(只查必要字段),同时用于统计数量和填充列表
|
|
|
List<ClassicPaperSetEntity> allSets = classicPaperSetBaseService.lambdaQuery() |
|
|
List<ClassicPaperSetEntity> allPaperSets = classicPaperSetBaseService.lambdaQuery() |
|
|
.select(ClassicPaperSetEntity::getClassicCategoryId) |
|
|
.select(ClassicPaperSetEntity::getClassicCategoryId,ClassicPaperSetEntity::getId, ClassicPaperSetEntity::getName) |
|
|
|
|
|
.orderByDesc(ClassicPaperSetEntity::getId) |
|
|
.list(); |
|
|
.list(); |
|
|
Map<Long, Integer> countMap = new HashMap<>(); |
|
|
Map<Long, Integer> countMap = new HashMap<>(); |
|
|
for (ClassicPaperSetEntity set : allSets) { |
|
|
for (ClassicPaperSetEntity set : allPaperSets) { |
|
|
Long catId = set.getClassicCategoryId(); |
|
|
Long catId = set.getClassicCategoryId(); |
|
|
if (catId != null) { |
|
|
if (catId != null) { |
|
|
countMap.merge(catId, 1, Integer::sum); |
|
|
countMap.merge(catId, 1, Integer::sum); |
|
|
@ -58,6 +57,17 @@ public class ClassicCategoryApplicationServiceImpl implements ClassicCategoryApp |
|
|
// 4. 注入 paperSetCount 到树节点
|
|
|
// 4. 注入 paperSetCount 到树节点
|
|
|
injectPaperSetCount(tree, countMap); |
|
|
injectPaperSetCount(tree, countMap); |
|
|
|
|
|
|
|
|
|
|
|
if (Boolean.TRUE.equals(withPaperSet)) { |
|
|
|
|
|
// 5. 按分类ID分组套题组
|
|
|
|
|
|
Map<Long, List<ClassicPaperSetDTO>> paperSetMap = allPaperSets.stream() |
|
|
|
|
|
.filter(s -> s.getClassicCategoryId() != null) |
|
|
|
|
|
.map(s -> s.toDTO(ClassicPaperSetDTO::new)) |
|
|
|
|
|
.collect(Collectors.groupingBy(ClassicPaperSetDTO::getClassicCategoryId)); |
|
|
|
|
|
|
|
|
|
|
|
// 6. 注入套题组到二级分类
|
|
|
|
|
|
injectPaperList(tree, paperSetMap); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return Result.success(tree); |
|
|
return Result.success(tree); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -81,6 +91,22 @@ public class ClassicCategoryApplicationServiceImpl implements ClassicCategoryApp |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 递归注入套题组列表: |
|
|
|
|
|
* 仅二级分类(level == 2)填充 childrenClassicPaperList |
|
|
|
|
|
*/ |
|
|
|
|
|
private void injectPaperList(List<ClassicCategoryDTO> nodes, Map<Long, List<ClassicPaperSetDTO>> paperSetMap) { |
|
|
|
|
|
for (ClassicCategoryDTO node : nodes) { |
|
|
|
|
|
if (node.getChildrenList() != null && !node.getChildrenList().isEmpty()) { |
|
|
|
|
|
// 一级分类:递归处理子节点
|
|
|
|
|
|
injectPaperList(node.getChildrenList(), paperSetMap); |
|
|
|
|
|
} else if (node.getLevel() != null && node.getLevel() == 2) { |
|
|
|
|
|
// 二级分类:填充套题组列表
|
|
|
|
|
|
node.setClassicPaperSetList(paperSetMap.getOrDefault(node.getId(), new ArrayList<>())); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public Result<ClassicCategoryDTO> save(ClassicCategoryDTO dto) throws Exception { |
|
|
public Result<ClassicCategoryDTO> save(ClassicCategoryDTO dto) throws Exception { |
|
|
if (StrUtil.isBlank(dto.getName())) { |
|
|
if (StrUtil.isBlank(dto.getName())) { |
|
|
|