feat(公司tab): 接线 生成记录——company_id 走 DataReadScope,空成员集一律空页
「tab 画出来了、请求真带了 company_id、后端读都不读」是撒谎窗口,比不接更坏: 页面标题写某公司、数据仍是全量。所以后半页必须同时落地读侧与前端。 - controller 收显式 company_id,菜单闸门仍在算范围之前;范围只在 DataReadScope 一处出口(不再有第二个 if)。DELETE /history 与 clearAllHistory 一字未动: 写操作不吃 company_id。 - service 的总数与行集共用同一个 filterOf,"同范围"由结构保证;空成员集在 分页归一之后早退,绝不把 [] 交给 in()(渲染成 IN () 就是 500)。 - 前端从原始 params 读 companyId——normalizeHistoryParams 是白名单构造,从 normalized 读会静默拿到 undefined,编译绿、请求里没有参数。 验证:ImageHistoryServiceTest 7/0,后端全量 3663 例 0 红;G1 缺口清单 7 页 → 6 页(admin_history 消失);前端 1726/1725/1(那条红是刻意留的接线缺口尺子)、 vue-tsc rc=0。变异 M-A(删 filter.in)与 M-B(早退改永假)各杀 2 支。
This commit is contained in:
+8
-2
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.imagehistory.controller;
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.service.DataReadScope;
|
||||
import com.nanri.aiimage.modules.imagehistory.model.vo.ImageHistoryListVo;
|
||||
import com.nanri.aiimage.modules.imagehistory.service.ImageHistoryService;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
@@ -28,6 +29,7 @@ public class ImageHistoryController {
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final ImageHistoryService imageHistoryService;
|
||||
private final PermissionMenuService permissionMenuService;
|
||||
private final DataReadScope dataReadScope;
|
||||
|
||||
@GetMapping("/history")
|
||||
@Operation(summary = "分页查询所有用户生成历史")
|
||||
@@ -36,12 +38,16 @@ public class ImageHistoryController {
|
||||
@RequestParam(name = "page_size", required = false, defaultValue = "15") Integer pageSize,
|
||||
@RequestParam(name = "user_id", required = false) Long userId,
|
||||
@RequestParam(name = "time_start", required = false) String timeStart,
|
||||
@RequestParam(name = "time_end", required = false) String timeEnd) {
|
||||
@RequestParam(name = "time_end", required = false) String timeEnd,
|
||||
@RequestParam(name = "company_id", required = false) Long companyId) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
if (!permissionMenuService.hasAnyAdminMenu(operator, List.of("admin_history"))) {
|
||||
throw new BusinessException(403, "无权访问生成记录模块");
|
||||
}
|
||||
ImageHistoryListVo vo = imageHistoryService.listHistory(page, pageSize, userId, timeStart, timeEnd);
|
||||
// 既有语义保留:显式传 user_id 时按那一个人筛;公司 tab 叠在它上面(交集)。
|
||||
List<Long> base = userId != null && userId > 0 ? List.of(userId) : null;
|
||||
List<Long> scoped = dataReadScope.readScope(operator, companyId).userIdsOr(base);
|
||||
ImageHistoryListVo vo = imageHistoryService.listHistory(page, pageSize, userId, timeStart, timeEnd, scoped);
|
||||
return ApiResponse.success(vo);
|
||||
}
|
||||
|
||||
|
||||
+25
-5
@@ -43,19 +43,31 @@ public class ImageHistoryService {
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public ImageHistoryListVo listHistory(Integer page, Integer pageSize, Long userId,
|
||||
String timeStart, String timeEnd) {
|
||||
String timeStart, String timeEnd, List<Long> userIds) {
|
||||
int safePage = page == null || page < 1 ? 1 : page;
|
||||
int safeSize = pageSize == null ? 15 : pageSize;
|
||||
if (safeSize < 10) safeSize = 10;
|
||||
if (safeSize > 50) safeSize = 50;
|
||||
|
||||
if (userIds != null && userIds.isEmpty()) {
|
||||
// 空集 = 这个公司在这页没有任何成员账号 ⇒ 空页。绝不退化成"不筛"(那是越权);
|
||||
// 也绝不把 [] 交给 filterOf:MP 的 in(boolean, …) 只看 condition 不看集合空不空,
|
||||
// [] 会渲染成 `IN ()`(MySQL 语法错 → 500)。两条都由 ImageHistoryServiceTest 钉着。
|
||||
ImageHistoryListVo empty = new ImageHistoryListVo();
|
||||
empty.setItems(List.of());
|
||||
empty.setTotal(0L);
|
||||
empty.setPage(safePage);
|
||||
empty.setPageSize(safeSize);
|
||||
return empty;
|
||||
}
|
||||
|
||||
LocalDateTime startDt = parseDateTime(timeStart, false);
|
||||
LocalDateTime endDt = parseDateTime(timeEnd, true);
|
||||
|
||||
Long total = imageHistoryMapper.selectCount(filterOf(userId, startDt, endDt));
|
||||
Long total = imageHistoryMapper.selectCount(filterOf(userId, userIds, startDt, endDt));
|
||||
|
||||
// 先只取本页 id(仅排 id/时间小列,避免对大文本 filesort 撑爆 sort buffer),再按 id 取行。
|
||||
LambdaQueryWrapper<ImageHistoryEntity> idPage = filterOf(userId, startDt, endDt)
|
||||
LambdaQueryWrapper<ImageHistoryEntity> idPage = filterOf(userId, userIds, startDt, endDt)
|
||||
.select(ImageHistoryEntity::getId)
|
||||
.orderByDesc(ImageHistoryEntity::getCreatedAt);
|
||||
int offset = (safePage - 1) * safeSize;
|
||||
@@ -89,12 +101,20 @@ public class ImageHistoryService {
|
||||
return removed;
|
||||
}
|
||||
|
||||
/** 按用户/时间范围构造筛选条件(不排序、不分页)。 */
|
||||
private LambdaQueryWrapper<ImageHistoryEntity> filterOf(Long userId, LocalDateTime startDt, LocalDateTime endDt) {
|
||||
/**
|
||||
* 按用户/公司成员集/时间范围构造筛选条件(不排序、不分页)。
|
||||
*
|
||||
* <p>总数与 id 页共用它,所以两条天然同范围。</p>
|
||||
*/
|
||||
private LambdaQueryWrapper<ImageHistoryEntity> filterOf(Long userId, List<Long> userIds,
|
||||
LocalDateTime startDt, LocalDateTime endDt) {
|
||||
LambdaQueryWrapper<ImageHistoryEntity> filter = new LambdaQueryWrapper<>();
|
||||
if (userId != null && userId > 0) {
|
||||
filter.eq(ImageHistoryEntity::getUserId, userId);
|
||||
}
|
||||
// userIds 由 controller 经 DataReadScope 算好:null = 不筛(超管没选公司,今天的行为),
|
||||
// 非 null = 只查这批账号。空集在 listHistory 已拦,这里不会收到 []。
|
||||
filter.in(userIds != null, ImageHistoryEntity::getUserId, userIds);
|
||||
if (startDt != null) {
|
||||
filter.ge(ImageHistoryEntity::getCreatedAt, startDt);
|
||||
}
|
||||
|
||||
+137
@@ -1,19 +1,42 @@
|
||||
package com.nanri.aiimage.modules.imagehistory.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.service.ReadScope;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.modules.imagehistory.mapper.ImageHistoryMapper;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.modules.imagehistory.model.entity.ImageHistoryEntity;
|
||||
import com.nanri.aiimage.modules.imagehistory.model.vo.ImageHistoryListVo;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import java.util.List;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** 验收反馈:生成记录数据要可清理——DELETE 清空全表并返回清理条数。 */
|
||||
class ImageHistoryServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// Lambda 列名(user_id/created_at)要从实体元数据里查;纯单测没有 Spring/MyBatis 装配,
|
||||
// 不初始化就会在 wrapper 上抛 "can not find lambda cache"。口径同 AdminUserListCompanyTest:47。
|
||||
TableInfoHelper.initTableInfo(
|
||||
new MapperBuilderAssistant(new MybatisConfiguration(), ""), ImageHistoryEntity.class);
|
||||
TableInfoHelper.initTableInfo(
|
||||
new MapperBuilderAssistant(new MybatisConfiguration(), ""), AdminUserEntity.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void clearAllHistoryDeletesAndReturnsCount() {
|
||||
ImageHistoryMapper historyMapper = mock(ImageHistoryMapper.class);
|
||||
@@ -39,4 +62,118 @@ class ImageHistoryServiceTest {
|
||||
assertThat(service.clearAllHistory("admin")).isZero();
|
||||
verify(historyMapper).delete(any());
|
||||
}
|
||||
|
||||
// ---------- 公司 tab 的成员集合怎么进这条查询(Task 8 后半页) ----------
|
||||
//
|
||||
// 范围在 controller 里由 DataReadScope 算,service 只收算好的 List<Long> userIds:
|
||||
// null = 不筛(超管没选公司,保持今天的行为),空集 = 这家公司没有成员账号(必须空页)。
|
||||
|
||||
@Test
|
||||
@DisplayName("总数与 id 页必须拿到同一个成员集合——只改一处就是「总数对不上但没人报错」")
|
||||
@SuppressWarnings("unchecked")
|
||||
void memberSetGoesToBothCountAndIdPageWrappers() {
|
||||
ImageHistoryMapper historyMapper = mock(ImageHistoryMapper.class);
|
||||
AdminUserMapper userMapper = mock(AdminUserMapper.class);
|
||||
ImageHistoryService service = new ImageHistoryService(historyMapper, userMapper, new ObjectMapper());
|
||||
when(historyMapper.selectCount(any())).thenReturn(7L);
|
||||
when(historyMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
ImageHistoryListVo vo = service.listHistory(1, 15, null, null, null, List.of(11L, 12L));
|
||||
|
||||
ArgumentCaptor<LambdaQueryWrapper<ImageHistoryEntity>>
|
||||
countCap = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
||||
ArgumentCaptor<LambdaQueryWrapper<ImageHistoryEntity>>
|
||||
rowsCap = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
||||
verify(historyMapper).selectCount(countCap.capture());
|
||||
verify(historyMapper).selectList(rowsCap.capture());
|
||||
|
||||
// 先碰一次 SqlSegment 才会铺出参数(MP 惯例,口径同 AdminUserServiceCompanySuperAdminTest:167)。
|
||||
String countSql = countCap.getValue().getSqlSegment();
|
||||
String rowsSql = rowsCap.getValue().getSqlSegment();
|
||||
assertThat(countSql).contains("user_id IN");
|
||||
assertThat(rowsSql).contains("user_id IN");
|
||||
assertThat(countCap.getValue().getParamNameValuePairs().values()).containsExactlyInAnyOrder(11L, 12L);
|
||||
assertThat(rowsCap.getValue().getParamNameValuePairs().values()).containsExactlyInAnyOrder(11L, 12L);
|
||||
// 那条总数确实是这条页的总数(不是另算一次的别的范围)。
|
||||
assertThat(vo.getTotal()).isEqualTo(7L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("成员集为空=空页:两支 mapper 一支都不许碰,绝不把 [] 交给 wrapper(IN () 是 500)")
|
||||
void emptyMemberSetReturnsEmptyPageWithoutTouchingMapper() {
|
||||
ImageHistoryMapper historyMapper = mock(ImageHistoryMapper.class);
|
||||
AdminUserMapper userMapper = mock(AdminUserMapper.class);
|
||||
ImageHistoryService service = new ImageHistoryService(historyMapper, userMapper, new ObjectMapper());
|
||||
|
||||
ImageHistoryListVo vo = service.listHistory(3, 20, null, null, null, List.of());
|
||||
|
||||
assertThat(vo.getItems()).isEmpty();
|
||||
assertThat(vo.getTotal()).isZero();
|
||||
assertThat(vo.getPage()).isEqualTo(3);
|
||||
assertThat(vo.getPageSize()).isEqualTo(20);
|
||||
verify(historyMapper, never()).selectCount(any());
|
||||
verify(historyMapper, never()).selectList(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("不筛时传 null: wrapper 上根本不该出现 user_id IN")
|
||||
@SuppressWarnings("unchecked")
|
||||
void unfilteredPassesNullAndRendersNoInClause() {
|
||||
ImageHistoryMapper historyMapper = mock(ImageHistoryMapper.class);
|
||||
AdminUserMapper userMapper = mock(AdminUserMapper.class);
|
||||
ImageHistoryService service = new ImageHistoryService(historyMapper, userMapper, new ObjectMapper());
|
||||
when(historyMapper.selectCount(any())).thenReturn(0L);
|
||||
when(historyMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
service.listHistory(1, 15, null, null, null, null);
|
||||
|
||||
ArgumentCaptor<LambdaQueryWrapper<ImageHistoryEntity>>
|
||||
countCap = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
||||
ArgumentCaptor<LambdaQueryWrapper<ImageHistoryEntity>>
|
||||
rowsCap = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
||||
verify(historyMapper).selectCount(countCap.capture());
|
||||
verify(historyMapper).selectList(rowsCap.capture());
|
||||
assertThat(countCap.getValue().getSqlSegment()).doesNotContain("user_id IN");
|
||||
assertThat(rowsCap.getValue().getSqlSegment()).doesNotContain("user_id IN");
|
||||
assertThat(countCap.getValue().getParamNameValuePairs()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("非超管请求别的公司:readScope 给空集,进这页只能落空页,退化成「不筛」就是越权")
|
||||
void crossCompanyEmptyScopeBecomesEmptyPageNotUnfiltered() {
|
||||
ImageHistoryMapper historyMapper = mock(ImageHistoryMapper.class);
|
||||
AdminUserMapper userMapper = mock(AdminUserMapper.class);
|
||||
ImageHistoryService service = new ImageHistoryService(historyMapper, userMapper, new ObjectMapper());
|
||||
|
||||
// DataReadScope 的越界分支给的就是 ReadScope.users(List.of())(空集),controller 只做 userIdsOr(base)
|
||||
// 之后原样传进来;这里连 base 都不给,就是「非超管 + 请求了别家」那一条。
|
||||
List<Long> scoped = ReadScope.users(List.of()).userIdsOr(null);
|
||||
assertThat(scoped).isEmpty();
|
||||
|
||||
ImageHistoryListVo vo = service.listHistory(1, 15, null, null, null, scoped);
|
||||
|
||||
assertThat(vo.getItems()).isEmpty();
|
||||
assertThat(vo.getTotal()).isZero();
|
||||
verify(historyMapper, never()).selectCount(any());
|
||||
verify(historyMapper, never()).selectList(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("显式 user_id 与公司成员集是叠加不是替换:同一条 wrapper 上既有 = 也有 IN")
|
||||
@SuppressWarnings("unchecked")
|
||||
void explicitUserIdStacksWithMemberSetInsteadOfReplacingIt() {
|
||||
ImageHistoryMapper historyMapper = mock(ImageHistoryMapper.class);
|
||||
AdminUserMapper userMapper = mock(AdminUserMapper.class);
|
||||
ImageHistoryService service = new ImageHistoryService(historyMapper, userMapper, new ObjectMapper());
|
||||
when(historyMapper.selectCount(any())).thenReturn(0L);
|
||||
when(historyMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
service.listHistory(1, 15, 9L, null, null, List.of(11L, 12L));
|
||||
|
||||
ArgumentCaptor<LambdaQueryWrapper<ImageHistoryEntity>>
|
||||
cap = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
||||
verify(historyMapper).selectCount(cap.capture());
|
||||
assertThat(cap.getValue().getSqlSegment()).contains("user_id IN");
|
||||
assertThat(cap.getValue().getParamNameValuePairs().values()).contains(9L, 11L, 12L);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user