package com.ywt.outpatient.taihe.rpc.domain.entity.center;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * (MedicalCard)表Repository类
 *
 * @author johnson lin
 * @since 2019-05-09 15:10:25
 */
public interface MedicalCardRepository extends JpaRepository<MedicalCard, Integer> {
    /**
     * 获取 MedicalCard
     *
     * @param id     主键
     * @param status 状态 {@link com.ywt.taihe.rpc.domain.enums.MedicalCardStatus}
     * @return {@link MedicalCard}
     */
    MedicalCard getByIdAndStatus(int id, int status);

    @Query(value = "select * from ywt_center.medical_card where id = ? and status = ? and user_id = ? limit 1", nativeQuery = true)
    MedicalCard getByIdAndStatusAndUserId(int id, int status, int userId);

    @Query(value = "select * from ywt_center.medical_card where his_patient_id = ? and hospital_id = ? limit 1", nativeQuery = true)
    MedicalCard getByHisPatientIdAndHospitalId(String hisPatientId, int hospitalId);

    @Query(value = "select * from ywt_center.medical_card where user_id = ? and patient_name = ? and id_no = ? and hospital_id = ? and status = ? limit 1", nativeQuery = true)
    MedicalCard getByUserIdAndPatientNameAndIdNoAndHospitalIdAndStatus(int userId, String name, String idNo, int hospitalId, int status);

    @Query(value = "select * from ywt_center.medical_card where id = ? and status = 1 limit 1", nativeQuery = true)
    MedicalCard getById(int id);

    @Query(value = "select * from ywt_center.medical_card where his_patient_id = ? and user_id = ? and status = 1 limit 1", nativeQuery = true)
    MedicalCard getByHisPatientIdAndUserId(String hisPatientId, int userId);

    MedicalCard getByUserIdAndMobileAndStatus(int userId, String mobile, int status);

    List<MedicalCard> getByUserIdAndIdNoAndStatusAndHospitalIdOrderByUpdateTimeDesc(int userId, String idNo, int status, int hospitalId);


    @Query(value = "select * from ywt_center.medical_card where user_id = ? and patient_name = ? and id_no = ? and card_type = ? and hospital_id = ? and status = ? limit 1", nativeQuery = true)
    MedicalCard getByUserIdAndPatientNameAndIdNoAndCardTypeAndHospitalIdAndStatus(int userId, String name, String idNo, String cardType, int hospitalId, int status);


    @Query(value = "select * from ywt_center.medical_card where user_id = ? and card_no = ? and hospital_id = ? limit 1", nativeQuery = true)
    MedicalCard getByUserIdAndCardNoAndHospitalId(int userId, String cardNo, int hospitalId);

}
