package com.yihu.wlyy.statistics.dao;

import com.yihu.wlyy.statistics.model.team.AdminTeam;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 行政团队DAO。
 * 
 * @author Sand
 */
public interface DoctorAdminTeamDao extends
        PagingAndSortingRepository<AdminTeam, Long>,
        JpaSpecificationExecutor<AdminTeam> {
    /**
     * 团队内，全科医生的签约数。
     *
     * @param doctorCode
     * @param teamId
     * @return
     */
    @Query("SELECT count(s) FROM SignFamily s WHERE doctor = :doctorCode AND adminTeamId = :teamId")
    Integer getDoctorSignCount(@Param(value = "doctorCode") String doctorCode,
                               @Param(value = "teamId") long teamId);

    /**
     * 团队内，健康管理师的签约数。
     *
     * @param healthDoctorCode
     * @param teamId
     * @return
     */
    @Query("SELECT count(s) FROM SignFamily s WHERE doctorHealth = :healthDoctorCode AND adminTeamId = :teamId")
    Integer getHealthDoctorSignCount(@Param(value = "healthDoctorCode") String healthDoctorCode,
                                     @Param(value = "teamId") long teamId);


    @Query("SELECT t.id from AdminTeam t WHERE t.leaderCode = :leaderCode")
    Long findIdByLeaderCode(@Param("leaderCode") String leaderCode);


    @Query(" from AdminTeam t")
    List<AdminTeam> findAllTeam();

    @Query("SELECT t from AdminTeam t WHERE t.leaderCode = :leaderCode")
    AdminTeam findByLeaderCode(@Param("leaderCode") String leaderCode);
}
