<?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.ruoyi.ems.mapper.WaterConsumptionMapper">

    <resultMap type="com.ruoyi.ems.model.WaterConsumptionVO" id="WaterConsumptionResult">
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="objCode" column="obj_code"/>
        <result property="objName" column="obj_name"/>
        <result property="objType" column="obj_type"/>
        <result property="statisticDate" column="statistic_date"/>
        <result property="statisticYear" column="statistic_year"/>
        <result property="statisticMonth" column="statistic_month"/>
        <result property="timeDimension" column="time_dimension"/>
        <result property="totalWaterQuantity" column="total_water_quantity"/>
        <result property="totalWaterCost" column="total_water_cost"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <resultMap type="com.ruoyi.ems.domain.WaterMeterH" id="WaterMeterHResult">
        <result property="id"    column="id"    />
        <result property="areaCode"    column="area_code"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="recordTime"    column="record_time"    />
        <result property="date"    column="date"    />
        <result property="time"    column="time"    />
        <result property="timeIndex"    column="time_index"    />
        <result property="waterQuantity"    column="water_quantity"    />
        <result property="meterUnitPrice"    column="meter_unit_price"    />
        <result property="createTime"    column="create_time"    />
        <result property="useWaterCost"    column="use_water_cost"    />
        <result property="meterReading" column="meter_reading"/>
    </resultMap>

    <!-- ==================== 水表抄表数据查询 ==================== -->
    <select id="selectMDevWaterConsumptionList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="WaterMeterHResult">
        select
        m.id, m.area_code, m.device_code, d.device_name, m.record_time, m.`date`, m.`time`, m.time_index, m.water_quantity, m.meter_unit_price, m.use_water_cost, m.meter_reading, m.create_time
        from adm_water_meter_h m
        left join adm_meter_device d on m.device_code = d.device_code
        <where>
            <if test="areaCode != null  and areaCode != ''">
                and m.area_code = #{areaCode}
            </if>
            <if test="deviceCode != null  and deviceCode != ''">
                and m.device_code = #{deviceCode}
            </if>
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime != ''">
                and m.record_time between #{startRecTime} and #{endRecTime}
            </if>
        </where>
        order by m.record_time ${orderFlag}
    </select>

    <select id="selectMDevWaterConsumptionSummary" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="WaterConsumptionResult">
        SELECT
        h.area_code,
        d.device_name as obj_name,
        d.device_code as obj_code,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as total_water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as total_water_cost
        FROM adm_water_meter_h h
        LEFT JOIN adm_meter_device d ON h.device_code = d.device_code
        <where>
            <if test="deviceCode != null  and deviceCode != ''">and h.device_code = #{deviceCode}</if>
            <if test="startRecTime != null and startRecTime != ''">
                AND h.record_time &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND h.record_time &lt;= #{endRecTime}
            </if>
        </where>
    </select>


    <!-- ==================== 区域用水统计查询（基于设备codes汇总） ==================== -->

    <!-- 根据设备代码列表查询区域日用水统计 -->
    <select id="selectAreaDailyWaterConsumptionByDevices" resultMap="WaterConsumptionResult">
        SELECT
        a.area_code,
        a.area_name as obj_name,
        a.area_code as obj_code,
        1 as obj_type,
        h.date as statistic_date,
        'DAY' as time_dimension,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as total_water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as total_water_cost,
        NOW() as create_time
        FROM adm_water_meter_h h
        INNER JOIN adm_meter_boundary_rel rel ON rel.meter_device = h.device_code AND rel.meter_cls = 70 AND rel.obj_type = 1
        INNER JOIN adm_area a ON a.area_code = rel.boundary_obj
        WHERE h.device_code IN
        <foreach collection="meterDevices" item="device" open="(" close=")" separator=",">
            #{device}
        </foreach>
        <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
            AND h.record_time &gt;= #{queryMeter.startRecTime}
        </if>
        <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
            AND h.record_time &lt;= #{queryMeter.endRecTime}
        </if>
        AND h.date IS NOT NULL
        GROUP BY a.area_code, a.area_name, h.date
        ORDER BY h.date ${queryMeter.orderFlag}, a.area_code
    </select>

    <!-- 根据设备代码列表查询区域月用水统计 -->
    <select id="selectAreaMonthlyWaterConsumptionByDevices" resultMap="WaterConsumptionResult">
        SELECT
        a.area_code,
        a.area_name as obj_name,
        a.area_code as obj_code,
        1 as obj_type,
        DATE(CONCAT(DATE_FORMAT(h.date, '%Y-%m'), '-01')) as statistic_date,
        DATE_FORMAT(h.date, '%Y-%m') as statistic_month,
        'MONTH' as time_dimension,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as total_water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as total_water_cost,
        NOW() as create_time
        FROM adm_water_meter_h h
        INNER JOIN adm_meter_boundary_rel rel ON rel.meter_device = h.device_code AND rel.meter_cls = 70 AND rel.obj_type = 1
        INNER JOIN adm_area a ON a.area_code = rel.boundary_obj
        WHERE h.device_code IN
        <foreach collection="meterDevices" item="device" open="(" close=")" separator=",">
            #{device}
        </foreach>
        <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
            AND h.record_time &gt;= #{queryMeter.startRecTime}
        </if>
        <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
            AND h.record_time &lt;= #{queryMeter.endRecTime}
        </if>
        AND h.date IS NOT NULL
        GROUP BY a.area_code, a.area_name, DATE_FORMAT(h.date, '%Y-%m')
        ORDER BY DATE_FORMAT(h.date, '%Y-%m') ${queryMeter.orderFlag}, a.area_code
    </select>

    <!-- 根据设备代码列表查询区域年用水统计 -->
    <select id="selectAreaYearlyWaterConsumptionByDevices" resultMap="WaterConsumptionResult">
        SELECT
        a.area_code,
        a.area_name as obj_name,
        a.area_code as obj_code,
        1 as obj_type,
        DATE(CONCAT(YEAR(h.date), '-01-01')) as statistic_date,
        YEAR(h.date) as statistic_year,
        'YEAR' as time_dimension,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as total_water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as total_water_cost,
        NOW() as create_time
        FROM adm_water_meter_h h
        INNER JOIN adm_meter_boundary_rel rel ON rel.meter_device = h.device_code AND rel.meter_cls = 70 AND rel.obj_type = 1
        INNER JOIN adm_area a ON a.area_code = rel.boundary_obj
        WHERE h.device_code IN
        <foreach collection="meterDevices" item="device" open="(" close=")" separator=",">
            #{device}
        </foreach>
        <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
            AND h.record_time &gt;= #{queryMeter.startRecTime}
        </if>
        <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
            AND h.record_time &lt;= #{queryMeter.endRecTime}
        </if>
        AND h.date IS NOT NULL
        GROUP BY a.area_code, a.area_name, YEAR(h.date)
        ORDER BY YEAR(h.date) ${queryMeter.orderFlag}, a.area_code
    </select>

    <!-- 根据设备代码列表查询区域用水汇总 -->
    <select id="selectAreaWaterConsumptionSummaryByDevices" resultMap="WaterConsumptionResult">
        SELECT
        'TOTAL' as area_code,
        '总计' as obj_name,
        'TOTAL' as obj_code,
        1 as obj_type,
        'SUMMARY' as time_dimension,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as total_water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as total_water_cost
        FROM adm_water_meter_h h
        WHERE h.device_code IN
        <foreach collection="meterDevices" item="device" open="(" close=")" separator=",">
            #{device}
        </foreach>
        <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
            AND h.record_time &gt;= #{queryMeter.startRecTime}
        </if>
        <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
            AND h.record_time &lt;= #{queryMeter.endRecTime}
        </if>
        AND h.date IS NOT NULL
    </select>

    <!-- ==================== 设施用水统计查询 ==================== -->

    <!-- 查询设施日用水统计 -->
    <select id="selectFacsDailyWaterConsumption" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="WaterConsumptionResult">
        SELECT
        f.ref_area as area_code,
        f.facs_name as obj_name,
        f.facs_code as obj_code,
        2 as obj_type,
        h.date as statistic_date,
        'DAY' as time_dimension,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as total_water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as total_water_cost,
        NOW() as create_time
        FROM adm_ems_facs f
        LEFT JOIN adm_meter_boundary_rel rel ON rel.obj_type = 2 AND rel.boundary_obj = f.facs_code
        LEFT JOIN adm_water_meter_h h ON h.device_code = rel.meter_device AND rel.meter_cls = 70
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                AND f.ref_area = #{areaCode}
            </if>
            <if test="objCode != null and objCode != '' and objCode != '-1'">
                AND f.facs_code = #{objCode}
            </if>
            <if test="facsCategory != null and facsCategory != ''">
                AND f.facs_category = #{facsCategory}
            </if>
            <if test="startRecTime != null and startRecTime != ''">
                AND h.record_time &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND h.record_time &lt;= #{endRecTime}
            </if>
            AND h.date IS NOT NULL
            AND f.enable = 1
        </where>
        GROUP BY f.facs_code, f.facs_name, f.ref_area, h.date
        ORDER BY h.date ${orderFlag}, f.facs_code
    </select>

    <!-- 查询设施月用水统计 -->
    <select id="selectFacsMonthlyWaterConsumption" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="WaterConsumptionResult">
        SELECT
        f.ref_area as area_code,
        f.facs_name as obj_name,
        f.facs_code as obj_code,
        2 as obj_type,
        DATE(CONCAT(DATE_FORMAT(h.date, '%Y-%m'), '-01')) as statistic_date,
        DATE_FORMAT(h.date, '%Y-%m') as statistic_month,
        'MONTH' as time_dimension,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as total_water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as total_water_cost,
        NOW() as create_time
        FROM adm_ems_facs f
        LEFT JOIN adm_meter_boundary_rel rel ON rel.obj_type = 2 AND rel.boundary_obj = f.facs_code
        LEFT JOIN adm_water_meter_h h ON h.device_code = rel.meter_device AND rel.meter_cls = 70
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                AND f.ref_area = #{areaCode}
            </if>
            <if test="objCode != null and objCode != '' and objCode != '-1'">
                AND f.facs_code = #{objCode}
            </if>
            <if test="facsCategory != null and facsCategory != ''">
                AND f.facs_category = #{facsCategory}
            </if>
            <if test="startRecTime != null and startRecTime != ''">
                AND h.record_time &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND h.record_time &lt;= #{endRecTime}
            </if>
            AND h.date IS NOT NULL
            AND f.enable = 1
        </where>
        GROUP BY f.facs_code, f.facs_name, f.ref_area, DATE_FORMAT(h.date, '%Y-%m')
        ORDER BY DATE_FORMAT(h.date, '%Y-%m') ${orderFlag}, f.facs_code
    </select>

    <!-- 查询设施年用水统计 -->
    <select id="selectFacsYearlyWaterConsumption" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="WaterConsumptionResult">
        SELECT
        f.ref_area as area_code,
        f.facs_name as obj_name,
        f.facs_code as obj_code,
        2 as obj_type,
        DATE(CONCAT(YEAR(h.date), '-01-01')) as statistic_date,
        YEAR(h.date) as statistic_year,
        'YEAR' as time_dimension,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as total_water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as total_water_cost,
        NOW() as create_time
        FROM adm_ems_facs f
        LEFT JOIN adm_meter_boundary_rel rel ON rel.obj_type = 2 AND rel.boundary_obj = f.facs_code
        LEFT JOIN adm_water_meter_h h ON h.device_code = rel.meter_device AND rel.meter_cls = 70
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                AND f.ref_area = #{areaCode}
            </if>
            <if test="objCode != null and objCode != '' and objCode != '-1'">
                AND f.facs_code = #{objCode}
            </if>
            <if test="facsCategory != null and facsCategory != ''">
                AND f.facs_category = #{facsCategory}
            </if>
            <if test="startRecTime != null and startRecTime != ''">
                AND h.record_time &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND h.record_time &lt;= #{endRecTime}
            </if>
            AND h.date IS NOT NULL
            AND f.enable = 1
        </where>
        GROUP BY f.facs_code, f.facs_name, f.ref_area, YEAR(h.date)
        ORDER BY YEAR(h.date) ${orderFlag}, f.facs_code
    </select>

    <!-- 查询设施用水统计汇总 -->
    <select id="selectFacsWaterConsumptionSummary" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="WaterConsumptionResult">
        SELECT
        COALESCE(#{areaCode}, '0') as area_code,
        '总计' as obj_name,
        'TOTAL' as obj_code,
        2 as obj_type,
        'SUMMARY' as time_dimension,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as total_water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as total_water_cost
        FROM adm_ems_facs f
        LEFT JOIN adm_meter_boundary_rel rel ON rel.obj_type = 2 AND rel.boundary_obj = f.facs_code
        LEFT JOIN adm_water_meter_h h ON h.device_code = rel.meter_device AND rel.meter_cls = 70
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                AND f.ref_area = #{areaCode}
            </if>
            <if test="facsCategory != null and facsCategory != ''">
                AND f.facs_category = #{facsCategory}
            </if>
            <if test="objCode != null and objCode != '' and objCode != '-1'">
                AND f.facs_code = #{objCode}
            </if>
            <if test="startRecTime != null and startRecTime != ''">
                AND h.record_time &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND h.record_time &lt;= #{endRecTime}
            </if>
            AND h.date IS NOT NULL
            AND f.enable = 1
        </where>
    </select>

    <!--
       区域小时用水明细查询
   -->
    <select id="selectAreaHourlyWaterConsumptionByDevices" resultMap="WaterMeterHResult">
        SELECT
        #{queryMeter.objCode} as device_code,
        #{queryMeter.objName} as device_name,
        h.area_code,
        h.record_time,
        h.date,
        h.time,
        h.time_index,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as use_water_cost
        FROM adm_water_meter_h h
        WHERE h.device_code IN
        <foreach collection="meterDevices" item="device" open="(" close=")" separator=",">
            #{device}
        </foreach>
        <if test="queryMeter.areaCode != null and queryMeter.areaCode != '' and queryMeter.areaCode != '-1'">
            AND h.area_code = #{queryMeter.areaCode}
        </if>
        <if test="queryMeter.startRecTime != null and queryMeter.startRecTime != ''">
            AND h.record_time &gt;= #{queryMeter.startRecTime}
        </if>
        <if test="queryMeter.endRecTime != null and queryMeter.endRecTime != ''">
            AND h.record_time &lt;= #{queryMeter.endRecTime}
        </if>
        GROUP BY h.date, h.time_index, h.area_code, h.record_time, h.time
        ORDER BY h.record_time ${queryMeter.orderFlag}
    </select>

    <!--
        设施小时用水明细查询
    -->
    <select id="selectFacsHourlyWaterConsumption" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="WaterMeterHResult">
        SELECT
        <choose>
            <when test="objCode != null and objCode != '' and objCode != areaCode and objCode != '-1'">
                f.facs_code as device_code,
                f.facs_name as device_name,
            </when>
            <otherwise>
                #{objCode} as device_code,
                #{objName} as device_name,
            </otherwise>
        </choose>
        f.ref_area as area_code,
        h.record_time,
        h.date,
        h.time,
        h.time_index,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as use_water_cost
        FROM adm_ems_facs f
        INNER JOIN adm_meter_boundary_rel rel ON rel.obj_type = 2
        AND rel.boundary_obj = f.facs_code
        AND rel.meter_cls = 70
        INNER JOIN adm_water_meter_h h ON h.device_code = rel.meter_device
        <where>
            f.enable = 1
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                AND f.ref_area = #{areaCode}
            </if>
            <if test="objCode != null and objCode != '' and objCode != areaCode and objCode != '-1'">
                AND f.facs_code = #{objCode}
            </if>
            <if test="facsCategory != null and facsCategory != ''">
                AND f.facs_category = #{facsCategory}
            </if>
            <if test="facsSubCategory != null and facsSubCategory != ''">
                AND f.facs_subcategory = #{facsSubCategory}
            </if>
            <if test="startRecTime != null and startRecTime != ''">
                AND h.record_time &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND h.record_time &lt;= #{endRecTime}
            </if>
        </where>
        GROUP BY
        <choose>
            <when test="objCode != null and objCode != '' and objCode != areaCode and objCode != '-1'">
                f.facs_code, f.facs_name, f.ref_area, h.date, h.time_index, h.record_time, h.time
            </when>
            <otherwise>
                f.ref_area, h.date, h.time_index, h.record_time, h.time
            </otherwise>
        </choose>
        ORDER BY h.record_time ${orderFlag}
    </select>

    <!--
        设施小时用水汇总统计
    -->
    <select id="selectFacsHourlyWaterSummary" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="WaterConsumptionResult">
        SELECT
        f.ref_area as area_code,
        2 as obj_type,
        f.facs_code as obj_code,
        f.facs_name as obj_name,
        'HOUR' as time_dimension,
        ROUND(SUM(COALESCE(h.water_quantity, 0)), 2) as total_water_quantity,
        ROUND(SUM(COALESCE(h.use_water_cost, 0)), 2) as total_water_cost
        FROM adm_ems_facs f
        LEFT JOIN adm_meter_boundary_rel rel ON rel.obj_type = 2
        AND rel.boundary_obj = f.facs_code
        AND rel.meter_cls = #{meterCls}
        LEFT JOIN adm_water_meter_h h ON h.device_code = rel.meter_device
        <where>
            f.enable = 1
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                AND f.ref_area = #{areaCode}
            </if>
            <if test="facsCategory != null and facsCategory != ''">
                AND f.facs_category = #{facsCategory}
            </if>
            <if test="facsSubCategory != null and facsSubCategory != ''">
                AND f.facs_subcategory = #{facsSubCategory}
            </if>
            <if test="startRecTime != null and startRecTime != ''">
                AND h.record_time &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND h.record_time &lt;= #{endRecTime}
            </if>
        </where>
        GROUP BY f.facs_code, f.facs_name, f.ref_area
        ORDER BY f.facs_code
    </select>
</mapper>