package com.ym.mec.biz.service.impl;


import com.ym.mec.auth.api.dto.MusicScoreQueryInfo;
import com.ym.mec.auth.api.entity.SysMenu;
import com.ym.mec.biz.dal.dao.SysMusicScoreCategoriesDao;
import com.ym.mec.biz.dal.entity.SysMusicScoreCategories;
import com.ym.mec.biz.service.SysMusicScoreCategoriesService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SysMusicScoreCategoriesServiceImpl extends BaseServiceImpl<Integer, SysMusicScoreCategories> implements SysMusicScoreCategoriesService {
	
	@Autowired
	private SysMusicScoreCategoriesDao sysMusicScoreCategoriesDao;

	@Override
	public BaseDAO<Integer, SysMusicScoreCategories> getDAO() {
		return sysMusicScoreCategoriesDao;
	}


	@Override
	public List<SysMusicScoreCategories> queryTree(MusicScoreQueryInfo menuQueryInfo) {
		List<SysMusicScoreCategories> scoreCategories = sysMusicScoreCategoriesDao.findByParentId(menuQueryInfo.getParentId());
		List<SysMusicScoreCategories> scoreCategoriesAllList = sysMusicScoreCategoriesDao.findByParentId(null);
		for (SysMusicScoreCategories categories : scoreCategories) {
			categories = getTree(categories,scoreCategoriesAllList);
		}
		return scoreCategories;
	}

	private SysMusicScoreCategories getTree(SysMusicScoreCategories categories,List<SysMusicScoreCategories> scoreCategoriesAllList){
		//得到根节点对象
		//获取子节点list
		List<SysMusicScoreCategories> scoreCategories = scoreCategoriesAllList.stream().filter(e->e.getParentId().equals(categories.getId())).collect(Collectors.toList());
		//如果存在子节点
		if(scoreCategories != null && scoreCategories.size() > 0) {
			//将子节点list放入父节点对象
			categories.setSysMusicScoreCategoriesList(scoreCategories);
			//遍历子节点....
			for (SysMusicScoreCategories sysMenu : scoreCategories) {
				getTree(sysMenu,scoreCategoriesAllList);
			}
		}
		return categories;
	}
}