|  |  | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  |  | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  | 
 |  |  |     @Override | 
 |  |  |     public List<SvyLibTemplateCategoryVO> selectSvyLibTemplateCategoryList(SvyLibTemplateCategory svyLibTemplateCategory) { | 
 |  |  |         List<SvyLibTemplateCategory> svyLibTemplateCategories = svyLibTemplateCategoryMapper.selectSvyLibTemplateCategoryList(svyLibTemplateCategory); | 
 |  |  |         //将pid为空的,过滤掉 | 
 |  |  |         svyLibTemplateCategories = svyLibTemplateCategories.stream().filter(assort -> assort.getPid() == null).collect(Collectors.toList()); | 
 |  |  |  | 
 |  |  |         List<SvyLibTemplateCategoryVO> svyLibTemplateCategoryVOList = DtoConversionUtils.sourceToTarget(svyLibTemplateCategories, SvyLibTemplateCategoryVO.class); | 
 |  |  |         //遍历查出来的数据,通过 id=pid 获取他们的子数据 | 
 |  |  |         for (SvyLibTemplateCategoryVO svyLibTemplateCategoryVO : svyLibTemplateCategoryVOList) { | 
 |  |  |             if (svyLibTemplateCategoryVO.getPid() != null) { | 
 |  |  |                 continue; | 
 |  |  |             } | 
 |  |  |             SvyLibTemplateCategory svyLibTemplateCategory1 = new SvyLibTemplateCategory(); | 
 |  |  |             svyLibTemplateCategory1.setPid(svyLibTemplateCategoryVO.getId()); | 
 |  |  |             List<SvyLibTemplateCategory> svyLibTemplateCategoryList = svyLibTemplateCategoryMapper.selectSvyLibTemplateCategoryList(svyLibTemplateCategory1); | 
 |  |  |             if (!Collections.isEmpty(svyLibTemplateCategoryList)) { | 
 |  |  |                 svyLibTemplateCategoryVO.setSvyLibTemplateCategoryList(svyLibTemplateCategoryList); | 
 |  |  |  | 
 |  |  |         List<SvyLibTemplateCategoryVO> rootList = new ArrayList<>(); | 
 |  |  |         Map<Long, SvyLibTemplateCategoryVO> categoryMap = new HashMap<>(); | 
 |  |  |  | 
 |  |  |         // 建立id->对象的映射 | 
 |  |  |         for (SvyLibTemplateCategoryVO category : svyLibTemplateCategoryVOList) { | 
 |  |  |             categoryMap.put(category.getId(), category); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         for (SvyLibTemplateCategoryVO category : svyLibTemplateCategoryVOList) { | 
 |  |  |             if (category.getPid() == null || category.getPid() == -1) { | 
 |  |  |                 // 顶层节点 | 
 |  |  |                 rootList.add(category); | 
 |  |  |             } else { | 
 |  |  |                 // 找到父节点,并添加到其子节点列表 | 
 |  |  |                 SvyLibTemplateCategoryVO parent = categoryMap.get(category.getPid()); | 
 |  |  |                 if (parent != null) { | 
 |  |  |                     SvyLibTemplateCategory sc = DtoConversionUtils.sourceToTarget(category, SvyLibTemplateCategory.class); | 
 |  |  |                     parent.getSvyLibTemplateCategoryList().add(sc); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return svyLibTemplateCategoryVOList; | 
 |  |  |         return rootList; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** |