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

    <!--期初金额 todo：定时任务每日凌晨批量盘库，做成盘点时间occurrence_time是 yyyy-MM-dd 00:00:00，-->
    <select id="selectAmount" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(T1.price * COALESCE(T1.total_quantity, 0)), 0) AS total_amount
        FROM wor_aut_supply_request T1
        WHERE T1.purpose_location_id = #{locationId}
          AND T1.type_enum = '44' -- 商品批量盘点
          AND TO_CHAR(T1.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') = #{time}
          AND T1.delete_flag = '0'
    </select>

    <!--采购入库金额 -->
    <select id="selectAmountNumber"
            resultType="com.openhis.web.reportmanage.dto.AmountNumberDto">
        SELECT
            COALESCE(SUM(total_price), 0) AS total_amount,
            COUNT(DISTINCT bus_no) AS total_number
        FROM wor_supply_request T1
                 INNER JOIN wor_supply_delivery T2 ON T1.id = T2.request_id AND T2.delete_flag = '0'
        WHERE T1.purpose_location_id = #{locationId}
          AND T1.type_enum = #{typeEnum}  -- 1:采购入库
          AND T1.status_enum = #{agree}--同意
          AND T2.status_enum = #{completed}--已完成
          AND TO_CHAR(T2.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startTime} AND #{endTime}
          AND T1.delete_flag = '0'
    </select>

    <!--调拨入库查询 -->
    <select id="selectTransferInAmountNumber"
            resultType="com.openhis.web.reportmanage.dto.AmountNumberDto">
        SELECT
            COALESCE(SUM(total_price), 0) AS total_amount,
            COUNT(DISTINCT bus_no) AS total_number
        FROM wor_supply_request T1
                 INNER JOIN wor_supply_delivery T2 ON T1.id = T2.request_id AND T2.delete_flag = '0'
        WHERE T1.purpose_location_id = #{locationId}
          AND T1.type_enum IN (#{productTransfer} ,#{productBatchTransfer} )
          AND T1.status_enum = #{agree}--同意
          AND T2.status_enum = #{completed}--已完成
          AND TO_CHAR(T2.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startTime} AND #{endTime}
          AND T1.delete_flag = '0'
    </select>

    <!--调拨出库查询 -->
    <select id="selectTransferOutAmountNumber"
            resultType="com.openhis.web.reportmanage.dto.AmountNumberDto">
        SELECT
            COALESCE(SUM(total_price), 0) AS total_amount,
            COUNT(DISTINCT bus_no) AS total_number
        FROM wor_supply_request T1
                 INNER JOIN wor_supply_delivery T2 ON T1.id = T2.request_id AND T2.delete_flag = '0'
        WHERE T1.source_location_id = #{locationId}
          AND T1.type_enum IN (#{productTransfer} ,#{productBatchTransfer} )
          AND T1.status_enum = #{agree}--同意
          AND T2.status_enum = #{completed}--已完成
          AND TO_CHAR(T2.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startTime} AND #{endTime}
          AND T1.delete_flag = '0'
    </select>

    <!--盘点查询 -->
    <select id="selectProfitLossAmountNumber"
            resultType="com.openhis.web.reportmanage.dto.AmountNumberDto">
        SELECT
            COALESCE(SUM(total_price), 0) AS total_amount,
            COUNT(DISTINCT bus_no) AS total_number
        FROM wor_supply_request T1
                 INNER JOIN wor_supply_delivery T2 ON T1.id = T2.request_id AND T2.delete_flag = '0'
        WHERE T1.purpose_location_id = #{locationId}
          AND T1.type_enum IN (#{productStocktaking} ,#{productBatchStocktaking} )
          AND T1.status_enum = #{agree}--同意
          AND T2.status_enum = #{completed}--已完成
          AND TO_CHAR(T2.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startTime} AND #{endTime}
          AND T1.delete_flag = '0'
    </select>

    <!--报损单查询 -->
    <select id="selectLossAmountNumber"
            resultType="com.openhis.web.reportmanage.dto.AmountNumberDto">
        SELECT
            COALESCE(SUM(total_price), 0) AS total_amount,
            COUNT(DISTINCT bus_no) AS total_number
        FROM wor_supply_request T1
                 INNER JOIN wor_supply_delivery T2 ON T1.id = T2.request_id AND T2.delete_flag = '0'
        WHERE T1.purpose_location_id = #{locationId}
          AND T1.type_enum = #{lossReportForm}
          AND T1.status_enum = #{agree}--同意
          AND T2.status_enum = #{completed}--已完成
          AND TO_CHAR(T2.occurrence_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startTime} AND #{endTime}
          AND T1.delete_flag = '0'
    </select>

    <!--药品发放查询 -->
    <select id="selectDrugIssue" resultType="java.math.BigDecimal">
        SELECT  COALESCE(SUM(total_money), 0) AS total_money_sum
        FROM (
                 /* ---------- 查询药品 ---------- */
                 SELECT
                     SUM(T1.quantity) * MAX(T8.unit_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'
                          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'
                 WHERE T2.location_id = #{locationId}
                   AND T1.status_enum = #{completed}--请求状态:已完成
                   AND T2.status_enum = #{agree}--请求状态:已发药
                   AND T1.delete_flag = '0'
                   AND TO_CHAR(T2.dispense_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startTime} AND #{endTime}
                 GROUP BY
                     T3.name,
                     T7.total_volume,
                     T1.lot_number,
                     T1.unit_code,
                     T3.manufacturer_text,
                     T2.location_id
                 UNION ALL
                 /* ---------- 查询耗材 ---------- */
                 SELECT
                     SUM(T4.quantity) * MAX(T9.unit_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'
                          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'
                 WHERE T5.location_id = #{locationId}
                   AND T4.status_enum = #{completed}--请求状态:已完成
                   AND T5.status_enum = #{agree}--请求状态:已发药
                   AND T4.delete_flag = '0'
                   AND TO_CHAR(T5.dispense_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startTime} AND #{endTime}
                 GROUP BY
                     T6.name,
                     T6.size,
                     T4.lot_number,
                     T4.unit_code,
                     T6.manufacturer_text,
                     T5.location_id
             )T
    </select>

</mapper>
