<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.caliverse.admin.domain.dao.admin.MenuMapper">
    <resultMap id="MenuResultMap" type="com.caliverse.admin.domain.entity.MenuBanner">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="title" column="title"/>
        <result property="isLink" column="is_link"/>
        <result property="status" column="status"/>
        <result property="startDt" column="start_dt"/>
        <result property="endDt" column="end_dt"/>
        <result property="orderId" column="order_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createDt" column="create_dt"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateDt" column="update_dt"/>
    </resultMap>
    <resultMap id="MessageResultMap" type="com.caliverse.admin.domain.entity.Message">
        <result property="title" column="title"/>
        <result property="language" column="language"/>
        <result property="content" column="content"/>
    </resultMap>

    <!--리스트 조회-->
    <select id="getBannerList" resultMap="MenuResultMap" parameterType="map">
        SELECT * FROM (
        SELECT (@row_number:=@row_number + 1) AS row_num, c.*
        FROM (
        SELECT
        a.id,
        a.title,
        a.is_link,
        CASE
        WHEN NOW() &lt; a.start_dt THEN 'WAIT'
        WHEN NOW() &gt;= a.start_dt AND NOW() &lt;= a.end_dt THEN 'RUNNING'
        WHEN NOW() &gt; a.end_dt THEN 'FINISH'
        END AS status,
        a.start_dt,
        a.end_dt,
        a.order_id,
        (SELECT email FROM admin WHERE id = a.create_by) AS create_by,
        a.create_dt,
        (SELECT email FROM admin WHERE id = a.update_by) AS update_by,
        a.update_dt
        FROM menu_banner a
        WHERE 1 = 1
        AND a.deleted = 0

        <if test="searchData != null and searchData != ''">
            AND a.title LIKE CONCAT('%',#{searchData},'%')
        </if>

        <if test="status != null and status != ''">
            <choose>
                <when test="status != 'ALL'">
                    AND CASE
                    WHEN NOW() &lt; a.start_dt THEN 'WAIT'
                    WHEN NOW() &gt;= a.start_dt AND NOW() &lt;= a.end_dt THEN 'RUNNING'
                    WHEN NOW() &gt; a.end_dt THEN 'FINISH'
                    END = #{status}
                </when>
            </choose>
        </if>

        <if test="startDate != null and startDate != '' and endDate !=null and endDate!= ''">
            AND a.start_dt &gt;= #{startDate, jdbcType=TIMESTAMP}
            AND a.end_dt &lt;= #{endDate, jdbcType=TIMESTAMP}
        </if>

        ) c,
        (SELECT @row_number:=0) AS t
        ORDER BY
            CASE
                WHEN c.status = 'RUNNING' THEN 0
                WHEN c.status = 'WAIT' THEN 1
                WHEN c.status = 'FINISH' THEN 2
                ELSE 3
            END,
            c.order_id,
            c.create_dt
        ) result
        <if test="orderby != null and orderby != ''">
            ORDER BY row_num ${orderby}
        </if>
        <if test="pageSize != null and pageSize != ''">
            LIMIT ${pageSize} OFFSET ${offset}
        </if>
    </select>

    <select id="getTotal" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM menu_banner WHERE deleted = 0
    </select>

    <select id="getBannerDetail" parameterType="java.lang.Long" resultMap="MenuResultMap" >
        SELECT
            a.id
             , a.title
             , a.is_link
             , a.start_dt
             , a.end_dt
             , CASE
                   WHEN NOW() &lt; a.start_dt THEN 'WAIT'
                   WHEN NOW() &gt;= a.start_dt AND NOW() &lt;= a.end_dt THEN 'RUNNING'
                   WHEN NOW() &gt; a.end_dt THEN 'FINISH'
                END AS status
             , a.order_id
             , (SELECT email FROM admin WHERE id = a.create_by ) AS create_by
             , a.create_dt
             , (SELECT email FROM admin WHERE id = a.update_by ) AS update_by
             , a.update_dt
        FROM menu_banner a
        WHERE a.id = #{id}
          AND deleted = 0
    </select>

    <select id="getMessage" parameterType="java.lang.Long" resultMap="MessageResultMap" >
        SELECT *
        FROM message
        WHERE target_id = #{id}
            AND type = 'BANNER'
    </select>

    <select id="getMaxOrderId" resultType="java.lang.Integer">
        SELECT COALESCE(MAX(order_id), 0)
        FROM menu_banner
        WHERE deleted = 0
        AND NOW() &lt;= end_dt
    </select>

    <insert id="insertBanner" parameterType="com.caliverse.admin.domain.request.MenuRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO menu_banner (title, is_link, start_dt, end_dt, order_id)
                  VALUES (#{title}, #{isLink}, #{startDt}, #{endDt}, #{orderId})
    </insert>

    <insert id="insertMessage" parameterType="map">
        INSERT INTO message (target_id, type, title, content, language)
                VALUES (#{id}, 'BANNER', #{title}, #{content}, #{language})
    </insert>

    <update id="updateBanner" parameterType="com.caliverse.admin.domain.request.MenuRequest">
        UPDATE menu_banner SET title = #{title}
                        , start_dt = #{startDt}
                        , end_dt = #{endDt}
                        , order_id = #{orderId}
                        , update_by = #{updateBy}
                        , update_dt = NOW()
        WHERE id = #{id}
    </update>

    <update id="deleteMessage" parameterType="map">
        DELETE FROM message
            WHERE target_id = #{mailId}
    </update>

    <update id="deleteBanner" parameterType="map">
        UPDATE menu_banner
        SET deleted = 1
        WHERE id = #{id}
    </update>

</mapper>
