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

    <resultMap type="com.ruoyi.ems.model.ElecConsumptionVO" id="ElecConsumptionResult">
        <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="totalElecQuantity" column="total_elec_quantity"/>
        <result property="sharpPeakQuantity" column="sharp_peak_quantity"/>
        <result property="peakQuantity" column="peak_quantity"/>
        <result property="normalQuantity" column="normal_quantity"/>
        <result property="valleyQuantity" column="valley_quantity"/>
        <result property="deepValleyQuantity" column="deep_valley_quantity"/>
        <result property="totalElecCost" column="total_elec_cost"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <resultMap type="com.ruoyi.ems.domain.ElecMeterH" id="ElecMeterHResult">
        <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="elecQuantity" column="elec_quantity"/>
        <result property="meterType" column="meter_type"/>
        <result property="meterUnitPrice" column="meter_unit_price"/>
        <result property="useElecCost" column="use_elec_cost"/>
        <result property="meterReading" column="meter_reading"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <resultMap type="com.ruoyi.ems.domain.EnergyMeter" id="EnergyMeterResult">
        <result property="areaCode" column="area_code"/>
        <result property="objType" column="obj_type"/>
        <result property="objCode" column="obj_code"/>
        <result property="objName" column="obj_name"/>
        <result property="quantity" column="elec_quantity"/>
        <result property="useCost" column="use_elec_cost"/>
    </resultMap>

    <!-- ==================== 查询分时类型 ==================== -->
    <select id="selectElecHourSupportTypes" resultType="java.lang.String">
        SELECT
        DISTINCT h.meter_type
        FROM adm_elec_meter_h h
        <where>
            h.meter_type IS NOT NULL
            AND 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>
        </where>
        ORDER BY h.meter_type DESC
    </select>

    <!-- ==================== 电表抄表数据查询 ==================== -->
    <select id="selectMDevElecConsumptionList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecMeterHResult">
        select
        m.id, m.area_code, m.device_code, d.device_name, m.record_time, m.`date`, m.`time`, m.time_index,
        m.elec_quantity, m.meter_type, m.meter_unit_price, m.use_elec_cost, m.meter_reading, m.create_time
        from adm_elec_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="selectMDevElecConsumptionSummary" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecConsumptionResult">
        SELECT
        h.area_code,
        d.device_name as obj_name,
        d.device_code as obj_code,
        ROUND(SUM(COALESCE(h.elec_quantity, 0)), 2) as total_elec_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as sharp_peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 0 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as normal_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as valley_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as deep_valley_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as total_elec_cost
        FROM adm_elec_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>

    <!-- ==================== 区域用电统计查询 ==================== -->

    <!-- 根据设备代码列表查询区域日用电统计 -->
    <select id="selectAreaDailyElecConsumptionByDevices" resultMap="ElecConsumptionResult">
        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.elec_quantity, 0)), 2) as total_elec_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as sharp_peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 0 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as normal_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as valley_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as deep_valley_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as total_elec_cost,
        NOW() as create_time
        FROM adm_elec_meter_h h
        INNER JOIN adm_meter_boundary_rel rel ON rel.meter_device = h.device_code AND rel.meter_cls = 45 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="selectAreaMonthlyElecConsumptionByDevices" resultMap="ElecConsumptionResult">
        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.elec_quantity, 0)), 2) as total_elec_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as sharp_peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 0 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as normal_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as valley_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as deep_valley_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as total_elec_cost,
        NOW() as create_time
        FROM adm_elec_meter_h h
        INNER JOIN adm_meter_boundary_rel rel ON rel.meter_device = h.device_code AND rel.meter_cls = 45 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="selectAreaYearlyElecConsumptionByDevices" resultMap="ElecConsumptionResult">
        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.elec_quantity, 0)), 2) as total_elec_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as sharp_peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 0 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as normal_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as valley_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as deep_valley_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as total_elec_cost,
        NOW() as create_time
        FROM adm_elec_meter_h h
        INNER JOIN adm_meter_boundary_rel rel ON rel.meter_device = h.device_code AND rel.meter_cls = 45 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="selectAreaElecConsumptionSummaryByDevices" resultMap="ElecConsumptionResult">
        SELECT
        'TOTAL' as area_code,
        '总计' as obj_name,
        'TOTAL' as obj_code,
        1 as obj_type,
        'SUMMARY' as time_dimension,
        ROUND(SUM(COALESCE(h.elec_quantity, 0)), 2) as total_elec_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as sharp_peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 0 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as normal_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as valley_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as deep_valley_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as total_elec_cost
        FROM adm_elec_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="selectFacsDailyElecConsumption" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecConsumptionResult">
        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.elec_quantity, 0)), 2) as total_elec_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as sharp_peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 0 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as normal_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as valley_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as deep_valley_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as total_elec_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_elec_meter_h h ON h.device_code = rel.meter_device AND rel.meter_cls = 45
        <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="selectFacsMonthlyElecConsumption" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecConsumptionResult">
        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.elec_quantity, 0)), 2) as total_elec_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as sharp_peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 0 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as normal_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as valley_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as deep_valley_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as total_elec_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_elec_meter_h h ON h.device_code = rel.meter_device AND rel.meter_cls = 45
        <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.date &gt;= DATE(#{startRecTime})
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND h.date &lt;= DATE(#{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="selectFacsYearlyElecConsumption" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecConsumptionResult">
        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.elec_quantity, 0)), 2) as total_elec_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as sharp_peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 0 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as normal_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as valley_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as deep_valley_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as total_elec_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_elec_meter_h h ON h.device_code = rel.meter_device AND rel.meter_cls = 45
        <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.date &gt;= DATE(#{startRecTime})
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND h.date &lt;= DATE(#{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="selectFacsElecConsumptionSummary" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecConsumptionResult">
        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.elec_quantity, 0)), 2) as total_elec_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as sharp_peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 0 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as normal_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as valley_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as deep_valley_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as total_elec_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_elec_meter_h h ON h.device_code = rel.meter_device AND rel.meter_cls = 45
        <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.date &gt;= DATE(#{startRecTime})
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND h.date &lt;= DATE(#{endRecTime})
            </if>
            AND h.date IS NOT NULL
            AND f.enable = 1
        </where>
    </select>

    <!-- ==================== 新增：小时粒度查询SQL ==================== -->

    <!--
        区域小时用电明细查询
    -->
    <select id="selectAreaHourlyElecConsumptionByDevices" resultMap="ElecMeterHResult">
        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.elec_quantity, 0)), 2) as elec_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as use_elec_cost
        FROM adm_elec_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="selectFacsHourlyElecConsumption" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecMeterHResult">
        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.elec_quantity, 0)), 2) as elec_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as use_elec_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 = 45
        INNER JOIN adm_elec_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="selectFacsHourlyElecSummary" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="ElecConsumptionResult">
        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.elec_quantity, 0)), 2) as total_elec_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as sharp_peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as peak_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = 0 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as normal_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -1 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as valley_quantity,
        ROUND(SUM(CASE WHEN h.meter_type = -2 THEN COALESCE(h.elec_quantity, 0) ELSE 0 END), 2) as deep_valley_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)), 2) as total_elec_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_elec_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>

    <!--
    查询日平均用电
    -->
    <select id="selectElecDayAvg" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="EnergyMeterResult">
        SELECT
        <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
            h.area_code,
        </if>
        <if test="deviceCode != null and deviceCode != ''">
            h.device_code as obj_code,
        </if>
        ROUND(SUM(COALESCE(h.elec_quantity, 0)) / NULLIF(COUNT(DISTINCT h.date), 0), 2) AS elec_quantity,
        ROUND(SUM(COALESCE(h.use_elec_cost, 0)) / NULLIF(COUNT(DISTINCT h.date), 0), 2) AS use_elec_cost
        FROM adm_elec_meter_h h
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                AND h.area_code = #{areaCode}
            </if>
            <if test="deviceCode != null and deviceCode != ''">
                AND h.device_code = #{deviceCode}
            </if>
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime != ''">
                AND h.date &gt;= #{startRecTime} AND h.date &lt;= #{endRecTime}
            </if>
        </where>
    </select>

    <!-- ==================== 按设备类型查询日用电 ==================== -->
    <!--
        按设备类型查询日用电
    -->
    <select id="selectDeviceElecByDay" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="EnergyMeterResult">
        SELECT
        d.device_name AS obj_name,
        ROUND(SUM(COALESCE(h.elec_quantity, 0)), 2) AS elec_quantity
        FROM adm_ems_device d
        INNER JOIN adm_elec_meter_h h ON d.device_code = h.device_code
        <where>
            h.date &gt;= #{startRecTime}
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
                AND h.area_code = #{areaCode}
            </if>
        </where>
        GROUP BY d.device_name
        ORDER BY elec_quantity DESC
    </select>
</mapper>