task-278(验收反馈): 生成记录加清理数据功能(DELETE /api/admin/history 二次确认+条数提示;含工作区在途的 imagehistory 分页优化)

This commit is contained in:
2026-09-06 02:25:44 +08:00
parent 6d9b4a13ee
commit 648935e757
5 changed files with 140 additions and 15 deletions
@@ -11,6 +11,7 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@@ -43,4 +44,15 @@ public class ImageHistoryController {
ImageHistoryListVo vo = imageHistoryService.listHistory(page, pageSize, userId, timeStart, timeEnd);
return ApiResponse.success(vo);
}
@DeleteMapping("/history")
@Operation(summary = "清空全部生成记录")
public ApiResponse<Long> clearHistory(HttpServletRequest request) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
if (!permissionMenuService.hasAnyAdminMenu(operator, List.of("admin_history"))) {
throw new BusinessException(403, "无权访问生成记录模块");
}
long removed = imageHistoryService.clearAllHistory(operator.getUsername());
return ApiResponse.success(removed);
}
}
@@ -49,24 +49,21 @@ public class ImageHistoryService {
if (safeSize < 10) safeSize = 10;
if (safeSize > 50) safeSize = 50;
LambdaQueryWrapper<ImageHistoryEntity> query = new LambdaQueryWrapper<>();
if (userId != null && userId > 0) {
query.eq(ImageHistoryEntity::getUserId, userId);
}
LocalDateTime startDt = parseDateTime(timeStart, false);
if (startDt != null) {
query.ge(ImageHistoryEntity::getCreatedAt, startDt);
}
LocalDateTime endDt = parseDateTime(timeEnd, true);
if (endDt != null) {
query.le(ImageHistoryEntity::getCreatedAt, endDt);
}
Long total = imageHistoryMapper.selectCount(query);
query.orderByDesc(ImageHistoryEntity::getCreatedAt);
Long total = imageHistoryMapper.selectCount(filterOf(userId, startDt, endDt));
// 先只取本页 id(仅排 id/时间小列,避免对大文本 filesort 撑爆 sort buffer),再按 id 取行。
LambdaQueryWrapper<ImageHistoryEntity> idPage = filterOf(userId, startDt, endDt)
.select(ImageHistoryEntity::getId)
.orderByDesc(ImageHistoryEntity::getCreatedAt);
int offset = (safePage - 1) * safeSize;
query.last("LIMIT " + safeSize + " OFFSET " + offset);
List<ImageHistoryEntity> rows = imageHistoryMapper.selectList(query);
idPage.last("LIMIT " + safeSize + " OFFSET " + offset);
List<Long> pageIds = imageHistoryMapper.selectList(idPage).stream()
.map(ImageHistoryEntity::getId)
.toList();
List<ImageHistoryEntity> rows = rowsByIds(pageIds);
Map<Long, String> usernameMap = loadUsernameMap(rows);
@@ -83,6 +80,50 @@ public class ImageHistoryService {
return vo;
}
/** 清空全部生成记录(验收反馈:生成记录数据要可清理)。返回清理条数。 */
public long clearAllHistory(String operatorName) {
Long count = imageHistoryMapper.selectCount(new LambdaQueryWrapper<>());
long removed = count == null ? 0L : count;
imageHistoryMapper.delete(new LambdaQueryWrapper<>());
log.info("[image-history] 管理员 {} 清空生成记录,共清理 {} 条", operatorName == null ? "unknown" : operatorName, removed);
return removed;
}
/** 按用户/时间范围构造筛选条件(不排序、不分页)。 */
private LambdaQueryWrapper<ImageHistoryEntity> filterOf(Long userId, LocalDateTime startDt, LocalDateTime endDt) {
LambdaQueryWrapper<ImageHistoryEntity> filter = new LambdaQueryWrapper<>();
if (userId != null && userId > 0) {
filter.eq(ImageHistoryEntity::getUserId, userId);
}
if (startDt != null) {
filter.ge(ImageHistoryEntity::getCreatedAt, startDt);
}
if (endDt != null) {
filter.le(ImageHistoryEntity::getCreatedAt, endDt);
}
return filter;
}
/** 按本页 id 顺序回表取完整行(id 集合小,避免对全量大文本字段排序)。 */
private List<ImageHistoryEntity> rowsByIds(List<Long> ids) {
if (ids == null || ids.isEmpty()) {
return Collections.emptyList();
}
List<ImageHistoryEntity> fetched = imageHistoryMapper.selectBatchIds(ids);
Map<Long, ImageHistoryEntity> byId = new HashMap<>(fetched.size() * 2);
for (ImageHistoryEntity row : fetched) {
byId.put(row.getId(), row);
}
List<ImageHistoryEntity> ordered = new ArrayList<>(ids.size());
for (Long id : ids) {
ImageHistoryEntity row = byId.get(id);
if (row != null) {
ordered.add(row);
}
}
return ordered;
}
private Map<Long, String> loadUsernameMap(List<ImageHistoryEntity> rows) {
Set<Long> userIds = new LinkedHashSet<>();
for (ImageHistoryEntity r : rows) {