<!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.MailMapper">
    <resultMap id="MailResultMap" type="com.caliverse.admin.domain.entity.Mail">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="target" column="target"/>
        <result property="isReserve" column="is_reserve"/>
        <result property="sendType" column="send_type"/>
        <result property="sendStatus" column="send_status"/>
        <result property="mailType" column="mail_type"/>
        <result property="userType" column="user_type"/>
        <result property="receiveType" column="receive_type"/>
        <result property="sendDt" column="send_dt"/>
        <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>
    <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="getMailList" resultMap="MailResultMap" parameterType="map">
    SELECT (@row_number:=@row_number + 1) AS row_num , c.* FROM
        (
        SELECT
            a.id
            , a.is_reserve
            , a.send_type
            , a.send_status
            , a.mail_type
            , a.receive_type
            , a.send_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
        FROM mail a
            LEFT JOIN message b ON (a.id = b.target_id
                AND b.type = 'MAIL'
                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
        AND a.deleted = 0

        <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="mail_title != null and mail_title != ''">
            AND b.title LIKE CONCAT('%',#{mail_title},'%')
        </if>
        <if test="send_type != null and send_type != ''">
            <choose>
                <when test="send_type != 'ALL' ">
                    AND send_type = #{send_type}
                </when>
            </choose>
        </if>
        <if test="send_status != null and send_status != ''">
            <choose>
                <when test="send_status != 'ALL' ">
                    AND a.send_status = #{send_status}
                </when>
            </choose>
        </if>
        <if test="mail_type != null and mail_type != ''">
            <choose>
                <when test="mail_type != 'ALL' ">
                    AND a.mail_type = #{mail_type}
                </when>
            </choose>
        </if>
        <if test="receive_type != null and receive_type != ''">
            <choose>
                <when test="receive_type != 'ALL' ">
                    AND a.receive_type = #{receive_type}
                </when>
            </choose>
        </if>
        <if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
            AND a.create_dt BETWEEN #{start_dt, jdbcType=TIMESTAMP} and #{end_dt, jdbcType=TIMESTAMP}
        </if>
        GROUP BY a.id
        ORDER BY a.create_dt
        )c
        , (SELECT @row_number:=0) AS t
        <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 mail  WHERE deleted = 0
    </select>
    <select id="getAllCnt" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM mail a
                        LEFT JOIN message b ON (a.id = b.target_id
                            AND b.type = 'MAIL'
                            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
        AND a.deleted = 0
        <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="mail_title != null and mail_title != ''">
            AND b.title LIKE CONCAT('%',#{mail_title},'%')
        </if>
        <if test="send_type != null and send_type != ''">
            <choose>
                <when test="send_type != 'ALL' ">
                    AND a.send_type = #{send_type}
                </when>
            </choose>
        </if>
        <if test="send_status != null and send_status != ''">
            <choose>
                <when test="send_status != 'ALL' ">
                    AND a.send_status = #{send_status}
                </when>
            </choose>
        </if>
        <if test="mail_type != null and mail_type != ''">
            <choose>
                <when test="mail_type != 'ALL' ">
                    AND a.mail_type = #{mail_type}
                </when>
            </choose>
        </if>
        <if test="receive_type != null and receive_type != ''">
            <choose>
                <when test="receive_type != 'ALL' ">
                    AND a.receive_type = #{receive_type}
                </when>
            </choose>
        </if>
        <if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
            AND a.create_dt BETWEEN #{start_dt, jdbcType=TIMESTAMP} and #{end_dt, jdbcType=TIMESTAMP}
        </if>
    </select>
    <select id="getMailDetail" parameterType="java.lang.Long" resultMap="MailResultMap" >
        SELECT
            a.id
             ,a.target
             , a.is_reserve
             , a.send_type
             , a.send_status
             , a.mail_type
             , a.receive_type
             , a.user_type
             , a.send_dt
             , a.deleted
             , (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 mail a
        WHERE a.id = #{id}
    </select>

    <select id="getMessage" parameterType="java.lang.Long" resultMap="MessageResultMap" >
        SELECT
            *
        FROM message
        WHERE target_id = #{id}
            AND type = 'MAIL'
    </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}
    </select>

    <insert id="postMail" parameterType="com.caliverse.admin.domain.request.MailRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO mail (target,is_reserve, send_type, mail_type ,receive_type,send_dt, create_by, user_type)
                  VALUES (#{target},#{isReserve},#{sendType}, #{mailType}, #{receiveType},#{sendDt},#{createBy}, #{userType})
    </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}, 'MAIL',#{title}, #{content}, #{language})
    </insert>

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

    <update id="updateMail" parameterType="com.caliverse.admin.domain.request.MailRequest">
        UPDATE mail SET target = #{target}
                        , is_reserve = #{isReserve}
                        , receive_type = #{receiveType}
                        , send_type =#{sendType}
                        , mail_type = #{mailType}
                        , send_dt = #{sendDt}
                        , update_by = #{updateBy}
                        , update_dt = NOW()
        WHERE id = #{id}
    </update>
    <update id="deleteMessage" parameterType="map">
        DELETE FROM message
            WHERE target_id = #{mailId}
    </update>
    <update id="deleteItem" parameterType="map">
        DELETE FROM item
            WHERE mail_id = #{mailId}
            AND type = 'MAIL'
    </update>

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

    <select id="getSchesuleMailList" resultMap="MailResultMap">
        SELECT id,
               receive_type,
               is_reserve,
               send_type,
               send_status,
               mail_type,
               send_dt,
               target,
               user_type
        FROM mail
        WHERE send_status = 'WAIT'
          AND deleted = false
    </select>

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

</mapper>
