<?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.ruoyi.generator.mapper.TemplateMapper">
    
    <resultMap type="com.ruoyi.generator.domain.Template" id="TemplateResult">
        <id     property="templateId"      column="template_id"      />
        <result property="templateName"    column="template_name"    />
        <result property="templatePath"    column="template_path"    />
        <result property="templateDesc"    column="template_desc"    />
        <result property="templateType"    column="template_type"    />
        <result property="templateStatus"  column="template_status"  />
        <collection property="templateFiles" javaType="java.util.List" resultMap="com.ruoyi.generator.mapper.TemplateFileMapper.TemplateFileResult" />
    </resultMap>

    <sql id="selectTemplateVo">
        select t.template_id, t.template_name, t.template_path, t.template_desc, t.template_type, t.template_status
        from sys_template t
    </sql>

    <select id="selectTemplateList" parameterType="com.ruoyi.generator.domain.Template" resultMap="TemplateResult">
        <include refid="selectTemplateVo"/>
        <where>  
            <if test="templateName != null  and templateName != ''">
                AND t.template_name like concat('%', #{templateName}, '%')
            </if>
            <if test="templateType != null  and templateType != ''">
                AND t.template_type = #{templateType}
            </if>
        </where>
    </select>
    
    <select id="selectTemplateByTemplateId" parameterType="Long" resultMap="TemplateResult">
        select t.template_id, t.template_name, t.template_path, t.template_desc, t.template_type, t.template_status,
               tf.template_file_id, tf.file_name, tf.file_path, tf.file_content, tf.create_by, tf.create_time, tf.update_by, tf.update_time
        from sys_template t
        left join sys_template_file tf on t.template_id = tf.template_id
        where t.template_id = #{templateId}
    </select>

    <select id="selectActiveTemplate" resultMap="TemplateResult">
        <include refid="selectTemplateVo"/>
        where t.template_status = 0
    </select>
        
    <insert id="insertTemplate" parameterType="com.ruoyi.generator.domain.Template" useGeneratedKeys="true" keyProperty="templateId">
        insert into sys_template
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="templateName != null and templateName != ''">template_name,</if>
            <if test="templatePath != null and templatePath != ''">template_path,</if>
            <if test="templateDesc != null and templateDesc != ''">template_desc,</if>
            <if test="templateType != null and templateType != ''">template_type,</if>
            <if test="templateStatus != null">template_status,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="templateName != null and templateName != ''">#{templateName},</if>
            <if test="templatePath != null and templatePath != ''">#{templatePath},</if>
            <if test="templateDesc != null and templateDesc != ''">#{templateDesc},</if>
            <if test="templateType != null and templateType != ''">#{templateType},</if>
            <if test="templateStatus != null">#{templateStatus},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
         </trim>
    </insert>

    <update id="updateTemplate" parameterType="com.ruoyi.generator.domain.Template">
        update sys_template
        <trim prefix="SET" suffixOverrides=",">
            <if test="templateName != null and templateName != ''">template_name = #{templateName},</if>
            <if test="templatePath != null and templatePath != ''">template_path = #{templatePath},</if>
            <if test="templateDesc != null and templateDesc != ''">template_desc = #{templateDesc},</if>
            <if test="templateType != null and templateType != ''">template_type = #{templateType},</if>
            <if test="templateStatus != null">template_status = #{templateStatus},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </trim>
        where template_id = #{templateId}
    </update>

    <delete id="deleteTemplateByTemplateId" parameterType="Long">
        delete from sys_template where template_id = #{templateId}
    </delete>

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

    <update id="updateTemplateStatus" parameterType="com.ruoyi.generator.domain.Template">
        update sys_template set template_status = #{templateStatus} where template_id = #{templateId}
    </update>
</mapper>