<?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.pharmacymanage.mapper.SummaryDispenseMedicineMapper">
    <select id="selectMedicationSummaryInfo" resultType="com.openhis.web.pharmacymanage.dto.MedicineSummaryDto">
        SELECT ii.tenant_id,
               ii.bus_no,--单据号
               ii.request_id,--供应请求id
               ii.delivery_id,--供应发放id
               ii.item_table,--项目表
               ii.item_name,--项目名
               ii.item_id,--项目id
               ii.total_volume,--规格
               ii.lot_number,--批号
               ii.quantity,--请求数
               ii.unit_code,--单位
               ii.max_unit_code,--常规单位
               ii.min_unit_code,--最小单位
               ii.source_location_id,--发放地点
               ii.source_location_name,
               ii.org_id,--申请科室
               ii.org_name,
               ii.applicant_id,--领药人
               ii.applicant_name,
               ii.apply_time,--申请时间
               ii.status_enum,--状态
               ii.patient_info --患者信息
        FROM (SELECT T1.tenant_id,
                     T1.bus_no,--单据号
                     T1.id                         AS request_id,--供应请求id
                     T2.id                         AS delivery_id,--供应发放id
                     T1.item_table,--项目表
                     T3.name                       AS item_name,--项目名
                     T1.item_id,--项目id
                     T4.total_volume,--规格
                     T1.lot_number,--批号
                     T1.item_quantity              AS quantity,--请求数
                     T1.unit_code,--单位
                     T3.unit_code                  AS max_unit_code,--常规单位
                     T3.min_unit_code,--最小单位
                     T1.source_location_id,--发放地点
                     T5.name                       AS source_location_name,
                     T1.purpose_location_id        AS org_id,--申请科室
                     T6.name                       AS org_name,
                     T1.applicant_id,--领药人
                     T7.name                       AS applicant_name,
                     T1.apply_time,--申请时间
                     T1.status_enum,--状态
                     STRING_AGG(CONCAT(T11.name, ' : ', T12.inpatient_bed), ',' ORDER BY T11.name,
                                T12.inpatient_bed) AS patient_info -- 患者信息
              FROM wor_supply_request T1
                       LEFT JOIN wor_supply_delivery AS T2
                                 ON T2.request_id = T1.id
                                     AND T2.delete_flag = '0'
                       LEFT JOIN med_medication_definition AS T3
                                 ON T1.item_id = T3.id
                                     AND T3.delete_flag = '0'
                       LEFT JOIN med_medication AS T4
                                 ON T3.id = T4.medication_def_id
                                     AND T4.delete_flag = '0'
                       LEFT JOIN adm_location AS T5
                                 ON T1.source_location_id = T5.id
                                     AND T5.delete_flag = '0'
                       LEFT JOIN adm_organization AS T6
                                 ON T1.purpose_location_id = T6.id
                                     AND T6.delete_flag = '0'
                       LEFT JOIN adm_practitioner AS T7
                                 ON T1.applicant_id = T7.id
                                     AND T7.delete_flag = '0'
                       LEFT JOIN (SELECT id,
                                         encounter_id,
                                         patient_id
                                  FROM (SELECT id,
                                               encounter_id,
                                               patient_id,
                                               ROW_NUMBER() OVER (PARTITION BY encounter_id ORDER BY id) AS rn
                                        FROM med_medication_dispense
                                        WHERE delete_flag = '0') t
                                  WHERE rn = 1) AS T8
                                 ON CONCAT(',', T2.based_on_ids, ',') LIKE CONCAT('%,', T8.id, ',%')
                       LEFT JOIN (SELECT STRING_AGG(T10.NAME, '_' ORDER BY T10.bus_no) AS inpatient_bed, T9.encounter_id
                                  FROM adm_encounter_location T9
                                           LEFT JOIN adm_location AS T10
                                                     ON T10.id = T9.location_id
                                                         AND T10.delete_flag = '0'
                                  WHERE T9.status_enum = '2'
                                    AND T9.delete_flag = '0'
                                  GROUP BY T9.encounter_id) AS T12 ON T8.encounter_id = T12.encounter_id
                       LEFT JOIN adm_patient AS T11
                                 ON T11.id = T8.patient_id
                                     AND T11.delete_flag = '0'
              WHERE T1.item_table = #{medicationDefinition}
                AND T1.type_enum = #{typeEnum}
                AND T1.category_enum = #{categoryEnum}
                AND T1.delete_flag = '0'
              GROUP BY T1.tenant_id,
                       T1.bus_no,
                       T1.id,
                       T2.id,
                       T1.item_table,
                       T3.name,
                       T1.item_id,
                       T4.total_volume,
                       T1.lot_number,
                       T1.item_quantity,
                       T1.unit_code,
                       T3.unit_code,
                       T3.min_unit_code,
                       T1.source_location_id,
                       T5.name,
                       T1.purpose_location_id,
                       T6.name,
                       T1.applicant_id,
                       T7.name,
                       T1.apply_time,
                       T1.status_enum
              UNION
              SELECT T1.tenant_id,
                     T1.bus_no,--单据号
                     T1.id                         AS request_id,--供应请求id
                     T2.id                         AS delivery_id,--供应发放id
                     T1.item_table,--项目表
                     T3.name                       AS item_name,--项目名
                     T1.item_id,--项目id
                     T3.size                       AS total_volume,--规格
                     T1.lot_number,--批号
                     T1.item_quantity              AS quantity,--请求数
                     T1.unit_code,--单位
                     T3.unit_code                  AS max_unit_code,--常规单位
                     T3.min_unit_code,--最小单位
                     T1.source_location_id,--发放地点
                     T5.name                       AS source_location_name,
                     T1.purpose_location_id        AS org_id,--申请科室
                     T6.name                       AS org_name,
                     T1.applicant_id,--领药人
                     T7.name                       AS applicant_name,
                     T1.apply_time,--申请时间
                     T1.status_enum,--状态
                     STRING_AGG(CONCAT(T11.name, ' : ', T12.inpatient_bed), ',' ORDER BY T11.name,
                                T12.inpatient_bed) AS patient_info -- 患者信息
              FROM wor_supply_request T1
                       LEFT JOIN wor_supply_delivery AS T2
                                 ON T2.request_id = T1.id
                                     AND T2.delete_flag = '0'
                       LEFT JOIN adm_device_definition AS T3
                                 ON T1.item_id = T3.id
                                     AND T3.delete_flag = '0'
                       LEFT JOIN adm_device AS T4
                                 ON T3.id = T4.device_def_id
                                     AND T4.delete_flag = '0'
                       LEFT JOIN adm_location AS T5
                                 ON T1.source_location_id = T5.id
                                     AND T5.delete_flag = '0'
                       LEFT JOIN adm_organization AS T6
                                 ON T1.purpose_location_id = T6.id
                                     AND T6.delete_flag = '0'
                       LEFT JOIN adm_practitioner AS T7
                                 ON T1.applicant_id = T7.id
                                     AND T7.delete_flag = '0'
                       LEFT JOIN (SELECT id,
                                         encounter_id,
                                         patient_id
                                  FROM (SELECT id,
                                               encounter_id,
                                               patient_id,
                                               ROW_NUMBER() OVER (PARTITION BY encounter_id ORDER BY id) AS rn
                                        FROM wor_device_dispense
                                        WHERE delete_flag = '0') t
                                  WHERE rn = 1) AS T8
                                 ON CONCAT(',', T2.based_on_ids, ',') LIKE CONCAT('%,', T8.id, ',%')
                       LEFT JOIN (SELECT STRING_AGG(T10.NAME, '_' ORDER BY T10.bus_no) AS inpatient_bed, T9.encounter_id
                                  FROM adm_encounter_location T9
                                           LEFT JOIN adm_location AS T10
                                                     ON T10.id = T9.location_id
                                                         AND T10.delete_flag = '0'
                                  WHERE T9.status_enum = '2'
                                    AND T9.delete_flag = '0'
                                  GROUP BY T9.encounter_id) AS T12 ON T8.encounter_id = T12.encounter_id
                       LEFT JOIN adm_patient AS T11
                                 ON T11.id = T8.patient_id
                                     AND T11.delete_flag = '0'
              WHERE T1.item_table = #{deviceDefinition}
                AND T1.type_enum = #{typeEnum}
                AND T1.category_enum = #{categoryEnum}
                AND T1.delete_flag = '0'
              GROUP BY T1.tenant_id,
                       T1.bus_no,
                       T1.id,
                       T2.id,
                       T1.item_table,
                       T3.name,
                       T1.item_id,
                       T3.size,
                       T1.lot_number,
                       T1.item_quantity,
                       T1.unit_code,
                       T3.unit_code,
                       T3.min_unit_code,
                       T1.source_location_id,
                       T5.name,
                       T1.purpose_location_id,
                       T6.name,
                       T1.applicant_id,
                       T7.name,
                       T1.apply_time,
                       T1.status_enum) AS ii
            ${ew.customSqlSegment}
    </select>


    <!--    <select id="selectEncounterInfoListPage" resultType="com.openhis.web.pharmacymanage.dto.InpatientMedicineEncounterInfoDto">-->
    <!--        SELECT ii.tenant_id,-->
    <!--               ii.org_id,-->
    <!--               ii.therapy_enum,-->
    <!--               ii.encounter_id,-->
    <!--               ii.bus_no,-->
    <!--               ii.patient_name,-->
    <!--               ii.gender_enum,-->
    <!--               ii.birth_date,-->
    <!--               ii.inpatient_bed,-->
    <!--               ii.effective_dose_start-->
    <!--        FROM (SELECT T1.tenant_id,-->
    <!--                     T1.org_id,               &#45;&#45;科室-->
    <!--                     T1.therapy_enum,         &#45;&#45;治疗类型  1:临时医嘱,2:长期医嘱,3:备用医嘱　	　-->
    <!--                     T3.id   AS encounter_id, &#45;&#45;就诊id-->
    <!--                     T3.bus_no,               &#45;&#45;住院号-->
    <!--                     T2.name AS patient_name, &#45;&#45;患者姓名-->
    <!--                     T2.gender_enum,          &#45;&#45;性别-->
    <!--                     T2.birth_date,           &#45;&#45;生日-->
    <!--                     STRING_AGG(T5.NAME, '_' ORDER BY T5.bus_no) AS inpatient_bed, &#45;&#45;床位-->
    <!--                     MIN(T1.effective_dose_start) AS effective_dose_start  &#45;&#45;请求开始时间-->
    <!--              FROM  med_medication_request AS T1-->
    <!--                       LEFT JOIN adm_patient AS T2-->
    <!--                                 ON T1.patient_id = T2.id-->
    <!--                                     AND T2.delete_flag = '0'-->
    <!--                       INNER JOIN adm_encounter AS T3-->
    <!--                                  ON T1.encounter_id = T3.id-->
    <!--                                      AND T3.class_enum = #{amb}-->
    <!--                                      AND T3.status_enum = 5-->
    <!--                                      AND T3.delete_flag = '0'-->
    <!--                       LEFT JOIN adm_encounter_location AS T4-->
    <!--                                 ON T4.encounter_id = T3.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.delete_flag = '0'-->
    <!--              GROUP BY T1.tenant_id,-->
    <!--                       T1.org_id,-->
    <!--                       T1.therapy_enum,-->
    <!--                       T3.id,-->
    <!--                       T3.bus_no,-->
    <!--                       T2.name,-->
    <!--                       T2.gender_enum,-->
    <!--                       T2.birth_date) AS ii-->
    <!--            ${ew.customSqlSegment}-->
    <!--    </select>-->
    <!--    <select id="selectPrescriptionInfoListPage" resultType="com.openhis.web.pharmacymanage.dto.InpatientMedicinePrescriptionInfoDto">-->
    <!--        SELECT ii.tenant_id,-->
    <!--               ii.request_id,&#45;&#45;药品请求id-->
    <!--               ii.encounter_id,&#45;&#45;就诊id-->
    <!--               ii.bus_no,  &#45;&#45;单据号-->
    <!--               ii.status_enum, &#45;&#45;状态-->
    <!--               ii.patient_name,&#45;&#45;患者姓名-->
    <!--               ii.medication_name,&#45;&#45;药品名-->
    <!--               ii.effective_dose_start,&#45;&#45;医嘱开始时间-->
    <!--               ii.effective_dose_end,&#45;&#45;医嘱结束时间-->
    <!--               ii.req_authored_time &#45;&#45;生成日期-->
    <!--        FROM (SELECT T1.tenant_id,-->
    <!--                     T1.id AS request_id,&#45;&#45;药品请求id-->
    <!--                     T1.encounter_id,&#45;&#45;就诊id-->
    <!--                     T1.bus_no,  &#45;&#45;单据号-->
    <!--                     T1.status_enum, &#45;&#45;状态-->
    <!--                     T2.name AS patient_name,&#45;&#45;患者姓名-->
    <!--                     T3.name AS medication_name,&#45;&#45;药品名-->
    <!--                     T1.effective_dose_start,&#45;&#45;医嘱开始时间-->
    <!--                     T1.effective_dose_end,&#45;&#45;医嘱结束时间-->
    <!--                     T1.req_authored_time &#45;&#45;生成日期-->
    <!--              FROM med_medication_request AS T1-->
    <!--                   LEFT JOIN adm_patient AS T2-->
    <!--                          ON T1.patient_id = T2.id-->
    <!--                          AND T2.delete_flag = '0'-->
    <!--                   LEFT JOIN med_medication_definition AS T3-->
    <!--                          ON T1.medication_id = T3.id-->
    <!--                          AND T3.delete_flag = '0'-->
    <!--              WHERE T1.encounter_id IN-->
    <!--              <foreach collection="encounterIdList" item="item" separator="," open="(" close=")">-->
    <!--                  #{item}-->
    <!--              </foreach>-->
    <!--                  AND T1.delete_flag = '0'-->
    <!--              GROUP BY T1.id,-->
    <!--                       T2.name,-->
    <!--                       T3.name) AS ii-->
    <!--        ${ew.customSqlSegment}-->
    <!--    </select>-->
    <!--    <select id="selectMedicationSummaryHeadInfo" resultType="com.openhis.web.pharmacymanage.dto.InpatientMedicineSummaryInfoDtoryInfoDto SELECT T2.dispense_time,&#45;&#45;发药日期-->
    <!--               T2.rec_practitioner_id,&#45;&#45;领药人-->
    <!--               T3.name  AS rec_practitioner_name,-->
    <!--               SUM(T5.total_price) AS  totalPrice,&#45;&#45;金额合计-->
    <!--               COUNT(DISTINCT T1.medication_id) AS  medVarietyCount,&#45;&#45;药品种数-->
    <!--               COUNT(T1.id)    AS    aggregated_order_count,&#45;&#45;汇总医嘱数-->
    <!--               T4.name    AS department&#45;&#45;科室病区-->
    <!--        FROM med_medication_request AS T1-->
    <!--                 LEFT JOIN med_medication_dispense AS T2-->
    <!--                           ON T2.med_req_id = T1.id-->
    <!--                               AND T2.delete_flag = '0'-->
    <!--                 LEFT JOIN adm_practitioner AS T3-->
    <!--                           ON T2.rec_practitioner_id = T3.id-->
    <!--                               AND T3.delete_flag = '0'-->
    <!--                 LEFT JOIN adm_organization AS T4-->
    <!--                           ON T3.org_id = T4.id-->
    <!--                               AND T4.delete_flag = '0'-->
    <!--                 LEFT JOIN adm_charge_item AS T5-->
    <!--                           ON T1.id = T5.service_id-->
    <!--                               AND T5.service_table = #{medicationRequest}-->
    <!--                               AND T5.delete_flag = '0'-->
    <!--        WHERE T1.id IN-->
    <!--            <foreach collection="requestIdList" item="item" separator="," open="(" close=")">-->
    <!--                #{item}-->
    <!--            </foreach>-->
    <!--          AND T1.delete_flag = '0'-->
    <!--        GROUP BY  T2.dispense_time,-->
    <!--                  T2.rec_practitioner_id,-->
    <!--                  T3.name,-->
    <!--                  T4.name-->
    <!--    </select>-->
<!--    <select id="selectMedicationSummaryInfo" resultType="com.openhis.web.pharmacymanage.dto.MedicineSummaryInfoDtoECT T2.name             AS med_name,&#45;&#45;药品名-->
<!--               T3.total_volume,&#45;&#45;规格-->
<!--               T5.name             AS supplier_name,&#45;&#45;供应商-->
<!--               CASE T1.unit_code-->
<!--               WHEN T2.min_unit_code THEN T8.amount * T2.part_percent-->
<!--               ELSE  T8.amount  END          AS purchase_price,&#45;&#45;采购价-->
<!--               CASE T1.unit_code-->
<!--               WHEN T2.min_unit_code THEN T9.amount * T2.part_percent-->
<!--               ELSE  T9.amount  END        AS unit_price,&#45;&#45;单价-->
<!--               T1.lot_number,&#45;&#45;批号-->
<!--               CASE T1.unit_code-->
<!--               WHEN T2.min_unit_code THEN T4.quantity-->
<!--               ELSE  T4.quantity/T2.part_percent  END   AS stock_quantity,&#45;&#45;库存数-->
<!--               SUM(CASE WHEN T1.status_enum = #{active} THEN T1.quantity ELSE 0 END) AS pending_quantity,&#45;&#45;待发药数-->
<!--               SUM(CASE WHEN T1.status_enum = #{completed} THEN T1.quantity ELSE 0 END) AS dispensed_quantity,&#45;&#45;已发药数-->
<!--               SUM(CASE WHEN T1.status_enum = #{cancelled} THEN T1.quantity ELSE 0 END) AS returned_quantity,&#45;&#45;已退药数-->
<!--               T1.unit_code, &#45;&#45;单位-->
<!--               T1.status_enum,&#45;&#45;状态-->
<!--               T1.trace_no&#45;&#45;追溯码-->
<!--        FROM med_medication_request AS T1-->
<!--                 LEFT JOIN med_medication_definition AS T2-->
<!--                           ON T1.medication_id = T2.id-->
<!--                               AND T2.delete_flag = '0'-->
<!--                 LEFT JOIN med_medication AS T3-->
<!--                           ON T2.id = T3.medication_def_id-->
<!--                               AND T3.delete_flag = '0'-->
<!--                 LEFT JOIN wor_inventory_item AS T4-->
<!--                           ON T4.item_id = T2.id-->
<!--                               AND T4.item_table = #{medicationDefinition}-->
<!--                               AND T1.lot_number = T4.lot_number-->
<!--                               AND T4.delete_flag = '0'-->
<!--                 LEFT JOIN adm_supplier AS T5-->
<!--                           ON T4.supplier_id = T5.id-->
<!--                               AND T5.delete_flag = '0'-->
<!--                 LEFT JOIN adm_charge_item AS T6-->
<!--                           ON T6.service_id = T1.id-->
<!--                               AND T6.service_table = #{medicationRequest}-->
<!--                               AND T6.delete_flag = '0'-->
<!--                LEFT JOIN adm_charge_item_definition T7-->
<!--                           ON T7.instance_id = T2.id-->
<!--                               AND T7.instance_table = #{medicationDefinition}-->
<!--                               AND T7.delete_flag = '0'-->
<!--                LEFT JOIN adm_charge_item_def_detail T8-->
<!--                           ON T8.definition_id = T7.id-->
<!--                              AND T8.condition_code = '1'&#45;&#45;1:批号进价-->
<!--                              AND T8.condition_value = T1.lot_number-->
<!--                              AND T8.delete_flag = '0'-->
<!--                LEFT JOIN adm_charge_item_def_detail T9-->
<!--                           ON T9.definition_id = T7.id-->
<!--                              AND T9.condition_code = '5'&#45;&#45;1:批号售价-->
<!--                              AND T9.condition_value = T1.lot_number-->
<!--                              AND T9.delete_flag = '0'-->
<!--        WHERE T1.id IN-->
<!--               <foreach collection="requestIdList" item="item" separator="," open="(" close=")">-->
<!--                   #{item}-->
<!--               </foreach>-->
<!--          AND T1.delete_flag = '0'-->
<!--        GROUP BY T2.name,-->
<!--                 T3.total_volume,-->
<!--                 T5.name,-->
<!--                 T1.unit_code,-->
<!--                 T2.min_unit_code,-->
<!--                 T8.amount,-->
<!--                 T2.part_percent,-->
<!--                 T9.amount,-->
<!--                 T1.lot_number,-->
<!--                 T4.quantity,-->
<!--                 T1.status_enum,-->
<!--                 T1.trace_no-->
<!--    </select>-->






<!--    <select id="selectMedicationPatientInfoList" resultType="com.openhis.web.pharmacymanage.dto.InpatientMedicineEncounterInfoDto">-->
<!--        SELECT T3.bus_no,&#45;&#45;住院号-->
<!--               T4.name   AS patient_name,&#45;&#45;患者姓名-->
<!--               T2.bus_no AS med_request_bus_no,&#45;&#45;发药单号-->
<!--               T6.name   AS occurrence_name,&#45;&#45;发药人-->
<!--               T5.occurrence_time,&#45;&#45;发药时间-->
<!--               T2.status_enum&#45;&#45;状态-->
<!--        FROM wor_supply_request AS T1-->
<!--                 INNER JOIN med_medication_request AS T2-->
<!--                            ON T1.item_id = T2.id-->
<!--                                AND T2.delete_flag = '0'-->
<!--                 INNER JOIN adm_encounter AS T3-->
<!--                            ON T2.encounter_id = T3.id-->
<!--                                AND T3.delete_flag = '0'-->
<!--                 LEFT JOIN adm_patient AS T4-->
<!--                           ON T1.patient_id = T4.id-->
<!--                               AND T4.delete_flag = '0'-->
<!--                 LEFT JOIN wor_supply_delivery AS T5-->
<!--                           ON T5.request_id = T1.id-->
<!--                               AND T5.delete_flag = '0'-->
<!--                 LEFT JOIN adm_practitioner AS T6-->
<!--                           ON T5.practitioner_id = T6.id-->
<!--                               AND T6.delete_flag = '0'-->
<!--        WHERE T1.bus_no = #{busNo}-->
<!--          AND T1.item_table = #{medicationRequest}-->
<!--          AND T1.delete_flag = '0'-->
<!--        GROUP BY T3.bus_no,-->
<!--                 T4.name,-->
<!--                 T2.bus_no,-->
<!--                 T6.name,-->
<!--                 T5.occurrence_time,-->
<!--                 T2.status_enum-->
<!--    </select>-->

</mapper>
