<?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.inhospitalnursestation.mapper.InpatientMedicineCollectionMapper">
    <select id="selectEncounterInfoListPage" resultType="com.openhis.web.inhospitalnursestation.dto.InpatientMedicineEncounterInfoDto">
        SELECT ii.tenant_id,
               ii.organization_id,
               ii.organization_name,
               ii.encounter_id,
               ii.bus_no,
               ii.patient_name,
               ii.patient_py_str,
               ii.patient_wb_str,
               ii.gender_enum,
               ii.birth_date,
               ii.inpatient_bed
        FROM (SELECT T1.tenant_id,
                     T1.organization_id,               --科室　
                     T3.name AS organization_name,
                     T1.id   AS encounter_id, --就诊id
                     T1.bus_no,               --住院号
                     T2.name AS patient_name, --患者姓名
                     T2.py_str AS patient_py_str,--拼音码
                     T2.wb_str AS patient_wb_str,--五笔码
                     T2.gender_enum,          --性别
                     T2.birth_date,           --生日
                     STRING_AGG(T5.NAME, '_' ORDER BY T5.bus_no) AS inpatient_bed --床位
              FROM  adm_encounter AS T1
                       LEFT JOIN adm_patient AS T2
                                 ON T1.patient_id = T2.id
                                     AND T2.delete_flag = '0'
                       LEFT JOIN adm_organization AS T3
                                 ON T1.organization_id = T3.id
                                     AND T3.delete_flag = '0'
                       LEFT JOIN adm_encounter_location AS T4
                                 ON T4.encounter_id = T1.id
                                     AND T4.status_enum = '2'
                                     AND T4.delete_flag = '0'
                       LEFT JOIN adm_location AS T5
                                 ON T5.id = T4.location_id
                                     AND T5.delete_flag = '0'
              WHERE T1.class_enum = #{amb}
                  --todo status_enum 的值
                AND T1.status_enum = 5
                 AND T1.delete_flag = '0'
              GROUP BY T1.tenant_id,
                       T1.organization_id,
                       T3.name,
                       T1.id,
                       T1.bus_no,
                       T2.name,
                       T2.py_str,
                       T2.wb_str,
                       T2.gender_enum,
                       T2.birth_date) AS ii
            ${ew.customSqlSegment}
    </select>
    <select id="selectPrescriptionInfoListPage" resultType="com.openhis.web.inhospitalnursestation.dto.InpatientMedicinePrescriptionInfoDto">
        SELECT ii.tenant_id,
               ii.dispense_id,--药品发放id
               ii.therapy_enum,--治疗类型
               ii.planned_dispense_time,--预定发放时间
               ii.encounter_id,--就诊id
               ii.patient_name,--患者姓名
               ii.status_enum,--发放状态
               ii.item_table,--项目表
               ii.item_id,--项目id
               ii.item_name,--项目名
               ii.quantity,--请求数量
               ii.unit_code,--请求单位编码
               ii.location_id,--发放地点
               ii.location_name,
               ii.lot_number,--批号
               ii.total_volume--规格
        FROM (SELECT T1.tenant_id,
                     T1.id AS  dispense_id,--发放id
                     T2.therapy_enum,--治疗类型
                     T1.planned_dispense_time,--预定发放时间
                     T1.encounter_id,--就诊id
                     T3.name AS patient_name,--患者姓名
                     T1.status_enum,--发放状态
                     #{medMedicationDefinition} AS   item_table,--项目表
                     T1.medication_id AS item_id,--项目id
                     T4.name AS item_name,--项目名
                     T1.quantity,--请求数量
                     T1.unit_code,--请求单位编码
                     T1.location_id,--发放地点
                     T6.name AS location_name,
                     T1.lot_number,--批号
                     T5.total_volume--规格
              FROM med_medication_dispense AS T1
                   LEFT JOIN med_medication_request AS T2
                          ON T1.med_req_id = T2.id
                          AND T2.delete_flag = '0'
                   LEFT JOIN adm_patient AS T3
                          ON T1.patient_id = T3.id
                          AND T3.delete_flag = '0'
                   LEFT JOIN med_medication_definition AS T4
                          ON T1.medication_id = T4.id
                          AND T4.delete_flag = '0'
                  LEFT JOIN med_medication AS T5
                          ON T4.id = T5.medication_def_id
                         AND T5.delete_flag = '0'
                  LEFT JOIN adm_location AS T6
                          ON T1.location_id = T6.id
                          AND T6.delete_flag = '0'
              WHERE T1.encounter_id IN
              <foreach collection="encounterIdList" item="item" separator="," open="(" close=")">
                  #{item}
              </foreach>
                  AND T1.status_enum = #{statusEnum}
                  AND T1.delete_flag = '0'
              GROUP BY T1.id,
                       T2.therapy_enum,
                       T3.name,
                       T4.name,
                       T6.name,
                       T5.total_volume
              UNION
               SELECT T1.tenant_id,
                      T1.id AS  dispense_id,--发放id
                      null AS therapy_enum,--治疗类型
                      T1.planned_dispense_time,--预定发放时间
                      T1.encounter_id,--就诊id
                      T3.name AS patient_name,--患者姓名
                      T1.status_enum,--发放状态
                      #{admDeviceDefinition} AS   item_table,--项目表
                      T1.device_def_id AS item_id,--项目id
                      T4.name AS item_name,--项目名
                      T1.quantity,--请求数量
                      T1.unit_code,--请求单位编码
                      T1.location_id,--发放地点
                      T6.name AS location_name,
                      T1.lot_number,--批号
                      T4.size AS total_volume--规格
               FROM wor_device_dispense AS T1
                      LEFT JOIN wor_device_request AS T2
                             ON T1.device_req_id = T2.id
                             AND T2.delete_flag = '0'
                      LEFT JOIN adm_patient AS T3
                             ON T1.patient_id = T3.id
                             AND T3.delete_flag = '0'
                      LEFT JOIN adm_device_definition AS T4
                             ON T1.device_def_id = T4.id
                             AND T4.delete_flag = '0'
                      LEFT JOIN adm_device AS T5
                             ON T4.id = T5.device_def_id
                             AND T5.delete_flag = '0'
                      LEFT JOIN adm_location AS T6
                             ON T1.location_id = T6.id
                             AND T6.delete_flag = '0'
               WHERE T1.encounter_id IN
               <foreach collection="encounterIdList" item="item" separator="," open="(" close=")">
                   #{item}
               </foreach>
                    AND T1.status_enum = #{statusEnum}
                    AND T1.delete_flag = '0'
               GROUP BY T1.id,
                        T3.name,
                        T4.name,
                        T6.name,
                        T4.size
              ) AS ii
        ${ew.customSqlSegment}
    </select>
    <select id="selectPrescriptionSummaryInfoListPage" resultType="com.openhis.web.inhospitalnursestation.dto.InpatientMedicinePrescriptionInfoDto">
        SELECT ii.tenant_id,
               ii.dispense_ids,
               ii.item_table,
               ii.item_id,
               ii.item_name,
               ii.quantity,
               ii.unit_code,
               ii.lot_number,
               ii.status_enum,
               ii.location_id,
               ii.location_enum
        FROM (SELECT T1.tenant_id,
                     string_agg(T1.id::text, ',' ORDER BY T1.id) AS dispense_ids,
                     #{medMedicationDefinition} AS   item_table,--项目表
                     T1.medication_id AS item_id,--项目id
                     T4.name AS item_name,--项目名
                     SUM(T1.quantity) AS quantity,--请求数量
                     T1.unit_code,--请求单位编码
                     T1.lot_number,--批号
                     T1.status_enum,--发放状态
                     T1.location_id,--发放地点
                     T6.form_enum AS location_enum--地点类型
              FROM med_medication_dispense AS T1
                    LEFT JOIN med_medication_request AS T2
                           ON T1.med_req_id = T2.id
                           AND T2.delete_flag = '0'
--                     LEFT JOIN adm_patient AS T3
--                            ON T1.patient_id = T3.id
--                            AND T3.delete_flag = '0'
                    LEFT JOIN med_medication_definition AS T4
                           ON T1.medication_id = T4.id
                           AND T4.delete_flag = '0'
                    LEFT JOIN med_medication AS T5
                           ON T4.id = T5.medication_def_id
                           AND T5.delete_flag = '0'
                    LEFT JOIN adm_location AS T6
                           ON T1.location_id = T6.id
                           AND T6.delete_flag = '0'
              WHERE  T1.id IN
              <foreach collection="medDispenseId" item="item" separator="," open="(" close=")">
                  #{item}
              </foreach>
                     AND T1.delete_flag = '0'
              GROUP BY T1.tenant_id,
                       T1.medication_id,
                       T1.unit_code,
                       T1.lot_number,
                       T1.location_id,
                       T4.name,
                       T6.form_enum
              UNION
              SELECT T1.tenant_id,
                     string_agg(T1.id::text, ',' ORDER BY T1.id) AS dispense_ids,
                     #{admDeviceDefinition} AS   item_table,--项目表
                     T1.device_def_id AS item_id,--项目id
                     T4.name AS item_name,--项目名
                     SUM(T1.quantity) AS quantity,--请求数量
                     T1.unit_code,--请求单位编码
                     T1.lot_number,--批号
                     T1.status_enum,--发放状态
                     T1.location_id,--发放地点
                     T6.form_enum AS location_enum--地点类型
              FROM wor_device_dispense AS T1
                   LEFT JOIN wor_device_request AS T2
                          ON T1.device_req_id = T2.id
                          AND T2.delete_flag = '0'
--                    LEFT JOIN adm_patient AS T3
--                           ON T1.patient_id = T3.id
--                           AND T3.delete_flag = '0'
                   LEFT JOIN adm_device_definition AS T4
                          ON T1.device_def_id = T4.id
                          AND T4.delete_flag = '0'
                   LEFT JOIN adm_device AS T5
                          ON T4.id = T5.device_def_id
                          AND T5.delete_flag = '0'
                   LEFT JOIN adm_location AS T6
                          ON T1.location_id = T6.id
                          AND T6.delete_flag = '0'
              WHERE  T1.id IN
              <foreach collection="devDispenseId" item="item" separator="," open="(" close=")">
                  #{item}
              </foreach>
              AND T1.delete_flag = '0'
              GROUP BY T1.tenant_id,
                       T1.device_def_id,
                       T1.unit_code,
                       T1.lot_number,
                       T1.location_id,
                       T4.name,
                       T6.form_enum) AS ii
        ${ew.customSqlSegment}
    </select>
</mapper>
