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 MedicalRecordsRepository extends JpaRepository<MedicalRecords, Integer> {

    @Query(value = "select * from medical_records where clinic_code = ? and type = 2 and deleted = 0 limit 1;", nativeQuery = true)
    MedicalRecords getOutpatientServiceRecord(String clinicCode);

    @Query(value = "select count(0) from medical_records where clinic_code = ? and type = 2 and deleted = 0;", nativeQuery = true)
    int getOutpatientServiceRecordCount(String clinicCode);

    /**
     * 根据患者ID获取患者病历数量
     *
     * @param patientId
     * @return
     */
    @Query(value = "select count(1) from medical_records where patient_id = ? and doctor_id = ? and deleted = 0 and type = 2 ", nativeQuery = true)
    public int getPatientMedicalRecordCount(int patientId, int doctorId);

    /**
     * 根据病人ID和医生ID查询所有符合条件的病历
     *
     * @param doctorId
     * @param patientId
     * @return
     */
    @Query(value = "select * from medical_records where  doctor_id = ? and patient_id = ? and deleted = 0 and type = 2  order by create_time desc ", nativeQuery = true)
    public List<MedicalRecords> getPatientMedicalRecordsList(int doctorId, int patientId);

    /**
     * 获取太和医院门诊病历数
     *
     * @param hisPatientId HIS系统患者ID（门诊号）
     * @param clinicCode   就诊流水号
     * @return
     */
    @Query(value = "select count(0) from medical_records where his_patient_id = ? and clinic_code = ? and hospital_id = ? and type = 2 and deleted = 0;", nativeQuery = true)
    int getTaiheOutpatientCount(String hisPatientId, String clinicCode, int hospitalId);

}
