<!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.BlackListMapper">
    <resultMap id="BlackListResultMap" type="com.caliverse.admin.domain.entity.BlackList">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="guid" column="guid"/>
        <result property="nickname" column="nickname"/>
        <result property="status" column="status"/>
        <result property="period" column="period"/>
        <result property="sanctions" column="sanctions"/>
        <result property="type" column="type"/>
        <result property="createBy" column="create_by"/>
        <result property="createDt" column="create_dt"/>
        <result property="startDt" column="start_dt"/>
        <result property="endDt" column="end_dt"/>

    </resultMap>
    <!--인게임메시지 리스트 조회-->
    <select id="getBlackList" resultMap="BlackListResultMap" parameterType="map">
        SELECT * FROM (
            SELECT (@row_number:=@row_number + 1) AS row_num
                    ,a.id
                    ,a.guid
                    ,a.nickname
                    ,a.status
                    ,a.period
                    ,a.sanctions
                    , (SELECT email FROM admin WHERE id = a.create_by ) AS create_by
                  FROM black_list a
                     ,(SELECT @row_number:=0) AS t
                  WHERE 1 = 1
                    AND a.deleted = 0
            <choose>
                <when test="search_type == 'NAME' or search_type == 'name' ">
                    <if test="search_key != null and search_key != ''">
                        AND a.nickname LIKE CONCAT('%',#{search_key},'%')
                    </if>
                </when>
                <otherwise>
                    <if test="search_key != null and search_key != ''">
                        AND a.guid = #{search_key}
                    </if>
                </otherwise>
            </choose>
            <if test="sanctions != null and sanctions != ''">
                <choose>
                    <when test="sanctions == 'ALL' ">
                    </when>
                    <otherwise>
                        AND a.sanctions = #{sanctions}
                    </otherwise>
                </choose>
            </if>
            <if test="status != null and status != ''">
                <choose>
                    <when test="status == 'ALL' ">
                    </when>
                    <otherwise>
                        AND a.status = #{status}
                    </otherwise>
                </choose>
            </if>
            <if test="period != null and period != ''">
                <choose>
                    <when test="period == 'ALL' ">
                    </when>
                    <otherwise>
                        AND a.period = #{period}
                    </otherwise>
                </choose>
            </if>
            ORDER BY a.create_dt
        ) c
        WHERE 1 = 1
        <if test="email != null and email != ''">
            AND c.create_by LIKE CONCAT('%',#{email},'%')
        </if>
        <if test="orderby != null and orderby != ''">
            ORDER BY c.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 black_list WHERE deleted = 0
    </select>
    <select id="getAllCnt" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM black_list a WHERE a.deleted = 0
        <choose>
            <when test="search_type == 'NAME' or search_type == 'name' ">
                <if test="search_key != null and search_key != ''">
                    AND a.nickname LIKE CONCAT('%',#{search_key},'%')
                </if>
            </when>
            <otherwise>
                <if test="search_key != null and search_key != ''">
                    AND a.guid = #{search_key}
                </if>
            </otherwise>
        </choose>
        <if test="sanctions != null and sanctions != ''">
            <choose>
                <when test="sanctions == 'ALL' ">
                </when>
                <otherwise>
                    AND a.sanctions = #{sanctions}
                </otherwise>
            </choose>
        </if>
        <if test="status != null and status != ''">
            <choose>
                <when test="status == 'ALL' ">
                </when>
                <otherwise>
                    AND a.status = #{status}
                </otherwise>
            </choose>
        </if>
        <if test="period != null and period != ''">
            <choose>
                <when test="period == 'ALL' ">
                </when>
                <otherwise>
                    AND a.period = #{period}
                </otherwise>
            </choose>
        </if>
    </select>
    <select id="getBlackListDetail" parameterType="java.lang.Long" resultMap="BlackListResultMap">
        SELECT a.id
                ,a.guid
                ,a.nickname
                ,a.status
                ,a.period
                ,a.sanctions
                ,a.type
                ,a.create_dt
                ,a.start_dt
                ,a.end_dt
             , (SELECT email FROM admin WHERE id = a.create_by ) AS create_by
        FROM black_list a WHERE a.id = #{id}
    </select>
    <select id="getHistoryByGuid" parameterType="java.lang.String" resultMap="BlackListResultMap">
        SELECT a.guid
             ,a.nickname
             ,a.status
             ,a.period
             ,a.sanctions
             ,a.type
             ,a.create_dt
             , (SELECT email FROM admin WHERE id = a.create_by ) AS create_by
             ,a.start_dt
             ,a.end_dt
        FROM black_list a WHERE guid = #{guid}
    </select>

    <select id="getCountByGuid" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM black_list WHERE guid = #{guid} and STATUS in ('INPROGRESS','WAIT') AND deleted = 0
    </select>

    <insert id="postBlackList" parameterType="com.caliverse.admin.domain.request.BlackListRequest">
        INSERT INTO black_list (guid, nickname, status, type, sanctions, period, start_dt, end_dt, create_by, create_dt)
        VALUES (#{guid},#{nickname}, #{status}, #{type}, #{sanctions},#{period}, #{startDt}, #{endDt}, #{createBy}, NOW())
    </insert>

    <update id="deleteBlackList" parameterType="map">
        UPDATE black_list SET deleted = 1 WHERE id = #{id}
    </update>

    <select id="getScheduleBlackList" resultMap="BlackListResultMap">
        SELECT  id,
                guid,
                nickname,
                status,
                type,
                sanctions,
                period,
                start_dt,
                end_dt
        FROM black_list
        WHERE STATUS &lt;&gt; 'EXPIRATION'
        AND STATUS &lt;&gt; 'FAIL'
        AND deleted = false
    </select>

    <update id="updateStatus" parameterType="map">
        UPDATE black_list SET status = #{status} WHERE id = #{id}
    </update>

</mapper>
