<?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.SysMailTemplateMapper">
    
    <resultMap type="SysMailTemplate" id="SysMailTemplateResult">
        <result property="templateId"    column="template_id"    />
        <result property="templateName"  column="template_name"  />
        <result property="templateCode"  column="template_code"  />
        <result property="subject"       column="subject"        />
        <result property="content"       column="content"        />
        <result property="contentType"   column="content_type"   />
        <result property="variables"     column="variables"      />
        <result property="status"        column="status"         />
        <result property="delFlag"       column="del_flag"       />
        <result property="createBy"      column="create_by"      />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"      column="update_by"      />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"        column="remark"         />
    </resultMap>

    <sql id="selectSysMailTemplateVo">
        select template_id, template_name, template_code, subject, content, content_type, 
               variables, status, del_flag, create_by, create_time, update_by, update_time, remark
        from sys_mail_template
    </sql>

    <select id="selectSysMailTemplateList" parameterType="SysMailTemplate" resultMap="SysMailTemplateResult">
        <include refid="selectSysMailTemplateVo"/>
        <where>
            del_flag = '0'
            <if test="templateName != null and templateName != ''">
                AND template_name like concat('%', #{templateName}, '%')
            </if>
            <if test="templateCode != null and templateCode != ''">
                AND template_code like concat('%', #{templateCode}, '%')
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectSysMailTemplateByTemplateId" parameterType="Long" resultMap="SysMailTemplateResult">
        <include refid="selectSysMailTemplateVo"/>
        where template_id = #{templateId} and del_flag = '0'
    </select>

    <select id="selectSysMailTemplateByCode" parameterType="String" resultMap="SysMailTemplateResult">
        <include refid="selectSysMailTemplateVo"/>
        where template_code = #{templateCode} and status = '0' and del_flag = '0'
    </select>

    <select id="checkTemplateCodeUnique" parameterType="String" resultMap="SysMailTemplateResult">
        <include refid="selectSysMailTemplateVo"/>
        where template_code = #{templateCode} and del_flag = '0' limit 1
    </select>

    <insert id="insertSysMailTemplate" parameterType="SysMailTemplate" useGeneratedKeys="true" keyProperty="templateId">
        insert into sys_mail_template
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="templateName != null and templateName != ''">template_name,</if>
            <if test="templateCode != null and templateCode != ''">template_code,</if>
            <if test="subject != null and subject != ''">subject,</if>
            <if test="content != null and content != ''">content,</if>
            <if test="contentType != null">content_type,</if>
            <if test="variables != null">variables,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="remark != null">remark,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="templateName != null and templateName != ''">#{templateName},</if>
            <if test="templateCode != null and templateCode != ''">#{templateCode},</if>
            <if test="subject != null and subject != ''">#{subject},</if>
            <if test="content != null and content != ''">#{content},</if>
            <if test="contentType != null">#{contentType},</if>
            <if test="variables != null">#{variables},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateSysMailTemplate" parameterType="SysMailTemplate">
        update sys_mail_template
        <trim prefix="SET" suffixOverrides=",">
            <if test="templateName != null and templateName != ''">template_name = #{templateName},</if>
            <if test="templateCode != null and templateCode != ''">template_code = #{templateCode},</if>
            <if test="subject != null and subject != ''">subject = #{subject},</if>
            <if test="content != null">content = #{content},</if>
            <if test="contentType != null">content_type = #{contentType},</if>
            <if test="variables != null">variables = #{variables},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = sysdate()
        </trim>
        where template_id = #{templateId}
    </update>

    <update id="deleteSysMailTemplateByTemplateId" parameterType="Long">
        update sys_mail_template set del_flag = '2' where template_id = #{templateId}
    </update>

    <update id="deleteSysMailTemplateByTemplateIds" parameterType="String">
        update sys_mail_template set del_flag = '2' where template_id in
        <foreach item="templateId" collection="array" open="(" separator="," close=")">
            #{templateId}
        </foreach>
    </update>

</mapper>
