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

    <resultMap type="SubsystemVoltage" id="SubsystemVoltageResult">
        <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="subsystemVoltage"    column="subsystem_voltage"    />
        <result property="subsystemCurrent"    column="subsystem_current"    />
        <result property="totalBatteryCount"    column="total_battery_count"    />
        <result property="frameStartBatteryNo"    column="frame_start_battery_no"    />
        <result property="frameBatteryCount"    column="frame_battery_count"    />
        <result property="batteryVoltages"    column="battery_voltages"    />
        <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="selectSubsystemVoltageVo">
        select id, device_id, timestamp, subsystem_count, subsystem_no, subsystem_voltage, subsystem_current, total_battery_count, frame_start_battery_no, frame_battery_count, battery_voltages, create_time, update_time, create_by, update_by from subsystem_voltage
    </sql>

    <select id="selectSubsystemVoltageList" parameterType="SubsystemVoltage" resultMap="SubsystemVoltageResult">
        <include refid="selectSubsystemVoltageVo"/>
        <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="subsystemVoltage != null "> and subsystem_voltage = #{subsystemVoltage}</if>
            <if test="subsystemCurrent != null "> and subsystem_current = #{subsystemCurrent}</if>
            <if test="totalBatteryCount != null "> and total_battery_count = #{totalBatteryCount}</if>
            <if test="frameStartBatteryNo != null "> and frame_start_battery_no = #{frameStartBatteryNo}</if>
            <if test="frameBatteryCount != null "> and frame_battery_count = #{frameBatteryCount}</if>
            <if test="batteryVoltages != null  and batteryVoltages != ''"> and battery_voltages = #{batteryVoltages}</if>
        </where>
    </select>

    <select id="selectSubsystemVoltageById" parameterType="Long" resultMap="SubsystemVoltageResult">
        <include refid="selectSubsystemVoltageVo"/>
        where id = #{id}
    </select>

    <insert id="insertSubsystemVoltage" parameterType="SubsystemVoltage" useGeneratedKeys="true" keyProperty="id">
        insert into subsystem_voltage
        <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="subsystemVoltage != null">subsystem_voltage,</if>
            <if test="subsystemCurrent != null">subsystem_current,</if>
            <if test="totalBatteryCount != null">total_battery_count,</if>
            <if test="frameStartBatteryNo != null">frame_start_battery_no,</if>
            <if test="frameBatteryCount != null">frame_battery_count,</if>
            <if test="batteryVoltages != null">battery_voltages,</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="subsystemCount != null">#{subsystemCount},</if>
            <if test="subsystemNo != null">#{subsystemNo},</if>
            <if test="subsystemVoltage != null">#{subsystemVoltage},</if>
            <if test="subsystemCurrent != null">#{subsystemCurrent},</if>
            <if test="totalBatteryCount != null">#{totalBatteryCount},</if>
            <if test="frameStartBatteryNo != null">#{frameStartBatteryNo},</if>
            <if test="frameBatteryCount != null">#{frameBatteryCount},</if>
            <if test="batteryVoltages != null">#{batteryVoltages},</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="updateSubsystemVoltage" parameterType="SubsystemVoltage">
        update subsystem_voltage
        <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="subsystemVoltage != null">subsystem_voltage = #{subsystemVoltage},</if>
            <if test="subsystemCurrent != null">subsystem_current = #{subsystemCurrent},</if>
            <if test="totalBatteryCount != null">total_battery_count = #{totalBatteryCount},</if>
            <if test="frameStartBatteryNo != null">frame_start_battery_no = #{frameStartBatteryNo},</if>
            <if test="frameBatteryCount != null">frame_battery_count = #{frameBatteryCount},</if>
            <if test="batteryVoltages != null">battery_voltages = #{batteryVoltages},</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="deleteSubsystemVoltageById" parameterType="Long">
        delete from subsystem_voltage where id = #{id}
    </delete>

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

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