<?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.yice.webadmin.app.dao.PromptTemplateMapper">
    <resultMap id="BaseResultMap" type="com.yice.webadmin.app.model.PromptTemplate">
        <id column="template_id" jdbcType="BIGINT" property="templateId"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="template_name" jdbcType="VARCHAR" property="templateName"/>
        <result column="template_label" jdbcType="VARCHAR" property="templateLabel"/>
        <result column="view_count" jdbcType="BIGINT" property="viewCount"/>
        <result column="template_content" jdbcType="VARCHAR" property="templateContent"/>
        <result column="template_source" jdbcType="TINYINT" property="templateSource"/>
        <result column="scenario_type" jdbcType="TINYINT" property="scenarioType"/>
        <result column="parameter_format" jdbcType="VARCHAR" property="parameterFormat"/>
        <result column="interface_address" jdbcType="VARCHAR" property="interfaceAddress"/>
        <result column="template_framework" jdbcType="TINYINT" property="templateFramework"/>
        <result column="template_variables" jdbcType="VARCHAR" property="templateVariables"/>
        <result column="negative_template_content" jdbcType="VARCHAR" property="negativeTemplateContent"/>
        <result column="negative_template_variables" jdbcType="VARCHAR" property="negativeTemplateVariables"/>
        <result column="is_preset" jdbcType="TINYINT" property="isPreset"/>
        <result column="is_collect" jdbcType="TINYINT" property="isCollect"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO lmp_prompt_template
        (template_id,
        create_user_id,
        create_time,
        update_user_id,
        update_time,
        template_name,
        template_label,
        view_count,
        template_content,
        template_source,
        scenario_type,
        parameter_format,
        interface_address,
        template_framework,
        template_variables,
        negative_template_content,
        negative_template_variables)
        VALUES
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.templateId},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.templateName},
            #{item.templateLabel},
            #{item.viewCount},
            #{item.templateContent},
            #{item.templateSource},
            #{item.scenarioType},
            #{item.parameterFormat},
            #{item.interfaceAddress},
            #{item.templateFramework},
            #{item.templateVariables},
            #{item.negativeTemplateContent},
            #{item.negativeTemplateVariables})
        </foreach>
    </insert>

    <!-- 如果有逻辑删除字段过滤，请写到这里 -->
    <sql id="filterRef">
        <!-- 这里必须加上全包名，否则当filterRef被其他Mapper.xml包含引用的时候，就会调用Mapper.xml中的该SQL片段 -->
        <include refid="com.yice.webadmin.app.dao.PromptTemplateMapper.inputFilterRef"/>
    </sql>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="promptTemplateFilter != null">
            <if test="promptTemplateFilter.templateName != null and promptTemplateFilter.templateName != ''">
                <bind name="safePromptTemplateTemplateName" value="'%' + promptTemplateFilter.templateName + '%'"/>
                AND lmp_prompt_template.template_name LIKE #{safePromptTemplateTemplateName}
            </if>
            <if test="promptTemplateFilter.templateContent != null and promptTemplateFilter.templateContent != ''">
                <bind name="safePromptTemplateTemplateContent"
                      value="'%' + promptTemplateFilter.templateContent + '%'"/>
                AND lmp_prompt_template.template_content LIKE #{safePromptTemplateTemplateContent}
            </if>
            <if test="promptTemplateFilter.createUserId != null and promptTemplateFilter.createUserId != ''">
                AND lmp_prompt_template.create_user_id = #{promptTemplateFilter.createUserId}
            </if>
            <if test="promptTemplateFilter.isPreset != null and promptTemplateFilter.isPreset != ''">
                AND lmp_prompt_template.is_preset = #{promptTemplateFilter.isPreset}
            </if>
            <if test="promptTemplateFilter.searchString != null and promptTemplateFilter.searchString != ''">
                <bind name="safePromptTemplateSearchString" value="'%' + promptTemplateFilter.searchString + '%'"/>
                AND CONCAT(IFNULL(lmp_prompt_template.template_name,''),
                IFNULL(lmp_prompt_template.template_content,''),
                IFNULL(lmp_prompt_template.negative_template_content,'')) LIKE #{safePromptTemplateSearchString}
            </if>
        </if>
    </sql>

    <select id="getPromptTemplateList" resultMap="BaseResultMap"
            parameterType="com.yice.webadmin.app.model.PromptTemplate">
        SELECT
        lmp_prompt_template.*,
        if (lmp_prompt_template_collect.template_id is not null,1,0) as is_collect
        FROM
        lmp_prompt_template
        LEFT JOIN lmp_prompt_template_collect ON lmp_prompt_template.template_id =
        lmp_prompt_template_collect.template_id
        <where>
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getPromptTemplateForCollectList" resultMap="BaseResultMap"
            parameterType="com.yice.webadmin.app.model.PromptTemplate">
        SELECT
        lmp_prompt_template.*,
        if (lmp_prompt_template_collect.template_id is not null,1,0) as is_collect
        FROM
        lmp_prompt_template
        LEFT JOIN lmp_prompt_template_collect ON lmp_prompt_template.template_id =
        lmp_prompt_template_collect.template_id
        <where>
            <include refid="filterRef"/>
            AND lmp_prompt_template_collect.user_id = #{promptTemplateFilter.createUserId}
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
