package com.yihu.wlyy.repository.doctor;

import com.yihu.wlyy.entity.doctor.team.admin.AdminTeam;
import com.yihu.wlyy.entity.patient.Patient;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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 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);

    @Modifying
    @Query("UPDATE AdminTeam t SET t.available = false where t.id = :teamId")
    void delete(@Param("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);

    /**
     * 团队内，健康管理师的签约患者。
     *
     * @param healthDoctorCode
     * @param pageable
     * @return
     */
    @Query("SELECT p FROM SignFamily s, Patient p WHERE s.doctorHealth = :healthDoctorCode AND s.patient = p.code and s.status >0 AND" +
            " s.adminTeamId = :teamId AND s.type = 2")
    Page<Patient> getHealthDoctorSigningPatients(@Param(value = "healthDoctorCode") String healthDoctorCode,
                                                 @Param(value = "teamId") long teamId,
                                                 Pageable pageable);

    /**
     * 团队内，健康管理师的签约患者。
     *
     * @param healthDoctorCode
     * @return
     */
    @Query("SELECT p FROM SignFamily s, Patient p WHERE s.doctorHealth = :healthDoctorCode AND s.patient = p.code and s.status >0 AND" +
            " s.adminTeamId = :teamId AND s.type = 2")
    List<Patient> getHealthDoctorSigningPatients(@Param(value = "healthDoctorCode") String healthDoctorCode,
                                                 @Param(value = "teamId") long teamId);

    /**
     * 通过患者约团队取得行政团队。
     *
     * @param patientCode
     * @return
     */
    @Query("SELECT t.id, t.name, t.leaderCode, f.doctor, f.doctorName, f.doctorHealth, f.doctorHealthName " +
            "FROM SignFamily f, AdminTeam t WHERE f.patient = :patientCode AND f.status in (-4, 1) AND " +
            "t.id=f.adminTeamId AND t.available = true ORDER BY f.czrq DESC")
    List<Object> findAdminTeamByPatientSignTeam(@Param("patientCode") String patientCode, Pageable pageable);

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

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

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

    List<AdminTeam> findByOrgCode(String orgCode);

    //  查询当前医生是否为团队长
    @Query("SELECT t.id from AdminTeam t WHERE t.available = true  AND t.leaderCode = ?1 ")
    Integer findLeader(String leaderCode);

}
