package com.ssj.dao.sys.organization.dao;

import com.ssj.bean.sys.organization.domain.Organization;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/** 
 * @author  ZhangCaibao 
 * @version 2017年4月14日 下午2:45:35 
 * 类说明 
 */
@Repository
public interface OrganizationDao extends JpaRepository<Organization, String>{

	 @Query(nativeQuery = true, value = "select  *  from sys_organization  where p_id =?1 and code   LIKE  CONCAT(?2,'%')  order by  sort_no desc ")
	 List<Organization>  getSubNodesByPid(String pId, String code);
	 
	 
	 @Query(nativeQuery = true, value = "select  *  from sys_organization  where p_id =?1 and code  in(?2)  order by  sort_no desc ")
	 List<Organization>  getPidAndCodes(String pid, List<String> codes);
	 
	 @Query(nativeQuery = true, value = "select  *  from sys_organization  where  code   in(?1)  order by  sort_no desc ")
	 List<Organization>  getPidAndCodes(List<String> codes);
	 
	 
	 @Query(nativeQuery = true, value = "update sys_organization set sort_no = ?2 where id =?1 ")
	 @Modifying
	 Integer updateSortNo(String id, int sortNo);
	  
	  public  List<Organization>  findByCodeContaining(String orgCode);
	  
	  @Query(nativeQuery = true, value = "SELECT  t.`code`  from  sys_organization t  JOIN  sys_organization s  \n" +
			  "on  t.id = s.p_id \n" +
			  "where  s.`code` = ?1 ")
	  public  String getParentCodeByOrgCode(String orgCode);
	  @Query(nativeQuery = true, value = "select  *  from sys_organization  where p_id =?1  order by  sort_no desc ")
	  public   List<Organization>  findByPid(String pid);
	  
	  @Query(nativeQuery = true, value = "select  p_id  from sys_organization  where code=?1 ")
	  String findByCode(String code);
	  
	  @Query(nativeQuery = true, value = "select *  from sys_organization  where code=?1 ")
	  Organization getByCode(String code);
	  
	  
	  public Page<Organization> findAll(Specification<Organization> specification, Pageable page);
	  
	  @Query(nativeQuery = true, value = "SELECT  o.* from  sys_organization o JOIN  sys_admin_org g\n" +
			  "on o.id  =g.org_id \n" +
			  "where g.admin_id =?1")
	  List<Organization>  getOrganizationByAdminOrg(String adminId);
	  @Query(nativeQuery = true, value = "SELECT  o.* from  sys_organization o JOIN  sys_admin_org g\n" +
			  "on o.id  =g.org_id \n" +
			  "where g.admin_id =?1 and o.org_level=?2")
	  List<Organization>  getOrganizationByAdminOrg(String adminId, Integer level);
}
