<?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.zioinfo.signage.mapper.FirmwareMapper">

  <resultMap id="firmwareMap" type="com.zioinfo.signage.domain.FirmwareVo">
    <id property="id"              column="id"/>
    <result property="tenantCode"      column="tenant_code"/>
    <result property="firmwareVersion" column="firmware_version"/>
    <result property="deviceType"      column="device_type"/>
    <result property="fileName"        column="file_name"/>
    <result property="filePath"        column="file_path"/>
    <result property="fileSize"        column="file_size"/>
    <result property="checksum"        column="checksum"/>
    <result property="description"     column="description"/>
    <result property="latest"          column="is_latest"/>
    <result property="createdAt"       column="created_at"/>
  </resultMap>

  <select id="findAll" resultMap="firmwareMap">
    SELECT id, tenant_code, firmware_version, device_type, file_name, file_path,
           file_size, checksum, description, is_latest, created_at
    FROM signage_firmware
    <where>
      <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
      <if test="deviceType != null and deviceType != ''">AND device_type = #{deviceType}</if>
    </where>
    ORDER BY created_at DESC
  </select>

  <select id="findById" resultMap="firmwareMap">
    SELECT id, tenant_code, firmware_version, device_type, file_name, file_path,
           file_size, checksum, description, is_latest, created_at
    FROM signage_firmware WHERE id = #{id}
  </select>

  <select id="findLatest" resultMap="firmwareMap">
    SELECT id, tenant_code, firmware_version, device_type, file_name, file_path,
           file_size, checksum, description, is_latest, created_at
    FROM signage_firmware
    WHERE device_type = #{deviceType} AND is_latest = true
    <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
    LIMIT 1
  </select>

  <insert id="insert" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO signage_firmware (tenant_code, firmware_version, device_type, file_name, file_path,
                               file_size, checksum, description, is_latest)
    VALUES (#{tenantCode}, #{firmwareVersion}, #{deviceType}, #{fileName}, #{filePath},
            #{fileSize}, #{checksum}, #{description}, #{latest})
  </insert>

  <update id="update">
    UPDATE signage_firmware SET
      firmware_version = #{firmwareVersion},
      description      = #{description},
      is_latest        = #{latest}
    WHERE id = #{id}
  </update>

  <update id="clearLatest">
    UPDATE signage_firmware SET is_latest = false
    WHERE device_type = #{deviceType}
    <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
  </update>

  <delete id="delete">DELETE FROM signage_firmware WHERE id = #{id}</delete>

</mapper>
