<?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.templateconfig.ITemplateConfigDao">

    <resultMap id="templateConfigDTO" type="cn.com.tenlion.freemarker.pojo.dtos.templateconfig.TemplateConfigDTO">
        <id column="template_config_id" property="templateConfigId"/>
        <result column="template_code" property="templateCode"/>
        <result column="template_url" property="templateUrl"/>
        <result column="template_name" property="templateName"/>
        <result column="template_content" property="templateContent"/>
        <result column="template_root_path" property="templateRootPath"/>
        <result column="template_order" property="templateOrder"/>
        <result column="template_file" property="templateFile"/>
        <result column="template_all_generate" property="templateAllGenerate"/>
        <result column="template_file_type" property="templateFileType"/>
        <result column="template_mode" property="templateMode"/>
        <result column="template_project" property="templateProject"/>
    </resultMap>

    <insert id="saveTemplateConfig" parameterType="map">
        INSERT INTO m_template_config(
            template_config_id,
            template_code,
            template_url,
            template_name,
            template_content,
            template_root_path,
            template_order,
            template_file,
            template_all_generate,
            template_file_type,
            template_mode,
            template_project,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{templateConfigId},
            #{templateCode},
            #{templateUrl},
            #{templateName},
            #{templateContent},
            #{templateRootPath},
            #{templateOrder},
            #{templateFile},
            #{templateAllGenerate},
            #{templateFileType},
            #{templateMode},
            #{templateProject},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

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

    <update id="updateTemplateConfigContent" parameterType="map">
        UPDATE
            m_template_config
        SET
        <if test="templateContent != null and templateContent != ''">
            template_content = #{templateContent},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            template_config_id = #{templateConfigId}
    </update>

    <update id="updateTemplateConfig" parameterType="map">
        UPDATE
            m_template_config
        SET
        <if test="templateCode != null and templateCode != ''">
            template_code = #{templateCode},
        </if>
        <if test="templateUrl != null and templateUrl != ''">
            template_url = #{templateUrl},
        </if>
        <if test="templateName != null and templateName != ''">
            template_name = #{templateName},
        </if>
        <if test="templateContent != null and templateContent != ''">
            template_content = #{templateContent},
        </if>
        <if test="templateRootPath != null and templateRootPath != ''">
            template_root_path = #{templateRootPath},
        </if>
        <if test="templateOrder != null">
            template_order = #{templateOrder},
        </if>
        <if test="templateFile != null and templateFile != ''">
            template_file = #{templateFile},
        </if>
        <if test="templateAllGenerate != null and templateAllGenerate != ''">
            template_all_generate = #{templateAllGenerate},
        </if>
        <if test="templateFileType != null and templateFileType != ''">
            template_file_type = #{templateFileType},
        </if>
        <if test="templateMode != null and templateMode != ''">
            template_mode = #{templateMode},
        </if>
        <if test="templateProject != null and templateProject != ''">
            template_project = #{templateProject},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            template_config_id = #{templateConfigId}
    </update>

    <select id="getTemplateConfigByCode" parameterType="map" resultMap="templateConfigDTO">
        SELECT
            t1.template_code,
            t1.template_url,
            t1.template_name,
            t1.template_content,
            t1.template_root_path,
            t1.template_order,
            t1.template_config_id,
            t1.template_file,
            t1.template_all_generate,
            t1.template_file_type,
            t1.template_mode,
            t1.template_project
        FROM
            m_template_config t1
        WHERE
            t1.is_delete = 0 AND t1.template_code = #{templateCode}
    </select>

    <select id="getTemplateConfig" parameterType="map" resultMap="templateConfigDTO">
        SELECT
            t1.template_code,
            t1.template_url,
            t1.template_name,
            t1.template_content,
            t1.template_root_path,
            t1.template_order,
            t1.template_config_id,
            t1.template_file,
            t1.template_all_generate,
            t1.template_file_type,
            t1.template_mode,
            t1.template_project
        FROM
            m_template_config t1
        WHERE
            t1.is_delete = 0
        <if test="templateConfigId != null and templateConfigId != ''">
        AND
            t1.template_config_id = #{templateConfigId}
        </if>
    </select>

    <select id="listTemplateConfig" parameterType="map" resultMap="templateConfigDTO">
        SELECT
            t1.template_code,
            t1.template_url,
            t1.template_name,
            t1.template_content,
            t1.template_root_path,
            t1.template_order,
            t1.template_config_id,
            t1.template_file,
            t1.template_all_generate,
            t1.template_file_type,
            t1.template_mode,
            t1.template_project
        FROM
            m_template_config t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
            AND
            (
                t1.template_name LIKE CONCAT('%', #{keywords}, '%')
            )
        </if>
        <if test="templateProject != null and templateProject != ''">
            AND t1.template_project = #{templateProject}
        </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="templateConfigIds != null and templateConfigIds.size > 0">
        AND
            t1.template_config_id IN
            <foreach collection="templateConfigIds" index="index" open="(" separator="," close=")">
            #{templateConfigIds[${index}]}
            </foreach>
        </if>
        ORDER BY
            t1.template_order
    </select>

</mapper>