<?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.FirmwareMapper">
    
    <resultMap type="com.fastbee.iot.domain.Firmware" id="FirmwareResult">
        <result property="firmwareId"    column="firmware_id"    />
        <result property="firmwareName"    column="firmware_name"    />
        <result property="productId"    column="product_id"    />
        <result property="productName"    column="product_name"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="tenantName"    column="tenant_name"    />
        <result property="isSys"    column="is_sys"    />
        <result property="isLatest"    column="is_latest"    />
        <result property="version"    column="version"    />
        <result property="filePath"    column="file_path"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectFirmwareVo">
        select firmware_id, firmware_name, product_id, product_name, tenant_id, tenant_name, is_sys,is_latest, version, file_path, create_time,  update_time, remark from iot_firmware
    </sql>

    <select id="selectFirmwareList" parameterType="com.fastbee.iot.domain.Firmware" resultMap="FirmwareResult">
        <include refid="selectFirmwareVo"/>
        <where>
            <if test="firmwareName != null  and firmwareName != ''"> and firmware_name like concat('%', #{firmwareName}, '%')</if>
            <if test="productName != null  and productName != ''"> and product_name like concat('%', #{productName}, '%')</if>
            <if test="tenantId != null  and tenantId != ''"> and tenant_id = #{tenantId}</if>
            <if test="productId != null"> and product_id = #{productId}</if>
            <if test="isLatest != null"> and is_latest = #{isLatest}</if>
        </where>
      order by create_time desc
    </select>
    
    <select id="selectFirmwareByFirmwareId" parameterType="Long" resultMap="FirmwareResult">
        <include refid="selectFirmwareVo"/>
        where firmware_id = #{firmwareId}
    </select>

    <select id="selectLatestFirmware" parameterType="Long" resultMap="FirmwareResult">
        select f.firmware_id, f.firmware_name, f.product_id, f.product_name, f.tenant_id, f.tenant_name, f.is_sys,f.is_latest, f.version,
               f.file_path, f.create_time, f.remark
        from iot_device d
                 left join iot_firmware f on d.product_id = f.product_id
        where d.device_id=#{deviceId} and f.is_latest=1
        order by f.create_time desc
        limit 1
    </select>

    <!--查询待升级固件版本列表-->
    <select id="selectUpGradeVersionList" parameterType="com.fastbee.iot.domain.Firmware" resultMap="FirmwareResult">
        <include refid="selectFirmwareVo"/>
        <where>
            del_flag = '0'
            <if test="tenantId != null  and tenantId != ''"> and tenant_id = #{tenantId}</if>
            <if test="productId != null"> and product_id = #{productId}</if>
            <if test="version != null"> and version &lt; #{version}</if>
        </where>
        order by create_time desc
    </select>

    <insert id="insertFirmware" parameterType="com.fastbee.iot.domain.Firmware" useGeneratedKeys="true" keyProperty="firmwareId">
        insert into iot_firmware
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="firmwareName != null and firmwareName != ''">firmware_name,</if>
            <if test="productId != null">product_id,</if>
            <if test="productName != null and productName != ''">product_name,</if>
            <if test="tenantId != null">tenant_id,</if>
            <if test="tenantName != null and tenantName != ''">tenant_name,</if>
            <if test="isSys != null">is_sys,</if>
            <if test="isLatest != null">is_latest,</if>
            <if test="version != null">version,</if>
            <if test="filePath != null and filePath != ''">file_path,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="firmwareName != null and firmwareName != ''">#{firmwareName},</if>
            <if test="productId != null">#{productId},</if>
            <if test="productName != null and productName != ''">#{productName},</if>
            <if test="tenantId != null">#{tenantId},</if>
            <if test="tenantName != null and tenantName != ''">#{tenantName},</if>
            <if test="isSys != null">#{isSys},</if>
            <if test="isLatest != null">#{isLatest},</if>
            <if test="version != null">#{version},</if>
            <if test="filePath != null and filePath != ''">#{filePath},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateFirmware" parameterType="com.fastbee.iot.domain.Firmware">
        update iot_firmware
        <trim prefix="SET" suffixOverrides=",">
            <if test="firmwareName != null and firmwareName != ''">firmware_name = #{firmwareName},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="productName != null and productName != ''">product_name = #{productName},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="tenantName != null and tenantName != ''">tenant_name = #{tenantName},</if>
            <if test="isSys != null">is_sys = #{isSys},</if>
            <if test="isLatest != null">is_latest = #{isLatest},</if>
            <if test="version != null">version = #{version},</if>
            <if test="filePath != null and filePath != ''">file_path = #{filePath},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where firmware_id = #{firmwareId}
    </update>

    <update id="updateOrtherFirmwareIsLast">
        update iot_firmware set is_latest = 0 where product_id = #{productId} and is_latest = 1 and firmware_id != #{firmwareId}
    </update>

    <delete id="deleteFirmwareByFirmwareId" parameterType="Long">
        delete from iot_firmware where firmware_id = #{firmwareId}
    </delete>

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