package com.ssj.service.sys.organization.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.organization.domain.Organization;
import com.ssj.dao.sys.organization.dao.OrganizationDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.service.sys.organization.service.OrganizationService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/** 
 * @author  ZhangCaibao 
 * @version 2017年4月14日 下午2:43:54 
 * 类说明 
 */
@Service(version = "1.0")
@Transactional
public class OrganizationServiceImpl  extends BaseServiceImpl<Organization, String> implements OrganizationService {
  
	@Autowired
	private OrganizationDao organizationDao;
	
	@Override
	public PagingAndSortingRepository<Organization, String> getDao() {
		return organizationDao;
	}

	@Override
	public List<Organization> getSubNodesByPid(String pId,String code) {
		return organizationDao.getSubNodesByPid(pId,code);
	}

	@Override
	public Integer updateSortNo(String id, int sortNo) {
		return organizationDao.updateSortNo(id, sortNo);
	}

	@Override
	public void changeSubCount(String id, boolean increase) {
		Organization org = this.getById(id);
		if(org!=null){
			 int subCount = org.getSubCount();
			 if(increase){
				 subCount ++;
			 }else{
				 subCount --;
			 }
			 org.setSubCount(subCount);
			 this.save(org);
		}
		
	}

	@Override
	public List<Organization> getOrganizationByCode(String orgCode) {
		return organizationDao.findByCodeContaining(orgCode);
	}

	@Override
	public String getParentCodeByOrgCode(String orgCode) {
		return organizationDao.getParentCodeByOrgCode(orgCode);
	}

	@Override
	public List<Organization> findByPid(String pid) {
		return organizationDao.findByPid(pid);
	}

	@Override
	public String findByCode(String code) {
		return organizationDao.findByCode(code);
	}

	@Override
	public Page<Organization> findByPage(Map<String, Object> params, Pageable sp) {
		 Map<String, SearchFilter> filters = SearchFilter.parse(params);
			Specification<Organization> specification = DynamicSpecifications.bySearchFilter(filters.values(), Organization.class);
			return  organizationDao.findAll(specification, sp);
	}

	@Override
	public List<Organization> getOrganizationByAdminOrg(String adminId) {
		return organizationDao.getOrganizationByAdminOrg(adminId);
	}

	@Override
	public List<Organization> getPidAndCodes(String pid, List<String> codes) {
		return organizationDao.getPidAndCodes(pid, codes);
	}

	@Override
	public List<Organization> getPidAndCodes(List<String> codes) {
		return organizationDao.getPidAndCodes(codes);
	}

	@Override
	public List<Organization> getOrganizationByAdminOrg(String adminId,
			Integer level) {
		return organizationDao.getOrganizationByAdminOrg(adminId, level);
	}

	@Override
	public Organization getByCode(String code) {
		return organizationDao.getByCode(code);
	}


}
