refactor: 统一RAG接口查询请求与返回体

This commit is contained in:
zhiye.sun
2026-05-19 16:21:51 +08:00
committed by bruce
parent fc97c3998d
commit bc225d3557
9 changed files with 135 additions and 4 deletions

View File

@@ -1,11 +1,15 @@
package com.bruce.rag.controller; package com.bruce.rag.controller;
import com.bruce.common.domain.model.RequestResult;
import com.bruce.rag.dto.request.RagDocumentQueryRequest;
import com.bruce.rag.entity.RagDocument; import com.bruce.rag.entity.RagDocument;
import com.bruce.rag.service.IRagDocumentService; import com.bruce.rag.service.IRagDocumentService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -21,7 +25,13 @@ public class RagDocumentController {
@Operation(summary = "查询全部知识库文档") @Operation(summary = "查询全部知识库文档")
@GetMapping @GetMapping
public List<RagDocument> list() { public RequestResult<List<RagDocument>> list() {
return ragDocumentService.list(); return RequestResult.success(ragDocumentService.list());
}
@Operation(summary = "按条件查询知识库文档")
@PostMapping("/query")
public RequestResult<List<RagDocument>> query(@RequestBody RagDocumentQueryRequest request) {
return RequestResult.success(ragDocumentService.query(request));
} }
} }

View File

@@ -1,11 +1,15 @@
package com.bruce.rag.controller; package com.bruce.rag.controller;
import com.bruce.common.domain.model.RequestResult;
import com.bruce.rag.dto.request.RagStoreQueryRequest;
import com.bruce.rag.entity.RagStore; import com.bruce.rag.entity.RagStore;
import com.bruce.rag.service.IRagStoreService; import com.bruce.rag.service.IRagStoreService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -21,7 +25,13 @@ public class RagStoreController {
@Operation(summary = "查询全部知识库") @Operation(summary = "查询全部知识库")
@GetMapping @GetMapping
public List<RagStore> list() { public RequestResult<List<RagStore>> list() {
return ragStoreService.list(); return RequestResult.success(ragStoreService.list());
}
@Operation(summary = "按条件查询知识库")
@PostMapping("/query")
public RequestResult<List<RagStore>> query(@RequestBody RagStoreQueryRequest request) {
return RequestResult.success(ragStoreService.query(request));
} }
} }

View File

@@ -0,0 +1,24 @@
package com.bruce.rag.dto.request;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
@Schema(description = "RAG知识库文档查询请求")
public class RagDocumentQueryRequest {
@Schema(description = "知识库ID")
private Long storeId;
@Schema(description = "附件ID")
private Long attachmentId;
@Schema(description = "解析状态")
private String parseStatus;
@Schema(description = "索引状态")
private String indexStatus;
@Schema(description = "是否启用")
private Boolean enabled;
}

View File

@@ -0,0 +1,18 @@
package com.bruce.rag.dto.request;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
@Schema(description = "RAG知识库查询请求")
public class RagStoreQueryRequest {
@Schema(description = "知识库编码")
private String storeCode;
@Schema(description = "知识库名称")
private String storeName;
@Schema(description = "状态")
private String status;
}

View File

@@ -1,7 +1,12 @@
package com.bruce.rag.service; package com.bruce.rag.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.bruce.rag.dto.request.RagDocumentQueryRequest;
import com.bruce.rag.entity.RagDocument; import com.bruce.rag.entity.RagDocument;
import java.util.List;
public interface IRagDocumentService extends IService<RagDocument> { public interface IRagDocumentService extends IService<RagDocument> {
List<RagDocument> query(RagDocumentQueryRequest request);
} }

View File

@@ -1,7 +1,12 @@
package com.bruce.rag.service; package com.bruce.rag.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.bruce.rag.dto.request.RagStoreQueryRequest;
import com.bruce.rag.entity.RagStore; import com.bruce.rag.entity.RagStore;
import java.util.List;
public interface IRagStoreService extends IService<RagStore> { public interface IRagStoreService extends IService<RagStore> {
List<RagStore> query(RagStoreQueryRequest request);
} }

View File

@@ -1,11 +1,29 @@
package com.bruce.rag.service.impl; package com.bruce.rag.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.rag.dto.request.RagDocumentQueryRequest;
import com.bruce.rag.entity.RagDocument; import com.bruce.rag.entity.RagDocument;
import com.bruce.rag.mapper.RagDocumentMapper; import com.bruce.rag.mapper.RagDocumentMapper;
import com.bruce.rag.service.IRagDocumentService; import com.bruce.rag.service.IRagDocumentService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
public class RagDocumentServiceImpl extends ServiceImpl<RagDocumentMapper, RagDocument> implements IRagDocumentService { public class RagDocumentServiceImpl extends ServiceImpl<RagDocumentMapper, RagDocument> implements IRagDocumentService {
@Override
public List<RagDocument> query(RagDocumentQueryRequest request) {
if (request == null) {
throw new IllegalArgumentException("查询请求不能为空");
}
return lambdaQuery()
.eq(request.getStoreId() != null, RagDocument::getStoreId, request.getStoreId())
.eq(request.getAttachmentId() != null, RagDocument::getAttachmentId, request.getAttachmentId())
.eq(request.getParseStatus() != null, RagDocument::getParseStatus, request.getParseStatus())
.eq(request.getIndexStatus() != null, RagDocument::getIndexStatus, request.getIndexStatus())
.eq(request.getEnabled() != null, RagDocument::getEnabled, request.getEnabled())
.orderByDesc(RagDocument::getId)
.list();
}
} }

View File

@@ -1,11 +1,28 @@
package com.bruce.rag.service.impl; package com.bruce.rag.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.rag.dto.request.RagStoreQueryRequest;
import com.bruce.rag.entity.RagStore; import com.bruce.rag.entity.RagStore;
import com.bruce.rag.mapper.RagStoreMapper; import com.bruce.rag.mapper.RagStoreMapper;
import com.bruce.rag.service.IRagStoreService; import com.bruce.rag.service.IRagStoreService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Service @Service
public class RagStoreServiceImpl extends ServiceImpl<RagStoreMapper, RagStore> implements IRagStoreService { public class RagStoreServiceImpl extends ServiceImpl<RagStoreMapper, RagStore> implements IRagStoreService {
@Override
public List<RagStore> query(RagStoreQueryRequest request) {
if (request == null) {
throw new IllegalArgumentException("查询请求不能为空");
}
return 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())
.orderByAsc(RagStore::getStoreCode)
.list();
}
} }

View File

@@ -3,9 +3,12 @@ package com.bruce.rag;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bruce.common.domain.model.RequestResult;
import com.bruce.rag.constant.RagSystemConstants; import com.bruce.rag.constant.RagSystemConstants;
import com.bruce.rag.controller.RagDocumentController; import com.bruce.rag.controller.RagDocumentController;
import com.bruce.rag.controller.RagStoreController; import com.bruce.rag.controller.RagStoreController;
import com.bruce.rag.dto.request.RagDocumentQueryRequest;
import com.bruce.rag.dto.request.RagStoreQueryRequest;
import com.bruce.rag.entity.RagDocument; import com.bruce.rag.entity.RagDocument;
import com.bruce.rag.entity.RagStore; import com.bruce.rag.entity.RagStore;
import com.bruce.rag.mapper.RagDocumentMapper; import com.bruce.rag.mapper.RagDocumentMapper;
@@ -17,6 +20,8 @@ import com.bruce.rag.service.impl.RagStoreServiceImpl;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -33,6 +38,25 @@ class RagComponentStructureTests {
assertTrue(ServiceImpl.class.isAssignableFrom(RagDocumentServiceImpl.class)); assertTrue(ServiceImpl.class.isAssignableFrom(RagDocumentServiceImpl.class));
} }
@Test
void ragControllersShouldExposeRequestResultAndQueryDtoMethods() throws NoSuchMethodException {
Method storeListMethod = RagStoreController.class.getMethod("list");
Method storeQueryMethod = RagStoreController.class.getMethod("query", RagStoreQueryRequest.class);
Method storeServiceQueryMethod = IRagStoreService.class.getMethod("query", RagStoreQueryRequest.class);
Method documentListMethod = RagDocumentController.class.getMethod("list");
Method documentQueryMethod = RagDocumentController.class.getMethod("query", RagDocumentQueryRequest.class);
Method documentServiceQueryMethod = IRagDocumentService.class.getMethod("query", RagDocumentQueryRequest.class);
assertEquals(RequestResult.class, storeListMethod.getReturnType());
assertEquals(RequestResult.class, storeQueryMethod.getReturnType());
assertEquals(List.class, storeServiceQueryMethod.getReturnType());
assertEquals(RequestResult.class, documentListMethod.getReturnType());
assertEquals(RequestResult.class, documentQueryMethod.getReturnType());
assertEquals(List.class, documentServiceQueryMethod.getReturnType());
}
@Test @Test
void ragSourceTypesAndDocumentRelationShouldExist() throws NoSuchFieldException { void ragSourceTypesAndDocumentRelationShouldExist() throws NoSuchFieldException {
Field storeIdField = RagDocument.class.getDeclaredField("storeId"); Field storeIdField = RagDocument.class.getDeclaredField("storeId");