<?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.evobms.project.Battery.mapper.ExtremeValuesMapper">
    
    <resultMap type="ExtremeValues" id="ExtremeValuesResult">
        <result property="id"    column="id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="timestamp"    column="timestamp"    />
        <result property="maxVoltageSubsystemNo"    column="max_voltage_subsystem_no"    />
        <result property="maxVoltageBatteryNo"    column="max_voltage_battery_no"    />
        <result property="maxVoltageValue"    column="max_voltage_value"    />
        <result property="minVoltageSubsystemNo"    column="min_voltage_subsystem_no"    />
        <result property="minVoltageBatteryNo"    column="min_voltage_battery_no"    />
        <result property="minVoltageValue"    column="min_voltage_value"    />
        <result property="maxTempSubsystemNo"    column="max_temp_subsystem_no"    />
        <result property="maxTempProbeNo"    column="max_temp_probe_no"    />
        <result property="maxTempValue"    column="max_temp_value"    />
        <result property="minTempSubsystemNo"    column="min_temp_subsystem_no"    />
        <result property="minTempProbeNo"    column="min_temp_probe_no"    />
        <result property="minTempValue"    column="min_temp_value"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectExtremeValuesVo">
        select id, device_id, timestamp, max_voltage_subsystem_no, max_voltage_battery_no, max_voltage_value, min_voltage_subsystem_no, min_voltage_battery_no, min_voltage_value, max_temp_subsystem_no, max_temp_probe_no, max_temp_value, min_temp_subsystem_no, min_temp_probe_no, min_temp_value, create_time, update_time, create_by, update_by from extreme_values
    </sql>

    <select id="selectExtremeValuesList" parameterType="ExtremeValues" resultMap="ExtremeValuesResult">
        <include refid="selectExtremeValuesVo"/>
        <where>  
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="timestamp != null "> and timestamp = #{timestamp}</if>
            <if test="maxVoltageSubsystemNo != null "> and max_voltage_subsystem_no = #{maxVoltageSubsystemNo}</if>
            <if test="maxVoltageBatteryNo != null "> and max_voltage_battery_no = #{maxVoltageBatteryNo}</if>
            <if test="maxVoltageValue != null "> and max_voltage_value = #{maxVoltageValue}</if>
            <if test="minVoltageSubsystemNo != null "> and min_voltage_subsystem_no = #{minVoltageSubsystemNo}</if>
            <if test="minVoltageBatteryNo != null "> and min_voltage_battery_no = #{minVoltageBatteryNo}</if>
            <if test="minVoltageValue != null "> and min_voltage_value = #{minVoltageValue}</if>
            <if test="maxTempSubsystemNo != null "> and max_temp_subsystem_no = #{maxTempSubsystemNo}</if>
            <if test="maxTempProbeNo != null "> and max_temp_probe_no = #{maxTempProbeNo}</if>
            <if test="maxTempValue != null "> and max_temp_value = #{maxTempValue}</if>
            <if test="minTempSubsystemNo != null "> and min_temp_subsystem_no = #{minTempSubsystemNo}</if>
            <if test="minTempProbeNo != null "> and min_temp_probe_no = #{minTempProbeNo}</if>
            <if test="minTempValue != null "> and min_temp_value = #{minTempValue}</if>
        </where>
    </select>
    
    <select id="selectExtremeValuesById" parameterType="Long" resultMap="ExtremeValuesResult">
        <include refid="selectExtremeValuesVo"/>
        where id = #{id}
    </select>

    <insert id="insertExtremeValues" parameterType="ExtremeValues" useGeneratedKeys="true" keyProperty="id">
        insert into extreme_values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id,</if>
            <if test="timestamp != null">timestamp,</if>
            <if test="maxVoltageSubsystemNo != null">max_voltage_subsystem_no,</if>
            <if test="maxVoltageBatteryNo != null">max_voltage_battery_no,</if>
            <if test="maxVoltageValue != null">max_voltage_value,</if>
            <if test="minVoltageSubsystemNo != null">min_voltage_subsystem_no,</if>
            <if test="minVoltageBatteryNo != null">min_voltage_battery_no,</if>
            <if test="minVoltageValue != null">min_voltage_value,</if>
            <if test="maxTempSubsystemNo != null">max_temp_subsystem_no,</if>
            <if test="maxTempProbeNo != null">max_temp_probe_no,</if>
            <if test="maxTempValue != null">max_temp_value,</if>
            <if test="minTempSubsystemNo != null">min_temp_subsystem_no,</if>
            <if test="minTempProbeNo != null">min_temp_probe_no,</if>
            <if test="minTempValue != null">min_temp_value,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
            <if test="timestamp != null">#{timestamp},</if>
            <if test="maxVoltageSubsystemNo != null">#{maxVoltageSubsystemNo},</if>
            <if test="maxVoltageBatteryNo != null">#{maxVoltageBatteryNo},</if>
            <if test="maxVoltageValue != null">#{maxVoltageValue},</if>
            <if test="minVoltageSubsystemNo != null">#{minVoltageSubsystemNo},</if>
            <if test="minVoltageBatteryNo != null">#{minVoltageBatteryNo},</if>
            <if test="minVoltageValue != null">#{minVoltageValue},</if>
            <if test="maxTempSubsystemNo != null">#{maxTempSubsystemNo},</if>
            <if test="maxTempProbeNo != null">#{maxTempProbeNo},</if>
            <if test="maxTempValue != null">#{maxTempValue},</if>
            <if test="minTempSubsystemNo != null">#{minTempSubsystemNo},</if>
            <if test="minTempProbeNo != null">#{minTempProbeNo},</if>
            <if test="minTempValue != null">#{minTempValue},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
         </trim>
    </insert>

    <update id="updateExtremeValues" parameterType="ExtremeValues">
        update extreme_values
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
            <if test="timestamp != null">timestamp = #{timestamp},</if>
            <if test="maxVoltageSubsystemNo != null">max_voltage_subsystem_no = #{maxVoltageSubsystemNo},</if>
            <if test="maxVoltageBatteryNo != null">max_voltage_battery_no = #{maxVoltageBatteryNo},</if>
            <if test="maxVoltageValue != null">max_voltage_value = #{maxVoltageValue},</if>
            <if test="minVoltageSubsystemNo != null">min_voltage_subsystem_no = #{minVoltageSubsystemNo},</if>
            <if test="minVoltageBatteryNo != null">min_voltage_battery_no = #{minVoltageBatteryNo},</if>
            <if test="minVoltageValue != null">min_voltage_value = #{minVoltageValue},</if>
            <if test="maxTempSubsystemNo != null">max_temp_subsystem_no = #{maxTempSubsystemNo},</if>
            <if test="maxTempProbeNo != null">max_temp_probe_no = #{maxTempProbeNo},</if>
            <if test="maxTempValue != null">max_temp_value = #{maxTempValue},</if>
            <if test="minTempSubsystemNo != null">min_temp_subsystem_no = #{minTempSubsystemNo},</if>
            <if test="minTempProbeNo != null">min_temp_probe_no = #{minTempProbeNo},</if>
            <if test="minTempValue != null">min_temp_value = #{minTempValue},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteExtremeValuesById" parameterType="Long">
        delete from extreme_values where id = #{id}
    </delete>

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

    <select id="selectLatestByDeviceId" resultMap="ExtremeValuesResult">
        <include refid="selectExtremeValuesVo"/>
        where device_id = #{deviceId}
        order by timestamp desc
        limit 1
    </select>

    <select id="selectLatestByDeviceIdBefore" resultMap="ExtremeValuesResult">
        <include refid="selectExtremeValuesVo"/>
        where device_id = #{deviceId}
          and timestamp &lt;= #{beforeTime}
        order by timestamp desc
        limit 1
    </select>
</mapper>