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 PatientRepository extends JpaRepository<Patient, Integer> {

    @Query(value = "select count(1) from patient where doctor_id = ? and mobile = ? and real_name = ? and deleted = 0 limit 1",nativeQuery = true)
    int checkAdded(int doctorId, String mobile, String name);

    @Query(value = "select count(1) from patient where doctor_id = ? and mobile = ? and real_name = ? and id != ? and deleted = 0 limit 1",nativeQuery = true)
    int checkAdded(int doctorId, String mobile, String name, int patientId);

    @Query(value = "select * from patient where mobile = ? and real_name = ? and deleted = 0 order by create_time desc limit 1", nativeQuery = true)
    Patient getPatient(String realName, String mobile);

    @Query(value = "select * from patient where doctor_id = ? and mobile = ? and real_name = ? and deleted = 0 order by create_time desc limit 1", nativeQuery = true)
    Patient getPatient(int doctorId, String mobile, String realName);

    @Query(value = "select * from patient where doctor_id = ? and id_card_no = ? and real_name = ? and deleted = 0 order by create_time desc limit 1;", nativeQuery = true)
    Patient getByIdCardNoAndName(int doctorId, String idCardNo, String realName);

    @Query(value = "select * from patient where doctor_id = ? and id_card_no = ? and deleted = 0 order by create_time desc limit 1;", nativeQuery = true)
    Patient getByIdCardNo(int doctorId, String idCardNo);

    /**
     * 添加某个医生曾经是否添加过该患者
     *
     * @param doctorId
     * @param mobile
     * @param name
     * @return
     */
    @Query(value = "select * from patient where doctor_id = ? and mobile = ? and real_name = ? limit 1",nativeQuery = true)
    List<Patient> checkAddedPatient(int doctorId, String mobile, String name);

}
