fix:当删除知识库记录时,自动删除相关的插件映射记录

This commit is contained in:
rainv123
2025-11-13 10:49:15 +08:00
parent ae321b26af
commit ad3c420191
5 changed files with 49 additions and 1 deletions
@@ -26,4 +26,11 @@ public interface AgentPluginMappingService extends IService<AgentPluginMapping>
* @param agentId
*/
void deleteByAgentId(String agentId);
/**
* 根据知识库ID删除插件映射记录
*
* @param knowledgeBaseId 知识库ID
*/
void deleteByKnowledgeBaseId(String knowledgeBaseId);
}
@@ -6,12 +6,14 @@ import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import xiaozhi.common.utils.JsonUtils;
import xiaozhi.modules.agent.dao.AgentPluginMappingMapper;
import xiaozhi.modules.agent.entity.AgentPluginMapping;
@@ -27,9 +29,11 @@ import xiaozhi.modules.model.service.ModelConfigService;
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class AgentPluginMappingServiceImpl extends ServiceImpl<AgentPluginMappingMapper, AgentPluginMapping>
implements AgentPluginMappingService {
private final AgentPluginMappingMapper agentPluginMappingMapper;
@Lazy
private final KnowledgeBaseService knowledgeBaseService;
private final ModelConfigService modelConfigService;
@@ -102,4 +106,19 @@ public class AgentPluginMappingServiceImpl extends ServiceImpl<AgentPluginMappin
agentPluginMappingMapper.delete(updateWrapper);
}
@Override
public void deleteByKnowledgeBaseId(String knowledgeBaseId) {
if (StringUtils.isBlank(knowledgeBaseId)) {
return;
}
UpdateWrapper<AgentPluginMapping> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("plugin_id", knowledgeBaseId);
int deletedCount = agentPluginMappingMapper.delete(updateWrapper);
if (deletedCount > 0) {
log.info("已删除 {} 条与知识库ID '{}' 相关的插件映射记录", deletedCount, knowledgeBaseId);
}
}
}
@@ -314,6 +314,8 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
// API删除成功后再删除本地记录
if (apiDeleteSuccess) {
log.info("数据库触发器将自动清理ai_agent_plugin_mapping表中与知识库ID '{}' 相关的映射记录", entity.getId());
int deleteCount = knowledgeBaseDao.deleteById(entity.getId());
log.info("本地数据库删除结果: {}", deleteCount > 0 ? "成功" : "失败");
}
@@ -0,0 +1,13 @@
-- 当删除知识库记录时,自动删除相关的插件映射记录
-- 先删除可能存在的同名触发器(如果存在)
DROP TRIGGER IF EXISTS trigger_delete_plugin_mapping_on_knowledgebase_delete;
-- 创建新的触发器
CREATE TRIGGER trigger_delete_plugin_mapping_on_knowledgebase_delete
AFTER DELETE ON ai_rag_dataset
FOR EACH ROW
BEGIN
-- 删除与该知识库ID相关的插件映射记录
DELETE FROM ai_agent_plugin_mapping WHERE plugin_id = OLD.id;
END;
@@ -415,4 +415,11 @@ databaseChangeLog:
changes:
- sqlFile:
encoding: utf8
path: classpath:db/changelog/202510251150.sql
path: classpath:db/changelog/202510251150.sql
- changeSet:
id: 202511131016
author: rainv123
changes:
- sqlFile:
encoding: utf8
path: classpath:db/changelog/202511131016.sql