package com.yihu.jw.doctor.dao;

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

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

public interface BaseDoctorHospitalDao extends PagingAndSortingRepository<BaseDoctorHospitalDO, Integer>, JpaSpecificationExecutor<BaseDoctorHospitalDO> {

    List<BaseDoctorHospitalDO> findByOrgCodeAndDoctorCode(String orgCode, String doctorCode);

    List<BaseDoctorHospitalDO> findByDeptCodeAndDoctorCode(String deptCode, String doctorCode);

    @Query("select id from BaseDoctorHospitalDO where doctorCode = ?1")
    Set<Object> findIdListByOrgCodeAndDoctorCode(String doctorCode);

    @Query(value = "select org_code as orgCode,org_name as orgName,doctor_duty_code as doctorDutyCode ,doctor_duty_name as doctorDutyName from base_doctor_hospital where doctor_code = ?1 GROUP BY orgCode ORDER BY orgName DESC",nativeQuery = true)
    List<Map<String,Object>> getOrgAndDutyByDoctorCode(String doctorCode);

    @Query("select bdo from BaseDoctorHospitalDO bdo where bdo.doctorCode = ?1 and bdo.del=1")
    List<BaseDoctorHospitalDO> findByDoctorCode(String doctorCode);

    @Query("select bdo from BaseDoctorHospitalDO bdo where bdo.doctorCode = ?1 and bdo.del=1 group by bdo.orgCode")
    List<BaseDoctorHospitalDO> findDistinctOrgByDoctorCode(String doctorCode);

    List<BaseDoctorHospitalDO> findByOrgCode(String orgCode);


    List<BaseDoctorHospitalDO> findByOrgCodeAndDeptCode(String orgCode,String deptCode);

    @Query("select bdo from BaseDoctorHospitalDO bdo where bdo.orgCode=?1 and bdo.deptCode=?2 and bdo.doctorCode = ?3 and bdo.del=1")
    List<BaseDoctorHospitalDO> findByOrgCodeAndDeptCodeAndDoctorCode(String orgCode,String deptCode,String doctorCode);

    @Query("select bdo from BaseDoctorHospitalDO bdo where bdo.orgCode=?1 and bdo.deptCode=?2 and bdo.doctorCode = ?3 and bdo.del=1")
    BaseDoctorHospitalDO findByOrgCodeAndDeptCodeAndDoctorCodeOnly(String orgCode,String deptCode,String doctorCode);


    List<BaseDoctorHospitalDO> findByOrgCodeAndDeptCodeAndDel(String orgCode,String deptCode,String del);


    @Query("select bdo from BaseDoctorHospitalDO bdo where bdo.deptCode = ?1 and bdo.doctorCode=?2 and bdo.del=1")
    List<BaseDoctorHospitalDO> findByDeptCodeDoctorCode(String dept,String doctorCode);

}
