<?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.openhis.web.outpatientmanage.mapper.OutpatientManageMapper">

    <!-- 门诊皮试记录相关查询-->
    <select id="getOutpatientSkinTestRecord"
            parameterType="com.openhis.web.outpatientmanage.dto.OutpatientSkinTestRecordSearchParam"
            resultType="com.openhis.web.outpatientmanage.dto.OutpatientSkinTestRecordDto">
        SELECT
        sr.id, --服务申请管理ID
        sr.prescription_no, --处方号
        e.bus_no as encounterBusNo, --就诊号
        pt.name AS patientName, --病人姓名
        pt.bus_no AS patientBusNo, --病人ID（前台显示用）
        sr.patient_id, --病人ID
        ad.name AS medicationInformation, --药品信息
        md.bus_no || ' ' || md.name || ' 规格：' || COALESCE(m.total_volume, '') AS medicationDetail,--药品
        m.lot_number AS medicationLotNumber, --药品批次号
        mr.status_enum AS medicationStatusEnum, --药品状态
        sr.performer_id, --执行护士
        sr.performer_check_id,--核对人
        to_char(sr.occurrence_start_time, 'YYYY-MM-DD HH24:MI:SS') AS occurrenceStartTime, --预计执行时间
        to_char(sr.occurrence_end_time, 'YYYY-MM-DD HH24:MI:SS') AS occurrenceEndTime, --预计结束时间
        ai.clinical_status_enum, --皮试结果
        ai.verification_status_enum, --皮试检查项目状态
        mr.practitioner_id as doctorId, --开单医生
        ai.note --备注
        FROM
        wor_service_request sr
        LEFT JOIN adm_encounter e ON e.id = sr.encounter_id
        LEFT JOIN adm_patient pt ON pt.id = sr.patient_id
        LEFT JOIN wor_activity_definition ad ON ad.id = sr.activity_id
        LEFT JOIN med_medication_request mr ON mr.prescription_no = sr.prescription_no
        LEFT JOIN med_medication m ON m.id = mr.medication_id
        LEFT JOIN med_medication_definition md ON md.id = m.medication_def_id
        LEFT JOIN cli_allergy_intolerance ai ON ai.request_id = sr.id
        LEFT JOIN adm_encounter_participant ep ON ep.encounter_id = sr.encounter_id
        <where>
            ad.bus_no = 'ps001' -- 皮试检查的编号，todo：编号未定，后期修改
            AND mr.skin_test_flag = 1
            AND md.skin_test_flag = 1
            AND sr.status_enum in (2,3,6)  --服务状态有效
            AND ep.type_code = '1' --参与者身份类型是医生

            <!-- 门诊号 查询条件-->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.encounterBusNo != null and OutpatientSkinTestRecordSearchParam.encounterBusNo != ''">
                AND e.bus_no LIKE CONCAT('%',#{OutpatientSkinTestRecordSearchParam.encounterBusNo}, '%')
            </if>

            <!-- 病人号 查询条件-->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.patientBusNo != null and OutpatientSkinTestRecordSearchParam.patientBusNo != ''">
                AND pt.bus_no LIKE CONCAT('%',#{OutpatientSkinTestRecordSearchParam.patientBusNo}, '%')
            </if>

            <!-- 处方号 查询条件-->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.prescriptionNo != null and OutpatientSkinTestRecordSearchParam.prescriptionNo != ''">
                AND sr.prescription_no LIKE CONCAT('%',#{OutpatientSkinTestRecordSearchParam.prescriptionNo}, '%')
            </if>

            <!-- 手机号 查询条件-->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.phone != null and OutpatientSkinTestRecordSearchParam.phone != ''">
                AND pt.phone LIKE CONCAT('%',#{OutpatientSkinTestRecordSearchParam.phone}, '%')
            </if>

            <!-- 时间筛选 -->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.beginTime != null and OutpatientSkinTestRecordSearchParam.endTime != null">
                AND ai.recorded_date BETWEEN
                TO_TIMESTAMP(#{OutpatientSkinTestRecordSearchParam.beginTime} || ' 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
                AND
                TO_TIMESTAMP(#{OutpatientSkinTestRecordSearchParam.endTime} || ' 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
            </if>

            <!-- 皮试项目检查状态 查询条件-->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.status != null and OutpatientSkinTestRecordSearchParam.status != null">
                AND ai.verification_status_enum = #{OutpatientSkinTestRecordSearchParam.status}
            </if>
        </where>
        ORDER BY sr.prescription_no,e.bus_no,md.bus_no
        LIMIT #{pageSize} OFFSET #{offset}
    </select>

    <select id="countOutpatientSkinTestRecords" resultType="long">
        SELECT COUNT(*)
        FROM
        wor_service_request sr
        LEFT JOIN adm_encounter e ON e.id = sr.encounter_id
        LEFT JOIN adm_patient pt ON pt.id = sr.patient_id
        LEFT JOIN wor_activity_definition ad ON ad.id = sr.activity_id
        LEFT JOIN med_medication_request mr ON mr.prescription_no = sr.prescription_no
        LEFT JOIN med_medication m ON m.id = mr.medication_id
        LEFT JOIN med_medication_definition md ON md.id = m.medication_def_id
        LEFT JOIN cli_allergy_intolerance ai ON ai.request_id = sr.id
        LEFT JOIN adm_encounter_participant ep ON ep.encounter_id = sr.encounter_id
        <where>
            ad.bus_no = 'ps001' -- 皮试检查的编号，todo：编号未定，后期修改
            AND mr.skin_test_flag = 1
            AND md.skin_test_flag = 1
            AND sr.status_enum in (2,3,6)  --服务状态有效
            AND ep.type_code = '1' --参与者身份类型是医生

            <!-- 门诊号 查询条件-->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.encounterBusNo != null and OutpatientSkinTestRecordSearchParam.encounterBusNo != ''">
                AND e.bus_no LIKE CONCAT('%',#{OutpatientSkinTestRecordSearchParam.encounterBusNo}, '%')
            </if>

            <!-- 病人号 查询条件-->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.patientBusNo != null and OutpatientSkinTestRecordSearchParam.patientBusNo != ''">
                AND pt.bus_no LIKE CONCAT('%',#{OutpatientSkinTestRecordSearchParam.patientBusNo}, '%')
            </if>

            <!-- 处方号 查询条件-->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.prescriptionNo != null and OutpatientSkinTestRecordSearchParam.prescriptionNo != ''">
                AND sr.prescription_no LIKE CONCAT('%',#{OutpatientSkinTestRecordSearchParam.prescriptionNo}, '%')
            </if>

            <!-- 手机号 查询条件-->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.phone != null and OutpatientSkinTestRecordSearchParam.phone != ''">
                AND pt.phone LIKE CONCAT('%',#{OutpatientSkinTestRecordSearchParam.phone}, '%')
            </if>

            <!-- 时间筛选 -->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.beginTime != null and OutpatientSkinTestRecordSearchParam.endTime != null">
                AND ai.recorded_date BETWEEN
                TO_TIMESTAMP(#{OutpatientSkinTestRecordSearchParam.beginTime} || ' 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
                AND
                TO_TIMESTAMP(#{OutpatientSkinTestRecordSearchParam.endTime} || ' 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
            </if>

            <!-- 皮试项目检查状态 查询条件-->
            <if test="OutpatientSkinTestRecordSearchParam != null and OutpatientSkinTestRecordSearchParam.status != null and OutpatientSkinTestRecordSearchParam.status != null">
                AND ai.verification_status_enum = #{OutpatientSkinTestRecordSearchParam.status}
            </if>
        </where>
    </select>

</mapper>