<?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.aegis.notification.mapper.NotificationTemplateMapper">

    <resultMap type="NotificationTemplate" id="NotificationTemplateResult">
        <id     property="id"                column="id"                 />
        <result property="templateCode"     column="template_code"      />
        <result property="templateName"     column="template_name"      />
        <result property="businessType"     column="business_type"      />
        <result property="channel"          column="channel"            />
        <result property="titleTemplate"    column="title_template"     />
        <result property="contentTemplate"  column="content_template"   />
        <result property="variables"        column="variables"          />
        <result property="status"            column="status"             />
        <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="selectNotificationTemplateVo">
        select id, template_code, template_name, business_type, channel, title_template,
               cast(content_template as char) as content_template, variables, status,
               create_by, create_time, update_by, update_time, remark
        from sys_notification_template
    </sql>

    <select id="selectNotificationTemplateList" parameterType="NotificationTemplate" resultMap="NotificationTemplateResult">
        <include refid="selectNotificationTemplateVo"/>
        <where>
            <if test="templateCode != null and templateCode != ''">
                AND template_code = #{templateCode}
            </if>
            <if test="templateName != null and templateName != ''">
                AND template_name like concat('%', #{templateName}, '%')
            </if>
            <if test="businessType != null and businessType != ''">
                AND business_type = #{businessType}
            </if>
            <if test="channel != null and channel != ''">
                AND channel = #{channel}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectNotificationTemplateById" parameterType="Long" resultMap="NotificationTemplateResult">
        <include refid="selectNotificationTemplateVo"/>
        where id = #{id}
    </select>

    <select id="selectNotificationTemplateByCode" parameterType="String" resultMap="NotificationTemplateResult">
        <include refid="selectNotificationTemplateVo"/>
        where template_code = #{templateCode}
    </select>

    <insert id="insertNotificationTemplate" parameterType="NotificationTemplate" useGeneratedKeys="true" keyProperty="id">
        insert into sys_notification_template(
            <if test="templateCode != null and templateCode != ''">template_code,</if>
            <if test="templateName != null and templateName != ''">template_name,</if>
            <if test="businessType != null and businessType != ''">business_type,</if>
            <if test="channel != null and channel != ''">channel,</if>
            <if test="titleTemplate != null and titleTemplate != ''">title_template,</if>
            <if test="contentTemplate != null">content_template,</if>
            <if test="variables != null">variables,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )values(
            <if test="templateCode != null and templateCode != ''">#{templateCode},</if>
            <if test="templateName != null and templateName != ''">#{templateName},</if>
            <if test="businessType != null and businessType != ''">#{businessType},</if>
            <if test="channel != null and channel != ''">#{channel},</if>
            <if test="titleTemplate != null and titleTemplate != ''">#{titleTemplate},</if>
            <if test="contentTemplate != null">#{contentTemplate},</if>
            <if test="variables != null">#{variables},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        )
    </insert>

    <update id="updateNotificationTemplate" parameterType="NotificationTemplate">
        update sys_notification_template
        <set>
            <if test="templateCode != null and templateCode != ''">template_code = #{templateCode},</if>
            <if test="templateName != null and templateName != ''">template_name = #{templateName},</if>
            <if test="businessType != null and businessType != ''">business_type = #{businessType},</if>
            <if test="channel != null and channel != ''">channel = #{channel},</if>
            <if test="titleTemplate != null and titleTemplate != ''">title_template = #{titleTemplate},</if>
            <if test="contentTemplate != null">content_template = #{contentTemplate},</if>
            <if test="variables != null">variables = #{variables},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

    <delete id="deleteNotificationTemplateById" parameterType="Long">
        delete from sys_notification_template where id = #{id}
    </delete>

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

    <select id="checkTemplateCodeUnique" resultMap="NotificationTemplateResult">
        <include refid="selectNotificationTemplateVo"/>
        where template_code = #{templateCode}
        <if test="id != null">
            and id != #{id}
        </if>
        limit 1
    </select>

</mapper>