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

import com.ym.mec.biz.dal.dao.SysAreaDao;
import com.ym.mec.biz.dal.dto.TreeDto;
import com.ym.mec.biz.dal.entity.Organization;
import com.ym.mec.biz.dal.entity.SysArea;
import com.ym.mec.biz.dal.page.OrganizationQueryInfo;
import com.ym.mec.biz.service.SysAreaService;
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 SysAreaServiceImpl extends BaseServiceImpl<Integer, SysArea> implements SysAreaService {
	
	@Autowired
	private SysAreaDao sysAreaDao;

	@Override
	public BaseDAO<Integer, SysArea> getDAO() {
		return sysAreaDao;
	}

	@Override
	public List<SysArea> queryTreePage(TreeDto treeDto) {
		List<SysArea> sysAreas = sysAreaDao.findRootList(treeDto.getParentId(),treeDto.getDelFlag());
		for (SysArea sysArea : sysAreas) {
			sysArea = getTree(sysArea);
		}
		return sysAreas;
	}

	@Override
	public List<SysArea> queryChild(TreeDto treeDto) {
		return sysAreaDao.findRootList(treeDto.getParentId(),treeDto.getDelFlag());
	}

	@Override
	public SysArea getParentArea(Integer id) {
		return sysAreaDao.getParentArea(id);
	}

	private SysArea getTree(SysArea area){
		//得到根节点对象
		//获取子节点list
		List<SysArea> sysAreas = sysAreaDao.findRootList(area.getId(), area.getDelFlag());
		//如果存在子节点
		if(sysAreas != null && sysAreas.size() > 0) {
			//将子节点list放入父节点对象
			area.setAreas(sysAreas);
			//遍历子节点....
			for (SysArea sysArea : sysAreas) {
				getTree(sysArea);
			}
		}
		return area;
	}
}