<?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.SysMailLogMapper">
    
    <resultMap type="SysMailLog" id="SysMailLogResult">
        <result property="logId"        column="log_id"        />
        <result property="configId"     column="config_id"     />
        <result property="templateId"   column="template_id"   />
        <result property="sender"       column="sender"        />
        <result property="receiver"     column="receiver"      />
        <result property="cc"           column="cc"            />
        <result property="bcc"          column="bcc"           />
        <result property="subject"      column="subject"       />
        <result property="content"      column="content"       />
        <result property="contentType"  column="content_type"  />
        <result property="attachments"  column="attachments"   />
        <result property="status"       column="status"        />
        <result property="errorMsg"     column="error_msg"     />
        <result property="sendTime"     column="send_time"     />
        <result property="createBy"     column="create_by"     />
        <result property="createTime"   column="create_time"   />
        <result property="configName"   column="config_name"   />
        <result property="templateName" column="template_name" />
    </resultMap>

    <sql id="selectSysMailLogVo">
        select l.log_id, l.config_id, l.template_id, l.sender, l.receiver, l.cc, l.bcc, 
               l.subject, l.content, l.content_type, l.attachments, l.status, l.error_msg, 
               l.send_time, l.create_by, l.create_time,
               c.config_name, t.template_name
        from sys_mail_log l
        left join sys_mail_config c on l.config_id = c.config_id
        left join sys_mail_template t on l.template_id = t.template_id
    </sql>

    <select id="selectSysMailLogList" parameterType="SysMailLog" resultMap="SysMailLogResult">
        <include refid="selectSysMailLogVo"/>
        <where>
            <if test="configId != null">
                AND l.config_id = #{configId}
            </if>
            <if test="templateId != null">
                AND l.template_id = #{templateId}
            </if>
            <if test="sender != null and sender != ''">
                AND l.sender like concat('%', #{sender}, '%')
            </if>
            <if test="receiver != null and receiver != ''">
                AND l.receiver like concat('%', #{receiver}, '%')
            </if>
            <if test="subject != null and subject != ''">
                AND l.subject like concat('%', #{subject}, '%')
            </if>
            <if test="status != null and status != ''">
                AND l.status = #{status}
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND l.send_time &gt;= #{params.beginTime}
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND l.send_time &lt;= #{params.endTime}
            </if>
        </where>
        order by l.send_time desc
    </select>

    <select id="selectSysMailLogByLogId" parameterType="Long" resultMap="SysMailLogResult">
        <include refid="selectSysMailLogVo"/>
        where l.log_id = #{logId}
    </select>

    <insert id="insertSysMailLog" parameterType="SysMailLog" useGeneratedKeys="true" keyProperty="logId">
        insert into sys_mail_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="configId != null">config_id,</if>
            <if test="templateId != null">template_id,</if>
            <if test="sender != null and sender != ''">sender,</if>
            <if test="receiver != null and receiver != ''">receiver,</if>
            <if test="cc != null">cc,</if>
            <if test="bcc != null">bcc,</if>
            <if test="subject != null and subject != ''">subject,</if>
            <if test="content != null">content,</if>
            <if test="contentType != null">content_type,</if>
            <if test="attachments != null">attachments,</if>
            <if test="status != null">status,</if>
            <if test="errorMsg != null">error_msg,</if>
            <if test="sendTime != null">send_time,</if>
            <if test="createBy != null">create_by,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="configId != null">#{configId},</if>
            <if test="templateId != null">#{templateId},</if>
            <if test="sender != null and sender != ''">#{sender},</if>
            <if test="receiver != null and receiver != ''">#{receiver},</if>
            <if test="cc != null">#{cc},</if>
            <if test="bcc != null">#{bcc},</if>
            <if test="subject != null and subject != ''">#{subject},</if>
            <if test="content != null">#{content},</if>
            <if test="contentType != null">#{contentType},</if>
            <if test="attachments != null">#{attachments},</if>
            <if test="status != null">#{status},</if>
            <if test="errorMsg != null">#{errorMsg},</if>
            <if test="sendTime != null">#{sendTime},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateSysMailLog" parameterType="SysMailLog">
        update sys_mail_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status = #{status},</if>
            <if test="errorMsg != null">error_msg = #{errorMsg},</if>
            <if test="sendTime != null">send_time = #{sendTime},</if>
        </trim>
        where log_id = #{logId}
    </update>

    <delete id="deleteSysMailLogByLogId" parameterType="Long">
        delete from sys_mail_log where log_id = #{logId}
    </delete>

    <delete id="deleteSysMailLogByLogIds" parameterType="String">
        delete from sys_mail_log where log_id in
        <foreach item="logId" collection="array" open="(" separator="," close=")">
            #{logId}
        </foreach>
    </delete>

    <delete id="cleanMailLog">
        truncate table sys_mail_log
    </delete>

</mapper>
