<?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.fastbee.iot.mapper.FirmwareTaskMapper">
    
    <resultMap type="com.fastbee.iot.domain.FirmwareTask" id="FirmwareTaskResult">
        <result property="id"    column="id"    />
        <result property="taskName"    column="task_name"    />
        <result property="firmwareId"    column="firmware_id"    />
        <result property="upgradeType"    column="upgrade_type"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="taskDesc"    column="task_desc"    />
        <result property="deviceAmount"    column="device_amount"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="bookTime"    column="book_time"    />
    </resultMap>

    <sql id="selectFirmwareTaskVo">
        select id, task_name, firmware_id, upgrade_type, create_time, update_time, task_desc, device_amount, del_flag, book_time from iot_firmware_task
    </sql>

    <select id="selectFirmwareTaskList" parameterType="com.fastbee.iot.domain.FirmwareTask" resultMap="FirmwareTaskResult">
        <include refid="selectFirmwareTaskVo"/>
        <where>
            <if test="id != null "> and id = #{id}</if>
            <if test="taskName != null  and taskName != ''"> and task_name like concat('%', #{taskName}, '%')</if>
            <if test="firmwareId != null "> and firmware_id = #{firmwareId}</if>
            <if test="upgradeType != null "> and upgrade_type = #{upgradeType}</if>
            <if test="taskDesc != null  and taskDesc != ''"> and task_desc = #{taskDesc}</if>
            <if test="deviceAmount != null "> and device_amount = #{deviceAmount}</if>
            <if test="bookTime != null "> and book_time = #{bookTime}</if>
        </where>
    </select>
    
    <select id="selectFirmwareTaskById" parameterType="Long" resultMap="FirmwareTaskResult">
        <include refid="selectFirmwareTaskVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertFirmwareTask" parameterType="com.fastbee.iot.domain.FirmwareTask" useGeneratedKeys="true" keyProperty="id">
        insert into iot_firmware_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskName != null and taskName != ''">task_name,</if>
            <if test="firmwareId != null">firmware_id,</if>
            <if test="upgradeType != null">upgrade_type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="taskDesc != null and taskDesc != ''">task_desc,</if>
            <if test="deviceAmount != null">device_amount,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="bookTime != null">book_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskName != null and taskName != ''">#{taskName},</if>
            <if test="firmwareId != null">#{firmwareId},</if>
            <if test="upgradeType != null">#{upgradeType},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="taskDesc != null and taskDesc != ''">#{taskDesc},</if>
            <if test="deviceAmount != null">#{deviceAmount},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="bookTime != null">#{bookTime},</if>
         </trim>
    </insert>

    <update id="updateFirmwareTask" parameterType="com.fastbee.iot.domain.FirmwareTask">
        update iot_firmware_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskName != null and taskName != ''">task_name = #{taskName},</if>
            <if test="firmwareId != null">firmware_id = #{firmwareId},</if>
            <if test="upgradeType != null">upgrade_type = #{upgradeType},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="taskDesc != null and taskDesc != ''">task_desc = #{taskDesc},</if>
            <if test="deviceAmount != null">device_amount = #{deviceAmount},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="bookTime != null">book_time = #{bookTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFirmwareTaskById" parameterType="Long">
        delete from iot_firmware_task where id = #{id}
    </delete>

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

    <select id="selectCountFirmwareTaskByTaskName" parameterType="java.lang.String" resultType="int">
        select count(t.task_name) from iot_firmware_task t
        where t.task_name = #{taskName,jdbcType=VARCHAR}
    </select>
</mapper>