<?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.SignImageMapper">

  <resultMap id="signImageMap" type="com.zioinfo.signage.domain.SignImageVo">
    <id     property="id" column="id"/>
    <result property="tenantCode" column="tenant_code"/>
    <result property="storeId" column="store_id"/>
    <result property="storeCode" column="store_code"/>
    <result property="imageCode" column="image_code"/>
    <result property="bizName" column="biz_name"/>
    <result property="phone" column="phone"/>
    <result property="message" column="message"/>
    <result property="templateCode" column="template_code"/>
    <result property="width" column="width"/>
    <result property="height" column="height"/>
    <result property="imagePath" column="image_path"/>
    <result property="status" column="status"/>
    <result property="createdAt" column="created_at"/>
  </resultMap>

  <select id="findAll" resultMap="signImageMap">
    SELECT id, tenant_code, store_id, store_code, image_code, biz_name, phone, message, template_code, width, height, image_path, status, created_at
    FROM signage_sign_image
    <where>
      <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
      <if test="status != null and status != ''">AND status = #{status}</if>
      <if test="keyword != null and keyword != ''">AND biz_name ILIKE '%' || #{keyword} || '%'</if>
    </where>
    ORDER BY created_at DESC
  </select>

  <select id="findById" resultMap="signImageMap">
    SELECT id, tenant_code, store_id, store_code, image_code, biz_name, phone, message, template_code, width, height, image_path, status, created_at FROM signage_sign_image WHERE id = #{id}
  </select>

  <insert id="insert" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO signage_sign_image (tenant_code, store_id, store_code, image_code, biz_name, phone, message, template_code, width, height, image_path, status)
    VALUES (#{tenantCode}, #{storeId}, #{storeCode}, #{imageCode}, #{bizName}, #{phone}, #{message}, #{templateCode}, #{width}, #{height}, #{imagePath}, #{status})
  </insert>

  <update id="update">
    UPDATE signage_sign_image SET
      tenant_code = #{tenantCode},
      store_id = #{storeId},
      store_code = #{storeCode},
      image_code = #{imageCode},
      biz_name = #{bizName},
      phone = #{phone},
      message = #{message},
      template_code = #{templateCode},
      width = #{width},
      height = #{height},
      image_path = #{imagePath},
      status = #{status}
    WHERE id = #{id}
  </update>

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

  <select id="countAll" resultType="long">
    SELECT COUNT(*) FROM signage_sign_image
    <where>
      <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
    </where>
  </select>

</mapper>
