<?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="cn.com.tenlion.freemarker.dao.templaterecord.ITemplateRecordDao">

    <resultMap id="templateRecordDTO" type="cn.com.tenlion.freemarker.pojo.dtos.templaterecord.TemplateRecordDTO">
        <id column="template_record_id" property="templateRecordId"/>
        <result column="template_id" property="templateId"/>
        <result column="business_content" property="businessContent"/>
        <result column="template_record_builder_time" property="templateRecordBuilderTime"/>
        <result column="template_record_path" property="templateRecordPath"/>
        <result column="template_record_url" property="templateRecordUrl"/>
        <result column="template_record_url_nginx" property="templateRecordUrlNginx"/>
        <result column="template_name" property="templateName"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <select id="getTemplateRecordByTemplateId" parameterType="map" resultMap="templateRecordDTO">
        SELECT
            t1.template_id,
            t1.business_content,
            t1.template_record_builder_time,
            t1.template_record_path,
            t1.template_record_url,
            t1.template_record_id,
            t1.template_record_url_nginx
        FROM
            m_template_record t1
        WHERE
            t1.is_delete = 0 AND t1.template_id = #{templateId}
        ORDER BY
            t1.gmt_create DESC
    </select>

    <insert id="saveTemplateRecord" parameterType="map">
        INSERT INTO m_template_record(
            template_record_id,
            template_id,
            business_content,
            template_record_builder_time,
            template_record_path,
            template_record_url,
            template_record_url_nginx,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{templateRecordId},
            #{templateId},
            #{businessContent},
            #{templateRecordBuilderTime},
            #{templateRecordPath},
            #{templateRecordUrl},
            #{templateRecordUrlNginx},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <update id="removeTemplateRecord" parameterType="map">
        UPDATE
            m_template_record
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            template_record_id IN
        <foreach collection="templateRecordIds" index="index" open="(" separator="," close=")">
            #{templateRecordIds[${index}]}
        </foreach>
    </update>

    <update id="updateTemplateRecord" parameterType="map">
        UPDATE
            m_template_record
        SET
        <if test="templateId != null and templateId != ''">
            template_id = #{templateId},
        </if>
        <if test="businessContent != null and businessContent != ''">
            business_content = #{businessContent},
        </if>
        <if test="templateRecordBuilderTime != null and templateRecordBuilderTime != ''">
            template_record_builder_time = #{templateRecordBuilderTime},
        </if>
        <if test="templateRecordPath != null and templateRecordPath != ''">
            template_record_path = #{templateRecordPath},
        </if>
        <if test="templateRecordUrl != null and templateRecordUrl != ''">
            template_record_url = #{templateRecordUrl},
        </if>
        <if test="templateRecordUrlNginx != null and templateRecordUrlNginx != ''">
            template_record_url_nginx = #{templateRecordUrlNginx},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            template_record_id = #{templateRecordId}
    </update>

    <select id="getTemplateRecord" parameterType="map" resultMap="templateRecordDTO">
        SELECT
            t1.template_id,
            t1.business_content,
            t1.template_record_builder_time,
            t1.template_record_path,
            t1.template_record_url,
            t1.template_record_id,
            t1.template_record_url_nginx,
            t1.gmt_create
        FROM
            m_template_record t1
        WHERE
            t1.is_delete = 0
        <if test="templateRecordId != null and templateRecordId != ''">
        AND
            t1.template_record_id = #{templateRecordId}
        </if>
    </select>

    <!-- 模板构建记录列表 -->
    <select id="listTemplateRecord" parameterType="map" resultMap="templateRecordDTO">
        SELECT
            t1.template_id,
            t1.business_content,
            t1.template_record_builder_time,
            t1.template_record_path,
            t1.template_record_url,
            t1.template_record_id,
            t1.template_record_url_nginx,
            t2.template_name,
            t1.gmt_create
        FROM
            m_template_record t1
        LEFT JOIN
            m_template_config t2
        ON
            t1.template_id = t2.template_config_id
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
            AND
            (
            t1.business_content LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.template_name LIKE CONCAT('%', #{keywords}, '%')
            OR
            t1.template_record_id 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="templateRecordIds != null and templateRecordIds.size > 0">
        AND
            t1.template_record_id IN
            <foreach collection="templateRecordIds" index="index" open="(" separator="," close=")">
            #{templateRecordIds[${index}]}
            </foreach>
        </if>
        ORDER BY
            t1.template_record_builder_time DESC
    </select>

</mapper>