package com.yihu.wlyy.repository.doctor;

import com.yihu.wlyy.entity.doctor.team.sign.DoctorTeamMember;
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;
import java.util.function.BooleanSupplier;

/**
 * Created by Administrator on 2016/7/30.
 */
public interface DoctorTeamMemberDao extends PagingAndSortingRepository<DoctorTeamMember, Long>, JpaSpecificationExecutor<DoctorTeamMember> {

    @Query(" FROM DoctorTeamMember a WHERE a.team=?1 and a.type=?2 and a.del='1'")
    DoctorTeamMember findDoctor2ByTeam(String code, int type);

    @Query(" FROM DoctorTeamMember a WHERE a.team=?1 and a.type=?2 and a.del='1'and signType='1'")
    DoctorTeamMember findDoctorSanshi2ByTeam(String code, int type);

    @Query(" FROM DoctorTeamMember a WHERE a.team=?1 and a.type=?2 and a.del='1'and signType='2'")
    DoctorTeamMember findDoctorJiating2ByTeam(String code, int type);

    @Query(" FROM DoctorTeamMember a WHERE a.team=?1 and a.memberCode=?2 and a.del='1'")
    DoctorTeamMember findMemberCodeAndTeam(String code, String doctor);

    @Modifying
    @Query("update DoctorTeamMember a set a.del = '0' where a.team = ?1 and a.type=1 ")
    void updateType1Del(String code);

    @Modifying
    @Query("update DoctorTeamMember a set a.del = '0' where a.team = ?1 and signType='2' ")
    void deleteMember(String code);

    @Query("select count(a) FROM DoctorTeamMember a WHERE a.memberCode =?1 and  a.del='1'")
    Integer teamListByDoctorCode(String doctorCode);

    @Query(" FROM DoctorTeamMember a WHERE a.team =?1 and  a.del='1' and  a.signType='1'")
    List<DoctorTeamMember> findBySanshiTeam(String teamCode);

    @Query(" FROM DoctorTeamMember a WHERE a.team =?1 and  a.del='1' and  a.signType='1' and  a.type=?2 ")
    DoctorTeamMember findSanshiByTeamAndType(String teamCode, int i);

    List<DoctorTeamMember> findByTeamAndDel(String team, String del);

    @Query(" FROM DoctorTeamMember a WHERE a.team =?1 and  a.del='1' and  a.signType='2' and  a.memberCode=?2 ")
    DoctorTeamMember findMemberByTeamAndCode(String teamCode, String oldDoctorCode);

    /**
     * 判断医生是否为三师签团队的成员。
     *
     * @param doctorCode
     * @return
     */
    @Query("SELECT case when count(a) > 0 THEN true else false END FROM DoctorTeamMember a WHERE a.signType = 1 AND a" +
            ".memberCode = :memberCode")
    Boolean isSanShiSigning(@Param("memberCode") String doctorCode);
}
