<?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.report.mapper.EmsReportMapper">


    <select id="energyStatisticalDnbReportList" resultType="com.ruoyi.ems.report.domain.EnergyStatisticalReport"
            parameterType="java.util.HashMap">
        WITH DT AS (SELECT CAST(#{timeSub} AS INT) TIMESUB)
        SELECT WU.WORK_UNIT_CODE                                                       workUnitCode,
        WU.WORK_UNIT_NAME                                                       workUnitName,
        MAX(RPD.INSTRUMENT_VALUE)                                               instrumentValue,
        SUM(RPD.EXPEND)                                                         expend,
        SUBSTRING(FORMAT(RPD.BEGIN_TIME, 'yyyy-MM-dd HH:mm:ss'), 1, DT.TIMESUB) beginTime,
        SUBSTRING(FORMAT(RPD.BEGIN_TIME, 'yyyy-MM-dd HH:mm:ss'), 1, DT.TIMESUB) endTime
        FROM ems_base_work_unit WU
        LEFT JOIN ems_base_monitor_work_unit MWU ON MWU.WORK_UNIT_CODE = WU.WORK_UNIT_CODE
        LEFT JOIN ems_report_point_dnb RPD ON RPD.MONITOR_CODE = MWU.MONITOR_CODE
        CROSS JOIN DT
        WHERE RPD.EXPEND IS NOT NULL
        <if test="beginCollectTime != null and beginCollectTime != '' and endCollectTime != null and endCollectTime != ''">
            and FORMAT(RPD.BEGIN_TIME, 'yyyy-MM-dd HH:mm:ss') between  #{beginCollectTime} and #{endCollectTime}
        </if>
        <if test="workUnitCode != null  and workUnitCode != ''">and WU.WORK_UNIT_CODE = #{workUnitCode}</if>
        GROUP BY WU.WORK_UNIT_CODE, WU.WORK_UNIT_NAME, SUBSTRING(FORMAT(RPD.BEGIN_TIME, 'yyyy-MM-dd HH:mm:ss'), 1, DT.TIMESUB)
        ORDER BY beginTime, workUnitCode
    </select>

    <select id="peaksValleysConsumptionReportList"
            resultType="com.ruoyi.ems.report.domain.PeaksValleysConsumptionReport" parameterType="java.util.HashMap">
        SELECT ebwu.work_unit_code workUnitCode, ebwu.work_unit_name workUnitName,
               dnb.pointTime, dnb.priceType, sum(expend) expend,
               sum(expend * dnb_price)     price
        FROM (
        SELECT erpd.monitor_code,
        <if test="dateType == 10">CONVERT(VARCHAR(10), erpd.begin_time, 120) pointTime,</if>
        <if test="dateType == 7">CONVERT(VARCHAR(7), erpd.begin_time, 120) pointTime,</if>
        <if test="dateType == 4">CONVERT(VARCHAR(4), erpd.begin_time, 120) pointTime,</if>
        NULLIF(erpd.expend, 0)                     expend,
        case
        when ebsp.price_type = 0 then '尖峰'
        when ebsp.price_type = 1 then '高峰'
        when ebsp.price_type = 2 then '平段'
        when ebsp.price_type = 3 then '低谷'
        when ebsp.price_type = 4 then '深谷' END priceType,
        ebsp.dnb_price
        FROM ems_report_point_dnb erpd
        join ems_base_share_price ebsp
        on LEFT(CONVERT(VARCHAR, erpd.begin_time, 23), 7) = ebsp.year_month_date
        and CAST(erpd.begin_time AS time) >= CAST(ebsp.start_hour AS time)
        and CAST(erpd.begin_time AS time) &lt; CAST(ebsp.end_hour AS time)
        <if test="beginCollectTime != null and beginCollectTime != '' and endCollectTime != null and endCollectTime != ''">
            WHERE FORMAT(erpd.begin_time, 'yyyy-MM-dd') between #{beginCollectTime} and #{endCollectTime}
        </if>
        ) dnb
        LEFT JOIN ems_base_monitor_info ebmi on ebmi.monitor_code = dnb.monitor_code
        LEFT JOIN ems_base_monitor_work_unit ebmwu on ebmi.monitor_code = ebmwu.monitor_code
        LEFT JOIN ems_base_work_unit ebwu on ebwu.work_unit_code = ebmwu.work_unit_code
        WHERE 1=1
        <if test="workUnitCode != null  and workUnitCode != ''">and ebwu.WORK_UNIT_CODE = #{workUnitCode}</if>
        GROUP BY ebwu.work_unit_code, ebwu.work_unit_name, dnb.pointTime, dnb.priceType
        ORDER BY ebwu.work_unit_code, ebwu.work_unit_name, dnb.pointTime, dnb.priceType
    </select>
</mapper>