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

import com.yihu.wlyy.entity.patient.SignFamily;
import com.yihu.wlyy.entity.patient.SignFamilyRenew;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

public interface SignFamilyRenewDao extends PagingAndSortingRepository<SignFamilyRenew, Long>, JpaSpecificationExecutor<SignFamilyRenew> {

    @Query("select a from SignFamilyRenew a where a.idcard = ?1 and a.type = 2 and a.status >= 0")
    List<SignFamilyRenew> findByIdcard(String idcard);

    /**
     * 获取所有有效的签约信息（不包含-1 -2）
     *
     * @param patient
     * @return
     */
    @Query(value = "select a from SignFamilyRenew a where a.patient = ?1 and a.status <> -1 and a.status <> -2 and a.isValid =0 order by a.czrq")
    List<SignFamilyRenew> findAllActiveSignByPatient(String patient);

    @Query(value = "select a from SignFamilyRenew a where a.doctor =?1 and a.patient = ?2 and a.signYear =?3 ")
    List<SignFamilyRenew> findByDoctorAndPatient(String doctor,String patient,String signYear);

    @Query(value = "select a from SignFamilyRenew a where a.patient = ?1 and a.signYear =?2 and a.status >= 0")
    List<SignFamilyRenew> findByDoctorAndPatientHossPost(String patient,String signYear);

    @Query(value = "select a from SignFamilyRenew a where a.doctor =?1 and a.patient = ?2 and a.signYear =?3 and a.status = 1 and a.expensesStatus ='1'")
    List<SignFamilyRenew> findByDoctorAndPatientIsValid(String doctor,String patient,String signYear);

    @Query(value = "select a from SignFamilyRenew a where a.doctorHealth =?1 and a.patient = ?2 and a.signYear =?3 and a.status = 1 and a.expensesStatus ='1'")
    List<SignFamilyRenew> findByDoctorHealthAndPatientIsValid(String doctorHealth,String patient,String signYear);

    @Query(value = "select a from SignFamilyRenew a where a.doctorHealth =?1 and a.patient = ?2 and a.signYear =?3")
    List<SignFamilyRenew> findByDoctorHealthAndPatient(String doctorHealth,String patient,String signYear);

    @Query(value = "select a from SignFamilyRenew a where a.doctorHealth =?1 and a.patient = ?2 and a.signYear =?3 and a.status=0")
    List<SignFamilyRenew> findByDoctorHealthAndPatientStatus0(String doctorHealth,String patient,String signYear);

    // 查询患者已生效的家庭续签约
    @Query("select a from SignFamilyRenew a where a.patient = ?1 and a.type = 2 and a.status = 0")
    SignFamilyRenew findSigningByPatient(String patient);

    @Query("select a from SignFamilyRenew a where a.teamCode = ?1 and a.type = 2 and a.code = ?2")
    List<SignFamilyRenew> findByTeamCodeIsValid(String teamCode,String code);

    SignFamilyRenew findByCode(String code);

    @Query("select a from SignFamilyRenew a where a.patient = ?1 and a.type = ?2 and a.status > 0")
    SignFamilyRenew findSignByPatient(String patient, int type);

    @Query("select a from SignFamilyRenew a where a.doctor = ?1 and a.patient = ?2 and a.type = 2 and a.status >= 0")
    SignFamilyRenew findByDoctorPatient(String doctor, String patient);
}
