<?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.moyz.adi.common.mapper.CharacterMemoryEmbeddingMapper">

    <select id="countByCharacterId" resultType="java.lang.Integer">
        select count(1)
        from
        <include refid="getTableName"/>
        where metadata ->> 'conv_id' = #{characterId}
    </select>

    <select id="selectByMsgId" resultType="com.moyz.adi.common.entity.CharacterMemoryEmbedding">
        select *
        from <include refid="getTableName"/>
        where metadata ->> 'conv_msg_id' = #{msgId}
    </select>

    <delete id="deleteByIds">
        delete from <include refid="getTableName"/> where embedding_id in
        <foreach collection="ids" open="(" separator="," close=")" item="id">
            <if test="id != null and id != ''">
                #{id}
            </if>
        </foreach>
    </delete>

    <delete id="deleteByMsgId">
        delete from <include refid="getTableName"/>
        where metadata ->> 'conv_msg_id' = #{msgId}
    </delete>

    <sql id="getTableName">
        <if test="tableSuffix != null and tableSuffix != ''">
            adi_character_memory_embedding_${tableSuffix}
        </if>
        <if test="tableSuffix == null or tableSuffix == ''">
            adi_character_memory_embedding
        </if>
    </sql>
</mapper>
