<?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.vehicledata.mapper.VehicleDataMapper">

    <resultMap type="VehicleData" id="VehicleDataResult">
        <result property="id"    column="id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="timestamp"    column="timestamp"    />
        <result property="vehicleStatus"    column="vehicle_status"    />
        <result property="chargingStatus"    column="charging_status"    />
        <result property="operationMode"    column="operation_mode"    />
        <result property="vehicleSpeed"    column="vehicle_speed"    />
        <result property="totalMileage"    column="total_mileage"    />
        <result property="totalVoltage"    column="total_voltage"    />
        <result property="totalCurrent"    column="total_current"    />
        <result property="soc"    column="soc"    />
        <result property="dcdcStatus"    column="dcdc_status"    />
        <result property="gearPosition"    column="gear_position"    />
        <result property="insulationResistance"    column="insulation_resistance"    />
        <result property="reservedData"    column="reserved_data"    />
        <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="selectVehicleDataVo">
        select id, device_id, timestamp, vehicle_status, charging_status, operation_mode, vehicle_speed, total_mileage, total_voltage, total_current, soc, dcdc_status, gear_position, insulation_resistance, reserved_data, create_time, update_time, create_by, update_by from vehicle_data
    </sql>

    <select id="selectVehicleDataList" parameterType="VehicleData" resultMap="VehicleDataResult">
        <include refid="selectVehicleDataVo"/>
        <where>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="timestamp != null "> and timestamp = #{timestamp}</if>
            <if test="vehicleStatus != null "> and vehicle_status = #{vehicleStatus}</if>
            <if test="chargingStatus != null "> and charging_status = #{chargingStatus}</if>
            <if test="operationMode != null "> and operation_mode = #{operationMode}</if>
            <if test="vehicleSpeed != null "> and vehicle_speed = #{vehicleSpeed}</if>
            <if test="totalMileage != null "> and total_mileage = #{totalMileage}</if>
            <if test="totalVoltage != null "> and total_voltage = #{totalVoltage}</if>
            <if test="totalCurrent != null "> and total_current = #{totalCurrent}</if>
            <if test="soc != null "> and soc = #{soc}</if>
            <if test="dcdcStatus != null "> and dcdc_status = #{dcdcStatus}</if>
            <if test="gearPosition != null "> and gear_position = #{gearPosition}</if>
            <if test="insulationResistance != null "> and insulation_resistance = #{insulationResistance}</if>
            <if test="reservedData != null  and reservedData != ''"> and reserved_data = #{reservedData}</if>
        </where>
    </select>

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

    <select id="selectByDeviceAndStartTime" resultMap="VehicleDataResult">
        <include refid="selectVehicleDataVo"/>
        where device_id = #{deviceId}
          and timestamp >= #{startTime}
        order by timestamp asc
    </select>

    <select id="selectVehicleDataById" parameterType="Long" resultMap="VehicleDataResult">
        <include refid="selectVehicleDataVo"/>
        where id = #{id}
    </select>

    <insert id="insertVehicleData" parameterType="VehicleData" useGeneratedKeys="true" keyProperty="id">
        insert into vehicle_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id,</if>
            <if test="timestamp != null">timestamp,</if>
            <if test="vehicleStatus != null">vehicle_status,</if>
            <if test="chargingStatus != null">charging_status,</if>
            <if test="operationMode != null">operation_mode,</if>
            <if test="vehicleSpeed != null">vehicle_speed,</if>
            <if test="totalMileage != null">total_mileage,</if>
            <if test="totalVoltage != null">total_voltage,</if>
            <if test="totalCurrent != null">total_current,</if>
            <if test="soc != null">soc,</if>
            <if test="dcdcStatus != null">dcdc_status,</if>
            <if test="gearPosition != null">gear_position,</if>
            <if test="insulationResistance != null">insulation_resistance,</if>
            <if test="reservedData != null">reserved_data,</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="vehicleStatus != null">#{vehicleStatus},</if>
            <if test="chargingStatus != null">#{chargingStatus},</if>
            <if test="operationMode != null">#{operationMode},</if>
            <if test="vehicleSpeed != null">#{vehicleSpeed},</if>
            <if test="totalMileage != null">#{totalMileage},</if>
            <if test="totalVoltage != null">#{totalVoltage},</if>
            <if test="totalCurrent != null">#{totalCurrent},</if>
            <if test="soc != null">#{soc},</if>
            <if test="dcdcStatus != null">#{dcdcStatus},</if>
            <if test="gearPosition != null">#{gearPosition},</if>
            <if test="insulationResistance != null">#{insulationResistance},</if>
            <if test="reservedData != null">#{reservedData},</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="updateVehicleData" parameterType="VehicleData">
        update vehicle_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
            <if test="timestamp != null">timestamp = #{timestamp},</if>
            <if test="vehicleStatus != null">vehicle_status = #{vehicleStatus},</if>
            <if test="chargingStatus != null">charging_status = #{chargingStatus},</if>
            <if test="operationMode != null">operation_mode = #{operationMode},</if>
            <if test="vehicleSpeed != null">vehicle_speed = #{vehicleSpeed},</if>
            <if test="totalMileage != null">total_mileage = #{totalMileage},</if>
            <if test="totalVoltage != null">total_voltage = #{totalVoltage},</if>
            <if test="totalCurrent != null">total_current = #{totalCurrent},</if>
            <if test="soc != null">soc = #{soc},</if>
            <if test="dcdcStatus != null">dcdc_status = #{dcdcStatus},</if>
            <if test="gearPosition != null">gear_position = #{gearPosition},</if>
            <if test="insulationResistance != null">insulation_resistance = #{insulationResistance},</if>
            <if test="reservedData != null">reserved_data = #{reservedData},</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="deleteVehicleDataById" parameterType="Long">
        delete from vehicle_data where id = #{id}
    </delete>

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