<?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.doctorstation.mapper.DoctorStationMainAppMapper">

    <select id="getPatientInfo" resultType="com.openhis.web.doctorstation.dto.PatientInfoDto">
        SELECT
        T10.encounter_id,
        T10.patient_id,
        T10.patient_name,
        T10.gender_enum,
        T10.id_card,
        T10.phone,
        T10.birth_date,
        T10.status_enum,
        T10.allergy_history_flag,
        T10.account_id,
        T10.type_code,
        T10.contract_name,
        T10.org_id,
        T10.organization_name,
        T10.register_time,
        T10.reception_time,
        T10.practitioner_user_id,
        T10.jz_practitioner_user_id
        from
        (
        SELECT T1.tenant_id AS tenant_id,
        T1.ID AS encounter_id,
        T1.organization_id AS organization_id,
        T2.NAME AS organization_name,
        T3.NAME AS healthcare_name,
        T5.user_id AS practitioner_user_id,
        T5.NAME AS practitioner_name,
        ap.user_id AS jz_practitioner_user_id,
        ap.NAME AS jz_practitioner_name,
        T6.id AS account_id,
        T6.type_code AS type_code,
        T7.contract_name AS contract_name,
        T8.ID AS patient_id,
        T8.NAME AS patient_name,
        T8.gender_enum AS gender_enum,
        T8.id_card AS id_card,
        T8.phone AS phone,
        T8.birth_date AS birth_date,
        T1.status_enum AS status_enum,
        T1.create_time AS register_time,
        T1.reception_time AS reception_time,
        T1.organization_id AS org_id,
        CASE
        WHEN T9.patient_id IS NOT NULL THEN 1
        ELSE 0
        END AS allergy_history_flag
        FROM adm_encounter AS T1
        LEFT JOIN adm_organization AS T2 ON T1.organization_id = T2.ID AND T2.delete_flag = '0'
        LEFT JOIN adm_healthcare_service AS T3 ON T1.service_type_id = T3.ID AND T3.delete_flag = '0'
        LEFT JOIN adm_encounter_participant AS T4
        ON T1.ID = T4.encounter_id AND T4.type_code = #{participantType}
        AND T4.status_enum = #{activityStatus}
        AND T4.delete_flag = '0'
        LEFT JOIN adm_practitioner AS T5 ON T5.ID = T4.practitioner_id AND T5.delete_flag = '0'
        LEFT JOIN adm_encounter_participant AS aep ON T1.ID = aep.encounter_id
        AND aep.type_code = #{participantType2}
        AND aep.status_enum = #{activityStatus}
        AND aep.delete_flag = '0'
        LEFT JOIN adm_practitioner AS ap ON ap.ID = aep.practitioner_id
        AND ap.delete_flag = '0'
        LEFT JOIN adm_account AS T6 ON T1.ID = T6.encounter_id AND T6.delete_flag = '0' and T6.encounter_flag = '1'
        LEFT JOIN fin_contract AS T7 ON T6.contract_no = T7.bus_no AND T7.delete_flag = '0'
        LEFT JOIN adm_patient AS T8 ON T1.patient_id = T8.ID AND T8.delete_flag = '0'
        LEFT JOIN cli_allergy_intolerance AS T9
        ON T1.patient_id = T9.patient_id AND T9.clinical_status_enum != #{ClinicalStatus} AND T9.delete_flag = '0'
        WHERE
        T1.delete_flag = '0'
        <!-- 当前登录账号ID 和 当前登录账号所属的科室ID 用于控制数据权限 -->
        <if test="userId != null and currentUserOrganizationId != null">
            AND ( T5.user_id = #{userId}
            OR (T1.organization_id = #{currentUserOrganizationId}) )
        </if>
        <if test="pricingFlag ==1">
            AND T1.status_enum != #{encounterStatus}
        </if>
        ORDER BY
        T1.create_time ) AS T10
        ${ew.customSqlSegment}
    </select>

    <select id="getPrescriptionPageInfo" resultType="com.openhis.web.doctorstation.dto.PrescriptionInfoBaseDto">
        SELECT ppi.prescription_no,
               ppi.encounter_id,
               ppi.patient_id,
               ppi.patient_name,
               ppi.birth_date,
               ppi.gender_enum,
               ppi.condition_definition_name,
               ppi.request_time,
               ppi.practitioner_id,
               ppi.practitioner_name,
               ppi.organization_id,
               ppi.bus_no,
               ppi.contract_name
        FROM (SELECT DISTINCT
              ON
                  ( mmr.prescription_no ) mmr.prescription_no, mmr.encounter_id,
                  mmr.tenant_id,
                  mmr.patient_id,
                  ap.NAME AS patient_name,
                  ap.birth_date AS birth_date,
                  ap.gender_enum AS gender_enum,
                  ccd.NAME AS condition_definition_name,
                  mmr.create_time AS request_time,
                  mmr.practitioner_id,
                  aper.NAME AS practitioner_name,
                  ae.organization_id,
                  ae.bus_no,
                  fc.contract_name
              FROM
                  med_medication_request AS mmr
                  LEFT JOIN adm_encounter AS ae
              ON ae.id = mmr.encounter_id AND ae.delete_flag = '0'
                  LEFT JOIN adm_account AS aa ON aa.encounter_id = ae.id AND aa.delete_flag = '0' AND aa.encounter_flag = 1
                  LEFT JOIN fin_contract AS fc ON fc.bus_no = aa.contract_no AND fc.delete_flag = '0'
                  LEFT JOIN adm_patient AS ap
                  ON ap.ID = mmr.patient_id
                  AND ap.delete_flag = '0'
                  LEFT JOIN cli_condition AS cc ON cc.ID = mmr.condition_id
                  AND cc.delete_flag = '0'
                  LEFT JOIN cli_condition_definition AS ccd ON ccd.ID = cc.definition_id
                  LEFT JOIN adm_practitioner AS aper ON aper.ID = mmr.practitioner_id
                  AND aper.delete_flag = '0'
              WHERE
                  mmr.delete_flag = '0'
                AND (mmr.prescription_no LIKE 'P%'
                 OR mmr.prescription_no LIKE 'C%')
                AND mmr.refund_medicine_id IS NULL
              ORDER BY
                  mmr.prescription_no DESC) AS ppi
            ${ew.customSqlSegment}
    </select>

    <select id="getPrescriptionDetailInfo" resultType="com.openhis.web.doctorstation.dto.PrescriptionInfoDetailDto">
        SELECT mmr.prescription_no,
        mmr.ID AS request_id,
        mmr.practitioner_id AS requester_id,
        mmr.create_time AS request_time,
        mmr.group_id AS group_id,
        mmd.NAME AS advice_name,
        mm.total_volume AS volume,
        mmr.lot_number AS lot_number,
        mmr.quantity AS quantity,
        mmr.unit_code AS unit_code,
        mmr.status_enum AS status_enum,
        mmr.method_code AS method_code,
        mmr.rate_code AS rate_code,
        mmr.dose AS dose,
        mmr.dose_unit_code AS dose_unit_code,
        aci.ID AS charge_item_id,
        aci.total_price AS total_price,
        aci.status_enum AS charge_status,
        al.ID AS position_id,
        al.NAME AS position_name,
        mmr.dispense_per_duration AS dispense_per_duration,
        mmd.part_percent AS part_percent,
        ccd.NAME AS condition_definition_name
        FROM med_medication_request AS mmr
        LEFT JOIN med_medication_definition AS mmd ON mmd.ID = mmr.medication_id
        AND mmd.delete_flag = '0'
        LEFT JOIN med_medication AS mm ON mm.medication_def_id = mmd.ID
        AND mm.delete_flag = '0'
        LEFT JOIN adm_charge_item AS aci ON aci.service_id = mmr.ID
        AND aci.delete_flag = '0'
        LEFT JOIN adm_location AS al ON al.ID = mmr.perform_location
        AND al.delete_flag = '0'
        LEFT JOIN cli_condition AS cc ON cc.ID = mmr.condition_id
        AND cc.delete_flag = '0'
        LEFT JOIN cli_condition_definition AS ccd ON ccd.ID = cc.definition_id
        WHERE mmr.delete_flag = '0'
        <if test="prescriptionNo != null">
            AND mmr.prescription_no = #{prescriptionNo}
        </if>
        <if test="encounterId != null">
            AND mmr.encounter_id = #{encounterId}
        </if>
    </select>

</mapper>