<?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.ElecLoadIndex15minMapper">
    
    <resultMap type="com.ruoyi.ems.domain.ElecLoadIndex" id="indexResult">
        <result property="id"    column="id"    />
        <result property="areaCode"    column="area_code"    />
        <result property="areaName"    column="area_name"    />
        <result property="areaShortName"    column="area_short_name"    />
        <result property="objType"    column="obj_type"    />
        <result property="objCode"    column="obj_code"    />
        <result property="objName"    column="obj_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="ua"    column="ua"    />
        <result property="ub"    column="ub"    />
        <result property="uc"    column="uc"    />
        <result property="la"    column="la"    />
        <result property="lb"    column="lb"    />
        <result property="lc"    column="lc"    />
        <result property="p"    column="p"    />
        <result property="pa"    column="pa"    />
        <result property="pb"    column="pb"    />
        <result property="pc"    column="pc"    />
        <result property="q"    column="q"    />
        <result property="qa"    column="qa"    />
        <result property="qb"    column="qb"    />
        <result property="qc"    column="qc"    />
        <result property="pf"    column="pf"    />
    </resultMap>

    <sql id="selectIndexVo">
        select id, area_code, obj_type, obj_code, record_time, `date`, `time`, time_index, ua, ub, uc, la, lb, lc, p, pa, pb, pc, q, qa, qb, qc, pf from adm_ems_elec_load_index_15min
    </sql>

    <select id="selectIndexList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="indexResult">
        select
        i.obj_code,
        i.obj_type,
        COALESCE(a2.area_name,
        f.facs_name,
        d.device_name) AS obj_name,
        f.facs_category,
        f.facs_subcategory,
        <if test="(facsCategory != null and facsCategory != '') or (facsSubCategory != null and facsSubCategory != '')">
            <if test="objCode == null or objCode == ''">
                fs.name as sub_obj_name,
            </if>
        </if>
        i.record_time,
        i.`date`,
        i.`time`,
        i.`time_index`,
        sum(i.ua) as ua,
        sum(i.ub) as ub,
        sum(i.uc) as uc,
        sum(i.la) as la,
        sum(i.lb) as lb,
        sum(i.lc) as lc,
        sum(i.p) as p,
        sum(i.pa) as pa,
        sum(i.pb) as pb,
        sum(i.pc) as pc,
        sum(i.q) as q,
        sum(i.qa) as qa,
        sum(i.qb) as qb,
        sum(i.qc) as qc,
        sum(i.pf) as pf
        from adm_ems_elec_load_index_15min i
        LEFT JOIN adm_ems_facs f ON i.`obj_code` = f.`facs_code`
        LEFT JOIN dim_ems_facs_subcategory fs ON f.`facs_subcategory` = fs.`code`
        LEFT JOIN adm_ems_device d ON i.`obj_code` = d.`device_code`
        LEFT JOIN adm_area a2 ON i.`obj_code` = a2.`area_code`
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">and i.area_code = #{areaCode}</if>
            <if test="objType != null">and i.obj_type = #{objType}</if>
            <if test="objCode != null  and objCode != ''">and i.obj_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 endRecTime != null and endRecTime !=''">
                and i.`record_time` &gt;= #{startRecTime} and i.`record_time` &lt;= #{endRecTime}
            </if>
            <if test="date != null">
                and i.`date` &gt;= #{date}
            </if>
        </where>
        GROUP BY i.`record_time`
        ORDER BY i.`record_time` desc
    </select>
    <select id="selectIndexDetailList" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="indexResult">
        select
        <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
            i.area_code,
            a.area_name,
            a.short_name as area_short_name,
        </if>
        <if test="objCode != null and objCode != ''">
            i.obj_code,
            CASE
            WHEN i.obj_type = 1 THEN a2.area_name
            WHEN i.obj_type = 2 THEN f.facs_name
            WHEN i.obj_type = 3 THEN d.device_name
            END as obj_name,
        </if>
        <if test="(facsCategory != null and facsCategory != '') or (facsSubCategory != null and facsSubCategory != '')">
            fa.facs_category,
            fa.facs_subcategory,
            <if test="objCode == null or objCode == ''">
                fs.name as obj_name,
            </if>
        </if>
        i.record_time,
        i.`date`,
        i.`time`,
        i.`time_index`,
        sum(i.ua) as ua,
        sum(i.ub) as ub,
        sum(i.uc) as uc,
        sum(i.la) as la,
        sum(i.lb) as lb,
        sum(i.lc) as lc,
        sum(i.p) as p,
        sum(i.pa) as pa,
        sum(i.pb) as pb,
        sum(i.pc) as pc,
        sum(i.q) as q,
        sum(i.qa) as qa,
        sum(i.qb) as qb,
        sum(i.qc) as qc,
        sum(i.pf) as pf
        from adm_ems_elec_load_index_15min i
        <if test="areaCode != null and areaCode != '' and areaCode != '-1'">
            LEFT JOIN adm_area a ON i.`area_code` = a.`area_code`
        </if>
        <if test="objCode != null and objCode != ''">
            LEFT JOIN adm_ems_facs f ON i.`obj_code` = f.`facs_code`
            LEFT JOIN adm_ems_device d ON i.`obj_code` = d.`device_code`
            LEFT JOIN adm_ems_area a2 ON i.`obj_code` = a2.`area_code`
        </if>
        <if test="(facsCategory != null  and facsCategory != '') or (facsSubCategory != null  and facsSubCategory != '')">
            LEFT JOIN adm_ems_facs fa ON i.`obj_code` = fa.`facs_code`
            <if test="objCode == null or objCode == ''">
                LEFT JOIN dim_ems_facs_subcategory fs ON fa.`facs_subcategory` = fs.`code`
            </if>
        </if>
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">and i.area_code = #{areaCode}</if>
            <if test="objType != null">and i.obj_type = #{objType}</if>
            <if test="objCode != null  and objCode != ''">and i.obj_code = #{objCode}</if>
            <if test="facsCategory != null and facsCategory != ''">and fa.facs_category = #{facsCategory}</if>
            <if test="facsSubCategory != null and facsSubCategory != ''">and fa.facs_subcategory = #{facsSubCategory}</if>
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and i.`record_time` &gt;= #{startRecTime} and i.`record_time` &lt;= #{endRecTime}
            </if>
        </where>
    </select>
    <select id="selectIndexMaxLoad" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="indexResult">
        SELECT
            MAX(t.total_p) AS p,
            MAX(t.total_q) AS q
        FROM (
            SELECT
             i.`date` as date_part,
             i.`time` as time_part,
             SUM(p) AS total_p,
             SUM(q) AS total_q
            FROM
        adm_ems_elec_load_index_15min i
        <if test="(facsCategory != null  and facsCategory != '') or (facsSubCategory != null  and facsSubCategory != '')">
            LEFT JOIN adm_ems_facs fa ON i.`obj_code` = fa.`facs_code`
            <if test="objCode == null or objCode == ''">
                LEFT JOIN dim_ems_facs_subcategory fs ON fa.`facs_subcategory` = fs.`code`
            </if>
        </if>
        <where>
            <if test="areaCode != null  and areaCode != '' and areaCode != '-1'">and i.area_code = #{areaCode}</if>
            <if test="objType != null">and i.obj_type = #{objType}</if>
            <if test="objCode != null  and objCode != ''">and i.obj_code = #{objCode}</if>
            <if test="facsCategory != null and facsCategory != ''">and fa.facs_category = #{facsCategory}</if>
            <if test="facsSubCategory != null and facsSubCategory != ''">and fa.facs_subcategory = #{facsSubCategory}</if>
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and i.`record_time` BETWEEN  #{startRecTime} and #{endRecTime}
            </if>
        </where>
        GROUP BY
            i.`date`,
            i.`time`) t
    </select>

    <select id="selectDayIndexMaxLoad" parameterType="com.ruoyi.ems.model.QueryMeter" resultMap="indexResult">
        SELECT
            t.date_part as `date`,
            MAX(t.total_p) AS p,
            MAX(t.total_q) AS q
        FROM (
             SELECT
               DATE(record_time) AS date_part,
               SUM(p) AS total_p,
               SUM(q) AS total_q
            FROM
        adm_ems_elec_load_index_15min i
        <if test="(facsCategory != null  and facsCategory != '') or (facsSubCategory != null  and facsSubCategory != '')">
            LEFT JOIN adm_ems_facs fa ON i.`obj_code` = fa.`facs_code`
            <if test="objCode == null or objCode == ''">
                LEFT JOIN dim_ems_facs_subcategory fs ON fa.`facs_subcategory` = fs.`code`
            </if>
        </if>
        <where>
            <if test="areaCode != null  and areaCode != '' and areaCode != '-1'">and i.area_code = #{areaCode}</if>
            <if test="objType != null">and i.obj_type = #{objType}</if>
            <if test="objCode != null  and objCode != ''">and i.obj_code = #{objCode}</if>
            <if test="facsCategory != null and facsCategory != ''">and fa.facs_category = #{facsCategory}</if>
            <if test="facsSubCategory != null and facsSubCategory != ''">and fa.facs_subcategory = #{facsSubCategory}</if>
            <if test="startRecTime != null and startRecTime != '' and endRecTime != null and endRecTime !=''">
                and i.`date` between #{startRecTime} and #{endRecTime}
            </if>
        </where>
        GROUP BY
            i.`date`,
            i.`time`
            ) AS t
        GROUP BY
            date_part
        ORDER BY
            date_part
    </select>

    <select id="selectIndexById" parameterType="Long" resultMap="indexResult">
        <include refid="selectIndexVo"/>
        where id = #{id}
    </select>

    <select id="selectNewIndex" resultMap="indexResult">
        <include refid="selectIndexVo"/>
        where area_code = #{areaCode} and obj_type = #{objType} and obj_code = #{objCode}
        order by record_time
        desc limit 1
    </select>
        
    <insert id="insertIndex" parameterType="com.ruoyi.ems.domain.ElecLoadIndex" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_elec_load_index_15min
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="objType != null">obj_type,</if>
            <if test="objCode != null and objCode != ''">obj_code,</if>
            <if test="recordTime != null">record_time,</if>
            <if test="date != null">`date`,</if>
            <if test="time != null">`time`,</if>
            <if test="timeIndex != null">time_index,</if>
            <if test="ua != null">ua,</if>
            <if test="ub != null">ub,</if>
            <if test="uc != null">uc,</if>
            <if test="la != null">la,</if>
            <if test="lb != null">lb,</if>
            <if test="lc != null">lc,</if>
            <if test="p != null">p,</if>
            <if test="pa != null">pa,</if>
            <if test="pb != null">pb,</if>
            <if test="pc != null">pc,</if>
            <if test="q != null">q,</if>
            <if test="qa != null">qa,</if>
            <if test="qb != null">qb,</if>
            <if test="qc != null">qc,</if>
            <if test="pf != null">pf,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="objType != null">#{objType},</if>
            <if test="objCode != null and objCode != ''">#{objCode},</if>
            <if test="recordTime != null">#{recordTime},</if>
            <if test="date != null">#{date},</if>
            <if test="time != null">#{time},</if>
            <if test="timeIndex != null">#{timeIndex},</if>
            <if test="ua != null">#{ua},</if>
            <if test="ub != null">#{ub},</if>
            <if test="uc != null">#{uc},</if>
            <if test="la != null">#{la},</if>
            <if test="lb != null">#{lb},</if>
            <if test="lc != null">#{lc},</if>
            <if test="p != null">#{p},</if>
            <if test="pa != null">#{pa},</if>
            <if test="pb != null">#{pb},</if>
            <if test="pc != null">#{pc},</if>
            <if test="q != null">#{q},</if>
            <if test="qa != null">#{qa},</if>
            <if test="qb != null">#{qb},</if>
            <if test="qc != null">#{qc},</if>
            <if test="pf != null">#{pf},</if>
         </trim>
    </insert>

    <update id="updateIndex" parameterType="com.ruoyi.ems.domain.ElecLoadIndex">
        update adm_ems_elec_load_index_15min
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="objType != null">obj_type = #{objType},</if>
            <if test="objCode != null and objCode != ''">obj_code = #{objCode},</if>
            <if test="recordTime != null">record_time = #{recordTime},</if>
            <if test="date != null">`date` = #{date},</if>
            <if test="time != null">`time` = #{time},</if>
            <if test="timeIndex != null">time_index = #{timeIndex},</if>
            <if test="ua != null">ua = #{ua},</if>
            <if test="ub != null">ub = #{ub},</if>
            <if test="uc != null">uc = #{uc},</if>
            <if test="la != null">la = #{la},</if>
            <if test="lb != null">lb = #{lb},</if>
            <if test="lc != null">lc = #{lc},</if>
            <if test="p != null">p = #{p},</if>
            <if test="pa != null">pa = #{pa},</if>
            <if test="pb != null">pb = #{pb},</if>
            <if test="pc != null">pc = #{pc},</if>
            <if test="q != null">q = #{q},</if>
            <if test="qa != null">qa = #{qa},</if>
            <if test="qb != null">qb = #{qb},</if>
            <if test="qc != null">qc = #{qc},</if>
            <if test="pf != null">pf = #{pf},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteIndexById" parameterType="Long">
        delete from adm_ems_elec_load_index_15min where id = #{id}
    </delete>

    <delete id="deleteIndexByIds" parameterType="String">
        delete from adm_ems_elec_load_index_15min where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>