<?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.FirmwareTaskDetailMapper">
    
    <resultMap type="com.fastbee.iot.domain.FirmwareTaskDetail" id="FirmwareTaskDetailResult">
    <result property="id"    column="id"    />
    <result property="taskId"    column="task_id"    />
    <result property="serialNumber"    column="serial_number"    />
    <result property="upgradeStatus"    column="upgrade_status"    />
    <result property="createTime"    column="create_time"    />
    <result property="updateTime"    column="update_time"    />
    <result property="detailMsg"    column="detail_msg"    />
    <result property="messageId"    column="message_id"    />
   </resultMap>

    <resultMap type="com.fastbee.iot.domain.FirmwareTaskDetail" id="FirmwareTaskDetailResult2">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="serialNumber"    column="serial_number"    />
        <result property="upgradeStatus"    column="upgrade_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="detailMsg"    column="detail_msg"    />
        <result property="messageId"    column="message_id"    />
        <result property="taskName"     column="task_name" jdbcType="VARCHAR"/>
        <result property="firmwareName" column="firmware_name" jdbcType="VARCHAR"/>
        <result property="version" column="firmware_version" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="selectFirmwareTaskDetailVo">
        select id, task_id, serial_number, upgrade_status, create_time, update_time, detail_msg, message_id from iot_firmware_task_detail
    </sql>

    <select id="selectFirmwareTaskDetailList" parameterType="com.fastbee.iot.domain.FirmwareTaskDetail" resultMap="FirmwareTaskDetailResult">
        <include refid="selectFirmwareTaskDetailVo"/>
        <where>  
            <if test="taskId != null "> and task_id = #{taskId}</if>
            <if test="serialNumber != null  and serialNumber != ''"> and serial_number = #{serialNumber}</if>
            <if test="upgradeStatus != null "> and upgrade_status = #{upgradeStatus}</if>
            <if test="detailMsg != null  and detailMsg != ''"> and detail_msg = #{detailMsg}</if>
            <if test="messageId != null  and messageId != ''"> and message_id = #{messageId}</if>
        </where>
    </select>

    <!-- 根据固件id查询下属设备列表 -->
    <select id="selectFirmwareTaskDetailListByFirmwareId" parameterType="com.fastbee.iot.model.FirmwareTaskDetailInput" resultType="com.fastbee.iot.model.FirmwareTaskDetailOutput">
        SELECT
            ftd.id,
            ft.firmware_id AS 'firmwareId',
            ftd.task_id AS 'taskId',
            ft.task_name AS 'taskName',
            d.device_name AS 'deviceName',
            ftd.serial_number AS 'serialNumber',
            ftd.message_id AS 'messageId',
            d.firmware_version AS 'version',
            ftd.upgrade_status AS 'upgradeStatus',
            ftd.detail_msg AS 'detailMsg',
            ftd.create_time AS 'createTime',
            ftd.update_time AS 'updateTime'
        FROM
        iot_firmware_task_detail ftd
        LEFT JOIN iot_firmware_task ft ON ftd.task_id = ft.id
        LEFT JOIN iot_device d ON ftd.serial_number = d.serial_number
        <where>
            ft.firmware_id = #{firmwareId}
            <if test = "taskId != null" >
                AND ftd.task_id = #{taskId,jdbcType=BIGINT}
            </if>
            <if test = "deviceName != null and deviceName != '' " >
                AND d.device_name = #{deviceName,jdbcType=VARCHAR}
            </if>
            <if test = "serialNumber != null and serialNumber != '' " >
                AND ftd.serial_number = #{serialNumber,jdbcType=VARCHAR}
            </if>
            <if test = "upgradeStatus != null" >
                AND ftd.upgrade_status = #{upgradeStatus}
            </if>
        </where>
    </select>

    <!--固件升级设备统计-->
    <select id="deviceStatistic" parameterType="com.fastbee.iot.model.FirmwareTaskDetailInput" resultType="com.fastbee.iot.model.FirmwareTaskDeviceStatistic">
        SELECT
            ftd.upgrade_status AS 'upgradeStatus',
            count(ftd.id) AS 'deviceCount'
        FROM
            iot_firmware_task_detail ftd
        LEFT JOIN iot_firmware_task ft ON ftd.task_id = ft.id
        <where>
            ft.firmware_id = #{firmwareId}
            <if test = "taskId != null" >
                AND ftd.task_id = #{taskId,jdbcType=BIGINT}
            </if>
        </where>
        GROUP BY ftd.upgrade_status
    </select>
    
    <select id="selectFirmwareTaskDetailById" parameterType="Long" resultMap="FirmwareTaskDetailResult">
        <include refid="selectFirmwareTaskDetailVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertFirmwareTaskDetail" parameterType="com.fastbee.iot.domain.FirmwareTaskDetail" useGeneratedKeys="true" keyProperty="id">
        insert into iot_firmware_task_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="serialNumber != null and serialNumber != ''">serial_number,</if>
            <if test="upgradeStatus != null">upgrade_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="detailMsg != null and detailMsg != ''">detail_msg,</if>
            <if test="messageId != null and messageId != ''">message_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="serialNumber != null and serialNumber != ''">#{serialNumber},</if>
            <if test="upgradeStatus != null">#{upgradeStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="detailMsg != null and detailMsg != ''">#{detailMsg},</if>
            <if test="messageId != null and messageId != ''">#{messageId},</if>
         </trim>
    </insert>

    <!--批量插入任务明细-->
    <insert id="insertBatchFirmwareTaskDetail" parameterType="com.fastbee.iot.domain.FirmwareTaskDetail" useGeneratedKeys="true" keyProperty="id">
        insert into iot_firmware_task_detail (task_id,serial_number,upgrade_status,detail_msg, create_time, message_id)
        values
        <foreach item="item" collection="list" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.taskId},#{item.serialNumber},#{item.upgradeStatus},#{item.detailMsg},#{item.createTime},#{item.messageId}
            </trim>
        </foreach>
    </insert>

    <update id="updateFirmwareTaskDetail" parameterType="com.fastbee.iot.domain.FirmwareTaskDetail">
        update iot_firmware_task_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="serialNumber != null and serialNumber != ''">serial_number = #{serialNumber},</if>
            <if test="upgradeStatus != null">upgrade_status = #{upgradeStatus},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="detailMsg != null and detailMsg != ''">detail_msg = #{detailMsg},</if>
            <if test="messageId != null and messageId != ''">message_id = #{messageId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFirmwareTaskDetailById" parameterType="Long">
        delete from iot_firmware_task_detail where id = #{id}
    </delete>

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

    <select id="selectFirmwareALLDetailList" parameterType="com.fastbee.iot.domain.FirmwareTaskDetail" resultMap="FirmwareTaskDetailResult2">
        select ftd.*,
               f.firmware_name,
               ft.task_name,
               f.version
        from
            iot_firmware_task_detail ftd
            left join iot_firmware_task ft on ftd.task_id = ft.id
            left join iot_firmware f on ft.firmware_id = f.firmware_id
        where 1=1
        <if test="id != null">
            and ftd.id = #{id}
        </if>
        <if test="taskName != null and taskName != '' ">
            and ft.task_name like CONCAT(CONCAT('%',#{taskName},'%'))
        </if>

        <if test="firmwareName != null and firmwareName != '' ">
            and f.firmware_name like CONCAT(CONCAT('%',#{firmwareName},'%'))
        </if>
        <if test="taskId != null">
            and ftd.task_id = #{taskId,jdbcType=BIGINT}
        </if>
        <if test="serialNumber != null and serialNumber != '' ">
            and ftd.serial_number = #{serialNumber,jdbcType=VARCHAR}
        </if>
        <if test="upgradeStatus != null">
            and ftd.upgrade_status = #{upgradeStatus,jdbcType=INTEGER}
        </if>
        <if test="messageId != null and messageId != '' ">
            and ftd.message_id = #{messageId,jdbcType=VARCHAR}
        </if>
    </select>
</mapper>