mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-29 13:53:55 +08:00
fix:当删除知识库记录时,自动删除相关的插件映射记录
This commit is contained in:
+7
@@ -26,4 +26,11 @@ public interface AgentPluginMappingService extends IService<AgentPluginMapping>
|
|||||||
* @param agentId
|
* @param agentId
|
||||||
*/
|
*/
|
||||||
void deleteByAgentId(String agentId);
|
void deleteByAgentId(String agentId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据知识库ID删除插件映射记录
|
||||||
|
*
|
||||||
|
* @param knowledgeBaseId 知识库ID
|
||||||
|
*/
|
||||||
|
void deleteByKnowledgeBaseId(String knowledgeBaseId);
|
||||||
}
|
}
|
||||||
|
|||||||
+19
@@ -6,12 +6,14 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import xiaozhi.common.utils.JsonUtils;
|
import xiaozhi.common.utils.JsonUtils;
|
||||||
import xiaozhi.modules.agent.dao.AgentPluginMappingMapper;
|
import xiaozhi.modules.agent.dao.AgentPluginMappingMapper;
|
||||||
import xiaozhi.modules.agent.entity.AgentPluginMapping;
|
import xiaozhi.modules.agent.entity.AgentPluginMapping;
|
||||||
@@ -27,9 +29,11 @@ import xiaozhi.modules.model.service.ModelConfigService;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class AgentPluginMappingServiceImpl extends ServiceImpl<AgentPluginMappingMapper, AgentPluginMapping>
|
public class AgentPluginMappingServiceImpl extends ServiceImpl<AgentPluginMappingMapper, AgentPluginMapping>
|
||||||
implements AgentPluginMappingService {
|
implements AgentPluginMappingService {
|
||||||
private final AgentPluginMappingMapper agentPluginMappingMapper;
|
private final AgentPluginMappingMapper agentPluginMappingMapper;
|
||||||
|
@Lazy
|
||||||
private final KnowledgeBaseService knowledgeBaseService;
|
private final KnowledgeBaseService knowledgeBaseService;
|
||||||
private final ModelConfigService modelConfigService;
|
private final ModelConfigService modelConfigService;
|
||||||
|
|
||||||
@@ -102,4 +106,19 @@ public class AgentPluginMappingServiceImpl extends ServiceImpl<AgentPluginMappin
|
|||||||
agentPluginMappingMapper.delete(updateWrapper);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
@@ -314,6 +314,8 @@ public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBaseDao,
|
|||||||
|
|
||||||
// API删除成功后再删除本地记录
|
// API删除成功后再删除本地记录
|
||||||
if (apiDeleteSuccess) {
|
if (apiDeleteSuccess) {
|
||||||
|
log.info("数据库触发器将自动清理ai_agent_plugin_mapping表中与知识库ID '{}' 相关的映射记录", entity.getId());
|
||||||
|
|
||||||
int deleteCount = knowledgeBaseDao.deleteById(entity.getId());
|
int deleteCount = knowledgeBaseDao.deleteById(entity.getId());
|
||||||
log.info("本地数据库删除结果: {}", deleteCount > 0 ? "成功" : "失败");
|
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;
|
||||||
@@ -416,3 +416,10 @@ databaseChangeLog:
|
|||||||
- sqlFile:
|
- sqlFile:
|
||||||
encoding: utf8
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user