<!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.BattleMapper">
    <resultMap id="BattleEventResultMap" type="com.caliverse.admin.domain.entity.BattleEvent">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="groupId" column="group_id"/>
        <result property="eventId" column="event_id"/>
        <result property="eventName" column="event_name"/>
        <result property="repeatType" column="repeat_type"/>
        <result property="eventOperationTime" column="event_operation_time"/>
        <result property="status" column="status"/>
        <result property="eventStartDt" column="event_start_dt"/>
        <result property="eventEndDt" column="event_end_dt"/>
        <result property="roundTime" column="round_time"/>
        <result property="roundCount" column="round_count"/>
        <result property="hotTime" column="hot_time"/>
        <result property="configId" column="config_id"/>
        <result property="instanceId" column="instance_id"/>
        <result property="rewardGroupId" column="reward_group_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>

    <!--리스트 조회-->
    <select id="getBattleEventList" resultMap="BattleEventResultMap" parameterType="map">
    SELECT *
    FROM(
    SELECT (@row_number:=@row_number + 1) AS row_num , c.* FROM
        (
        SELECT
            a.id
            , a.group_id
            , a.event_id
            , a.event_name
            , a.repeat_type
            , a.event_operation_time
            , a.status
            , a.event_start_dt
            , a.event_end_dt
            , a.round_time
            , a.round_count
            , a.hot_time
            , a.config_id
            , a.reward_group_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 battle_event a
        WHERE 1 = 1

        <choose>
            <when test="search_type == 'NAME' or search_type == 'name' ">
                <if test="search_data != null and search_data != ''">
                    AND a.event_name LIKE CONCAT('%',#{search_data},'%')
                </if>
            </when>
            <otherwise>
                <if test="search_data != null and search_data != ''">
                    AND a.event_id = #{search_data}
                </if>
            </otherwise>
        </choose>
        <if test="config_id != null and config_id != ''">
            <choose>
                <when test="config_id != 'ALL' ">
                    AND a.config_id = #{config_id}
                </when>
            </choose>
        </if>
        <if test="reward_id != null and reward_id != ''">
            <choose>
                <when test="reward_id != 'ALL' ">
                    AND a.reward_group_id = #{reward_id}
                </when>
            </choose>
        </if>
        <if test="repeat_type != null and repeat_type != ''">
            <choose>
                <when test="repeat_type != 'ALL' ">
                    AND a.repeat_type = #{repeat_type}
                </when>
            </choose>
        </if>
        <if test="status != null and status != ''">
            <choose>
                <when test="status != 'ALL' ">
                    AND a.status = #{status}
                </when>
            </choose>
        </if>
        <if test="hot_time != null and hot_time != ''">
            <choose>
                <when test="hot_time != 'ALL' ">
                    AND a.hot_time = #{hot_time}
                </when>
            </choose>
        </if>
        <if test="round_count != null and round_count != ''">
            <choose>
                <when test="round_count != 'ALL' ">
                    AND a.round_count = #{round_count}
                </when>
            </choose>
        </if>
        <if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
            AND DATE(a.event_start_dt) &gt;= DATE(#{start_dt, jdbcType=TIMESTAMP})
            AND DATE(a.event_end_dt) &lt;= DATE(#{end_dt, jdbcType=TIMESTAMP})
        </if>
        GROUP BY a.id
        ORDER BY status
        )c
        , (SELECT @row_number:=0) AS t
        ) Z
        ORDER BY
        CASE WHEN  Z.status = 'STOP' THEN 2 WHEN Z.status = 'END' THEN 3 WHEN Z.status = 'FAIL' OR Z.status = 'CANCEL' THEN 4 WHEN Z.status = 'RUNNING' THEN 0 ELSE 1 END,
        CASE WHEN DATE(Z.event_start_dt) &lt;= DATE(NOW()) THEN 0 ELSE 1 END,
        CASE WHEN DATE(Z.event_start_dt) &lt;= DATE(NOW()) THEN TIME(Z.event_start_dt) ELSE Z.event_start_dt END,
        <if test="orderby != null and orderby != ''">
            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 battle_event
    </select>

    <select id="getAllCnt" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM battle_event a
        WHERE 1 = 1
        <choose>
            <when test="search_type == 'NAME' or search_type == 'name' ">
                <if test="search_data != null and search_data != ''">
                    AND a.event_name LIKE CONCAT('%',#{search_data},'%')
                </if>
            </when>
            <otherwise>
                <if test="search_data != null and search_data != ''">
                    AND a.event_id = #{search_data}
                </if>
            </otherwise>
        </choose>
        <if test="config_id != null and config_id != ''">
            <choose>
                <when test="config_id != 'ALL' ">
                    AND a.config_id = #{config_id}
                </when>
            </choose>
        </if>
        <if test="reward_id != null and reward_id != ''">
            <choose>
                <when test="reward_id != 'ALL' ">
                    AND a.reward_group_id = #{reward_id}
                </when>
            </choose>
        </if>
        <if test="repeat_type != null and repeat_type != ''">
            <choose>
                <when test="repeat_type != 'ALL' ">
                    AND a.repeat_type = #{repeat_type}
                </when>
            </choose>
        </if>
        <if test="hot_time != null and hot_time != ''">
            <choose>
                <when test="hot_time != 'ALL' ">
                    AND a.hot_time = #{hot_time}
                </when>
            </choose>
        </if>
        <if test="round_count != null and round_count != ''">
            <choose>
                <when test="round_count != 'ALL' ">
                    AND a.round_count = #{round_count}
                </when>
            </choose>
        </if>
        <if test="status != null and status != ''">
            <choose>
                <when test="status != 'ALL' ">
                    AND a.status = #{status}
                </when>
            </choose>
        </if>
        <if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
            AND DATE(a.event_start_dt) &gt;= DATE(#{start_dt, jdbcType=TIMESTAMP})
            AND DATE(a.event_end_dt) &lt;= DATE(#{end_dt, jdbcType=TIMESTAMP})
        </if>
    </select>

    <select id="getCheckBattleEventList" parameterType="com.caliverse.admin.domain.request.BattleEventRequest" resultMap="BattleEventResultMap">
        SELECT id
             , event_id
             , repeat_type
             , event_operation_time
             , event_start_dt
             , event_end_dt
        FROM battle_event
        WHERE status NOT IN ('END', 'FAIL', 'CANCEL', 'STOP')
        <if test="id != null and id != ''">
            AND id &lt;&gt; #{id}
        </if>
    </select>

    <select id="getCheckBattleEventList_bak" parameterType="com.caliverse.admin.domain.request.BattleEventRequest" resultMap="BattleEventResultMap">
        SELECT id
        , event_id
        , repeat_type
        , event_operation_time
        , event_start_dt
        , event_end_dt
        FROM battle_event
        WHERE status NOT IN ('END', 'FAIL', 'CANCEL', 'STOP')
        <if test="id != null and id != ''">
            AND id &lt;&gt; #{id}
        </if>
                AND (
                    /* NONE 타입인 경우 */
                    (repeat_type = 'NONE' AND DATE (event_start_dt) BETWEEN
                    DATE (#{eventStartDt})
                  AND DATE (#{eventEndDt}))
                   OR
                    /* DAY 타입인 경우 - 기간이 조금이라도 겹치면 가져옴 */
                    (repeat_type = 'DAY'
                  AND DATE (event_start_dt) &lt;= DATE (#{eventEndDt})
                  AND DATE (event_end_dt) &gt;= DATE (#{eventStartDt}))
                   OR
                    /* 요일 타입인 경우 - 기간이 겹치고 같은 요일이 있는 경우만 */
                    (repeat_type = #{repeatType}
                  AND DATE (event_start_dt) &lt;= DATE (#{eventEndDt})
                  AND DATE (event_end_dt) &gt;= DATE (#{eventStartDt})
                    )
                )
    </select>

    <select id="chkTimeOver" parameterType="com.caliverse.admin.domain.request.BattleEventRequest" resultType="integer">
        SELECT COUNT(*)
        FROM battle_event
        WHERE status = 'REGISTER'
        AND (
        /* 실행 날짜 체크 */
        CASE
        /* 기존 데이터가 NONE 타입일 때 */
        WHEN type = 'NONE' THEN
        DATE(event_start_dt) = DATE(#{eventStartDt})

        /* 기존 데이터가 DAY 타입일 때 */
        WHEN type = 'DAY' THEN
        DATE(event_start_dt) &lt;= DATE(#{eventStartDt}) AND
        DATE(event_end_dt) &gt;= DATE(#{eventStartDt})

        /* 기존 데이터가 요일 타입일 때 */
        WHEN type IN ('SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY') THEN
        DATE(event_start_dt) &lt;= DATE(#{eventStartDt}) AND
        DATE(event_end_dt) &gt;= DATE(#{eventStartDt}) AND
        DAYOFWEEK(event_start_dt) = DAYOFWEEK(#{eventStartDt})
        END
        )
        /* 시간 겹침 체크 */
        AND (
        TIME(event_start_dt) &lt;= TIME(#{eventStartDt})
        AND TIME(DATE_ADD(event_start_dt, INTERVAL event_operation_time SECOND)) &gt; TIME(#{eventStartDt})
        OR
        TIME(event_start_dt) &lt; TIME(DATE_ADD(#{eventStartDt}, INTERVAL #{event_operation_time} SECOND))
        AND TIME(DATE_ADD(event_start_dt, INTERVAL event_operation_time SECOND)) &gt;= TIME(DATE_ADD(#{eventStartDt}, INTERVAL #{eventOperationTime} SECOND))
        OR
        TIME(event_start_dt) &gt;= TIME(#{eventStartDt})
        AND TIME(DATE_ADD(event_start_dt, INTERVAL event_operation_time SECOND)) &lt;= TIME(DATE_ADD(#{eventStartDt}, INTERVAL #{eventOperationTime} SECOND))
        )
    </select>

    <!--이벤트 상세 조회-->
    <select id="getBattleEventDetail" parameterType="java.lang.Long" resultMap="BattleEventResultMap" >
        SELECT
            a.id
             , a.group_id
             , a.event_id
             , a.event_name
             , a.repeat_type
             , a.event_operation_time
             , a.status
             , a.event_start_dt
             , a.event_end_dt
             , a.round_time
             , a.round_count
             , a.hot_time
             , a.config_id
             , a.reward_group_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 battle_event a
        WHERE a.id = #{id}
    </select>

    <!--저장-->
    <insert id="postBattleEvent" parameterType="com.caliverse.admin.domain.request.BattleEventRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO battle_event (group_id, event_id, event_name, repeat_type, event_operation_time, event_start_dt, event_end_dt, config_id, reward_group_id, round_time, round_count, hot_time, instance_id, create_by, update_by)
                  VALUES (#{groupId}, #{eventId}, #{eventName}, #{repeatType}, #{eventOperationTime}, #{eventStartDt}, #{eventEndDt}, #{configId}, #{rewardGroupId}, #{roundTime}, #{roundCount}, #{hotTime}, #{instanceId}, #{createBy}, #{updateBy})
    </insert>

    <!--수정-->
    <update id="updateBattleEvent" parameterType="com.caliverse.admin.domain.request.BattleEventRequest">
        UPDATE battle_event SET group_id = #{groupId}
                        , event_name = #{eventName}
                        , event_operation_time = #{eventOperationTime}
                        , config_id = #{configId}
                        , event_start_dt = #{eventStartDt}
                        , event_end_dt = #{eventEndDt}
                        , reward_group_id = #{rewardGroupId}
                        , round_time = #{roundTime}
                        , round_count = #{roundCount}
                        , hot_time = #{hotTime}
                        , status = 'WAIT'
                        , update_by = #{updateBy}
                        , update_dt = NOW()
        WHERE id = #{id}
    </update>

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

    <update id="updateStatusBattleEvent" parameterType="map">
        UPDATE battle_event
        SET status = #{status}
        <if test="updateBy != null and updateBy != ''">
            , update_by = #{updateBy}
            , update_dt = NOW()
        </if>
        where id = #{id}
    </update>

    <select id="getScheduleBattleEventList" resultMap="BattleEventResultMap">
        SELECT id
             , group_id
             , event_id
             , event_name
             , repeat_type
             , event_operation_time
             , status
             , event_start_dt
             , event_end_dt
             , round_time
             , round_count
             , hot_time
             , config_id
             , reward_group_id
        FROM battle_event
        WHERE (status = 'WAIT' or status = 'RUNNING' or status = 'REGISTER')
          AND deleted = 0
    </select>

</mapper>
