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

    <resultMap type="com.ruoyi.ems.domain.AdmEmsElecBoxEqptIndex" id="AdmEmsElecBoxEqptIndexResult">
        <result property="id" column="id"/>
        <result property="boxCode" column="box_code"/>
        <result property="eqptCode" column="eqpt_code"/>
        <result property="areaCode" column="area_code"/>
        <result property="date" column="date"/>
        <result property="time" column="time"/>
        <result property="voltage" column="voltage"/>
        <result property="current" column="current"/>
        <result property="power" column="power"/>
        <result property="eqptSpec" column="eqpt_spec"/>
    </resultMap>

    <select id="selectAdmEmsElecBoxEqptLatestIndex" parameterType="com.ruoyi.ems.domain.AdmEmsElecBoxEqptIndex"
            resultMap="AdmEmsElecBoxEqptIndexResult">
        SELECT
            latestIndex.*,
            elecEqpt.eqpt_spec,
            elecEqpt.rated_current
        FROM
            (SELECT
                 a.*
             FROM
                 `adm_ems_elec_box_eqpt_index` a
                     JOIN
                 (SELECT
                      eqpt_code,
                      MAX(id) AS id
                  FROM
                      `adm_ems_elec_box_eqpt_index`
                  WHERE
                      box_code = #{boxCode}
                  GROUP BY
                      eqpt_code) latestTime
                 ON
                     a.eqpt_code = latestTime.eqpt_code AND a.id = latestTime.id
             WHERE
                 a.box_code = #{boxCode}
            ) latestIndex
                JOIN
            adm_box_elec_eqpt elecEqpt
            ON
                latestIndex.eqpt_code = elecEqpt.eqpt_code
    </select>


    <select id="selectAdmEmsElecBoxEqptIndexList" parameterType="com.ruoyi.ems.domain.AdmEmsElecBoxEqptIndex"
            resultMap="AdmEmsElecBoxEqptIndexResult">
        <include refid="selectAdmEmsElecBoxEqptIndexVo"/>
        <where>
            <if test="boxCode != null  and boxCode != ''">and box_code = #{boxCode}</if>
            <if test="eqptCode != null  and eqptCode != ''">and eqpt_code = #{eqptCode}</if>
            <if test="areaCode != null  and areaCode != ''">and area_code = #{areaCode}</if>
        </where>
    </select>

    <sql id="selectAdmEmsElecBoxEqptIndexVo">
        select id, box_code, eqpt_code, area_code, date, time, voltage, current, power
        from adm_ems_elec_box_eqpt_index
    </sql>

    <select id="selectAdmEmsElecBoxEqptIndexById" parameterType="Long" resultMap="AdmEmsElecBoxEqptIndexResult">
        <include refid="selectAdmEmsElecBoxEqptIndexVo"/>
        where id = #{id}
    </select>

    <insert id="insertAdmEmsElecBoxEqptIndex" parameterType="com.ruoyi.ems.domain.AdmEmsElecBoxEqptIndex"
            useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_elec_box_eqpt_index
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="boxCode != null and boxCode != ''">box_code,</if>
            <if test="eqptCode != null and eqptCode != ''">eqpt_code,</if>
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="date != null">date,</if>
            <if test="time != null">time,</if>
            <if test="voltage != null">voltage,</if>
            <if test="current != null">current,</if>
            <if test="power != null">power,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="boxCode != null and boxCode != ''">#{boxCode},</if>
            <if test="eqptCode != null and eqptCode != ''">#{eqptCode},</if>
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="date != null">#{date},</if>
            <if test="time != null">#{time},</if>
            <if test="voltage != null">#{voltage},</if>
            <if test="current != null">#{current},</if>
            <if test="power != null">#{power},</if>
        </trim>
    </insert>

    <update id="updateAdmEmsElecBoxEqptIndex" parameterType="com.ruoyi.ems.domain.AdmEmsElecBoxEqptIndex">
        update adm_ems_elec_box_eqpt_index
        <trim prefix="SET" suffixOverrides=",">
            <if test="boxCode != null and boxCode != ''">box_code = #{boxCode},</if>
            <if test="eqptCode != null and eqptCode != ''">eqpt_code = #{eqptCode},</if>
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="date != null">date = #{date},</if>
            <if test="time != null">time = #{time},</if>
            <if test="voltage != null">voltage = #{voltage},</if>
            <if test="current != null">current = #{current},</if>
            <if test="power != null">power = #{power},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAdmEmsElecBoxEqptIndexById" parameterType="Long">
        delete
        from adm_ems_elec_box_eqpt_index
        where id = #{id}
    </delete>

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