package com.sqx.modules.firstLogin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.firstLogin.dao.PostSkillDao;
import com.sqx.modules.firstLogin.entity.PostSkill;
import com.sqx.modules.firstLogin.service.PostSkillService;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PostSkillImpl extends ServiceImpl<PostSkillDao,PostSkill> implements PostSkillService {
    @Override
    public List<PostSkill> getPostSkill(PostSkill postSkill) {
        return getPostSkillTree(baseMapper.selectList(new QueryWrapper<>(postSkill)));
    }
    private List<PostSkill> getPostSkillTree(List<PostSkill> records) {
        return records.stream()
                .filter(e -> e.getParentId() == 0)
                .peek((menu) -> menu.setChildrenList(getChildrenList(menu, records)))
                .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());
    }
    private List<PostSkill> getChildrenList(PostSkill root, List<PostSkill> all) {
        return all.stream().filter(categoryEntity -> categoryEntity.getParentId().equals(root.getPostSkillId())).peek(categoryEntity -> {
            //1、找到子菜单(递归)
            categoryEntity.setChildrenList(getChildrenList(categoryEntity, all));
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
    }
}
