<?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.DoctorStationChineseMedicalAppMapper">

    <select id="getTcmEncounterDiagnosis" resultType="com.openhis.web.doctorstation.dto.DiagnosisQueryDto">
        SELECT T1.id AS encounter_diagnosis_id,
               T1.condition_id,
               T1.syndrome_group_no,
               T2.definition_id,
               T3.source_enum,
               T3.NAME,
               T1.maindise_flag,
               T1.diag_srt_no,
               T1.med_type_code,
               T2.verification_status_enum,
               T2.yb_no
        FROM adm_encounter_diagnosis AS T1
                 LEFT JOIN cli_condition AS T2 ON T2.ID = T1.condition_id
            AND T2.delete_flag = '0' AND T2.tcm_flag = 1
                 LEFT JOIN cli_condition_definition AS T3 ON t3.ID = T2.definition_id
            AND T3.delete_flag = '0'
        WHERE T1.delete_flag = '0'
          AND T1.tcm_flag = 1
          AND T1.encounter_id = #{encounterId}
        ORDER BY T1.diag_srt_no
    </select>

    <select id="getTcmRequestBaseInfo" resultType="com.openhis.web.doctorstation.dto.RequestBaseDto">
        SELECT 1 AS advice_type,
        T1.id AS request_id,
        T1.id || '-1' AS unique_key,
        T1.prescription_no AS prescription_no,
        T1.practitioner_id AS requester_id,
        T1.create_time AS request_time,
        CASE WHEN T1.practitioner_id = #{practitionerId} THEN '1' ELSE '0' END AS biz_request_flag,
        T1.content_json AS content_json,
        T1.skin_test_flag AS skin_test_flag,
        T1.infusion_flag AS inject_flag,
        T1.group_id AS group_id,
        T2.NAME AS advice_name,
        T3.total_volume AS volume,
        T1.lot_number AS lot_number,
        T1.quantity AS quantity,
        T1.unit_code AS unit_code,
        T1.status_enum AS status_enum,
        T1.method_code AS method_code,
        T1.rate_code AS rate_code,
        T1.dose AS dose,
        T1.dose_unit_code AS dose_unit_code,
        T4.id AS charge_item_id,
        T4.total_price AS total_price,
        T4.status_enum AS charge_status,
        al.id AS position_id,
        al.name AS position_name,
        T1.dispense_per_duration AS dispense_per_duration,
        T1.chinese_herbs_dose_quantity AS chinese_herbs_dose_quantity,
        T1.suffering_flag AS suffering_flag,
        T2.part_percent AS part_percent,
        ccd.name AS condition_definition_name
        FROM med_medication_request AS T1
        LEFT JOIN med_medication_definition AS T2 ON T2.ID = T1.medication_id
        AND T2.delete_flag = '0'
        LEFT JOIN med_medication AS T3 ON T3.medication_def_id = T2.ID
        AND T3.delete_flag = '0'
        LEFT JOIN adm_charge_item AS T4 ON T4.service_id = T1.ID AND T4.delete_flag = '0' AND
        T4.service_table = #{MED_MEDICATION_REQUEST}
        LEFT JOIN adm_location AS al ON al.ID = T1.perform_location AND al.delete_flag = '0'
        LEFT JOIN cli_condition AS cc ON cc.id = T1.condition_id AND cc.delete_flag = '0'
        LEFT JOIN cli_condition_definition AS ccd ON ccd.id = cc.definition_id
        WHERE T1.delete_flag = '0' AND T1.tcm_flag = 1
        <if test="historyFlag == '0'.toString()">
            AND T1.encounter_id = #{encounterId}
        </if>
        <if test="historyFlag == '1'.toString()">
            AND T1.patient_id = #{patientId} AND T1.encounter_id != #{encounterId}
        </if>
        AND T1.refund_medicine_id IS NULL
        ORDER BY T1.group_id,T1.prescription_no, T1.status_enum
    </select>

    <select id="getTcmRequestHistoryInfo" resultType="com.openhis.web.doctorstation.dto.RequestBaseDto">
        SELECT 1 AS advice_type,
        T1.id AS request_id,
        T1.id || '-1' AS unique_key,
        T1.prescription_no AS prescription_no,
        T1.practitioner_id AS requester_id,
        T1.create_time AS request_time,
        CASE WHEN T1.practitioner_id = #{practitionerId} THEN '1' ELSE '0' END AS biz_request_flag,
        T1.content_json AS content_json,
        T1.skin_test_flag AS skin_test_flag,
        T1.infusion_flag AS inject_flag,
        T1.group_id AS group_id,
        T2.NAME AS advice_name,
        T3.total_volume AS volume,
        T1.lot_number AS lot_number,
        T1.quantity AS quantity,
        T1.unit_code AS unit_code,
        T1.status_enum AS status_enum,
        T1.method_code AS method_code,
        T1.rate_code AS rate_code,
        T1.dose AS dose,
        T1.dose_unit_code AS dose_unit_code,
        T4.id AS charge_item_id,
        T4.total_price AS total_price,
        T4.status_enum AS charge_status,
        al.id AS position_id,
        al.name AS position_name,
        T1.dispense_per_duration AS dispense_per_duration,
        T1.chinese_herbs_dose_quantity AS chinese_herbs_dose_quantity,
        T1.suffering_flag AS suffering_flag,
        T2.part_percent AS part_percent,
        ccd.name AS condition_definition_name
        FROM med_medication_request AS T1
        LEFT JOIN med_medication_definition AS T2 ON T2.ID = T1.medication_id
        AND T2.delete_flag = '0'
        LEFT JOIN med_medication AS T3 ON T3.medication_def_id = T2.ID
        AND T3.delete_flag = '0'
        LEFT JOIN adm_charge_item AS T4 ON T4.service_id = T1.ID AND T4.delete_flag = '0' AND
        T4.service_table = #{MED_MEDICATION_REQUEST}
        LEFT JOIN adm_location AS al ON al.ID = T1.perform_location AND al.delete_flag = '0'
        LEFT JOIN cli_condition AS cc ON cc.id = T1.condition_id AND cc.delete_flag = '0'
        LEFT JOIN cli_condition_definition AS ccd ON ccd.id = cc.definition_id
        WHERE T1.delete_flag = '0' AND T1.tcm_flag = 1
        <if test="historyFlag == '0'.toString()">
            AND T1.encounter_id = #{encounterId}
        </if>
        <if test="historyFlag == '1'.toString()">
            AND T1.patient_id = #{patientId} AND T1.encounter_id != #{encounterId}
        </if>
        AND T1.refund_medicine_id IS NULL
        ORDER BY T1.group_id,T1.prescription_no, T1.status_enum
    </select>

    <select id="getConditionIds" resultType="long">
        SELECT condition_id
        FROM adm_encounter_diagnosis
        WHERE delete_flag = '0'
          AND syndrome_group_no = #{syndromeGroupNo}
    </select>

    <select id="getAdviceNumByConditionIds" resultType="integer">
        SELECT
        count(1)
        FROM
        med_medication_request
        WHERE
        delete_flag = '0'
        <if test="conditionIds != null and !conditionIds.isEmpty()">
            AND condition_id IN
            <foreach collection="conditionIds" item="itemId" open="(" separator="," close=")">
                #{itemId}
            </foreach>
        </if>
    </select>

</mapper>