<!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.NoticeMapper">
    <resultMap id="InGameResultMap" type="com.caliverse.admin.domain.entity.InGame">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="messageType" column="message_type"/>
        <result property="sendDt" column="send_dt"/>
        <result property="endDt" column="end_dt"/>
        <result property="sendCnt" column="send_cnt"/>
        <result property="isRepeat" column="is_repeat"/>
        <result property="repeatType" column="repeat_type"/>
        <result property="repeatDt" column="repeat_dt"/>
        <result property="repeatCnt" column="repeat_cnt"/>
        <result property="deleted" column="deleted"/>
        <result property="createBy" column="create_by"/>
        <result property="createName" column="create_name"/>
        <result property="createDt" column="create_dt"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateName" column="update_name"/>
        <result property="updateDt" column="update_dt"/>
        <result property="content" column="content"/>
        <result property="sendStatus" column="send_status"/>

    </resultMap>

    <!--인게임메시지 리스트 조회-->
    <select id="getNoticeList" resultMap="InGameResultMap">
        SELECT (@row_number:=@row_number + 1) AS row_num, d.*
        FROM (SELECT a.id
                   , a.message_type
                   , a.send_dt
                   , a.end_dt
                   , a.send_cnt
                   , a.is_repeat
                   , a.repeat_type
                   , a.repeat_dt
                   , a.repeat_cnt
                   , a.create_dt
                   , a.update_dt
                   , a.send_status
                   ,(SELECT email FROM admin WHERE a.create_by = id) AS create_by
                   ,(SELECT email FROM admin WHERE a.update_by = id) AS update_by
                   ,(SELECT name FROM admin WHERE a.create_by = id) AS create_name
                   ,(SELECT name FROM admin WHERE a.update_by = id) AS update_name
                   , IFNULL(b.content, '') AS content
              FROM notice a
                       LEFT JOIN message b ON (a.id = b.target_id
                        AND b.type = 'NOTICE'
                        AND (
                               (language = 'KO' AND language IS NOT NULL) -- KO인 content
                               OR
                               (language &lt;&gt; 'KO' AND language IS NOT NULL AND NOT EXISTS (
                                   SELECT 1 FROM message m2
                                   WHERE m2.target_id = a.id AND m2.language = 'KO' AND m2.language IS NOT NULL
                                   )
                                )
                            )
                        )
                        LEFT JOIN admin c ON(a.create_by = c.email)
              WHERE 1 = 1
                AND a.deleted = 0
              GROUP BY a.id
              ORDER BY a.create_dt) d
           , (SELECT @row_number:=0) AS t
        ORDER BY row_num desc
    </select>

    <select id="getNoticeDetail" parameterType="java.lang.Long" resultMap="InGameResultMap" >
        SELECT
            a.id
             , a.message_type
             , a.send_dt
             , a.end_dt
             , a.send_cnt
             , a.is_repeat
             , a.repeat_type
             , a.repeat_dt
             , a.repeat_cnt
             , a.create_dt
             , a.update_dt
             , a.deleted
             ,(SELECT email FROM admin WHERE a.create_by = id) AS create_by
             ,(SELECT email FROM admin WHERE a.update_by = id) AS update_by
             ,(SELECT name FROM admin WHERE a.create_by = id) AS create_name
             ,(SELECT name FROM admin WHERE a.update_by = id) AS update_name
        FROM notice a
        WHERE a.id = #{id}
    </select>

    <select id="getMessage" parameterType="java.lang.Long" resultType="com.caliverse.admin.domain.entity.Message" >
        SELECT
            *
        FROM message
        WHERE target_id = #{id}
            AND type = 'NOTICE'
    </select>

    <insert id="postNotice" parameterType="com.caliverse.admin.domain.request.NoticeRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO notice (message_type, send_dt, is_repeat, repeat_dt, repeat_cnt, create_by, end_dt, repeat_type)
                  VALUES (#{messageType}, #{sendDt}, #{isRepeat}, #{repeatDt}, #{repeatCnt},#{createBy}, #{endDt}, #{repeatType})
    </insert>

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

    <update id="updateNotice" parameterType="com.caliverse.admin.domain.request.NoticeRequest">
        UPDATE notice SET  message_type = #{messageType}
                        ,  send_dt = #{sendDt}
                        , end_dt = #{endDt}
                        , is_repeat = #{isRepeat}
                        , repeat_type = #{repeatType}
                        , repeat_dt = #{repeatDt}
                        , repeat_cnt = #{repeatCnt}
                        , update_by = #{updateBy}
                        , update_dt = NOW()
        WHERE id = #{id}
    </update>
    <update id="deleteMessage" parameterType="java.lang.Long">
        DELETE FROM message
            WHERE target_id = #{id}
                AND type = 'NOTICE'
    </update>

    <update id="deleteNotice" parameterType="java.lang.Long">
        UPDATE notice SET deleted = 1
            WHERE id = #{id}
    </update>

    <select id="getMessageById" parameterType="java.lang.Long" resultType="java.lang.String">
        SELECT content
            FROM message
        WHERE target_id = #{id}
            AND TYPE = 'NOTICE'
            AND (
                    (language = 'KO' AND language IS NOT NULL) -- KO인 content
                        OR
                    (language &lt;&gt; 'KO' AND language IS NOT NULL AND NOT EXISTS (
                        SELECT 1 FROM message m2
                        WHERE m2.target_id = #{id} AND m2.language = 'KO' AND m2.language IS NOT NULL
                    ))
                )

    </select>

    <select id="getSchesuleNoticeList" resultMap="InGameResultMap">
        select id,
               message_type,
               send_dt,
               is_repeat,
               repeat_dt,
               repeat_cnt,
               send_cnt,
               deleted,
               end_dt,
               repeat_type,
               send_status
        from notice
        where send_status in ('WAIT', 'RUNNING')
        and deleted = false
    </select>

    <update id="updateStatusNotice" parameterType="map">
        UPDATE notice SET send_status = #{status}
        where id = #{id}
    </update>

    <update id="updateCountNotice" parameterType="java.lang.Long">
        UPDATE notice SET send_cnt = send_cnt + 1
        where id = #{id}
    </update>

</mapper>
