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 TeamDoctorRepository extends JpaRepository<TeamDoctor, Integer> {

    /**
     * 判断团队ID中是否存在该医生ID
     *
     * create by daiyihua
     * @param teamId
     * @param doctorId
     * @return
     */
    @Query(value = "select count(1) from team_doctor where team_id = ? and doctor_id = ? and deleted = 0 limit 1 ;",nativeQuery = true)
    int checkTeamDoctor(int teamId, int doctorId);

    /**
     * 根据医生ID得到该医生已加入的团队
     *
     * create by daiyihua
     * @param teamId
     * @param doctorId
     * @return
     */
    @Query(value = "select * from team_doctor where team_id = ? and doctor_id = ? and deleted = 0 limit 1 ;",nativeQuery = true)
    TeamDoctor getTeamDoctor(int teamId, int doctorId);


    /**
     * 根据team_id和doctor_id得到相应的记录（被删除的记录）
     *
     * create by daiyihua
     * @param teamId
     * @param doctorId
     * @return
     */
    @Query(value = "select * from team_doctor where team_id = ? and doctor_id = ? and deleted = 1 order by create_time desc limit 1 ;",nativeQuery = true)
    TeamDoctor getTeamDoctorInfo(int teamId, int doctorId);

    /**
     * 根据医生ID得到该医生已加入的团队
     *
     * create by daiyihua
     * @param doctorId
     * @return
     */
    @Query(value = "select * from team_doctor where doctor_id = ? and deleted = 0 and status = 1 ;", nativeQuery = true)
    List<TeamDoctor> getMyTeam(int doctorId);

    /**
     * 根据团队ID得到该团队中已加入和待加入的成员
     *
     * create by daiyihua
     * @param teamId
     * @return
     */
    @Query(value = "select * from team_doctor where team_id = ? and deleted = 0 and status != 2 ;", nativeQuery = true)
    List<TeamDoctor> getTeamDoctorByTeamId(int teamId);


    /**
     * 根据团队ID得到该团队添加过的所有人员
     *
     * create by daiyihua
     * @param teamId
     * @return
     */
    @Query(value = "select * from team_doctor where team_id = ? and deleted = 0 ;", nativeQuery = true)
    List<TeamDoctor> getAllTeamDoctorByTeamId(int teamId);

    @Query(value = "select doctor_id from team_doctor where team_id = ? and deleted = 0 and status = 1 ;", nativeQuery = true)
    List<Integer> getAllTeamDoctorIdByTeamId(int teamId);

    /**
     * 根据医生ID得到该医生创建和加入的团队总数量
     *
     * crate by daiyihua
     * @param doctorId
     * @return
     */
    @Query(value = "select count(team_id) from team_doctor where doctor_id = ? and deleted = 0 and status = 1;", nativeQuery = true)
    int getMyTeamCount(int doctorId);

    /**
     * 根据团队id获取所有已加入的医生
     * @param teamId
     * @return
     */
    @Query(value = "select * from team_doctor where team_id = ? and deleted = 0 and status = 1;", nativeQuery = true)
    List<TeamDoctor> getTeamDoctorsByTeamId(int teamId);
}
