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

    <resultMap type="com.ruoyi.ems.domain.AdmEmsElecPgIndex" id="AdmEmsElecPgIndexResult">
        <result property="id" column="id"/>
        <result property="areaCode" column="area_code"/>
        <result property="facsCode" column="facs_code"/>
        <result property="date" column="date"/>
        <result property="time" column="time"/>
        <result property="voltage" column="voltage"/>
        <result property="electricity" column="electricity"/>
        <result property="power" column="power"/>
    </resultMap>

    <sql id="selectAdmEmsElecPgIndexVo">
        select id, area_code, facs_code, date, time, voltage, electricity, power
        from adm_ems_elec_pg_index
    </sql>

    <select id="selectAdmEmsElecPgIndexList" parameterType="com.ruoyi.ems.domain.AdmEmsElecPgIndex"
            resultMap="AdmEmsElecPgIndexResult">
        <include refid="selectAdmEmsElecPgIndexVo"/>
        <where>
            <if test="areaCode != null  and areaCode != ''">and area_code = #{areaCode}</if>
            <if test="facsCode != null  and facsCode != ''">and facs_code = #{facsCode}</if>
            <if test="date != null ">and date = #{date}</if>
            <if test="time != null ">and time = #{time}</if>
            <if test="voltage != null ">and voltage = #{voltage}</if>
            <if test="electricity != null ">and electricity = #{electricity}</if>
            <if test="power != null ">and power = #{power}</if>
        </where>
    </select>

    <select id="selectAdmEmsElecPgIndexById" parameterType="Long" resultMap="AdmEmsElecPgIndexResult">
        <include refid="selectAdmEmsElecPgIndexVo"/>
        where id = #{id}
    </select>

    <insert id="insertAdmEmsElecPgIndex" parameterType="com.ruoyi.ems.domain.AdmEmsElecPgIndex" useGeneratedKeys="true"
            keyProperty="id">
        insert into adm_ems_elec_pg_index
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="facsCode != null and facsCode != ''">facs_code,</if>
            <if test="date != null">date,</if>
            <if test="time != null">time,</if>
            <if test="voltage != null">voltage,</if>
            <if test="electricity != null">electricity,</if>
            <if test="power != null">power,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="facsCode != null and facsCode != ''">#{facsCode},</if>
            <if test="date != null">#{date},</if>
            <if test="time != null">#{time},</if>
            <if test="voltage != null">#{voltage},</if>
            <if test="electricity != null">#{electricity},</if>
            <if test="power != null">#{power},</if>
        </trim>
    </insert>

    <update id="updateAdmEmsElecPgIndex" parameterType="com.ruoyi.ems.domain.AdmEmsElecPgIndex">
        update adm_ems_elec_pg_index
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="facsCode != null and facsCode != ''">facs_code = #{facsCode},</if>
            <if test="date != null">date = #{date},</if>
            <if test="time != null">time = #{time},</if>
            <if test="voltage != null">voltage = #{voltage},</if>
            <if test="electricity != null">electricity = #{electricity},</if>
            <if test="power != null">power = #{power},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAdmEmsElecPgIndexById" parameterType="Long">
        delete
        from adm_ems_elec_pg_index
        where id = #{id}
    </delete>

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

    <select id="qryLatestObjIndex" resultType="Map">
        SELECT
        id, area_code as areaCode, facs_code as facsCode, date, time, voltage, electricity, power
        FROM
        ( SELECT *, RANK() OVER ( PARTITION BY facs_code ORDER BY time DESC ) AS num FROM adm_ems_elec_pg_index pgInde
        where facs_code
        <foreach item="item" index="index" collection="set" open="in (" separator=", " close=")">
            #{item}
        </foreach>
        ) f
        WHERE
        f.num =1
    </select>

    <select id="qryAreaDayPowerIndex" resultType="Map">
        SELECT avgIndex.area_code areaCode,
               DATE_FORMAT(avgIndex.DATE, '%Y-%m-%d') AS dateIndex,
               sum(power)                                power
        FROM (SELECT pgIndex.area_code,
                     pgIndex.DATE,
                     pgIndex.facs_code,
                     AVG(power) power
              FROM adm_ems_elec_pg_index pgIndex
              WHERE area_code = #{areaCode}
                AND pgIndex.DATE >= #{date}
                AND power > 0
              GROUP BY pgIndex.area_code,
                       pgIndex.DATE,
                       pgIndex.facs_code) avgIndex
        GROUP BY avgIndex.area_code,
                 dateIndex
    </select>

</mapper>
