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

import com.ym.mec.auth.api.dto.MenuQueryInfo;
import com.ym.mec.biz.dal.dao.ResourceRespositoryDao;
import com.ym.mec.biz.dal.entity.ResourceRespository;
import com.ym.mec.biz.service.ResourceRespositoryService;
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;

@Service
public class ResourceRespositoryServiceImpl extends BaseServiceImpl<Integer, ResourceRespository> implements ResourceRespositoryService{

	@Autowired
	private ResourceRespositoryDao resourceRespositoryDao;
	
	@Override
	public BaseDAO<Integer, ResourceRespository> getDAO() {
		return resourceRespositoryDao;
	}

	@Override
	public List<ResourceRespository> queryTree(MenuQueryInfo menuQueryInfo) {
		if(menuQueryInfo.getSubjectId() != null){
			Integer subjectId = menuQueryInfo.getSubjectId();
			menuQueryInfo.setSubjectId(subjectId == 5?6:subjectId == 23?18:subjectId == 3?2:subjectId == 16?15:subjectId == 20?18:subjectId == 50?18:subjectId == 53?18
					:subjectId == 54?18:subjectId == 55?18:subjectId == 56?18:subjectId == 60?18:subjectId == 8?4:subjectId);
		}
		List<ResourceRespository> resourceRespositories = resourceRespositoryDao.findList(menuQueryInfo.getParentId(),menuQueryInfo.getDelFlag(),menuQueryInfo.getSubjectId());
		for (ResourceRespository repository:resourceRespositories) {
			repository = getTree(repository,menuQueryInfo.getDelFlag(),menuQueryInfo.getSubjectId());
		}
		return resourceRespositories;
	}

	private ResourceRespository getTree(ResourceRespository repository, String delFlag,Integer subjectId){
		//得到根节点对象
		//获取子节点list
		List<ResourceRespository> repositories = null;
		repositories = resourceRespositoryDao.findList(repository.getId(),delFlag,subjectId);
		//如果存在子节点
		if(repositories != null && repositories.size() > 0) {
			//将子节点list放入父节点对象
			repository.setRepositories(repositories);
			//遍历子节点....
			for (ResourceRespository sysMenu : repositories) {
				getTree(sysMenu,delFlag,subjectId);
			}
		}
		return repository;
	}
}