| | |
| | | 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<SvyCategoryVO> selectSvyCategoryList(SvyCategory svyCategory) { |
| | | List<SvyCategory> svyCategories = svyCategoryMapper.selectSvyCategoryList(svyCategory); |
| | | //将pid为空的,过滤掉 |
| | | // svyCategories = svyCategories.stream().filter(assort -> assort.getPid() == null).collect(Collectors.toList()); |
| | | |
| | | List<SvyCategoryVO> svyCategoryVOS = DtoConversionUtils.sourceToTarget(svyCategories, SvyCategoryVO.class); |
| | | //遍历查出来的数据,通过 id=pid 获取他们的子数据 |
| | | for (SvyCategoryVO svyCategoryVO : svyCategoryVOS) { |
| | | // if (svyCategoryVO.getPid() != null) { |
| | | // continue; |
| | | // } |
| | | SvyCategory svyCategory1 = new SvyCategory(); |
| | | svyCategory1.setPid(svyCategoryVO.getId()); |
| | | List<SvyCategory> svyCategoryList = svyCategoryMapper.selectSvyCategoryList(svyCategory1); |
| | | if (!Collections.isEmpty(svyCategoryList)) { |
| | | svyCategoryVO.setSvyCategoryList(svyCategoryList); |
| | | |
| | | List<SvyCategoryVO> rootList = new ArrayList<>(); |
| | | Map<Long, SvyCategoryVO> categoryMap = new HashMap<>(); |
| | | |
| | | // 建立id->对象的映射 |
| | | for (SvyCategoryVO category : svyCategoryVOS) { |
| | | categoryMap.put(category.getId(), category); |
| | | } |
| | | |
| | | for (SvyCategoryVO category : svyCategoryVOS) { |
| | | if (category.getPid() == null || category.getPid() == -1) { |
| | | // 顶层节点 |
| | | rootList.add(category); |
| | | } else { |
| | | // 找到父节点,并添加到其子节点列表 |
| | | SvyCategoryVO parent = categoryMap.get(category.getPid()); |
| | | if (parent != null) { |
| | | SvyCategory sc = DtoConversionUtils.sourceToTarget(category, SvyCategory.class); |
| | | parent.getSvyCategoryList().add(sc); |
| | | } |
| | | } |
| | | return svyCategoryVOS; |
| | | } |
| | | return rootList; |
| | | } |
| | | |
| | | /** |