<!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.WorldEventMapper">
    <resultMap id="WorldEventResultMap" type="com.caliverse.admin.domain.entity.WorldEvent">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="title" column="title"/>
        <result property="globalEventActionId" column="global_event_action_id"/>
        <result property="personalEventActionId" column="personal_event_action_id"/>
        <result property="status" column="status"/>
        <result property="startDt" column="start_dt"/>
        <result property="endDt" column="end_dt"/>
        <result property="maxPoint" column="max_point"/>
        <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="getWorldEventList" resultMap="WorldEventResultMap" parameterType="map">
        SELECT * FROM (
        SELECT (@row_number:=@row_number + 1) AS row_num, c.*
        FROM (
            SELECT
            a.id,
            a.title,
            a.global_event_action_id,
            a.personal_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,
            max_point,
            (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 world_event 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 world_event WHERE deleted = 0
    </select>

    <!--이벤트 상세 조회-->
    <select id="getWorldEventDetail" parameterType="java.lang.Long" resultMap="WorldEventResultMap" >
        SELECT
            a.id
             , a.title
             , a.global_event_action_id
             , a.personal_event_action_id
             , a.start_dt
             , a.end_dt
             , a.max_point
             , 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 world_event a
        WHERE a.id = #{id}
          AND deleted = 0
    </select>

    <select id="checkOverlap" parameterType="com.caliverse.admin.domain.request.WorldEventRequest" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM world_event a
        WHERE a.global_event_action_id = #{globalEventActionId}
        AND a.deleted = 0
        AND a.global_event_action_id != #{globalEventActionId}
        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="postWorldEvent" parameterType="com.caliverse.admin.domain.request.WorldEventRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO world_event (title, global_event_action_id, personal_event_action_id, start_dt, end_dt, max_point, create_by, update_by)
                  VALUES (#{title}, #{globalEventActionId}, #{personalEventActionId}, #{startDt}, #{endDt}, #{maxPoint}, #{createBy}, #{updateBy})
    </insert>

    <!--수정-->
    <update id="updateWorldEvent" parameterType="com.caliverse.admin.domain.request.WorldEventRequest">
        UPDATE world_event SET title = #{title}
                        , global_event_action_id = #{globalEventActionId}
                        , personal_event_action_id = #{personalEventActionId}
                        , start_dt = #{startDt}
                        , end_dt = #{endDt}
                        , max_point = #{maxPoint}
                        , update_by = #{updateBy}
                        , update_dt = NOW()
        WHERE id = #{id}
    </update>

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

</mapper>
