<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.eitc.patient.mapper.PatientFollowUpMapper">
<!--    以病历为维度-->
    <!--    <select id="findAppointmentLists" resultType="com.eitc.patient.dto.PatientAppointmentDto">
            select * from (
                SELECT emr.id as appointmentId,
                    ep.medical_record_num as medicalRecordNum,
                    ep.patient_name as patientName,
                    ep.id as patientId,
                    epa.outpatient_service as outpatientService,
                    epa.department,
                    ep.patient_phone as linkPhone,
                    ep.birthday,
                    ep.sex,
                    epa.appointment_type as appointmentType,
                    ep.patient_source as patientSource,
                    emr.attending_doctor_name as attendingDoctor,
                    epa.appointment_start_time as appointmentStartTime,
                    epa.appointment_end_time as appointmentEndTime,
                    epa.appointment_project as appointmentProject,
                    (select count(*) from erm_patient_follow_up epfu where epfu.is_del = 0 and epfu.patient_appointment_id = emr.id) as followUpCount
                FROM erm_medical_record emr
                    LEFT JOIN erm_patient ep ON emr.patient_id = ep.id
                    left join erm_patient_appointment epa on emr.id = epa.medical_record_id
                    <where>
                        and emr.is_del = 0 and ep.is_del = 0
                        <if test="patientName != null and patientName != ''"> and ep.patient_name like concat('%', #{patientName}, '%')</if>
                        <if test="medicalRecordNum != null and medicalRecordNum != ''"> and ep.medical_record_num like concat('%', #{medicalRecordNum}, '%')</if>
                        <if test="appointmentProject != null and appointmentProject != ''"> and epa.appointment_project like concat('%', #{appointmentProject}, '%')</if>
                        <if test="attendingDoctor != null and attendingDoctor != ''"> and emr.attending_doctor_name like concat('%', #{attendingDoctor}, '%')</if>
                    </where>
                    ORDER BY emr.create_date DESC
            ) a where a.followUpCount = 0
        </select>
    -->
<!--    以预约为维度-->
        <select id="findAppointmentLists" resultType="com.eitc.patient.dto.PatientAppointmentDto">
            select * from (
            SELECT epa.id as appointmentId,
            ep.medical_record_num as medicalRecordNum,
            ep.patient_name as patientName,
            ep.id as patientId,
            epa.outpatient_service as outpatientService,
            epa.department,
            ep.patient_phone as linkPhone,
            ep.birthday,
            ep.sex,
            epa.appointment_type as appointmentType,
            ep.patient_source as patientSource,
            epa.attending_doctor as attendingDoctor,
            epa.appointment_start_time as appointmentStartTime,
            epa.appointment_end_time as appointmentEndTime,
            epa.appointment_project as appointmentProject,
            (select count(*) from erm_patient_follow_up epfu where epfu.is_del = 0 and epfu.patient_appointment_id = epa.id) as followUpCount
            FROM erm_patient_appointment epa
            LEFT JOIN erm_patient ep ON epa.appointment_person_id = ep.id
            <where>
                and epa.is_del = 0 and ep.is_del = 0
                <if test="patientName != null and patientName != ''"> and ep.patient_name like concat('%', #{patientName}, '%')</if>
                <if test="medicalRecordNum != null and medicalRecordNum != ''"> and ep.medical_record_num like concat('%', #{medicalRecordNum}, '%')</if>
                <if test="appointmentProject != null and appointmentProject != ''"> and epa.appointment_project like concat('%', #{appointmentProject}, '%')</if>
                <if test="attendingDoctor != null and attendingDoctor != ''"> and epa.attending_doctor like concat('%', #{attendingDoctor}, '%')</if>
            </where>
            ORDER BY epa.appointment_start_time DESC
            ) a
            <where>
                <if test='followUpCount != null and followUpCount == "99"'> and a.followUpCount > 0</if>
                <if test='followUpCount != null and followUpCount == "0"'> and a.followUpCount = 0</if>
            </where>
        </select>

<!--    以病历为维度-->
    <!--        <select id="findFollowUpLists" resultType="com.eitc.patient.vo.PatientFollowUpVO">
                select emr.id                     as patientAppointmentId,
                    ep.id                      as patientId,
                    ep.patient_name            as patientName,
                    ep.medical_record_num      as medicalRecordNum,
                    ep.identification_card     as identificationCard,
                    ep.sex,
                    ep.patient_phone           as patientPhone,
                    ep.birthday,
                    emr.create_date            as appointmentStartTime,
                    epfu.follow_up_user_id     as followUpUserId,
                    epfu.follow_up_user        as followUpUser,
                    epfu.follow_up_time        as followUpTime,
                    epfu.follow_up_type        as followUpType,
                    epfu.follow_up_result      as followUpResult,
                    epfu.follow_up_status      as followUpStatus,
                    epfu.follow_up_content     as followUpContent
                from (select * from erm_patient_follow_up where is_del = 0 group by patient_appointment_id) epfu
                    left join erm_medical_record emr on epfu.patient_appointment_id = emr.id
                    left join erm_patient ep on emr.patient_id = ep.id
                <where>
                    and emr.is_del = 0 and ep.is_del = 0
                    <if test="patientName != null and patientName != ''"> and ep.patient_name like concat('%', #{patientName}, '%')</if>
                    <if test="followUpStatus != null and followUpStatus != ''"> and epfu.follow_up_status = #{followUpStatus}</if>
                    <if test="followUpUser != null and followUpUser != ''"> and epfu.follow_up_user like concat('%', #{followUpUser}, '%')</if>

                    <if test="followUpStartTime != null and followUpStartTime != ''">
                and date_format(epfu.follow_up_time,'%Y-%m-%d %H:%i:%S') &gt;= date_format(#{followUpStartTime},'%Y-%m-%d %H:%i:%S')
            </if>
            <if test="followUpEndTime != null and followUpEndTime != ''">
                and date_format(epfu.follow_up_time,'%Y-%m-%d %H:%i:%S') &lt;= date_format(#{followUpEndTime},'%Y-%m-%d %H:%i:%S')
            </if>
        </where>
        ORDER BY emr.create_date DESC
    </select>
        -->
    <!--    以预约为维度-->
    <select id="findFollowUpLists" resultType="com.eitc.patient.vo.PatientFollowUpVO">
        select epa.id                     as patientAppointmentId,
        ep.id                      as patientId,
        ep.patient_name            as patientName,
        ep.medical_record_num      as medicalRecordNum,
        ep.identification_card     as identificationCard,
        ep.sex,
        ep.patient_phone           as patientPhone,
        ep.birthday,
        epa.appointment_start_time as appointmentStartTime,
        epfu.follow_up_user_id     as followUpUserId,
        epfu.follow_up_user        as followUpUser,
        epfu.follow_up_time        as followUpTime,
        epfu.follow_up_type        as followUpType,
        epfu.follow_up_result      as followUpResult,
        epfu.follow_up_status      as followUpStatus,
        epfu.follow_up_content     as followUpContent
        from (select * from erm_patient_follow_up where is_del = 0 group by patient_appointment_id) epfu
        left join erm_patient_appointment epa on epfu.patient_appointment_id = epa.id
        left join erm_patient ep on epa.appointment_person_id = ep.id
        where epa.is_del = 0 and ep.is_del = 0
            <if test="patientName != null and patientName != ''"> and ep.patient_name like concat('%', #{patientName}, '%')</if>
            <if test="followUpStatus != null and followUpStatus != ''"> and epfu.follow_up_status = #{followUpStatus}</if>
            <if test="followUpUser != null and followUpUser != ''"> and epfu.follow_up_user like concat('%', #{followUpUser}, '%')</if>
            <if test="followUpStartTime != null and followUpStartTime != ''"><!-- 开始时间检索 -->
                and date_format(epfu.follow_up_time,'%Y-%m-%d %H:%i:%S') &gt;= date_format(#{followUpStartTime},'%Y-%m-%d %H:%i:%S')
            </if>
            <if test="followUpEndTime != null and followUpEndTime != ''"><!-- 结束时间检索 -->
                and date_format(epfu.follow_up_time,'%Y-%m-%d %H:%i:%S') &lt;= date_format(#{followUpEndTime},'%Y-%m-%d %H:%i:%S')
            </if>
        ORDER BY epa.appointment_start_time DESC
    </select>
</mapper>
