<?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="org.xyzh.ai.mapper.TbKnowledgeMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.ai.dto.TbKnowledge">
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="knowledge_id" property="knowledgeId" jdbcType="VARCHAR"/>
        <result column="title" property="title" jdbcType="VARCHAR"/>
        <result column="avatar" property="avatar" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="dify_dataset_id" property="difyDatasetId" jdbcType="VARCHAR"/>
        <result column="dify_indexing_technique" property="difyIndexingTechnique" jdbcType="VARCHAR"/>
        <result column="embedding_model" property="embeddingModel" jdbcType="VARCHAR"/>
        <result column="embedding_model_provider" property="embeddingModelProvider" jdbcType="VARCHAR"/>
        <result column="rerank_model" property="rerankModel" jdbcType="VARCHAR"/>
        <result column="rerank_model_provider" property="rerankModelProvider" jdbcType="VARCHAR"/>
        <result column="reranking_enable" property="rerankingEnable" jdbcType="BOOLEAN"/>
        <result column="retrieval_top_k" property="retrievalTopK" jdbcType="INTEGER"/>
        <result column="retrieval_score_threshold" property="retrievalScoreThreshold" jdbcType="DECIMAL"/>
        <result column="document_count" property="documentCount" jdbcType="INTEGER"/>
        <result column="total_chunks" property="totalChunks" jdbcType="INTEGER"/>
        <result column="service" property="service" jdbcType="VARCHAR"/>
        <result column="project_id" property="projectId" jdbcType="VARCHAR"/>
        <result column="category" property="category" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="dept_path" property="deptPath" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <sql id="Base_Column_List">
        optsn, knowledge_id, title, avatar, description, dify_dataset_id,
        dify_indexing_technique, embedding_model, embedding_model_provider,
        rerank_model, rerank_model_provider, reranking_enable,
        retrieval_top_k, retrieval_score_threshold, document_count, total_chunks,
        service, project_id, category, creator, dept_path, updater,
        create_time, update_time, delete_time, deleted
    </sql>

    <insert id="insertKnowledge" parameterType="org.xyzh.api.ai.dto.TbKnowledge">
        INSERT INTO ai.tb_knowledge (
            optsn, knowledge_id, title, creator
            <if test="avatar != null">, avatar</if>
            <if test="description != null">, description</if>
            <if test="difyDatasetId != null">, dify_dataset_id</if>
            <if test="difyIndexingTechnique != null">, dify_indexing_technique</if>
            <if test="embeddingModel != null">, embedding_model</if>
            <if test="embeddingModelProvider != null">, embedding_model_provider</if>
            <if test="rerankModel != null">, rerank_model</if>
            <if test="rerankModelProvider != null">, rerank_model_provider</if>
            <if test="rerankingEnable != null">, reranking_enable</if>
            <if test="retrievalTopK != null">, retrieval_top_k</if>
            <if test="retrievalScoreThreshold != null">, retrieval_score_threshold</if>
            <if test="service != null">, service</if>
            <if test="projectId != null">, project_id</if>
            <if test="category != null">, category</if>
            <if test="deptPath != null">, dept_path</if>
        ) VALUES (
            #{optsn}, #{knowledgeId}, #{title}, #{creator}
            <if test="avatar != null">, #{avatar}</if>
            <if test="description != null">, #{description}</if>
            <if test="difyDatasetId != null">, #{difyDatasetId}</if>
            <if test="difyIndexingTechnique != null">, #{difyIndexingTechnique}</if>
            <if test="embeddingModel != null">, #{embeddingModel}</if>
            <if test="embeddingModelProvider != null">, #{embeddingModelProvider}</if>
            <if test="rerankModel != null">, #{rerankModel}</if>
            <if test="rerankModelProvider != null">, #{rerankModelProvider}</if>
            <if test="rerankingEnable != null">, #{rerankingEnable}</if>
            <if test="retrievalTopK != null">, #{retrievalTopK}</if>
            <if test="retrievalScoreThreshold != null">, #{retrievalScoreThreshold}</if>
            <if test="service != null">, #{service}</if>
            <if test="projectId != null">, #{projectId}</if>
            <if test="category != null">, #{category}</if>
            <if test="deptPath != null">, #{deptPath}</if>
        )
    </insert>

    <update id="updateKnowledge" parameterType="org.xyzh.api.ai.dto.TbKnowledge">
        UPDATE ai.tb_knowledge
        <set>
            <if test="title != null">title = #{title},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="description != null">description = #{description},</if>
            <if test="difyDatasetId != null">dify_dataset_id = #{difyDatasetId},</if>
            <if test="difyIndexingTechnique != null">dify_indexing_technique = #{difyIndexingTechnique},</if>
            <if test="embeddingModel != null">embedding_model = #{embeddingModel},</if>
            <if test="embeddingModelProvider != null">embedding_model_provider = #{embeddingModelProvider},</if>
            <if test="rerankModel != null">rerank_model = #{rerankModel},</if>
            <if test="rerankModelProvider != null">rerank_model_provider = #{rerankModelProvider},</if>
            <if test="rerankingEnable != null">reranking_enable = #{rerankingEnable},</if>
            <if test="retrievalTopK != null">retrieval_top_k = #{retrievalTopK},</if>
            <if test="retrievalScoreThreshold != null">retrieval_score_threshold = #{retrievalScoreThreshold},</if>
            <if test="documentCount != null">document_count = #{documentCount},</if>
            <if test="totalChunks != null">total_chunks = #{totalChunks},</if>
            <if test="service != null">service = #{service},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="category != null">category = #{category},</if>
            <if test="updater != null">updater = #{updater},</if>
            update_time = now()
        </set>
        WHERE knowledge_id = #{knowledgeId} AND deleted = false
    </update>

    <update id="deleteKnowledge" parameterType="org.xyzh.api.ai.dto.TbKnowledge">
        UPDATE ai.tb_knowledge
        SET deleted = true,
            delete_time = now(),
            updater = #{updater}
        WHERE knowledge_id = #{knowledgeId} AND deleted = false
    </update>

    <update id="updateKnowledgeFileCount">
        UPDATE ai.tb_knowledge
        SET document_count = document_count + #{num} 
        WHERE  knowledge_id = #{knowledgeId} AND deleted = false
    </update>

    <select id="selectKnowledgeById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_knowledge
        WHERE knowledge_id = #{knowledgeId} AND deleted = false
    </select>

    <select id="selectKnowledgeByDifyId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_knowledge
        WHERE dify_dataset_id = #{difyDatasetId} AND deleted = false
    </select>

    <select id="selectKnowledgeList" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_knowledge
        WHERE deleted = false
        <if test="filter != null">
            <if test="filter.title != null and filter.title != ''">
                AND title LIKE CONCAT('%', #{filter.title}, '%')
            </if>
            <if test="filter.service != null and filter.service != ''">
                AND service = #{filter.service}
            </if>
            <if test="filter.projectId != null and filter.projectId != ''">
                AND project_id = #{filter.projectId}
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND creator = #{filter.creator}
            </if>
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="selectKnowledgePage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM ai.tb_knowledge
        WHERE deleted = false
        <if test="filter != null">
            <if test="filter.title != null and filter.title != ''">
                AND title LIKE CONCAT('%', #{filter.title}, '%')
            </if>
            <if test="filter.service != null and filter.service != ''">
                AND service = #{filter.service}
            </if>
            <if test="filter.projectId != null and filter.projectId != ''">
                AND project_id = #{filter.projectId}
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND creator = #{filter.creator}
            </if>
        </if>
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countKnowledges" resultType="long">
        SELECT COUNT(*)
        FROM ai.tb_knowledge
        WHERE deleted = false
        <if test="filter != null">
            <if test="filter.title != null and filter.title != ''">
                AND title LIKE CONCAT('%', #{filter.title}, '%')
            </if>
            <if test="filter.service != null and filter.service != ''">
                AND service = #{filter.service}
            </if>
            <if test="filter.projectId != null and filter.projectId != ''">
                AND project_id = #{filter.projectId}
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
            <if test="filter.creator != null and filter.creator != ''">
                AND creator = #{filter.creator}
            </if>
        </if>
    </select>
</mapper>
