<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yushu.system.mapper.SysMailInboxMapper">
    
    <resultMap type="SysMailInbox" id="SysMailInboxResult">
        <result property="inboxId" column="inbox_id"/>
        <result property="configId" column="config_id"/>
        <result property="messageId" column="message_id"/>
        <result property="fromAddress" column="from_address"/>
        <result property="fromName" column="from_name"/>
        <result property="toAddress" column="to_address"/>
        <result property="ccAddress" column="cc_address"/>
        <result property="subject" column="subject"/>
        <result property="content" column="content"/>
        <result property="contentType" column="content_type"/>
        <result property="attachments" column="attachments"/>
        <result property="hasAttachment" column="has_attachment"/>
        <result property="isRead" column="is_read"/>
        <result property="isStarred" column="is_starred"/>
        <result property="folder" column="folder"/>
        <result property="receiveTime" column="receive_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="configName" column="config_name"/>
    </resultMap>

    <sql id="selectSysMailInboxVo">
        select i.inbox_id, i.config_id, i.message_id, i.from_address, i.from_name, i.to_address, i.cc_address,
               i.subject, i.content, i.content_type, i.attachments, i.has_attachment, i.is_read, i.is_starred,
               i.folder, i.receive_time, i.del_flag, i.create_time, c.config_name
        from sys_mail_inbox i
        left join sys_mail_config c on i.config_id = c.config_id
    </sql>

    <select id="selectSysMailInboxByInboxId" parameterType="Long" resultMap="SysMailInboxResult">
        <include refid="selectSysMailInboxVo"/>
        where i.inbox_id = #{inboxId}
    </select>

    <select id="selectByMessageId" resultMap="SysMailInboxResult">
        <include refid="selectSysMailInboxVo"/>
        where i.config_id = #{configId} and i.message_id = #{messageId}
    </select>

    <select id="selectSysMailInboxList" parameterType="SysMailInbox" resultMap="SysMailInboxResult">
        <include refid="selectSysMailInboxVo"/>
        <where>
            i.del_flag = '0'
            <if test="configId != null">and i.config_id = #{configId}</if>
            <if test="fromAddress != null and fromAddress != ''">and i.from_address like concat('%', #{fromAddress}, '%')</if>
            <if test="subject != null and subject != ''">and i.subject like concat('%', #{subject}, '%')</if>
            <if test="isRead != null and isRead != ''">and i.is_read = #{isRead}</if>
            <if test="isStarred != null and isStarred != ''">and i.is_starred = #{isStarred}</if>
            <if test="folder != null and folder != ''">and i.folder = #{folder}</if>
            <if test="params.beginReceiveTime != null and params.beginReceiveTime != ''">
                and i.receive_time &gt;= #{params.beginReceiveTime}
            </if>
            <if test="params.endReceiveTime != null and params.endReceiveTime != ''">
                and i.receive_time &lt;= #{params.endReceiveTime}
            </if>
        </where>
        order by i.receive_time desc
    </select>

    <select id="selectUnreadCount" parameterType="Long" resultType="int">
        select count(*) from sys_mail_inbox where del_flag = '0' and is_read = '0'
        <if test="configId != null">and config_id = #{configId}</if>
    </select>

    <insert id="insertSysMailInbox" parameterType="SysMailInbox" useGeneratedKeys="true" keyProperty="inboxId">
        insert into sys_mail_inbox (
            config_id, message_id, from_address, from_name, to_address, cc_address,
            subject, content, content_type, attachments, has_attachment, is_read, is_starred,
            folder, receive_time, del_flag, create_time
        ) values (
            #{configId}, #{messageId}, #{fromAddress}, #{fromName}, #{toAddress}, #{ccAddress},
            #{subject}, #{content}, #{contentType}, #{attachments}, #{hasAttachment}, #{isRead}, #{isStarred},
            #{folder}, #{receiveTime}, '0', sysdate()
        )
    </insert>

    <update id="updateSysMailInbox" parameterType="SysMailInbox">
        update sys_mail_inbox
        <set>
            <if test="isRead != null">is_read = #{isRead},</if>
            <if test="isStarred != null">is_starred = #{isStarred},</if>
            <if test="folder != null">folder = #{folder},</if>
        </set>
        where inbox_id = #{inboxId}
    </update>

    <update id="markAsRead" parameterType="Long">
        update sys_mail_inbox set is_read = '1' where inbox_id = #{inboxId}
    </update>

    <update id="batchMarkAsRead" parameterType="Long">
        update sys_mail_inbox set is_read = '1' where inbox_id in
        <foreach collection="array" item="inboxId" open="(" separator="," close=")">
            #{inboxId}
        </foreach>
    </update>

    <update id="toggleStar" parameterType="Long">
        update sys_mail_inbox set is_starred = if(is_starred = '1', '0', '1') where inbox_id = #{inboxId}
    </update>

    <update id="deleteSysMailInboxByInboxId" parameterType="Long">
        update sys_mail_inbox set del_flag = '2' where inbox_id = #{inboxId}
    </update>

    <update id="deleteSysMailInboxByInboxIds" parameterType="Long">
        update sys_mail_inbox set del_flag = '2' where inbox_id in
        <foreach collection="array" item="inboxId" open="(" separator="," close=")">
            #{inboxId}
        </foreach>
    </update>

</mapper>
