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

  <resultMap id="adScheduleMap" type="com.zioinfo.signage.domain.AdScheduleVo">
    <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="adName" column="ad_name"/>
    <result property="mediaType" column="media_type"/>
    <result property="mediaPath" column="media_path"/>
    <result property="templateCode" column="template_code"/>
    <result property="startAt" column="start_at"/>
    <result property="endAt" column="end_at"/>
    <result property="dailyStart" column="daily_start"/>
    <result property="dailyEnd" column="daily_end"/>
    <result property="priority" column="priority"/>
    <result property="status" column="status"/>
    <result property="createdAt" column="created_at"/>
  </resultMap>

  <select id="findAll" resultMap="adScheduleMap">
    SELECT id, tenant_code, store_id, store_code, ad_name, media_type, media_path, template_code, start_at, end_at, daily_start, daily_end, priority, status, created_at
    FROM signage_ad_schedule
    <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 ad_name ILIKE '%' || #{keyword} || '%'</if>
    </where>
    ORDER BY created_at DESC
  </select>

  <select id="findById" resultMap="adScheduleMap">
    SELECT id, tenant_code, store_id, store_code, ad_name, media_type, media_path, template_code, start_at, end_at, daily_start, daily_end, priority, status, created_at FROM signage_ad_schedule WHERE id = #{id}
  </select>

  <insert id="insert" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO signage_ad_schedule (tenant_code, store_id, store_code, ad_name, media_type, media_path, template_code, start_at, end_at, daily_start, daily_end, priority, status)
    VALUES (#{tenantCode}, #{storeId}, #{storeCode}, #{adName}, #{mediaType}, #{mediaPath}, #{templateCode}, #{startAt}, #{endAt}, #{dailyStart}, #{dailyEnd}, #{priority}, #{status})
  </insert>

  <update id="update">
    UPDATE signage_ad_schedule SET
      tenant_code = #{tenantCode},
      store_id = #{storeId},
      store_code = #{storeCode},
      ad_name = #{adName},
      media_type = #{mediaType},
      media_path = #{mediaPath},
      template_code = #{templateCode},
      start_at = #{startAt},
      end_at = #{endAt},
      daily_start = #{dailyStart},
      daily_end = #{dailyEnd},
      priority = #{priority},
      status = #{status}
    WHERE id = #{id}
  </update>

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

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

</mapper>
