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:
2026-10-05 08:32:40 +08:00
parent 4a8c05329a
commit 83a444def6
6 changed files with 186 additions and 9 deletions
@@ -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);
}
@@ -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);
}