[update] 知识库 删除校验

This commit is contained in:
Liuyang 2025-01-20 13:16:23 +08:00
parent c84022d00b
commit 54c2a296ad
3 changed files with 92 additions and 23 deletions

View File

@ -74,7 +74,7 @@ public interface ApplicationService {
* @param labelId 标签id
* @return 应用
*/
Map<Long,String> getApplicationByLabelId (Long labelId);
Map<Long, String> getApplicationByLabelId (Long labelId);
/**
* 根据 Prompt模版id 获取 应用
@ -83,4 +83,12 @@ public interface ApplicationService {
* @return 应用
*/
Map<Long, String> validatePromptTemplateUsesInApp (Long templateId);
/**
* 根据 知识库id 获取 应用
*
* @param knowledgeId 知识库id
* @return 应用
*/
Map<Long, String> getApplicationByKnowledgeId (Long knowledgeId);
}

View File

@ -339,4 +339,28 @@ public class ApplicationServiceImpl implements ApplicationService {
return applications.stream().collect(Collectors.toMap(ApplicationDO::getId, ApplicationDO::getAppName));
}
/**
* 根据 知识库id 获取 应用
*
* @param knowledgeId 知识库id
* @return 应用
*/
@Override
public Map<Long, String> getApplicationByKnowledgeId (Long knowledgeId) {
if (knowledgeId == null) {
return Collections.emptyMap();
}
List<ApplicationDO> applications = this.applicationMapper.selectList(new LambdaQueryWrapper<ApplicationDO>()
.select(ApplicationDO::getId, ApplicationDO::getAppName)
.eq(ApplicationDO::getModelServiceId, knowledgeId));
if (CollectionUtils.isEmpty(applications)) {
return Collections.emptyMap();
}
return applications.stream().collect(Collectors.toMap(ApplicationDO::getId, ApplicationDO::getAppName));
}
}

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.llm.service.knowledgebase;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
@ -9,24 +10,27 @@ import cn.iocoder.yudao.module.llm.controller.admin.knowledgebase.vo.KnowledgeBa
import cn.iocoder.yudao.module.llm.controller.admin.knowledgebase.vo.KnowledgeBaseSaveReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.knowledgedocuments.vo.KnowledgeDocumentsRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.knowledgedocuments.vo.KnowledgeDocumentsSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.knowledgebase.KnowledgeBaseDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.knowledgedocuments.KnowledgeDocumentsDO;
import cn.iocoder.yudao.module.llm.dal.mysql.knowledgebase.KnowledgeBaseMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.knowledgedocuments.KnowledgeDocumentsMapper;
import cn.iocoder.yudao.module.llm.service.application.ApplicationService;
import cn.iocoder.yudao.module.llm.service.async.AsyncKnowledgeBase;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.KNOWLEDGE_BASE_NAME_NOT_EXISTS;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.KNOWLEDGE_BASE_NOT_EXISTS;
/**
* 知识库 Service 实现类
*
@ -42,9 +46,11 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
private KnowledgeDocumentsMapper knowledgeDocumentsMapper;
@Resource
private AsyncKnowledgeBase asyncKnowledgeBase;
@Autowired
private ApplicationService applicationService;
@Override
public Long createKnowledgeBase(KnowledgeBaseSaveReqVO createReqVO) {
public Long createKnowledgeBase (KnowledgeBaseSaveReqVO createReqVO) {
validateKnowledgeBaseNameExists(createReqVO);
// 插入
KnowledgeBaseDO knowledgeBase = BeanUtils.toBean(createReqVO, KnowledgeBaseDO.class);
@ -54,7 +60,7 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
}
@Override
public void updateKnowledgeBase(KnowledgeBaseSaveReqVO updateReqVO) {
public void updateKnowledgeBase (KnowledgeBaseSaveReqVO updateReqVO) {
// 校验存在
validateKnowledgeBaseExists(updateReqVO.getId());
validateKnowledgeBaseNameExists(updateReqVO);
@ -63,15 +69,15 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
knowledgeBaseMapper.updateById(updateObj);
List<KnowledgeDocumentsDO> knowledgeDocumentsList = new ArrayList<>();
// 附表增加数据
if (!CollectionUtils.isAnyEmpty(updateReqVO.getKnowledgeDocuments())){
if (!CollectionUtils.isAnyEmpty(updateReqVO.getKnowledgeDocuments())) {
List<Long> ids = updateReqVO.getKnowledgeDocuments().stream().filter(
knowledgeDocuments -> knowledgeDocuments.getId() != null
).map(KnowledgeDocumentsSaveReqVO::getId).collect(Collectors.toList());
if (!CollectionUtils.isAnyEmpty(ids)){
if (!CollectionUtils.isAnyEmpty(ids)) {
knowledgeDocumentsMapper.delete(new LambdaQueryWrapperX<KnowledgeDocumentsDO>()
.eq(KnowledgeDocumentsDO::getKnowledgeBaseId, updateReqVO.getId())
.notIn(KnowledgeDocumentsDO::getId, ids));
}else {
} else {
knowledgeDocumentsMapper.delete(new LambdaQueryWrapperX<KnowledgeDocumentsDO>()
.eq(KnowledgeDocumentsDO::getKnowledgeBaseId, updateReqVO.getId()));
}
@ -79,68 +85,99 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
knowledgeDocuments -> {
KnowledgeDocumentsDO knowledgeDocumentsDO = BeanUtils.toBean(knowledgeDocuments, KnowledgeDocumentsDO.class);
knowledgeDocumentsDO.setKnowledgeBaseId(updateReqVO.getId());
if (knowledgeDocuments.getId() == null){
if (knowledgeDocuments.getId() == null) {
knowledgeDocumentsList.add(knowledgeDocumentsDO);
}
knowledgeDocumentsMapper.insertOrUpdate(knowledgeDocumentsDO);
}
);
List<Long> deleteIds = knowledgeDocumentsMapper.selectDeleteIds(updateReqVO.getId());
asyncKnowledgeBase.createKnowledgeBase(knowledgeDocumentsList,deleteIds);
asyncKnowledgeBase.createKnowledgeBase(knowledgeDocumentsList, deleteIds);
} else {
knowledgeDocumentsMapper.delete(new LambdaQueryWrapperX<KnowledgeDocumentsDO>()
.eq(KnowledgeDocumentsDO::getKnowledgeBaseId, updateReqVO.getId()));
List<Long> deleteIds = knowledgeDocumentsMapper.selectDeleteIds(updateReqVO.getId());
if (!CollectionUtils.isAnyEmpty(deleteIds)){
asyncKnowledgeBase.createKnowledgeBase(null,deleteIds);
if (!CollectionUtils.isAnyEmpty(deleteIds)) {
asyncKnowledgeBase.createKnowledgeBase(null, deleteIds);
}
}
}
@Override
public void deleteKnowledgeBase(Long id) {
public void deleteKnowledgeBase (Long id) {
// 校验存在
validateKnowledgeBaseExists(id);
// 校验使用
validateKnowledgeBaseUse(id);
// 删除
knowledgeBaseMapper.deleteById(id);
}
private void validateKnowledgeBaseExists(Long id) {
/**
* 校验使用
*
* @param id id
*/
private void validateKnowledgeBaseUse (Long id) {
// 获取 知识库信息用来返回错误信息
KnowledgeBaseDO knowledgeBaseDO = knowledgeBaseMapper.selectById(id);
String name = knowledgeBaseDO.getKnowledgeBaseName();
// 检查 知识库 是否在 应用 中有使用
validateKnowledgeBaseUsesInApp(id, name);
}
/**
* 检查 知识库 是否在 应用 中有使用
*
* @param id 知识库 id
* @param name 知识库 名称
*/
private void validateKnowledgeBaseUsesInApp (Long id, String name) {
Map<Long, String> apps = applicationService.getApplicationByKnowledgeId(id);
if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(apps)) {
String msg = String.format("知识库【%s】在 应用 %s 有使用,请先删除应用中的知识库", name, apps.values());
throw exception(new ErrorCode(10001_001, msg));
}
}
private void validateKnowledgeBaseExists (Long id) {
if (knowledgeBaseMapper.selectById(id) == null) {
throw exception(KNOWLEDGE_BASE_NOT_EXISTS);
}
}
private void validateKnowledgeBaseNameExists(KnowledgeBaseSaveReqVO reqVO) {
private void validateKnowledgeBaseNameExists (KnowledgeBaseSaveReqVO reqVO) {
LambdaQueryWrapper<KnowledgeBaseDO> wrapper = new LambdaQueryWrapper<KnowledgeBaseDO>()
.eq(KnowledgeBaseDO::getKnowledgeBaseName, reqVO.getKnowledgeBaseName());
if (reqVO.getId() != null){
if (reqVO.getId() != null) {
wrapper.ne(KnowledgeBaseDO::getId, reqVO.getId());
}
List<KnowledgeBaseDO> knowledgeBaseDos = knowledgeBaseMapper.selectList(wrapper);
if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(knowledgeBaseDos)){
if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(knowledgeBaseDos)) {
throw exception(KNOWLEDGE_BASE_NAME_NOT_EXISTS);
}
}
@Override
public KnowledgeBaseDO getKnowledgeBase(Long id) {
public KnowledgeBaseDO getKnowledgeBase (Long id) {
return knowledgeBaseMapper.selectById(id);
}
@Override
public PageResult<KnowledgeBaseDO> getKnowledgeBasePage(KnowledgeBasePageReqVO pageReqVO) {
public PageResult<KnowledgeBaseDO> getKnowledgeBasePage (KnowledgeBasePageReqVO pageReqVO) {
return knowledgeBaseMapper.selectPage(pageReqVO);
}
@Override
public List<KnowledgeBaseDO> getKnowledgeBaseList() {
public List<KnowledgeBaseDO> getKnowledgeBaseList () {
return knowledgeBaseMapper.selectList();
}
@Override
public KnowledgeBaseRespVO getKnowledgeBase1(Long id) {
public KnowledgeBaseRespVO getKnowledgeBase1 (Long id) {
KnowledgeBaseDO knowledgeBaseDO = knowledgeBaseMapper.selectById(id);
if (knowledgeBaseDO != null) {
KnowledgeBaseRespVO knowledgeBaseRespVO = BeanUtils.toBean(knowledgeBaseDO, KnowledgeBaseRespVO.class);