<?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.reportmanage.mapper.MedicationDeviceReportMapper">

    <select id="selectMedDdevInfo"
            resultType="com.openhis.web.reportmanage.dto.MedUseReportInfoDto">
               SELECT
                   name,
                   total_volume,
                   lot_number,
                   manufacturer_text,
                   org_id,
                   SUM(total_quantity)            AS total_quantity,
                   unit_code,
                   unit_price,
                   SUM(total_money)               AS total_money,
                   dispense_time
               FROM (
                        /* ---------- 第一段查询 ---------- */
                        SELECT
                            name,
                            total_volume,
                            lot_number,
                            manufacturer_text,
                            total_quantity,
                            unit_code,
                            unit_price,
                            total_money,
                            org_id,
                            dispense_time
                        FROM (
                                 SELECT
                                     T3.name,
                                     T7.total_volume,
                                     T1.lot_number,
                                     T3.manufacturer_text,
                                     T1.quantity       AS total_quantity,
                                     T1.unit_code unit_code,
                                     T8.unit_price,
                                     T8.total_price AS total_money,
                                     T1.org_id,
                                     T2.dispense_time
                                 FROM med_medication_request T1
                                          LEFT JOIN med_medication_dispense T2
                                                    ON T2.med_req_id = T1.id AND T2.delete_flag = '0'
                                          LEFT JOIN med_medication_definition T3
                                                    ON T3.id = T1.medication_id AND T3.delete_flag = '0'
                                          LEFT JOIN med_medication T7
                                                    ON T3.id = T7.medication_def_id AND T7.delete_flag = '0'
                                          LEFT JOIN adm_charge_item T8
                                                    ON T8.service_table = #{medReqTableName} --'med_medication_request'
                                                        AND T8.service_id = T1.id
                                                        AND T8.delete_flag = '0'
                                          LEFT JOIN med_medication_request T17
                                                    on T1.id = T17.refund_medicine_id
                                                        AND T17.delete_flag = '0'
                                 WHERE T1.status_enum = #{completed} --'3'
                                   AND T2.status_enum = #{eventStatus} --'4'
                                   AND T1.delete_flag = '0'
                                   AND TO_CHAR(T2.dispense_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{dispenseDateSTime} AND #{dispenseDateETime}
                                   AND T1.refund_medicine_id IS NULL
                                   AND T17.refund_medicine_id IS NULL

                                 UNION ALL

                                 SELECT
                                     T6.name,
                                     T6.size         AS total_volume,
                                     T4.lot_number,
                                     T6.manufacturer_text,
                                     T4.quantity               AS total_quantity,
                                     T4.unit_code              AS unit_code,
                                     T9.unit_price             AS unit_price,
                                     T9.total_price AS total_money,
                                     T4.org_id,
                                     T5.dispense_time
                                 FROM wor_device_request T4
                                          LEFT JOIN wor_device_dispense T5
                                                    ON T5.device_req_id = T4.id AND T5.delete_flag = '0'
                                          LEFT JOIN adm_device_definition T6
                                                    ON T6.id = T5.device_def_id AND T6.delete_flag = '0'
                                          LEFT JOIN adm_charge_item T9
                                                    ON T9.service_table = #{devReqTableName} --'wor_device_request'
                                                        AND T9.service_id = T4.id
                                                        AND T9.delete_flag = '0'
                                          LEFT JOIN wor_device_request T17
                                                    on T4.id = T17.refund_device_id
                                                        AND T17.delete_flag = '0'
                                 WHERE T4.status_enum =  #{completed} --'3'
                                   AND T5.status_enum = #{eventStatus} --'4'
                                   AND T4.delete_flag = '0'
                                   AND TO_CHAR(T5.dispense_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{dispenseDateSTime} AND #{dispenseDateETime}
                                   AND T4.refund_device_id IS NULL
                                   AND T17.refund_device_id IS NULL
                             ) AS q1

                        UNION ALL

                        /* ---------- 第二段查询 ---------- */
                        SELECT
                            name,
                            total_volume,
                            lot_number,
                            manufacturer_text,
                            total_quantity,
                            unit_code,
                            unit_price,
                            total_money,
                            org_id,
                            dispense_time
                        FROM (
                                 SELECT
                                     name,
                                     total_volume,
                                     lot_number,
                                     manufacturer_text,
                                     org_id,
                                     SUM(quantity)  AS total_quantity,
                                     unit_code,
                                     price AS unit_price,
                                     SUM(total_price) AS total_money,
                                     dispense_time
                                 FROM (
                                          SELECT
                                              T10.name,
                                              T11.total_volume,
                                              T9.lot_number,
                                              T10.manufacturer_text,
                                              CASE WHEN T9.type_enum = #{returnIssue} THEN T9.item_quantity * -1
                                                   ELSE T9.item_quantity END AS quantity,
                                              T9.unit_code,
                                              T9.price,
                                              CASE WHEN T9.type_enum = #{returnIssue} THEN T9.total_price * -1
                                                   ELSE T9.total_price END AS total_price,
                                              CASE WHEN T9.type_enum = #{returnIssue} THEN T9.source_location_id
                                                   ELSE T9.purpose_location_id END AS org_id,
                                              T12.occurrence_time AS dispense_time
                                          FROM wor_supply_request T9
                                                   LEFT JOIN med_medication_definition T10
                                                             ON T9.item_id = T10.id AND T10.delete_flag = '0'
                                                   LEFT JOIN med_medication T11
                                                             ON T10.id = T11.medication_def_id AND T11.delete_flag = '0'
                                                   LEFT JOIN wor_supply_delivery T12
                                                             ON T12.request_id = T9.id AND T12.delete_flag = '0'
                                          WHERE (T9.type_enum = #{returnIssue} OR T9.type_enum = #{issueInventory})
                                            AND T9.item_table = #{medDefTableName} --'med_medication_definition'
                                            AND T12.status_enum = #{eventStatus}--'4'
                                            AND T9.delete_flag = '0'
                                            AND TO_CHAR(T12.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{dispenseDateSTime} AND #{dispenseDateETime}

                                          UNION ALL

                                          SELECT
                                              T14.name,
                                              T14.size AS total_volume,
                                              T13.lot_number,
                                              T14.manufacturer_text,
                                              CASE WHEN T13.type_enum = #{returnIssue} THEN T13.item_quantity * -1
                                                   ELSE T13.item_quantity END AS quantity,
                                              T13.unit_code,
                                              T13.price,
                                              CASE WHEN T13.type_enum = #{returnIssue} THEN T13.total_price * -1
                                                   ELSE T13.total_price END AS total_price,
                                              CASE WHEN T13.type_enum = #{returnIssue} THEN T13.source_location_id
                                                   ELSE T13.purpose_location_id END AS org_id,
                                              T15.occurrence_time AS dispense_time
                                          FROM wor_supply_request T13
                                                   LEFT JOIN adm_device_definition T14
                                                             ON T13.item_id = T14.id AND T14.delete_flag = '0'
                                                   LEFT JOIN wor_supply_delivery T15
                                                             ON T15.request_id = T13.id AND T15.delete_flag = '0'
                                          WHERE (T13.type_enum = #{returnIssue} OR T13.type_enum = #{issueInventory})
                                            AND T13.item_table = #{devDefTableName} --'adm_device_definition'
                                            AND T15.status_enum =  #{eventStatus}--'4'
                                            AND T13.delete_flag = '0'
                                            AND TO_CHAR(T15.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{dispenseDateSTime} AND #{dispenseDateETime}
                                      ) AS q2
                                 GROUP BY
                                     name,
                                     total_volume,
                                     lot_number,
                                     manufacturer_text,
                                     org_id,
                                     unit_code,
                                     price,
                                     dispense_time
                             ) AS q3
                    ) AS merged
               WHERE org_id = #{orgId}
               <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>

               GROUP BY
                   name,
                   total_volume,
                   lot_number,
                   manufacturer_text,
                   org_id,
                   unit_code,
                   unit_price,
                   dispense_time
               ORDER BY
                   dispense_time DESC
    </select>



    <select id="selectMedDevReport"
            resultType="com.openhis.web.reportmanage.dto.MedDevUseReportDto">
        SELECT org_id,
               org_name,
               SUM(total_money) AS total_money
        FROM (
                 /* ---------- 第一段：药品申领 + 耗材申领 ---------- */
                 SELECT T1.org_id,
                        T16.name                                 AS org_name,
                        SUM(T8.total_price) AS total_money
                 FROM med_medication_request T1
                          LEFT JOIN med_medication_dispense T2
                                    ON T2.med_req_id = T1.id
                                        AND T2.delete_flag = '0'
                                        /* 时间区间 */
                                        AND
                                       TO_CHAR(T2.dispense_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{dispenseDateSTime} AND #{dispenseDateETime}
                          LEFT JOIN adm_charge_item T8
                                    ON T8.service_table = #{medReqTableName} --'med_medication_request'
                                        AND T8.service_id = T1.id
                                        AND T8.delete_flag = '0'
                          LEFT JOIN med_medication_request T17
                                    on T1.id = T17.refund_medicine_id
                                        AND T17.delete_flag = '0'
                          LEFT JOIN adm_organization T16
                                    ON T1.org_id = T16.id
                                        AND T16.delete_flag = '0'
                 WHERE T1.status_enum = #{completed} --'3'
                   AND T2.status_enum = #{eventStatus} --'4'
                   AND T1.delete_flag = '0'
                     /* 确保有发放记录才纳入汇总 */
                   AND T2.dispense_time IS NOT NULL
                   AND T1.refund_medicine_id IS NULL
                   AND T17.refund_medicine_id IS NULL
                 GROUP BY T1.org_id,
                          T16.name

                 UNION ALL

                 SELECT T4.org_id,
                        T16.name                                 AS org_name,
                        SUM(T9.total_price) AS total_money
                 FROM wor_device_request T4
                          LEFT JOIN wor_device_dispense T5
                                    ON T5.device_req_id = T4.id
                                        AND T5.delete_flag = '0'
                                        /* 时间区间 */
                                        AND
                                       TO_CHAR(T5.dispense_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{dispenseDateSTime} AND #{dispenseDateETime}
                          LEFT JOIN adm_charge_item T9
                                    ON T9.service_table = #{devReqTableName} --'wor_device_request'
                                        AND T9.service_id = T4.id
                                        AND T9.delete_flag = '0'
                          LEFT JOIN wor_device_request T17
                                    on T4.id = T17.refund_device_id
                                        AND T17.delete_flag = '0'
                          LEFT JOIN adm_organization T16
                                    ON T4.org_id = T16.id
                                        AND T16.delete_flag = '0'
                 WHERE T4.status_enum = #{completed} --'3'
                   AND T5.status_enum = #{eventStatus} --'4'
                   AND T4.delete_flag = '0'
                   AND T5.dispense_time IS NOT NULL
                   AND T4.refund_device_id IS NULL
                   AND T17.refund_device_id IS NULL
                 GROUP BY T4.org_id,
                          T16.name

                 /* ---------- 第二段：supply_request ---------- */
                 UNION ALL

                 SELECT CASE
                            WHEN T9.type_enum = '9' THEN T9.source_location_id
                            ELSE T9.purpose_location_id END AS org_id,
                        CASE
                            WHEN T9.type_enum = '9' THEN T16.name
                            ELSE T17.name END               AS org_name,
                        SUM(CASE
                                WHEN T9.type_enum = '9' THEN T9.total_price * -1
                                ELSE T9.total_price END)    AS total_money
                 FROM wor_supply_request T9
                          LEFT JOIN wor_supply_delivery T12
                                    ON T12.request_id = T9.id
                                        AND T12.delete_flag = '0'
                                        /* 时间区间 */
                                        AND
                                       TO_CHAR(T12.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{dispenseDateSTime} AND #{dispenseDateETime}
                          LEFT JOIN adm_organization T16
                                    ON T9.source_location_id = T16.id
                                        AND T16.delete_flag = '0'
                          LEFT JOIN adm_organization T17
                                    ON T9.purpose_location_id = T17.id
                                        AND T17.delete_flag = '0'
                 WHERE (T9.type_enum = '9' OR T9.type_enum = '7')
                   AND T9.item_table IN (#{medDefTableName}, #{devDefTableName})--('med_medication_definition','adm_device_definition')
                   AND T12.status_enum = #{eventStatus}--'4'
                   AND T9.delete_flag = '0'
                   AND T12.occurrence_time IS NOT NULL
                 GROUP BY CASE
                              WHEN T9.type_enum = '9' THEN T9.source_location_id
                              ELSE T9.purpose_location_id END,
                          CASE
                              WHEN T9.type_enum = '9' THEN T16.name
                              ELSE T17.name END) AS merged
        GROUP BY org_id,
                 org_name
        ORDER BY org_id
    </select>

</mapper>
