<!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.EventMapper">
    <resultMap id="EventResultMap" type="com.caliverse.admin.domain.entity.Event">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="eventType" column="event_type"/>
        <result property="status" column="status"/>
        <result property="startDt" column="start_dt"/>
        <result property="endDt" column="end_dt"/>
        <result property="deleteDesc" column="delete_desc"/>
        <result property="addFlag" column="add_flag"/>
        <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="getEventList" resultMap="EventResultMap" parameterType="map">
    SELECT *
    FROM(
    SELECT (@row_number:=@row_number + 1) AS row_num , c.* FROM
        (
        SELECT
            a.id
            , a.event_type
            , a.status
            , a.start_dt
            , a.end_dt
            , (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
            , b.title
            , a.add_flag
        FROM event a
            LEFT JOIN message b ON (a.id = b.target_id
                AND b.type = 'EVENT'
                AND
                    (
                        (b.language = 'KO' AND b.language IS NOT NULL) -- KO인 content
                        OR
                        (b.language &lt;&gt; 'KO' AND b.language IS NOT NULL AND NOT EXISTS (
                            SELECT 1 FROM message m2
                                WHERE m2.target_id = a.id AND m2.language IS NOT NULL
                            )
                        )
                    )
                )
        WHERE 1 = 1

        <if test="content != null and content != ''">
            <foreach item="content" index="index" collection="content.split(' ')" open="AND (" separator="OR" close=")">
                b.content LIKE CONCAT('%', #{content}, '%')
            </foreach>
        </if>

        <if test="title != null and title != ''">
            AND b.title LIKE CONCAT('%',#{title},'%')
        </if>
        <if test="event_type != null and event_type != ''">
            <choose>
                <when test="event_type != 'ALL' ">
                    AND event_type = #{event_type}
                </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 a.start_dt &gt;= #{start_dt, jdbcType=TIMESTAMP}
            AND a.end_dt &lt;= #{end_dt, jdbcType=TIMESTAMP}
        </if>
        GROUP BY a.id
        ORDER BY a.create_dt
        )c
        , (SELECT @row_number:=0) AS t
        ) Z
        ORDER BY
        CASE WHEN Z.status = 'RUNNING' THEN 0 ELSE 1 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 event
    </select>

    <select id="getAllCnt" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM event a
                        LEFT JOIN message b ON (a.id = b.target_id
                            AND b.type = 'EVENT'
                            AND
                            (
                                (b.language = 'KO' AND b.language IS NOT NULL) -- KO인 content
                                    OR
                                (b.language &lt;&gt; 'KO' AND b.language IS NOT NULL AND NOT EXISTS (
                                    SELECT 1 FROM message m2
                                    WHERE m2.target_id = a.id AND m2.language IS NOT NULL
                                    )
                                )
                            )
                        )
        WHERE 1 = 1
        <if test="content != null and content != ''">
            <foreach item="content" index="index" collection="content.split(' ')" open="AND (" separator="OR" close=")">
                b.content LIKE CONCAT('%', #{content}, '%')
            </foreach>
        </if>
        <if test="title != null and title != ''">
            AND b.title LIKE CONCAT('%',#{title},'%')
        </if>
        <if test="event_type != null and event_type != ''">
            <choose>
                <when test="event_type != 'ALL' ">
                    AND event_type = #{event_type}
                </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 a.start_dt &gt;= #{start_dt, jdbcType=TIMESTAMP}
            AND a.end_dt &lt;= #{end_dt, jdbcType=TIMESTAMP}
        </if>
    </select>

    <!--이벤트 상세 조회-->
    <select id="getEventDetail" parameterType="java.lang.Long" resultMap="EventResultMap" >
        SELECT
            a.id
             , a.event_type
             , a.status
             , a.start_dt
             , a.end_dt
             , (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
             , a.delete_desc
             , a.add_flag
        FROM event a
        WHERE a.id = #{id}
    </select>

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

    <select id="getItem" parameterType="java.lang.Long" resultType="com.caliverse.admin.domain.entity.Item" >
        SELECT
            reward_group_id AS item
            , item_cnt AS itemCnt
        FROM item
        WHERE mail_id = #{id}
          AND type = 'EVENT'
    </select>

    <!--이벤트 저장-->
    <insert id="postEvent" parameterType="com.caliverse.admin.domain.request.EventRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO event (event_type, start_dt, end_dt, create_by)
                  VALUES (#{eventType}, #{startDt}, #{endDt}, #{createBy})
    </insert>

    <insert id="insertGuid" parameterType="map">
        INSERT INTO guid (mail_id, guid)
        VALUES (#{mailId}, #{guid})
    </insert>

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

    <insert id="insertItem" parameterType="map">
        INSERT INTO item (mail_id,reward_group_id,item_cnt,type) VALUES (#{mailId} , #{goodsId}, #{itemCnt}, 'EVENT')
    </insert>

    <!--이벤트 수정-->
    <update id="updateEvent" parameterType="com.caliverse.admin.domain.request.EventRequest">
        UPDATE event SET event_type = #{eventType}
                        , start_dt = #{startDt}
                        , end_dt = #{endDt}
                        , update_by = #{updateBy}
                        , update_dt = NOW()
        WHERE id = #{id}
    </update>

    <update id="deleteMessage" parameterType="map">
        DELETE FROM message
            WHERE target_id = #{mailId}
              AND type = 'EVENT'
    </update>
    <update id="deleteItem" parameterType="map">
        DELETE FROM item
            WHERE mail_id = #{mailId}
            AND type = 'EVENT'
    </update>

    <update id="deleteEvent" parameterType="map">
        UPDATE event
        SET deleted = 1
            ,status = 'DELETE'
            ,delete_desc = #{desc}
        WHERE id = #{mailId}
    </update>

    <update id="updateStatusEvent" parameterType="map">
        UPDATE event
        SET status = #{status}
        where id = #{id}
    </update>

    <update id="updateAddFlag" parameterType="java.lang.Long">
        UPDATE event SET add_flag = true
        where id = #{id}
    </update>

    <select id="getScheduleEventList" resultMap="EventResultMap">
        SELECT id,
               event_type,
               status,
               start_dt,
               end_dt,
               add_flag
        FROM event
        WHERE (status = 'WAIT' or status = 'RUNNING')
          AND deleted = false
    </select>

</mapper>
