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

    <resultMap type="SubsystemTemperature" id="SubsystemTemperatureResult">
        <result property="id"    column="id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="timestamp"    column="timestamp"    />
        <result property="subsystemCount"    column="subsystem_count"    />
        <result property="subsystemNo"    column="subsystem_no"    />
        <result property="tempProbeCount"    column="temp_probe_count"    />
        <result property="temperatureValues"    column="temperature_values"    />
        <result property="createTime"    column="create_time"    />
        <result property="updatedTime"    column="updated_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectSubsystemTemperatureVo">
        select id, device_id, timestamp, subsystem_count, subsystem_no, temp_probe_count, temperature_values, create_time, updated_time, create_by, update_by from subsystem_temperature
    </sql>

    <select id="selectSubsystemTemperatureList" parameterType="SubsystemTemperature" resultMap="SubsystemTemperatureResult">
        <include refid="selectSubsystemTemperatureVo"/>
        <where>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="timestamp != null "> and timestamp = #{timestamp}</if>
            <if test="subsystemCount != null "> and subsystem_count = #{subsystemCount}</if>
            <if test="subsystemNo != null "> and subsystem_no = #{subsystemNo}</if>
            <if test="tempProbeCount != null "> and temp_probe_count = #{tempProbeCount}</if>
            <if test="temperatureValues != null  and temperatureValues != ''"> and temperature_values = #{temperatureValues}</if>
            <if test="updatedTime != null "> and updated_time = #{updatedTime}</if>
        </where>
    </select>

    <select id="selectSubsystemTemperatureById" parameterType="Long" resultMap="SubsystemTemperatureResult">
        <include refid="selectSubsystemTemperatureVo"/>
        where id = #{id}
    </select>

    <insert id="insertSubsystemTemperature" parameterType="SubsystemTemperature" useGeneratedKeys="true" keyProperty="id">
        insert into subsystem_temperature
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id,</if>
            <if test="timestamp != null">timestamp,</if>
            <if test="subsystemCount != null">subsystem_count,</if>
            <if test="subsystemNo != null">subsystem_no,</if>
            <if test="tempProbeCount != null">temp_probe_count,</if>
            <if test="temperatureValues != null">temperature_values,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updatedTime != null">updated_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="subsystemCount != null">#{subsystemCount},</if>
            <if test="subsystemNo != null">#{subsystemNo},</if>
            <if test="tempProbeCount != null">#{tempProbeCount},</if>
            <if test="temperatureValues != null">#{temperatureValues},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
        </trim>
    </insert>

    <update id="updateSubsystemTemperature" parameterType="SubsystemTemperature">
        update subsystem_temperature
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
            <if test="timestamp != null">timestamp = #{timestamp},</if>
            <if test="subsystemCount != null">subsystem_count = #{subsystemCount},</if>
            <if test="subsystemNo != null">subsystem_no = #{subsystemNo},</if>
            <if test="tempProbeCount != null">temp_probe_count = #{tempProbeCount},</if>
            <if test="temperatureValues != null">temperature_values = #{temperatureValues},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updatedTime != null">updated_time = #{updatedTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSubsystemTemperatureById" parameterType="Long">
        delete from subsystem_temperature where id = #{id}
    </delete>

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

    <select id="selectLatestByDeviceAndSubsystem" resultMap="SubsystemTemperatureResult">
        <include refid="selectSubsystemTemperatureVo"/>
        where device_id = #{deviceId}
          and subsystem_no = #{subsystemNo}
        order by timestamp desc
        limit 1
    </select>
</mapper>
