<?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.cm.manager.sms.dao.email.IEmailDao">

    <resultMap id="emailDTO" type="com.cm.manager.sms.pojo.dtos.email.EmailDTO">
        <id column="email_id" property="emailId"/>
        <result column="email_sender" property="emailSender"/>
        <result column="email_receiver" property="emailReceiver"/>
        <result column="email_cc" property="emailCc"/>
        <result column="email_type" property="emailType"/>
        <result column="email_subject" property="emailSubject"/>
        <result column="email_content" property="emailContent"/>
        <result column="sending_status" property="sendingStatus"/>
    </resultMap>

    <!-- 新增短信 -->
    <insert id="saveEmail" parameterType="map">
        INSERT INTO sms_email(
            email_id,
            email_sender,
            email_receiver,
            email_cc,
            email_type,
            email_subject,
            email_content,
            sending_status,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{emailId},
            #{emailSender},
            #{emailReceiver},
            #{emailCc},
            #{emailType},
            #{emailSubject},
            #{emailContent},
            #{sendingStatus},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除短信 -->
    <update id="removeEmail" parameterType="map">
        UPDATE
            sms_email
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            email_id IN
        <foreach collection="smsIds" index="index" open="(" separator="," close=")">
            #{smsIds[${index}]}
        </foreach>
    </update>

    <!-- 更新邮件发送状态 -->
    <update id="updateEmailSendingStatus" parameterType="map">
        UPDATE
            sms_email
        SET
            sending_status = #{sendingStatus}
        WHERE
            is_delete = 0
        AND
            email_id = #{emailId}
    </update>

    <!-- 短信详情 -->
    <select id="getEmail" parameterType="map" resultMap="emailDTO">
        SELECT
            t1.email_id,
            t1.email_sender,
            t1.email_receiver,
            t1.email_cc,
            t1.email_type,
            t1.email_subject,
            t1.email_content,
            t1.sending_status,
            t1.gmt_create
        FROM
            sms_email t1
        WHERE
            t1.is_delete = 0
        <if test="emailId != null and emailId != ''">
        AND
            t1.email_id = #{emailId}
        </if>
    </select>

    <!-- 短信列表 -->
    <select id="listEmail" parameterType="map" resultMap="emailDTO">
        SELECT
            t1.email_id,
            t1.email_sender,
            t1.email_receiver,
            t1.email_cc,
            t1.email_type,
            t1.email_subject,
            t1.email_content,
            t1.sending_status,
            t1.gmt_create
        FROM
            sms_email t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.email_sender LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.email_receiver LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.email_cc LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.email_subject LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="smsIds != null and smsIds.size > 0">
        AND
            t1.email_id IN
            <foreach collection="smsIds" index="index" open="(" separator="," close=")">
            #{emailIds[${index}]}
            </foreach>
        </if>
    </select>

</mapper>