package com.yihu.wlyy.repository.doctor;

import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.entity.doctor.team.admin.AdminTeam;
import com.yihu.wlyy.entity.doctor.team.admin.AdminTeamMember;
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 DoctorAdminTeamMemberDao extends
        PagingAndSortingRepository<AdminTeamMember, Long>,
        JpaSpecificationExecutor<AdminTeamMember> {

    AdminTeamMember findByTeamIdAndDoctorCodeOrderByDoctorCodeAsc(long teamId, String doctorCode);

    @Query("SELECT t FROM AdminTeamMember m, AdminTeam t WHERE m.doctorCode = :doctorCode " +
            "AND t.id = m.teamId AND t.available = true AND m.available = true")
    List<AdminTeam> findDoctorTeams(@Param(value = "doctorCode") String doctorCode);

    @Query("SELECT d FROM AdminTeamMember m, AdminTeam t, Doctor d WHERE t.id = :teamId AND t.id = m.teamId " +
            "AND m.doctorCode = d.code AND t.available = true AND m.available = true ORDER BY d.level")
    List<Doctor> findAllMembers(@Param(value = "teamId") long teamId);

    @Query("SELECT case when count(A) > 0 then true else false END FROM AdminTeamMember A WHERE A.teamId = :teamId " +
            "AND A.doctorCode = :doctorCode AND A.available = true")
    Boolean isMemberExist(@Param(value = "teamId") long teamId, @Param(value = "doctorCode") String doctorCode);

    @Query("SELECT d from Hospital h, Doctor d WHERE d.name like :doctorName " +
            "AND h.code = d.hospital AND h.code = :orgCode AND d.code not in " +
            "(select t.doctorCode from AdminTeamMember t WHERE t.teamId = :teamId)")
    Page<Doctor> findTeamExcludedMembers(@Param(value = "orgCode") String orgCode,
                                         @Param(value = "doctorName") String doctorName,
                                         @Param(value = "teamId") long teamId,
                                         Pageable pageable);

    @Modifying
    @Query("UPDATE AdminTeamMember t SET t.available = false where t.id = :teamId and doctorCode = :doctorCode")
    void removeMember(@Param("teamId") long teamId, @Param("doctorCode") String doctorCode);

    @Query("SELECT d FROM AdminTeamMember m, AdminTeam t, Doctor d WHERE t.id = ?1 AND t.id = m.teamId AND " +
            "m.doctorCode = d.code and  d.code != ?2 AND t.available = true and d.level != 1 ")
    List<Doctor> findAllHeathExcludeThis(long teamId, String doctorCode);

    /**
     * 判断该医生是否属于患者的签约行政团队
     */
    @Query("select m from AdminTeamMember m,SignFamily s where m.teamId = s.adminTeamId and s.status >0 and s.patient=?1 and m.doctorCode =?2")
    List<AdminTeamMember> findByAdminTeam(String patient,String doctorCode);


}
