<?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.ota.mapper.OtaTasksMapper">

    <resultMap type="OtaTasks" id="OtaTasksResult">
        <result property="id"    column="id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="taskName"    column="task_name"    />
        <result property="firmwareVersion"    column="firmware_version"    />
        <result property="status"    column="status"    />
        <result property="progress"    column="progress"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="errorMessage"    column="error_message"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createBy"    column="create_by"    />
    </resultMap>

    <sql id="selectOtaTasksVo">
        select id, device_id, task_name, firmware_version, status, progress, start_time, end_time, error_message, create_time, update_time, update_by, create_by from ota_tasks
    </sql>

    <select id="selectOtaTasksList" parameterType="OtaTasks" resultMap="OtaTasksResult">
        <include refid="selectOtaTasksVo"/>
        <where>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="taskName != null  and taskName != ''"> and task_name like concat('%', #{taskName}, '%')</if>
            <if test="firmwareVersion != null  and firmwareVersion != ''"> and firmware_version = #{firmwareVersion}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="progress != null "> and progress = #{progress}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="errorMessage != null  and errorMessage != ''"> and error_message = #{errorMessage}</if>
        </where>
    </select>

    <select id="selectOtaTasksById" parameterType="String" resultMap="OtaTasksResult">
        <include refid="selectOtaTasksVo"/>
        where id = #{id}
    </select>

    <insert id="insertOtaTasks" parameterType="OtaTasks">
        insert into ota_tasks
        <trim prefix="(" suffix=")" suffixOverrides=",">
            id,
            <if test="deviceId != null and deviceId != ''">device_id,</if>
            <if test="taskName != null and taskName != ''">task_name,</if>
            <if test="firmwareVersion != null and firmwareVersion != ''">firmware_version,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="progress != null">progress,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="errorMessage != null">error_message,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <!-- 未传 id 时用 MySQL UUID() 自动生成 -->
            COALESCE(#{id}, UUID()),
            <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
            <if test="taskName != null and taskName != ''">#{taskName},</if>
            <if test="firmwareVersion != null and firmwareVersion != ''">#{firmwareVersion},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="progress != null">#{progress},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="errorMessage != null">#{errorMessage},</if>
            <!-- 未传 createTime 时用 NOW()，也可交给表默认值 -->
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
        </trim>
    </insert>

    <update id="updateOtaTasks" parameterType="OtaTasks">
        update ota_tasks
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
            <if test="taskName != null and taskName != ''">task_name = #{taskName},</if>
            <if test="firmwareVersion != null and firmwareVersion != ''">firmware_version = #{firmwareVersion},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="progress != null">progress = #{progress},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteOtaTasksById" parameterType="String">
        delete from ota_tasks where id = #{id}
    </delete>

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