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

    <resultMap type="com.ruoyi.ems.domain.AdmEmsElecBoxIndex" id="AdmEmsElecBoxIndexResult">
        <result property="id" column="id"/>
        <result property="boxCode" column="box_code"/>
        <result property="boxType" column="box_type"/>
        <result property="areaCode" column="area_code"/>
        <result property="date" column="date"/>
        <result property="time" column="time"/>
        <result property="voltageA" column="voltage_a"/>
        <result property="voltageB" column="voltage_b"/>
        <result property="voltageC" column="voltage_c"/>
        <result property="electricityA" column="electricity_a"/>
        <result property="electricityB" column="electricity_b"/>
        <result property="electricityC" column="electricity_c"/>
        <result property="power" column="power"/>
    </resultMap>

    <sql id="selectAdmEmsElecBoxIndexVo">
        select id,
               box_code,
               box_type,
               area_code, date, time, voltage_a, voltage_b, voltage_c, electricity_a, electricity_b, electricity_c, power
        from adm_ems_elec_box_index
    </sql>

    <select id="selectAdmEmsElecBoxIndexList" parameterType="com.ruoyi.ems.domain.AdmEmsElecBoxIndex"
            resultMap="AdmEmsElecBoxIndexResult">
        <include refid="selectAdmEmsElecBoxIndexVo"/>
        <where>
            <if test="boxCode != null  and boxCode != ''">and box_code = #{boxCode}</if>
            <if test="boxType != null ">and box_type = #{boxType}</if>
            <if test="areaCode != null  and areaCode != ''">and area_code = #{areaCode}</if>
        </where>
    </select>

    <select id="selectAdmEmsElecBoxIndexById" parameterType="Long" resultMap="AdmEmsElecBoxIndexResult">
        <include refid="selectAdmEmsElecBoxIndexVo"/>
        where id = #{id}
    </select>
    <select id="selectElecBoxLatestIndexByBoxCode" parameterType="String"
            resultType="com.ruoyi.ems.domain.AdmEmsElecBoxIndex">
        SELECT *
        FROM (SELECT a.*
              FROM adm_ems_elec_box_index a
                       JOIN (SELECT box_code,
                                    MAX(id) AS max_id
                             FROM adm_ems_elec_box_index
                             WHERE box_code = #{boxCode}
                             GROUP BY box_code) latestId
                            ON
                                a.box_code = latestId.box_code AND a.id = latestId.max_id
              WHERE a.box_code = #{boxCode}) latestIndex;
    </select>

    <insert id="insertAdmEmsElecBoxIndex" parameterType="com.ruoyi.ems.domain.AdmEmsElecBoxIndex"
            useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_elec_box_index
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="boxCode != null and boxCode != ''">box_code,</if>
            <if test="boxType != null">box_type,</if>
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="date != null">date,</if>
            <if test="time != null">time,</if>
            <if test="voltageA != null">voltage_a,</if>
            <if test="voltageB != null">voltage_b,</if>
            <if test="voltageC != null">voltage_c,</if>
            <if test="electricityA != null">electricity_a,</if>
            <if test="electricityB != null">electricity_b,</if>
            <if test="electricityC != null">electricity_c,</if>
            <if test="power != null">power,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="boxCode != null and boxCode != ''">#{boxCode},</if>
            <if test="boxType != null">#{boxType},</if>
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="date != null">#{date},</if>
            <if test="time != null">#{time},</if>
            <if test="voltageA != null">#{voltageA},</if>
            <if test="voltageB != null">#{voltageB},</if>
            <if test="voltageC != null">#{voltageC},</if>
            <if test="electricityA != null">#{electricityA},</if>
            <if test="electricityB != null">#{electricityB},</if>
            <if test="electricityC != null">#{electricityC},</if>
            <if test="power != null">#{power},</if>
        </trim>
    </insert>

    <update id="updateAdmEmsElecBoxIndex" parameterType="com.ruoyi.ems.domain.AdmEmsElecBoxIndex">
        update adm_ems_elec_box_index
        <trim prefix="SET" suffixOverrides=",">
            <if test="boxCode != null and boxCode != ''">box_code = #{boxCode},</if>
            <if test="boxType != null">box_type = #{boxType},</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="voltageA != null">voltage_a = #{voltageA},</if>
            <if test="voltageB != null">voltage_b = #{voltageB},</if>
            <if test="voltageC != null">voltage_c = #{voltageC},</if>
            <if test="electricityA != null">electricity_a = #{electricityA},</if>
            <if test="electricityB != null">electricity_b = #{electricityB},</if>
            <if test="electricityC != null">electricity_c = #{electricityC},</if>
            <if test="power != null">power = #{power},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAdmEmsElecBoxIndexById" parameterType="Long">
        delete
        from adm_ems_elec_box_index
        where id = #{id}
    </delete>

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