package com.blueland.bpm.identity.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;

import com.blueland.bpm.identity.OrgService;
import com.blueland.bpm.identity.dao.OrgQueryDao;
import com.blueland.bpm.identity.model.Org;
import com.blueland.bpm.identity.util.IdentityException;
import com.blueland.bpm.identity.util.OrgServiceImplCondition;

@Service("orgServiceImpl")
@Conditional(OrgServiceImplCondition.class)
public class OrgServiceImpl implements OrgService{
	private static final Log logger = LogFactory.getLog(OrgServiceImpl.class);
	@Autowired
	private OrgQueryDao orgQueryDao;
	
	@Override
	public Org getOrgById(String orgId) {
		Org org=null;
		try{
			org=orgQueryDao.getById(orgId);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return org;
	}

	@Override
	public List<Org> findSubOrg(String orgId) {
		List<Org> orgs=null;
		try{
			Org param=new Org();
			param.setOrgParentId(orgId);
			orgs=orgQueryDao.findByExample(param);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return orgs;
	}

	@Override
	public List<Org> findSubOrgTree(String orgId) {
		List<Org> orgs=null;
		try{
			orgs=orgQueryDao.findSubOrgTree(orgId);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return orgs;
	}

	@Override
	public List<Org> findParentOrgTree(String orgId) {
		return findParentOrgTree(orgId,false);
	}
	
	@Override
	public List<Org> findParentOrgTree(String orgId,boolean isIncludeSelf) {
		List<Org> orgs=null;
		try{
			Org param=new Org();
			param.setOrgId(orgId);
			orgs=orgQueryDao.findParentOrgTree(param, isIncludeSelf);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return orgs;
	}

	@Override
	public List<Org> findOrgByEntityCriteria(Org paramEntity) {
		List<Org> orgs=null;
		try{
			orgs=orgQueryDao.findByExample(paramEntity);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return orgs;
	}

	@Override
	public boolean hasSubOrgs(String orgId) {
		boolean hasSubOrgsFlag=false;
		try{
			Org org =new Org();
			org.setOrgParentId(orgId);
			hasSubOrgsFlag=orgQueryDao.findEntityCount(org)>0;
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return hasSubOrgsFlag;
	}

	@Override
	public boolean isInOrg(String parentOrgId, String subOrgId) {
		boolean isInOrg=false;
		try{
			Org params =new Org();
			params.setOrgParentId(parentOrgId);
			params.setOrgId(subOrgId);
			Object org=orgQueryDao.getSqlSessionTemplate().selectOne(orgQueryDao.getIbatisMapperNamesapce()+".isInOrg",params);
			System.out.println(org);
			isInOrg=(org!=null);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return isInOrg;
	}

	@Override
	public List<Org> findAllCompany() {
		List<Org> orgs=null;
		try{
			Org org=new Org();
			org.setOrgType("UNIT");
			orgs=orgQueryDao.findByExample(org);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return orgs;
	}

	@Override
	public List<Org> findAllDept() {
		List<Org> orgs=null;
		try{
			Org org=new Org();
			org.setOrgType("DEPT");
			orgs=orgQueryDao.findByExample(org);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return orgs;
	}

	@Override
	public List<Org> findParentOrgTree(String orgId, Integer orgLevel) {
		List<Org> orgs=null;
		try{
			Org params=new Org();
			params.setOrgId(orgId);
			params.setOrgLevel(orgLevel);
			orgs=orgQueryDao.findParentOrgTree(params,false);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return orgs;
	}

	@Override
	public List<Org> findParentOrgTree(Org params) {
		List<Org> orgs=null;
		try{
			orgs=orgQueryDao.findParentOrgTree(params,false);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return orgs;
	}
/*
	@Override
	public List<Org> findOrgByOrgIds(List<String> orgIds) {
		List<Org> orgs=null;
		try{
			orgs=orgQueryDao.findOrgByIds(orgIds);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return orgs;
	}
	*/
	
	@Override
	public List<Org> findOrgByOrgIds(List<String> orgIds) {
		List<Org> orgs=null;
		try{
			orgs=orgQueryDao.findOrgByIds(orgIds);
		}catch(Exception e){
			logger.error(e);
			throw new IdentityException(e);
		}
		return orgs;
	}

	@Override
	public String getFullPathName(String orgId,String pjStr) {
		String result = "";
		try {
			List<Org> orgs=orgQueryDao.getSqlSessionTemplate().selectList(orgQueryDao.getIbatisMapperNamesapce()+".findFullPath",orgId);
			for(int i=orgs.size()-1; i>=0; i--) {
				Org org = orgs.get(i);
				if(org.getOrgLevel() >=1)
					result += org.getOrgName() + pjStr;
			}
			if(StringUtils.isNotEmpty(result)){
				result=result.substring(0,result.lastIndexOf(pjStr));
			}				
		}catch(Exception e) {
			logger.error(e);
			throw new IdentityException(e);
		}
		return result;
	}

	@Override
	public List<Org> listRootOrgNodes() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Org queryProvincialOrgNode(String orgBizCode, String orgUnitId) {
		// TODO Auto-generated method stub
		return null;
	}
}
