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

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.dao.OrganizationDao;
import com.ym.mec.biz.dal.entity.Organization;
import com.ym.mec.biz.dal.page.OrganizationQueryInfo;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;

@Service
public class OrganizationServiceImpl extends BaseServiceImpl<Integer, Organization> implements OrganizationService {
	
	@Autowired
	private OrganizationDao organizationDao;

	@Override
	public BaseDAO<Integer, Organization> getDAO() {
		return organizationDao;
	}

	@Override
	public PageInfo<Organization> queryTreePage(OrganizationQueryInfo queryInfo){
		PageInfo<Organization> pageInfo = queryPage(queryInfo);
		for (Organization organization:pageInfo.getRows()) {
			organization = getTree(organization);
		}
		return pageInfo;
	}



	private Organization getTree(Organization org){
		//得到根节点对象
		//获取子节点list
		List<Organization> byParentId = organizationDao.findByParentId(org.getId(), null);
		//如果存在子节点
		if(byParentId != null && byParentId.size() > 0) {
			//将子节点list放入父节点对象
			org.setOrganizations(byParentId);
			//遍历子节点....
			for (Organization organization : byParentId) {
				getTree(organization);
			}
		}
			return org;
	}
}