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

import com.yihu.wlyy.statistics.model.doctor.DoctorPatientGroup;
import com.yihu.wlyy.statistics.model.doctor.DoctorPatientGroupInfo;
import com.yihu.wlyy.statistics.model.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 java.util.List;

public interface DoctorPatientGroupInfoDao extends PagingAndSortingRepository<DoctorPatientGroupInfo, Long>, JpaSpecificationExecutor<DoctorPatientGroupInfo> {

	// 根据签约类型获取患者信息
	@Query("select p from DoctorPatientGroupInfo p where p.group = ?1")
	Iterable<DoctorPatientGroupInfo> findPatientByType(String type);

	@Query("select p from DoctorPatientGroupInfo p where p.doctor = ?1 and p.patient = ?2")
	DoctorPatientGroupInfo findGroupInfoByPatient(String doctor, String patient);

	@Query("select p from DoctorPatientGroupInfo p where p.doctor = ?1 and p.patient = ?2 and p.status = 1 and p.signType = '2'")
	List<DoctorPatientGroupInfo> findGroupInfoByPatientAndDoctor(String doctor, String patient);

	// 删除分组时将原来分组患者移动到other分组
	@Modifying
	@Query("update DoctorPatientGroupInfo a set a.group = 'other' where a.group = ?1")
	int moveByCode(String group);

	// 查询所有分组下的患者信息
	@Query("SELECT p FROM Patient p, DoctorPatientGroupInfo d where p.code = d.patient and d.doctor = ?1")
	List<Patient> findPatientByDisease(String doctor);

	// 片段总数+1
	@Modifying
	@Query("update DoctorPatientGroupInfo a set a.partAmount = a.partAmount + 1 where a.doctor = ?1 and a.patient = ?2")
	int addPartAmount(String doctor, String patient);

	// 片段总数-1
	@Modifying
	@Query("update DoctorPatientGroupInfo a set a.partAmount = a.partAmount - 1 where a.doctor = ?1 and a.patient = ?2")
	int reducePartAmount(String doctor, String patient);

	// 病历总数+1
	@Modifying
	@Query("update Patient a set a.recordAmount = a.recordAmount + 1 where a.code = ?1")
	int addRecordAmount(String patient);

	@Query("SELECT a FROM DoctorPatientGroupInfo a where a.patient = ?1")
	List<DoctorPatientGroupInfo> findByPatient(String patient);

	@Modifying
	@Query("delete DoctorPatientGroupInfo a where a.doctor = ?1 and a.patient = ?2")
	int deleteByDoctorPatient(String doctor, String patient);
	
	@Query("select count(1) from DoctorPatientGroupInfo a where a.doctor = ?1 and a.group = ?2 and a.patient = ?3 and a.status = 1")
	int isExistsInGroup(String doctor, String group, String patient);

	@Modifying
	@Query("update DoctorPatientGroupInfo a set a.group ='2'  where a.patient = ?1 and a.group in ('1','3')")
	int updateGroupByParient(String parientCode);

	@Modifying
	@Query("update DoctorPatientGroupInfo a set a.status =0  where a.patient = ?1 and signType='2'")
	void deleteByPatient(String patient);

	//查找家庭签约的病人所在的分组
	@Query("SELECT a FROM DoctorPatientGroupInfo a where a.patient = ?1 and a.signType='2' and a.group in ('1','2','3') and a.status=1 " )
	List<DoctorPatientGroupInfo> findByMorenPatient(String patient);

	/**
	 * 查询患者存在的医生自建组
	 *
	 * @param patient
	 * @return
	 */
	@Query("select a from DoctorPatientGroupInfo a where a.patient = ?1 and a.status = 1 and a.group not in ('1','2','3')")
	List<DoctorPatientGroupInfo> findPatientGroups(String patient);

	//查找所有的签约患者所在的分组
	@Query("select a from DoctorPatientGroupInfo a where  a.status = 1 and a.signType in ('2','3') and a.group in ('1','2','3')")
	List<DoctorPatientGroupInfo> findAllPatient();

	@Query("select a from DoctorPatientGroupInfo a where  a.status = 1 and a.doctor = ?1 and a.patient = ?2 and a.group = ?3")
	List<DoctorPatientGroupInfo> findDoctorPatientGroup(String doctor, String patient, String group);

	@Query("select count(*) from DoctorPatientGroupInfo a where  a.status = 1 and a.doctor = ?1 and a.patient = ?2 and a.group = ?3")
	long countDoctorPatientGroup(String doctor, String patient, String group);

	@Query("select count(*) from DoctorPatientGroupInfo a where  a.status = 1 and a.doctor = ?1 and a.patient = ?2 and a.group = ?3 and a.signType = ?4")
	long countDoctorPatientGroupType(String doctor, String patient, String group, String type);

	@Modifying
	@Query("update DoctorPatientGroupInfo set status = ?4 where doctor = ?1 and patient = ?2 and group = ?3 and status = 1")
	int updateDoctorPatientGroup(String doctor, String patient, String group, int status);

	@Query("select a from DoctorPatientGroupInfo a where  a.status = 1 and a.doctor = ?2 and a.patient = ?1 and a.signType =2")
	List<DoctorPatientGroupInfo> findByPatientAndDoctor(String patient, String oldDoctorCode);

	List<DoctorPatientGroupInfo> findByGroupAndStatus(String group, Integer status);

	@Query("select a from DoctorPatientGroupInfo a where a.id between ?1 and ?2 and a.status = 1")
	Page<DoctorPatientGroupInfo> findByIdRange(Long start, Long end, Pageable pageRequest);
}
