package com.yihu.jw.org.dao;

import com.yihu.jw.entity.base.org.BaseOrgDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

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

/**
 * 
 * 机构信息 数据库访问层
 * 
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * litaohong 	1.0  		2018年08月31日
 *
 * </pre>
 * @since
 */
public interface BaseOrgDao extends PagingAndSortingRepository<BaseOrgDO, String>, JpaSpecificationExecutor<BaseOrgDO>  {

    @Query(value="select b.* from base_org b where b.code= ?1 and b.saasid= ?2 limit 1",nativeQuery = true)
    BaseOrgDO findByCodeAndSaasId(String code, String saasId);

    boolean existsByCode(String code);

    BaseOrgDO findByCode(String code);

    @Modifying
    @Query("delete from BaseOrgDO p where p.saasid=?1 ")
    void deleteBySaasId(String saasId);

    @Query("select id from BaseOrgDO where del = 1 and saasid = ?1")
    List findOrgCodeBySaasId(String saasId);

   /* @Query("select code as code,name as name from BaseOrgDO where del = 1 and saasid = ?1 ")
    List<Map<String,Object>> findOrgListBySaasId(String saasId);*/

    @Query("select code as code,name as name from BaseOrgDO where del = 1")
    List<Map<String,Object>> findOrgListBySaasId();

    @Query("select a from BaseOrgDO a where a.del = '1'")
    List<BaseOrgDO> findByAll();

    List<BaseOrgDO> findByCityCode(String cityCode);

    List<BaseOrgDO> findByTownCode(String townCode);

    List<BaseOrgDO> findByCityCodeAndOrgLevel(String cityCode,Integer level);

    List<BaseOrgDO> findByDel(String del);
    @Query(value="select b.* from base_org b where b.del=?1  and b.name like ?2 ORDER BY sort DESC",nativeQuery = true)
    List<BaseOrgDO> findByDelAndName(String del,String name);
    
    @Query(value="select b.* from base_org b where b.org_level= ?1 and b.del=?2  and b.name like ?3 ORDER BY sort DESC",nativeQuery = true)
    List<BaseOrgDO> findByLevelAndName(Integer level,String del,String name);
    
    @Query(value="select b.* from base_org b where b.org_level= ?1 and b.del=?2 ORDER BY sort DESC",nativeQuery = true)
    List<BaseOrgDO> findByLevelAndDel(Integer level,String del);

    @Query(value="select Distinct org from BaseOrgDO org,BaseDoctorHospitalDO h where org.code = h.orgCode and h.del=1 and org.del=1 and h.doctorCode=?1 and org.type=?2 " )
    List<BaseOrgDO> findByDoctorAndType(String doctor,String type);

    @Query(value="select org from BaseOrgDO org where org.del=1  and org.type in ?1 " )
    List<BaseOrgDO> findByTypeIn(String[] type);
}