package com.ywt.outpatient.domain.entity.center;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;


public interface DoctorScheduleRepository extends JpaRepository<DoctorSchedule, Integer> {

    /**
     * 根据医生ID得到排班信息
     * @param doctorId
     * @return
     */
    @Query(value = "select * from doctor_schedule where doctor_id = ? and deleted = 0",nativeQuery = true)
    List<DoctorSchedule> getDoctorScheduleByDoctorId(int doctorId);


    /**
     * 根据医生ID得到排班信息
     * @param doctorId
     * @param date
     * @return
     */
    @Query(value = "select * from doctor_schedule where doctor_id = ? and deleted = 0 and clinic_time >= ? order by clinic_time ;",nativeQuery = true)
    List<DoctorSchedule> getDoctorScheduleByDoctorId(int doctorId, String date);


    /**
     * 根据医生ID得到最近三个月的排班信息
     * @param doctorId
     * @param startDate
     * @param endDate
     * @return
     */
    @Query(value = "select * from doctor_schedule where doctor_id = ? and deleted = 0 and clinic_time >= ? and clinic_time <= ? order by clinic_time ; ",nativeQuery = true)
    List<DoctorSchedule> getLatestSchedule(int doctorId, String startDate, String endDate);

    /**
     * 根据条件得到对象
     * @param doctorId
     * @param clinicTime
     * @param morning
     * @param afternoon
     * @param night
     * @return
     */
    @Query(value = "select * from doctor_schedule where doctor_id = ? and deleted = 0 and clinic_time = ? and morning = ? and  afternoon = ? and night = ? limit 1 ; ",nativeQuery = true)
    DoctorSchedule findSchedule(int doctorId, String clinicTime, int morning, int afternoon, int night);

    /**
     *
     * @param doctorId
     * @param type
     */
    @Query(value = "select * from doctor_schedule where doctor_id = ? and deleted = 0 and type = ? ; ",nativeQuery = true)
    List<DoctorSchedule> getDoctorScheduleByDoctorId(int doctorId, int type);

    /**
     * 根据医生ID查看医生时间最大的排班
     * create by daiyihua
     * @param doctorId
     * @return
     */
    @Query(value = "select * from doctor_schedule where doctor_id = ? and clinic_time >= DATE_FORMAT(now(),'%Y-%m-%d 00:00:00') and deleted = 0 order by clinic_time desc limit 3 ; ",nativeQuery = true)
    List<DoctorSchedule> getThatDayDoctorScheduleList(int doctorId);

    /**
     * 得到医生今天之后的排班
     * @param doctorId
     * @return
     */
    @Query(value = "select count(1) from doctor_schedule where doctor_id = ? and clinic_time > now() and deleted = 0  ",nativeQuery = true)
    int  getCount(int doctorId);

    /**
     * 根据医生ID和时间得到上午的排班数量
     * @param doctorId
     * @param dateTime
     * @return
     */
    @Query(value = "select count(1) from doctor_schedule where doctor_id = ? and clinic_time = ? and morning = 1 and deleted = 0  ",nativeQuery = true)
    int  getMorningCount(int doctorId, String dateTime);

    /**
     * 根据医生ID和时间得到下午的排班数量
     * @param doctorId
     * @param dateTime
     * @return
     */
    @Query(value = "select count(1) from doctor_schedule where doctor_id = ? and clinic_time = ? and afternoon = 1 and deleted = 0  ",nativeQuery = true)
    int  getAfternoonCount(int doctorId, String dateTime);

    /**
     * 根据医生ID和时间得到晚上的排班数量
     * @param doctorId
     * @param dateTime
     * @return
     */
    @Query(value = "select count(1) from doctor_schedule where doctor_id = ? and clinic_time = ? and night = 1 and deleted = 0  ",nativeQuery = true)
    int  getNightCount(int doctorId, String dateTime);

}
