<?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.DepartmentRevenueStatisticsMapper">
    <!--门诊收入明细-->
    <select id="selectPage"
            resultType="com.openhis.web.reportmanage.dto.DepartmentRevenueStatisticsPageDto">
        select A.medication_name,
               A.bus_no,
               A.yb_no,
               A.total_volume,
               A.unit_code,
               A.dispense_quantity,
               A.dispense_price,
               A.category_code,
               A.dispense_time,
               A.tenant_id
        from (
                SELECT def."name"           AS medication_name,
                       def.bus_no,
                       def.yb_no,
                       mm.total_volume,
                       mmd.unit_code,
                       SUM( mmd.dispense_quantity) AS dispense_quantity,
                       SUM( aci.total_price )     AS dispense_price,
                       def.category_code,
                       mmd.dispense_time,
                       mmd.tenant_id
                FROM med_medication_dispense mmd
                  LEFT JOIN med_medication_request mmr
                    ON mmd.med_req_id = mmr.id
                    AND mmr.delete_flag = '0'
                  INNER JOIN adm_charge_item aci
                    ON aci.service_id = mmr.id
                    AND aci.delete_flag = '0'
                  LEFT JOIN med_medication_definition def
                    ON mmd.medication_id = def.id
                    AND def.delete_flag = '0'
                  LEFT JOIN med_medication mm
                    ON mm.medication_def_id = def.id
                    AND mm.delete_flag = '0'
                  LEFT JOIN med_medication_request mmrr
                    on mmr.id = mmrr.refund_medicine_id
                    AND mmrr.delete_flag = '0'
                WHERE mmd.status_enum = #{disCompleted}
                 AND mmr.status_enum = #{reqCompleted}
                 AND mmr.refund_medicine_id IS NULL
                 AND mmrr.refund_medicine_id IS NULL
                 AND mmd.delete_flag = '0'
                <if test="antibioticFlg == 1" >
                    AND def.antibiotic_flag = 1
                </if>
                <if test="antibioticFlg == 0" >
                    AND def.antibiotic_flag = 0
                </if>
                GROUP BY def."name",
                         def.bus_no,
                         def.yb_no,
                         mm.total_volume,
                         mmd.unit_code,
                         def.category_code,
                         mmd.dispense_time,
                         mmd.tenant_id
        ) AS A
        ${ew.customSqlSegment}
        ORDER BY A.bus_no DESC
    </select>
</mapper>