feat(admin): paginate tools list endpoint and console UI

- GET /tools returns ToolPageView{total,page,pageSize,records}, pageSize=0 keeps full-list mode
- App.vue adds pager below tools table; scope/audit/category pickers use allTools
- add backend pagination tests and front-end pager contract test
This commit is contained in:
my_local_kimi_dev2
2026-09-16 09:52:19 +08:00
parent 6e8cf60fd3
commit a575736c81
4 changed files with 80 additions and 18 deletions
@@ -162,16 +162,24 @@ public class AdminConsoleController {
return tokenService.rotate(id, adminId(session));
}
/** pageSize=0 表示不分页返回全量,供作用域配置、审计筛选等需要完整工具列表的场景使用。 */
@GetMapping("/tools")
public List<ToolView> tools(@RequestParam(defaultValue = "") String category,
@RequestParam(defaultValue = "") String kind,
@RequestParam(defaultValue = "") String keyword) {
public ToolPageView tools(@RequestParam(defaultValue = "") String category,
@RequestParam(defaultValue = "") String kind,
@RequestParam(defaultValue = "") String keyword,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "20") int pageSize) {
int pageNo = Math.max(page, 1);
int size = pageSize == 0 ? 0 : pageSize < 1 ? 20 : Math.min(pageSize, 100);
LambdaQueryWrapper<Tool> query = new LambdaQueryWrapper<Tool>().eq(Tool::getSoftDelete, 0);
if (!category.isBlank()) query.eq(Tool::getCategory, category);
if (!kind.isBlank()) query.eq(Tool::getKind, kind);
if (!keyword.isBlank()) query.and(w -> w.like(Tool::getName, keyword.trim())
.or().like(Tool::getDescription, keyword.trim()));
List<Tool> tools = toolMapper.selectList(query.orderByAsc(Tool::getCategory, Tool::getName));
long total = toolMapper.selectCount(query);
LambdaQueryWrapper<Tool> paged = query.orderByAsc(Tool::getCategory, Tool::getName);
if (size > 0) paged.last("LIMIT " + (long) (pageNo - 1) * size + "," + size);
List<Tool> tools = toolMapper.selectList(paged);
Map<Long, AgentScope> scopes = scopeMapper.selectList(new LambdaQueryWrapper<AgentScope>()
.eq(AgentScope::getSoftDelete, 0)).stream()
.collect(Collectors.toMap(AgentScope::getId, Function.identity()));
@@ -180,9 +188,10 @@ public class AdminConsoleController {
.filter(r -> scopes.containsKey(r.getScopeId()))
.collect(Collectors.groupingBy(ScopeTool::getToolId,
Collectors.mapping(r -> scopes.get(r.getScopeId()).getName(), Collectors.toList())));
return tools.stream().map(tool -> new ToolView(tool.getId(), tool.getName(), tool.getCategory(),
tool.getDescription(), tool.getKind(), tool.getStatus(), tool.getModifyTime(),
List<ToolView> records = tools.stream().map(tool -> new ToolView(tool.getId(), tool.getName(),
tool.getCategory(), tool.getDescription(), tool.getKind(), tool.getStatus(), tool.getModifyTime(),
scopeNames.getOrDefault(tool.getId(), List.of()))).toList();
return new ToolPageView(total, size > 0 ? pageNo : 1, size, records);
}
@PatchMapping("/tools/{id}/status")
@@ -397,6 +406,7 @@ public class AdminConsoleController {
public record StatusRequest(byte status) {}
public record ToolView(Long id, String name, String category, String description, String kind,
Byte status, Date modifyTime, List<String> scopeNames) {}
public record ToolPageView(long total, int page, int pageSize, List<ToolView> records) {}
public record ScopeRequest(String name, String code, String memo) {}
public record ScopeView(Long id, String name, String code, String memo, Byte status,
Date modifyTime, long toolCount) {}
@@ -46,11 +46,15 @@ class AdminConsoleControllerTest {
new org.apache.ibatis.builder.MapperBuilderAssistant(
new com.baomidou.mybatisplus.core.MybatisConfiguration(), ""),
me.iiv.agent.entity.m0.User.class);
com.baomidou.mybatisplus.core.metadata.TableInfoHelper.initTableInfo(
new org.apache.ibatis.builder.MapperBuilderAssistant(
new com.baomidou.mybatisplus.core.MybatisConfiguration(), ""),
me.iiv.agent.entity.svc.Tool.class);
controller = new AdminConsoleController(userMapper, authorityMapper, agentMapper, tokenMapper,
scopeMapper, toolMapper, scopeToolMapper, auditMapper, tokenScopeMapper,
permissionService, tokenService);
when(userMapper.selectCount(any())).thenReturn(0L);
when(userMapper.selectList(any())).thenReturn(List.of());
org.mockito.Mockito.lenient().when(userMapper.selectCount(any())).thenReturn(0L);
org.mockito.Mockito.lenient().when(userMapper.selectList(any())).thenReturn(List.of());
}
@Test
@@ -78,4 +82,33 @@ class AdminConsoleControllerTest {
assertTrue(sql.contains("enterprise_type"), "用户列表必须过滤物业账号: " + sql);
assertTrue(sql.contains("IS NULL"), "历史 NULL 数据需保留: " + sql);
}
@Test
void toolsClampsPageSizeToHundred() {
when(toolMapper.selectCount(any())).thenReturn(30L);
when(toolMapper.selectList(any())).thenReturn(List.of());
when(scopeMapper.selectList(any())).thenReturn(List.of());
when(scopeToolMapper.selectList(any())).thenReturn(List.of());
AdminConsoleController.ToolPageView view = controller.tools("", "", "", 1, 500);
assertEquals(100, view.pageSize());
assertEquals(1, view.page());
assertEquals(30, view.total());
}
@Test
void toolsPageSizeZeroReturnsAllWithoutLimit() {
when(toolMapper.selectCount(any())).thenReturn(30L);
when(toolMapper.selectList(any())).thenReturn(List.of());
when(scopeMapper.selectList(any())).thenReturn(List.of());
when(scopeToolMapper.selectList(any())).thenReturn(List.of());
AdminConsoleController.ToolPageView view = controller.tools("", "", "", 3, 0);
assertEquals(0, view.pageSize());
assertEquals(1, view.page());
assertEquals(30, view.total());
org.mockito.ArgumentCaptor<com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<me.iiv.agent.entity.svc.Tool>>
captor = org.mockito.ArgumentCaptor.forClass(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper.class);
org.mockito.Mockito.verify(toolMapper).selectList(captor.capture());
assertTrue(captor.getValue().getSqlSegment() == null || !captor.getValue().getSqlSegment().contains("LIMIT"),
"全量模式不应带 LIMIT: " + captor.getValue().getSqlSegment());
}
}