feat:【AI 大模型】增加知识库的删除功能

This commit is contained in:
YunaiV 2025-05-03 16:51:17 +08:00
parent e11ee654ef
commit 3e8596ee4b
5 changed files with 63 additions and 3 deletions

View File

@ -64,6 +64,15 @@ public class AiKnowledgeController {
return success(true); return success(true);
} }
@DeleteMapping("/delete")
@Operation(summary = "删除知识库")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
@PreAuthorize("@ss.hasPermission('ai:knowledge:delete')")
public CommonResult<Boolean> deleteKnowledge(@RequestParam("id") Long id) {
knowledgeService.deleteKnowledge(id);
return success(true);
}
@GetMapping("/simple-list") @GetMapping("/simple-list")
@Operation(summary = "获得知识库的精简列表") @Operation(summary = "获得知识库的精简列表")
public CommonResult<List<AiKnowledgeRespVO>> getKnowledgeSimpleList() { public CommonResult<List<AiKnowledgeRespVO>> getKnowledgeSimpleList() {

View File

@ -36,4 +36,8 @@ public interface AiKnowledgeDocumentMapper extends BaseMapperX<AiKnowledgeDocume
return selectList(AiKnowledgeDocumentDO::getStatus, status); return selectList(AiKnowledgeDocumentDO::getStatus, status);
} }
default List<AiKnowledgeDocumentDO> selectListByKnowledgeId(Long knowledgeId) {
return selectList(AiKnowledgeDocumentDO::getKnowledgeId, knowledgeId);
}
} }

View File

@ -81,6 +81,13 @@ public interface AiKnowledgeDocumentService {
*/ */
void deleteKnowledgeDocument(Long id); void deleteKnowledgeDocument(Long id);
/**
* 根据知识库编号批量删除文档
*
* @param knowledgeId 知识库编号
*/
void deleteKnowledgeDocumentByKnowledgeId(Long knowledgeId);
/** /**
* 校验文档是否存在 * 校验文档是否存在
* *
@ -105,6 +112,14 @@ public interface AiKnowledgeDocumentService {
*/ */
List<AiKnowledgeDocumentDO> getKnowledgeDocumentList(Collection<Long> ids); List<AiKnowledgeDocumentDO> getKnowledgeDocumentList(Collection<Long> ids);
/**
* 根据知识库编号获取文档列表
*
* @param knowledgeId 知识库编号
* @return 文档列表
*/
List<AiKnowledgeDocumentDO> getKnowledgeDocumentListByKnowledgeId(Long knowledgeId);
/** /**
* 获取文档 Map * 获取文档 Map
* *

View File

@ -211,4 +211,24 @@ public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentServic
return knowledgeDocumentMapper.selectBatchIds(ids); return knowledgeDocumentMapper.selectBatchIds(ids);
} }
@Override
public List<AiKnowledgeDocumentDO> getKnowledgeDocumentListByKnowledgeId(Long knowledgeId) {
return knowledgeDocumentMapper.selectListByKnowledgeId(knowledgeId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteKnowledgeDocumentByKnowledgeId(Long knowledgeId) {
// 1. 获取该知识库下的所有文档
List<AiKnowledgeDocumentDO> documents = knowledgeDocumentMapper.selectListByKnowledgeId(knowledgeId);
if (CollUtil.isEmpty(documents)) {
return;
}
// 2. 逐个删除文档及其对应的段落
for (AiKnowledgeDocumentDO document : documents) {
deleteKnowledgeDocument(document.getId());
}
}
} }

View File

@ -12,6 +12,7 @@ import cn.iocoder.yudao.module.ai.service.model.AiModelService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
@ -34,6 +35,8 @@ public class AiKnowledgeServiceImpl implements AiKnowledgeService {
private AiModelService modelService; private AiModelService modelService;
@Resource @Resource
private AiKnowledgeSegmentService knowledgeSegmentService; private AiKnowledgeSegmentService knowledgeSegmentService;
@Resource
private AiKnowledgeDocumentService knowledgeDocumentService;
@Override @Override
public Long createKnowledge(AiKnowledgeSaveReqVO createReqVO) { public Long createKnowledge(AiKnowledgeSaveReqVO createReqVO) {
@ -66,8 +69,17 @@ public class AiKnowledgeServiceImpl implements AiKnowledgeService {
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public void deleteKnowledge(Long id) { public void deleteKnowledge(Long id) {
// 1. 校验存在
validateKnowledgeExists(id);
// 2. 删除知识库下的所有文档及段落
knowledgeDocumentService.deleteKnowledgeDocumentByKnowledgeId(id);
// 3. 删除知识库
// 特殊知识库需要最后删除不然相关的配置会找不到
knowledgeMapper.deleteById(id);
} }
@Override @Override
@ -77,11 +89,11 @@ public class AiKnowledgeServiceImpl implements AiKnowledgeService {
@Override @Override
public AiKnowledgeDO validateKnowledgeExists(Long id) { public AiKnowledgeDO validateKnowledgeExists(Long id) {
AiKnowledgeDO knowledgeBase = knowledgeMapper.selectById(id); AiKnowledgeDO knowledge = knowledgeMapper.selectById(id);
if (knowledgeBase == null) { if (knowledge == null) {
throw exception(KNOWLEDGE_NOT_EXISTS); throw exception(KNOWLEDGE_NOT_EXISTS);
} }
return knowledgeBase; return knowledge;
} }
@Override @Override