feat:新增知识库管理页面并联调知识库接口

This commit is contained in:
zhiye.sun
2026-05-21 13:09:33 +08:00
parent 387681a6ab
commit 91e6d5bdd3
12 changed files with 1506 additions and 17 deletions

View File

@@ -2,36 +2,74 @@ package com.bruce.rag.controller;
import com.bruce.common.domain.model.RequestResult;
import com.bruce.rag.dto.request.RagStoreQueryRequest;
import com.bruce.rag.dto.request.RagStoreSaveRequest;
import com.bruce.rag.dto.response.RagStoreResponse;
import com.bruce.rag.service.IRagStoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Tag(name = "RAG知识库管理")
@Slf4j
@RestController
@RequestMapping("/api/rag/stores")
@RequestMapping("/api/rag/store")
public class RagStoreController {
@Autowired
private IRagStoreService ragStoreService;
@Operation(summary = "查询全部知识库")
@GetMapping
@PostMapping("/list")
public RequestResult<List<RagStoreResponse>> list() {
return RequestResult.success(ragStoreService.listResponses());
log.info("RagStoreController.list start");
List<RagStoreResponse> responses = ragStoreService.listResponses();
log.info("RagStoreController.list success, count={}", responses.size());
return RequestResult.success(responses);
}
@Operation(summary = "按条件查询知识库")
@PostMapping("/query")
public RequestResult<List<RagStoreResponse>> query(@RequestBody RagStoreQueryRequest request) {
return RequestResult.success(ragStoreService.query(request));
public RequestResult<List<RagStoreResponse>> query(@RequestBody(required = false) RagStoreQueryRequest request) {
log.info("RagStoreController.query start, request={}", request);
List<RagStoreResponse> responses = ragStoreService.query(request);
log.info("RagStoreController.query success, count={}", responses.size());
return RequestResult.success(responses);
}
@Operation(summary = "查询知识库详情")
@GetMapping("/detail")
public RequestResult<RagStoreResponse> getById(@RequestParam("id") Long id) {
log.info("RagStoreController.getById start, id={}", id);
RagStoreResponse response = ragStoreService.getResponseById(id);
log.info("RagStoreController.getById success, id={}, found={}", id, response != null);
return RequestResult.success(response);
}
@Operation(summary = "新增或修改知识库")
@PostMapping("/save")
public RequestResult<Boolean> saveOrUpdate(@RequestBody RagStoreSaveRequest request) {
log.info("RagStoreController.saveOrUpdate start, request={}", request);
Boolean result = ragStoreService.saveOrUpdate(request);
log.info("RagStoreController.saveOrUpdate success, id={}, storeCode={}, result={}",
request.getId(), request.getStoreCode(), result);
return RequestResult.success(result);
}
@Operation(summary = "删除知识库")
@PostMapping("/delete")
public RequestResult<Boolean> deleteById(@RequestParam("id") Long id) {
log.info("RagStoreController.deleteById start, id={}", id);
Boolean result = ragStoreService.removeById(id);
log.info("RagStoreController.deleteById success, id={}, result={}", id, result);
return RequestResult.success(result);
}
}

View File

@@ -0,0 +1,27 @@
package com.bruce.rag.dto.request;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
@Schema(description = "RAG知识库保存请求")
public class RagStoreSaveRequest {
@Schema(description = "主键ID")
private Long id;
@Schema(description = "知识库编码")
private String storeCode;
@Schema(description = "知识库名称")
private String storeName;
@Schema(description = "知识库描述")
private String description;
@Schema(description = "状态")
private String status;
@Schema(description = "备注")
private String remark;
}

View File

@@ -1,14 +1,19 @@
package com.bruce.rag.dto.response;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.bruce.rag.entity.RagStore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import java.util.Date;
@Data
@Schema(description = "RAG知识库响应")
public class RagStoreResponse {
@JsonSerialize(using = ToStringSerializer.class)
@Schema(description = "主键ID")
private Long id;
@@ -27,6 +32,12 @@ public class RagStoreResponse {
@Schema(description = "备注")
private String remark;
@Schema(description = "创建时间")
private Date createTime;
@Schema(description = "更新时间")
private Date updateTime;
public static RagStoreResponse fromEntity(RagStore entity) {
if (entity == null) {
return null;

View File

@@ -2,6 +2,7 @@ package com.bruce.rag.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bruce.rag.dto.request.RagStoreQueryRequest;
import com.bruce.rag.dto.request.RagStoreSaveRequest;
import com.bruce.rag.dto.response.RagStoreResponse;
import com.bruce.rag.entity.RagStore;
@@ -12,4 +13,8 @@ public interface IRagStoreService extends IService<RagStore> {
List<RagStoreResponse> listResponses();
List<RagStoreResponse> query(RagStoreQueryRequest request);
RagStoreResponse getResponseById(Long id);
boolean saveOrUpdate(RagStoreSaveRequest request);
}

View File

@@ -2,34 +2,92 @@ package com.bruce.rag.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.rag.dto.request.RagStoreQueryRequest;
import com.bruce.rag.dto.request.RagStoreSaveRequest;
import com.bruce.rag.dto.response.RagStoreResponse;
import com.bruce.rag.entity.RagStore;
import com.bruce.rag.mapper.RagStoreMapper;
import com.bruce.rag.service.IRagStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Slf4j
@Service
public class RagStoreServiceImpl extends ServiceImpl<RagStoreMapper, RagStore> implements IRagStoreService {
@Override
public List<RagStoreResponse> listResponses() {
return toResponses(list());
log.info("RagStoreServiceImpl.listResponses start");
List<RagStoreResponse> responses = toResponses(list());
log.info("RagStoreServiceImpl.listResponses success, count={}", responses.size());
return responses;
}
@Override
public List<RagStoreResponse> query(RagStoreQueryRequest request) {
if (request == null) {
throw new IllegalArgumentException("查询请求不能为空");
}
return toResponses(lambdaQuery()
.eq(StringUtils.hasText(request.getStoreCode()), RagStore::getStoreCode, request.getStoreCode())
.like(StringUtils.hasText(request.getStoreName()), RagStore::getStoreName, request.getStoreName())
.eq(StringUtils.hasText(request.getStatus()), RagStore::getStatus, request.getStatus())
log.info("RagStoreServiceImpl.query start, request={}", request);
RagStoreQueryRequest queryRequest = request == null ? new RagStoreQueryRequest() : request;
List<RagStoreResponse> responses = toResponses(lambdaQuery()
.eq(StringUtils.hasText(queryRequest.getStoreCode()), RagStore::getStoreCode, queryRequest.getStoreCode())
.like(StringUtils.hasText(queryRequest.getStoreName()), RagStore::getStoreName, queryRequest.getStoreName())
.eq(StringUtils.hasText(queryRequest.getStatus()), RagStore::getStatus, queryRequest.getStatus())
.orderByAsc(RagStore::getStoreCode)
.list());
log.info("RagStoreServiceImpl.query success, count={}", responses.size());
return responses;
}
@Override
public RagStoreResponse getResponseById(Long id) {
log.info("RagStoreServiceImpl.getResponseById start, id={}", id);
RagStoreResponse response = RagStoreResponse.fromEntity(getById(id));
log.info("RagStoreServiceImpl.getResponseById success, id={}, found={}", id, response != null);
return response;
}
@Override
public boolean saveOrUpdate(RagStoreSaveRequest request) {
log.info("RagStoreServiceImpl.saveOrUpdate start, request={}", request);
validateSaveRequest(request);
RagStore existingStore = lambdaQuery()
.eq(RagStore::getStoreCode, request.getStoreCode().trim())
.ne(request.getId() != null, RagStore::getId, request.getId())
.one();
if (existingStore != null) {
log.warn("RagStoreServiceImpl.saveOrUpdate duplicate storeCode detected, requestId={}, existingId={}, storeCode={}",
request.getId(), existingStore.getId(), request.getStoreCode().trim());
throw new IllegalArgumentException("知识库编码已存在: " + request.getStoreCode().trim());
}
RagStore ragStore = new RagStore();
ragStore.setId(request.getId());
ragStore.setStoreCode(request.getStoreCode().trim());
ragStore.setStoreName(request.getStoreName().trim());
ragStore.setDescription(trimToNull(request.getDescription()));
ragStore.setStatus(StringUtils.hasText(request.getStatus()) ? request.getStatus().trim() : "启用");
ragStore.setRemark(trimToNull(request.getRemark()));
boolean result = super.saveOrUpdate(ragStore);
log.info("RagStoreServiceImpl.saveOrUpdate success, requestId={}, savedId={}, storeCode={}, result={}",
request.getId(), ragStore.getId(), ragStore.getStoreCode(), result);
return result;
}
public void validateSaveRequest(RagStoreSaveRequest request) {
log.info("RagStoreServiceImpl.validateSaveRequest start");
if (request == null) {
throw new IllegalArgumentException("保存请求不能为空");
}
if (!StringUtils.hasText(request.getStoreCode())) {
throw new IllegalArgumentException("知识库编码不能为空");
}
if (!StringUtils.hasText(request.getStoreName())) {
throw new IllegalArgumentException("知识库名称不能为空");
}
log.info("RagStoreServiceImpl.validateSaveRequest success, id={}, storeCode={}, storeName={}",
request.getId(), request.getStoreCode(), request.getStoreName());
}
private List<RagStoreResponse> toResponses(List<RagStore> stores) {
@@ -37,4 +95,11 @@ public class RagStoreServiceImpl extends ServiceImpl<RagStoreMapper, RagStore> i
.map(RagStoreResponse::fromEntity)
.toList();
}
private String trimToNull(String value) {
if (!StringUtils.hasText(value)) {
return null;
}
return value.trim();
}
}

View File

@@ -9,6 +9,7 @@ import com.bruce.rag.controller.RagDocumentController;
import com.bruce.rag.controller.RagStoreController;
import com.bruce.rag.dto.request.RagDocumentQueryRequest;
import com.bruce.rag.dto.request.RagStoreQueryRequest;
import com.bruce.rag.dto.request.RagStoreSaveRequest;
import com.bruce.rag.dto.response.RagDocumentResponse;
import com.bruce.rag.dto.response.RagStoreResponse;
import com.bruce.rag.entity.RagDocument;
@@ -44,8 +45,13 @@ class RagComponentStructureTests {
void ragControllersShouldExposeRequestResultAndQueryDtoMethods() throws NoSuchMethodException {
Method storeListMethod = RagStoreController.class.getMethod("list");
Method storeQueryMethod = RagStoreController.class.getMethod("query", RagStoreQueryRequest.class);
Method storeDetailMethod = RagStoreController.class.getMethod("getById", Long.class);
Method storeSaveMethod = RagStoreController.class.getMethod("saveOrUpdate", RagStoreSaveRequest.class);
Method storeDeleteMethod = RagStoreController.class.getMethod("deleteById", Long.class);
Method storeResponseListMethod = IRagStoreService.class.getMethod("listResponses");
Method storeServiceQueryMethod = IRagStoreService.class.getMethod("query", RagStoreQueryRequest.class);
Method storeServiceDetailMethod = IRagStoreService.class.getMethod("getResponseById", Long.class);
Method storeServiceSaveMethod = IRagStoreService.class.getMethod("saveOrUpdate", RagStoreSaveRequest.class);
Method documentListMethod = RagDocumentController.class.getMethod("list");
Method documentQueryMethod = RagDocumentController.class.getMethod("query", RagDocumentQueryRequest.class);
@@ -54,11 +60,17 @@ class RagComponentStructureTests {
assertEquals(RequestResult.class, storeListMethod.getReturnType());
assertEquals(RequestResult.class, storeQueryMethod.getReturnType());
assertEquals(RequestResult.class, storeDetailMethod.getReturnType());
assertEquals(RequestResult.class, storeSaveMethod.getReturnType());
assertEquals(RequestResult.class, storeDeleteMethod.getReturnType());
assertEquals(List.class, storeServiceQueryMethod.getReturnType());
assertEquals(RagStoreResponse.class, storeServiceDetailMethod.getReturnType());
assertEquals(boolean.class, storeServiceSaveMethod.getReturnType());
assertTrue(storeResponseListMethod.getGenericReturnType().getTypeName().contains("RagStoreResponse"));
assertTrue(storeServiceQueryMethod.getGenericReturnType().getTypeName().contains("RagStoreResponse"));
assertTrue(storeListMethod.getGenericReturnType().getTypeName().contains("RagStoreResponse"));
assertTrue(storeQueryMethod.getGenericReturnType().getTypeName().contains("RagStoreResponse"));
assertTrue(storeDetailMethod.getGenericReturnType().getTypeName().contains("RagStoreResponse"));
assertEquals(RagStoreResponse.class, RagStoreResponse.class.getMethod("fromEntity", RagStore.class).getReturnType());
assertEquals(RequestResult.class, documentListMethod.getReturnType());

View File

@@ -0,0 +1,22 @@
package com.bruce.rag;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.bruce.rag.dto.response.RagStoreResponse;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
class RagStoreResponseSerializationTests {
@Test
void idShouldSerializeAsStringForFrontendPrecisionSafety() throws Exception {
RagStoreResponse response = new RagStoreResponse();
response.setId(2057302206052372481L);
response.setStoreCode("TEXT-1");
response.setStoreName("测试库1");
String json = new ObjectMapper().writeValueAsString(response);
assertTrue(json.contains("\"id\":\"2057302206052372481\""));
}
}

View File

@@ -0,0 +1,39 @@
package com.bruce.rag;
import com.bruce.rag.dto.request.RagStoreSaveRequest;
import com.bruce.rag.service.impl.RagStoreServiceImpl;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
class RagStoreSaveValidationTests {
@Test
void saveShouldRejectBlankStoreCode() {
RagStoreServiceImpl service = new RagStoreServiceImpl();
RagStoreSaveRequest request = new RagStoreSaveRequest();
request.setStoreName("产品制度库");
assertThrows(IllegalArgumentException.class, () -> service.validateSaveRequest(request));
}
@Test
void saveShouldRejectBlankStoreName() {
RagStoreServiceImpl service = new RagStoreServiceImpl();
RagStoreSaveRequest request = new RagStoreSaveRequest();
request.setStoreCode("PROD_DOC");
assertThrows(IllegalArgumentException.class, () -> service.validateSaveRequest(request));
}
@Test
void saveShouldAcceptMinimalValidRequest() {
RagStoreServiceImpl service = new RagStoreServiceImpl();
RagStoreSaveRequest request = new RagStoreSaveRequest();
request.setStoreCode("PROD_DOC");
request.setStoreName("产品制度库");
assertDoesNotThrow(() -> service.validateSaveRequest(request));
}
}