/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.repository.doctor;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.entity.doctor.team.sign.DoctorPatient;

public interface DoctorPatientDao extends PagingAndSortingRepository<DoctorPatient, Long> {

	// 查询患者与家庭医生的关联关系
	@Query("select a from DoctorPatient a where a.patient = ?1 and a.doctor = ?2 and a.teamType = ?3 and a.status = 1")
	DoctorPatient findByPatientDoctorTeamType(String patient, String doctor, int teamType);

	// 查询三师医生团队或家庭医生团队
	@Query("select a from DoctorPatient a where a.patient = ?1 and a.teamType = ?2 and a.status = 1")
	List<DoctorPatient> findPatientTeam(String patient, int teamType);
	
	//查询团队下所有的医生信息
	@Query("select a from Doctor a, DoctorPatient b where b.patient = ?1 and b.teamType = ?2 and b.status = 1 and a.code = b.doctor")
	List<Doctor> findDoctors(String patient, int teamType);

	//查询团队下所有的医生信息
	@Query("select a from Doctor a, DoctorTeamMember b where b.team = ?1 and a.code = b.memberCode and b.type <> 5")
	List<Doctor> findTeamDoctors(String team);

	// 更新家庭医生与患者关系为无效
	@Modifying
	@Query("update DoctorPatient a set a.status = 0 where a.patient = ?1 and a.teamType = 2")
	int disabledDoctorPatient(String patient);

	// 查询团队其他的医生
	@Query("select a from DoctorPatient a where a.patient = ?1 and a.doctor <> ?2 and a.teamType = ?3 and a.status = 1")
	List<DoctorPatient> findTeamOtherDoctor(String patient, String doctor, int teamType);

	// 根据医生类型查询团队中的医生
	@Query("select a.doctor from DoctorPatient a where a.patient = ?1 and a.teamType = ?2 and a.doctorType = ?3 and a.status = 1")
	String findPatientDoctorByType(String patient, int teamType, int doctorType);

	// 查询团队所有的医生
	@Query("select a.doctor from DoctorPatient a where a.patient = ?1 and a.teamType = ?2 and a.status = 1")
	List<String> findPatientDoctors(String patient, int teamType);
	
	@Query("select a.code, a.name, a.photo, a.sex, a.birthday, b.signType from Patient a, DoctorPatient b where b.doctor = ?1 and b.disease = ?2 and a.code = b.patient group by b.patient")
	List<Object> findPatientByDisease(String doctor, int disease);
	
	@Query("select a.code, a.name, a.photo, a.sex, a.birthday, b.signType from Patient a, DoctorPatient b where b.doctor = ?1 and b.disease = ?2 and b.birthday < ?3 and a.code = b.patient group by b.patient")
	List<Object> findPatientByDisease(String doctor, int disease, Date date);
	
	@Query("select a.code, a.name, a.photo, a.sex, a.birthday, b.signType from Patient a, DoctorPatient b where b.doctor = ?1 and b.disease = ?2 and b.birthday >= ?3 and a.code = b.patient group by b.patient")
	List<Object> findOldPatientByDisease(String doctor, int disease, Date date);

	//查询某个医生签约的慢病人数
	int countByDoctorAndDisease(String doctor,int disease);
	
	//查询某个医生签约的普通人群
	@Query("select count(*) from Patient a, DoctorPatient b where b.doctor = ?1 and b.disease = ?2 and b.birthday < ?3 and a.code = b.patient group by b.patient")
	int countNormalPatient(String doctor,int disease,Date date);
	
	//查询某个医生签约的65岁以上人群
	@Query("select count(*) from Patient a, DoctorPatient b where b.doctor = ?1 and b.disease = ?2 and b.birthday >= ?3 and a.code = b.patient group by b.patient")
	int countOldPatient(String doctor,int disease,Date date);
}
