mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 23:23:55 +08:00
Merge branch 'main' into manager-web-logo-i18n
This commit is contained in:
+18
-6
@@ -1,6 +1,9 @@
|
||||
package xiaozhi.modules.agent.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
@@ -15,12 +18,6 @@ import xiaozhi.modules.agent.entity.AgentChatHistoryEntity;
|
||||
*/
|
||||
@Mapper
|
||||
public interface AiAgentChatHistoryDao extends BaseMapper<AgentChatHistoryEntity> {
|
||||
/**
|
||||
* 根据智能体ID删除音频
|
||||
*
|
||||
* @param agentId 智能体ID
|
||||
*/
|
||||
void deleteAudioByAgentId(String agentId);
|
||||
|
||||
/**
|
||||
* 根据智能体ID删除聊天历史记录
|
||||
@@ -35,4 +32,19 @@ public interface AiAgentChatHistoryDao extends BaseMapper<AgentChatHistoryEntity
|
||||
* @param agentId 智能体ID
|
||||
*/
|
||||
void deleteAudioIdByAgentId(String agentId);
|
||||
|
||||
/**
|
||||
* 根据智能体ID获取所有音频ID列表
|
||||
*
|
||||
* @param agentId 智能体ID
|
||||
* @return 音频ID列表
|
||||
*/
|
||||
List<String> getAudioIdsByAgentId(String agentId);
|
||||
|
||||
/**
|
||||
* 批量删除音频
|
||||
*
|
||||
* @param audioIds 音频ID列表
|
||||
*/
|
||||
void deleteAudioByIds(@Param("audioIds") List<String> audioIds);
|
||||
}
|
||||
|
||||
+11
-2
@@ -84,7 +84,16 @@ public class AgentChatHistoryServiceImpl extends ServiceImpl<AiAgentChatHistoryD
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteByAgentId(String agentId, Boolean deleteAudio, Boolean deleteText) {
|
||||
if (deleteAudio) {
|
||||
baseMapper.deleteAudioByAgentId(agentId);
|
||||
// 分批删除音频,避免超时
|
||||
List<String> audioIds = baseMapper.getAudioIdsByAgentId(agentId);
|
||||
if (audioIds != null && !audioIds.isEmpty()) {
|
||||
int batchSize = 1000; // 每批删除1000条
|
||||
for (int i = 0; i < audioIds.size(); i += batchSize) {
|
||||
int end = Math.min(i + batchSize, audioIds.size());
|
||||
List<String> batch = audioIds.subList(i, end);
|
||||
baseMapper.deleteAudioByIds(batch);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (deleteAudio && !deleteText) {
|
||||
baseMapper.deleteAudioIdByAgentId(agentId);
|
||||
@@ -107,7 +116,7 @@ public class AgentChatHistoryServiceImpl extends ServiceImpl<AiAgentChatHistoryD
|
||||
// 添加此行,确保查询结果按照创建时间降序排列
|
||||
// 使用id的原因:数据形式,id越大的创建时间就越晚,所以使用id的结果和创建时间降序排列结果一样
|
||||
// id作为降序排列的优势,性能高,有主键索引,不用在排序的时候重新进行排除扫描比较
|
||||
.orderByDesc(AgentChatHistoryEntity::getId);
|
||||
.orderByDesc(AgentChatHistoryEntity::getId);
|
||||
|
||||
// 构建分页查询,查询前50页数据
|
||||
Page<AgentChatHistoryEntity> pageParam = new Page<>(0, 50);
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- 为智能体聊天历史记录添加音频ID索引
|
||||
ALTER TABLE ai_agent_chat_history ADD INDEX idx_ai_agent_chat_history_audio_id (audio_id);
|
||||
@@ -452,3 +452,10 @@ databaseChangeLog:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202512161529.sql
|
||||
- changeSet:
|
||||
id: 202512192245
|
||||
author: hrz
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202512192245.sql
|
||||
@@ -22,13 +22,18 @@
|
||||
created_at, updated_at
|
||||
</sql>
|
||||
|
||||
<delete id="deleteAudioByAgentId">
|
||||
DELETE FROM ai_agent_chat_audio
|
||||
WHERE id IN (
|
||||
SELECT audio_id
|
||||
FROM ai_agent_chat_history
|
||||
WHERE agent_id = #{agentId}
|
||||
)
|
||||
<select id="getAudioIdsByAgentId" resultType="java.lang.String">
|
||||
SELECT DISTINCT audio_id
|
||||
FROM ai_agent_chat_history
|
||||
WHERE agent_id = #{agentId} AND audio_id IS NOT NULL
|
||||
</select>
|
||||
|
||||
<delete id="deleteAudioByIds">
|
||||
DELETE FROM ai_agent_chat_audio
|
||||
WHERE id IN
|
||||
<foreach collection="audioIds" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
|
||||
<update id="deleteAudioIdByAgentId">
|
||||
|
||||
Reference in New Issue
Block a user