<?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.ElecStoreIndexMapper">
    
    <resultMap type="com.ruoyi.ems.domain.ElecStoreIndex" id="ElecStoreIndexResult">
        <result property="id"    column="id"    />
        <result property="areaCode"    column="area_code"    />
        <result property="facsCode"    column="facs_code"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="date"    column="date"    />
        <result property="time"    column="time"    />
        <result property="chargeVoltage"    column="charge_voltage"    />
        <result property="dischargePower"    column="discharge_power"    />
        <result property="currentCapacity"    column="current_capacity"    />
    </resultMap>

    <sql id="selectElecStoreIndexVo">
        select id, area_code, facs_code, device_code, date, time, charge_voltage, discharge_power, current_capacity from adm_ems_elec_store_index
    </sql>

    <select id="selectElecStoreIndexList" parameterType="com.ruoyi.ems.domain.ElecStoreIndex" resultMap="ElecStoreIndexResult">
        <include refid="selectElecStoreIndexVo"/>
        <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="deviceCode != null  and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="date != null "> and `date` = #{date}</if>
            <if test="time != null "> and `time` = #{time}</if>
            <if test="startRecTime != null and startRecTime != ''">
                AND `time` &gt;= #{startRecTime}
            </if>
            <if test="endRecTime != null and endRecTime != ''">
                AND `time` &lt;= #{endRecTime}
            </if>
        </where>
    </select>

    <select id="selectLatestByDevice" parameterType="com.ruoyi.ems.domain.ElecStoreIndex" resultMap="ElecStoreIndexResult">
        SELECT t1.id, t1.area_code, t1.facs_code, t1.device_code, t1.date, t1.time,
        t1.charge_voltage, t1.discharge_power, t1.current_capacity
        FROM adm_ems_elec_store_index t1
        INNER JOIN (
        SELECT device_code, MAX(`time`) AS max_time
        FROM adm_ems_elec_store_index
        <where>
            <if test="areaCode != null and areaCode != ''">AND area_code = #{areaCode}</if>
            <if test="facsCode != null and facsCode != ''">AND facs_code = #{facsCode}</if>
        </where>
        GROUP BY device_code
        ) t2 ON t1.device_code = t2.device_code AND t1.`time` = t2.max_time
        <where>
            <if test="areaCode != null and areaCode != ''">AND t1.area_code = #{areaCode}</if>
            <if test="facsCode != null and facsCode != ''">AND t1.facs_code = #{facsCode}</if>
        </where>
        ORDER BY t1.area_code, t1.facs_code, t1.device_code
    </select>

        
    <insert id="insertElecStoreIndex" parameterType="com.ruoyi.ems.domain.ElecStoreIndex" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_elec_store_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="deviceCode != null and deviceCode != ''">device_code,</if>
            <if test="date != null">`date`,</if>
            <if test="time != null">`time`,</if>
            <if test="chargeVoltage != null">charge_voltage,</if>
            <if test="dischargePower != null">discharge_power,</if>
            <if test="currentCapacity != null">current_capacity,</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="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
            <if test="date != null">#{date},</if>
            <if test="time != null">#{time},</if>
            <if test="chargeVoltage != null">#{chargeVoltage},</if>
            <if test="dischargePower != null">#{dischargePower},</if>
            <if test="currentCapacity != null">#{currentCapacity},</if>
         </trim>
    </insert>

    <update id="updateElecStoreIndex" parameterType="com.ruoyi.ems.domain.ElecStoreIndex">
        update adm_ems_elec_store_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="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
            <if test="date != null">`date` = #{date},</if>
            <if test="time != null">`time` = #{time},</if>
            <if test="chargeVoltage != null">charge_voltage = #{chargeVoltage},</if>
            <if test="dischargePower != null">discharge_power = #{dischargePower},</if>
            <if test="currentCapacity != null">current_capacity = #{currentCapacity},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteElecStoreIndexById" parameterType="Long">
        delete from adm_ems_elec_store_index where id = #{id}
    </delete>

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