<?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.TbKnowledgeFileLogMapper">

    <resultMap id="BaseResultMap" type="org.xyzh.api.ai.dto.TbKnowledgeFileLog">
        <result column="optsn" property="optsn" jdbcType="VARCHAR"/>
        <result column="log_id" property="logId" jdbcType="VARCHAR"/>
        <result column="knowledge_id" property="knowledgeId" jdbcType="VARCHAR"/>
        <result column="file_root_id" property="fileRootId" jdbcType="VARCHAR"/>
        <result column="file_id" property="fileId" jdbcType="VARCHAR"/>
        <result column="file_name" property="fileName" jdbcType="VARCHAR"/>
        <result column="action" property="action" jdbcType="VARCHAR"/>
        <result column="version" property="version" jdbcType="INTEGER"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="creator_name" property="creatorName" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        optsn, log_id, knowledge_id, file_root_id, file_id, file_name, version,
        action, creator, creator_name, create_time
    </sql>

    <insert id="addKnowledgeFileLog" parameterType="org.xyzh.api.ai.dto.TbKnowledgeFileLog">
        INSERT INTO ai.tb_knowledge_file_log (
            optsn, log_id, knowledge_id, file_root_id, file_id, file_name, version,
            action, service, creator, creator_name, create_time
        ) VALUES (
            #{optsn}, #{logId}, #{knowledgeId}, #{fileRootId}, #{fileId}, 
            <choose>
                <!-- action为delete时，查询历史fileName，无数据则填空字符串 -->
                <when test="action == 'delete'">
                    (SELECT COALESCE(file_name, '') FROM ai.tb_knowledge_file_log WHERE file_id = #{fileId} LIMIT 1)
                </when>
                <!-- 其他操作直接使用传入的fileName -->
                <otherwise>
                    #{fileName}
                </otherwise>
            </choose>, 
            #{version}, #{action}, #{service}, #{creator}, #{creatorName}, NOW()
        )
    </insert>

    <select id="getKnowledgeFileLogList" resultMap="BaseResultMap">
        SELECT 
            <include refid="Base_Column_List"/>
        FROM ai.tb_knowledge_file_log
        WHERE 1=1 
            <if test="filter.knowledgeId != null and filter.knowledgeId != ''">
                AND knowledge_id = #{filter.knowledgeId}
            </if>
            <if test="filter.service != null and filter.service != ''">
                AND service = #{filter.service}
            </if>
            <if test="filter.fileName != null and filter.fileName != ''">
                AND file_name LIKE CONCAT('%', #{filter.fileName}, '%')
            </if>
            <if test="filter.creatorName != null and filter.creatorName != ''">
                AND creator_name LIKE CONCAT('%', #{filter.creatorName}, '%')
            </if>
        ORDER BY create_time DESC
    </select>
    
    <select id="getKnowledgeFileLogPage" resultMap="BaseResultMap">
        SELECT 
            <include refid="Base_Column_List"/>
        FROM ai.tb_knowledge_file_log
        WHERE 1=1 
            <if test="filter.knowledgeId != null and filter.knowledgeId != ''">
                AND knowledge_id = #{filter.knowledgeId}
            </if>
            <if test="filter.service != null and filter.service != ''">
                AND service = #{filter.service}
            </if>
            <if test="filter.fileName != null and filter.fileName != ''">
                AND file_name LIKE CONCAT('%', #{filter.fileName}, '%')
            </if>
            <if test="filter.creatorName != null and filter.creatorName != ''">
                AND creator_name LIKE CONCAT('%', #{filter.creatorName}, '%')
            </if>
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <select id="countKnowledgeFileLog" resultType="int">
        SELECT 
            count(log_id)
        FROM ai.tb_knowledge_file_log
        WHERE 1=1 
            <if test="filter.knowledgeId != null and filter.knowledgeId != ''">
                AND knowledge_id = #{filter.knowledgeId}
            </if>
            <if test="filter.service != null and filter.service != ''">
                AND service = #{filter.service}
            </if>
            <if test="filter.fileName != null and filter.fileName != ''">
                AND file_name LIKE CONCAT('%', #{filter.fileName}, '%')
            </if>
            <if test="filter.creatorName != null and filter.creatorName != ''">
                AND creator_name LIKE CONCAT('%', #{filter.creatorName}, '%')
            </if>
    </select>
</mapper>
