<!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.RankMapper">
    <resultMap id="RankScheduleResultMap" type="com.caliverse.admin.domain.entity.RankingSchedule">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="guid" column="guid"/>
        <result property="title" column="title"/>
        <result property="metaId" column="meta_id"/>
        <result property="eventActionId" column="event_action_id"/>
        <result property="status" column="status"/>
        <result property="startDt" column="start_dt"/>
        <result property="endDt" column="end_dt"/>
        <result property="baseDt" column="base_dt"/>
        <result property="refreshInterval" column="refresh_interval"/>
        <result property="initializationInterval" column="initialization_interval"/>
        <result property="snapshotInterval" column="snapshot_interval"/>
        <result property="deleted" column="deleted"/>
        <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>

    <!--리스트 조회-->
    <select id="getRankingScheduleList" resultMap="RankScheduleResultMap" parameterType="map">
        SELECT * FROM (
        SELECT (@row_number:=@row_number + 1) AS row_num, c.*
        FROM (
            SELECT
            a.id,
            a.guid,
            a.title,
            a.meta_id,
            a.event_action_id,
            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.base_dt,
            a.refresh_interval,
            a.initialization_interval,
            a.snapshot_interval,
            (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 ranking_schedule 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.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 ranking_schedule WHERE deleted = 0
    </select>

    <!--이벤트 상세 조회-->
    <select id="getRankingScheduleDetail" parameterType="java.lang.Long" resultMap="RankScheduleResultMap" >
        SELECT
            a.id
            , a.guid
            , a.title
            , a.meta_id
            , a.event_action_id
            , a.start_dt
            , a.end_dt
            , a.base_dt
            , a.refresh_interval
            , a.initialization_interval
            , a.snapshot_interval
             , 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
             , (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 ranking_schedule a
        WHERE a.id = #{id}
          AND deleted = 0
    </select>

    <select id="checkOverlap" parameterType="com.caliverse.admin.domain.request.RankRequest" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM ranking_schedule a
        WHERE a.meta_id = #{metaId}
        AND a.deleted = 0
        AND a.meta_id != #{metaId}
        AND (
            (#{startDt} BETWEEN a.start_dt AND a.end_dt)
            OR (#{endDt} BETWEEN a.start_dt AND a.end_dt)
            OR (a.start_dt BETWEEN #{startDt} AND #{endDt})
            OR (a.end_dt BETWEEN #{startDt} AND #{endDt})
        )
    </select>


    <!--저장-->
    <insert id="postRankingSchedule" parameterType="com.caliverse.admin.domain.request.RankRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ranking_schedule (guid, title, meta_id, event_action_id, start_dt, end_dt, base_dt, refresh_interval, initialization_interval, snapshot_interval, create_by, update_by)
                  VALUES (#{guid}, #{title}, #{metaId}, #{eventActionId}, #{startDt}, #{endDt}, #{baseDt}, #{refreshInterval}, #{initializationInterval}, #{snapshotInterval}, #{createBy}, #{updateBy})
    </insert>

    <!--수정-->
    <update id="updateRankingSchedule" parameterType="com.caliverse.admin.domain.request.RankRequest">
        UPDATE ranking_schedule
        SET title = #{title}
            , meta_id = #{metaId}
            , event_action_id = #{eventActionId}
            , start_dt = #{startDt}
            , end_dt = #{endDt}
            , base_dt = #{baseDt}
            , refresh_interval = #{refreshInterval}
            , initialization_interval = #{initializationInterval}
            , snapshot_interval = #{snapshotInterval}
            , update_by = #{updateBy}
            , update_dt = NOW()
        WHERE id = #{id}
    </update>

    <!--삭제-->
    <update id="deleteRankingSchedule" parameterType="map">
        UPDATE ranking_schedule
        SET deleted = 1
            , update_by = #{updateBy}
            , update_dt = NOW()
        WHERE id = #{id}
    </update>

</mapper>
