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

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

import java.util.List;


public interface MedicalCardRepository extends JpaRepository<MedicalCard, Integer> {

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

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

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

    @Query(value = "select * from medical_card where id >= ? and id <= ? order by id;", nativeQuery = true)
    List<MedicalCard> getMedicalCardList(int minId, int maxId);

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

    @Query(value = "select * from medical_card where user_id = ? and patient_name = ? and status = 1 limit 1;", nativeQuery = true)
    MedicalCard getCardByUserIdAndPatientName(int userId, String patientName);

    @Query(value = "select * from medical_card where hospital_id = ? and his_patient_id = ? and status = 1 order by id;", nativeQuery = true)
    List<MedicalCard> getMedicalCardListByPatientId(int hospitalId, String hisPatientId);

    @Query(value = "select * from medical_card where id_no = ? and hospital_id = ? and status = 1 limit 1;", nativeQuery = true)
    MedicalCard getMedicalCardByIdNo(String idNo, int hospitalId);

    @Query(value = "select * from medical_card where user_id = ? and status = 1;", nativeQuery = true)
    List<MedicalCard> getCardByUserId(int userId);

    @Query(value = "select count(1) from medical_card where user_id = ? and hospital_id = ? and status = 1 limit 1;", nativeQuery = true)
    int checkHasCard(int userId, int hospitalId);

    @Query(value = "select * from medical_card where user_id = ? and id_no = ? and status = 1;", nativeQuery = true)
    List<MedicalCard> getCardByUserIdAndIdNo(int userId, String idNo);

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