Commit a9152dbd by zhangxingmin

push

parent 149c9d8a
......@@ -25,10 +25,8 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
......@@ -207,23 +205,49 @@ public class ApiQuestionnairesServiceImpl implements ApiQuestionnairesService {
ApiFirstAndSecondCategoryDto firstCategoryDto = new ApiFirstAndSecondCategoryDto();
firstCategoryDto.setFirstCategory(entry.getKey());
// 对当前一级分类下的数据进行二级分类分组
// 对当前一级分类下的数据进行二级分类分组,并按relQuestionnaireQuestionsSortOrder升序排序
Map<String, List<ApiRelQuestionsGroupDto>> secondCategoryMap = entry.getValue().stream()
// 过滤掉secondCategory为null的记录
.filter(dto -> dto.getSecondCategory() != null)
.collect(Collectors.groupingBy(ApiRelQuestionsGroupDto::getSecondCategory));
// 收集到LinkedHashMap中保持排序
.collect(Collectors.groupingBy(
ApiRelQuestionsGroupDto::getSecondCategory,
// 使用LinkedHashMap来保持插入顺序
LinkedHashMap::new,
// 对每个分组内的数据按照relQuestionnaireQuestionsSortOrder升序排序
Collectors.collectingAndThen(
Collectors.toList(),
list -> list.stream()
.sorted(Comparator.comparing(ApiRelQuestionsGroupDto::getRelQuestionnaireQuestionsSortOrder))
.collect(Collectors.toList())
)
));
// 然后对Map的key(二级分类)按照其下最小relQuestionnaireQuestionsSortOrder进行排序
List<Map.Entry<String, List<ApiRelQuestionsGroupDto>>> sortedEntries = secondCategoryMap.entrySet().stream()
.sorted(Comparator.comparing(entry2 ->
entry2.getValue().stream()
.mapToInt(ApiRelQuestionsGroupDto::getRelQuestionnaireQuestionsSortOrder)
.min()
.orElse(Integer.MAX_VALUE)
))
.collect(Collectors.toList());
// 重新构建排序后的Map
Map<String, List<ApiRelQuestionsGroupDto>> sortedSecondCategoryMap = new LinkedHashMap<>();
for (Map.Entry<String, List<ApiRelQuestionsGroupDto>> sortedEntry : sortedEntries) {
sortedSecondCategoryMap.put(sortedEntry.getKey(), sortedEntry.getValue());
}
// 转换为二级分类DTO列表
List<ApiSecondCategoryDto> secondCategoryDtoList = secondCategoryMap.entrySet().stream()
List<ApiSecondCategoryDto> secondCategoryDtoList = sortedSecondCategoryMap.entrySet().stream()
.map(secondEntry -> {
ApiSecondCategoryDto secondCategoryDto = new ApiSecondCategoryDto();
secondCategoryDto.setSecondCategory(secondEntry.getKey());
// 设置问题列表
// 设置问题列表(已经按relQuestionnaireQuestionsSortOrder排序过)
List<ApiRelQuestionsGroupDto> questionsDtoList = secondEntry.getValue();
if (!CollectionUtils.isEmpty(questionsDtoList)) {
//设置问题列表
secondCategoryDto.setQuestionsDtoList(questionsDtoList(questionsDtoList,questionnaireBizId,objectBizId));
}
return secondCategoryDto;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment