feat(taskcenter): 两表 UNION 任务列表 + 筛选 + 店铺/进度装配
- biz_file_task(12 个客户端模块)∪ brand_crawl_tasks,品牌任务号合成 brand-<id>、 终态用 updated_at 充当 finished_at;子查询内下推过滤、外层排序分页 - 筛选:账号名(users LIKE → ∩ 可见账号)、业务、状态、设备、店铺(4 个来源 EXISTS)、 创建时间范围;选具体业务时品牌分支恒空,带店铺筛选时品牌分支直接排除(防撞号误判) - 状态口径单一来源:SQL 字面量与 Java 集合由测试守同步,服务层判「卡住」复用同一集合 - 店铺来源优先级链:上架结果文件 → 任务结果分组 → 结果文件名 → 快照 scope → 请求体 items[].shopName(最后一级批量一次拉取,列表主查询不拉大 JSON 列);品牌行店铺恒空 - 进度:快照(按 moduleType+taskId 匹配,防品牌 id 撞号)优先,无快照用行内文件计数 Tests run: 42, Failures: 0, Errors: 0
This commit is contained in:
+11
@@ -4,8 +4,10 @@ import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskQuery;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.vo.TaskCenterDeviceVo;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.vo.TaskCenterOverviewVo;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.vo.TaskCenterTaskPageVo;
|
||||
import com.nanri.aiimage.modules.taskcenter.service.TaskCenterQueryService;
|
||||
import com.nanri.aiimage.modules.taskcenter.service.TaskCenterScopeService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@@ -52,6 +54,15 @@ public class AdminTaskCenterController {
|
||||
return ApiResponse.success(queryService.devices(scope));
|
||||
}
|
||||
|
||||
@GetMapping("/tasks")
|
||||
@Operation(summary = "任务分页列表",
|
||||
description = "biz_file_task(12 个客户端模块)∪ brand_crawl_tasks 按创建时间倒序;"
|
||||
+ "店铺跨 4 个来源模糊匹配;进行中且 >10 分钟无更新标记 stuck。")
|
||||
public ApiResponse<TaskCenterTaskPageVo> tasks(HttpServletRequest request, TaskCenterTaskQuery query) {
|
||||
TaskCenterScopeService.ScopeContext scope = resolveScope(request);
|
||||
return ApiResponse.success(queryService.tasks(scope, query));
|
||||
}
|
||||
|
||||
private TaskCenterScopeService.ScopeContext resolveScope(HttpServletRequest request) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
|
||||
permissionMenuService.requireClientTaskCenterAccess(operator);
|
||||
|
||||
+182
-6
@@ -1,12 +1,18 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.mapper;
|
||||
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterDeviceRow;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterShopRow;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterSnapshotRow;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskRow;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterUserRow;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 客户端任务管理(后台看板)只读查询。
|
||||
@@ -26,17 +32,32 @@ public interface TaskCenterQueryMapper {
|
||||
+ "'SHOP_MATCH','PATROL_DELETE','QUERY_ASIN','WITHDRAW','APPEARANCE_PATENT',"
|
||||
+ "'SIMILAR_ASIN','COLLECT_DATA','SHOP_DATA_CRAWL'";
|
||||
|
||||
/** biz_file_task 进行中状态。 */
|
||||
String ACTIVE_STATUSES = "'PENDING','RUNNING','SCHEDULED'";
|
||||
/**
|
||||
* 状态口径的单一来源。{@code @Select} 注解值必须是编译期常量,所以 SQL 里用下面
|
||||
* 的字符串字面量、Java 侧判卡住用这里的集合,两者由
|
||||
* {@code TaskCenterQueryMapperSqlTest.statusLiteralsStayInSyncWithSets} 守同步。
|
||||
*/
|
||||
Set<String> ACTIVE_STATUS_SET = Set.of("PENDING", "RUNNING", "SCHEDULED");
|
||||
|
||||
/** biz_file_task 终态。 */
|
||||
String TERMINAL_STATUSES = "'SUCCESS','FAILED','CANCELLED','CANCELED'";
|
||||
/** biz_file_task 终态(CANCELLED 与历史脏值 CANCELED 并存)。 */
|
||||
Set<String> TERMINAL_STATUS_SET = Set.of("SUCCESS", "FAILED", "CANCELLED", "CANCELED");
|
||||
|
||||
/** brand_crawl_tasks 进行中(该表状态小写)。 */
|
||||
String BRAND_ACTIVE_STATUSES = "'pending','running'";
|
||||
Set<String> BRAND_ACTIVE_STATUS_SET = Set.of("pending", "running");
|
||||
|
||||
/** brand_crawl_tasks 终态。 */
|
||||
String BRAND_TERMINAL_STATUSES = "'success','failed','cancelled'";
|
||||
Set<String> BRAND_TERMINAL_STATUS_SET = Set.of("success", "failed", "cancelled");
|
||||
|
||||
String ACTIVE_STATUSES = "'PENDING','RUNNING','SCHEDULED'";
|
||||
String TERMINAL_STATUSES = "'CANCELED','CANCELLED','FAILED','SUCCESS'";
|
||||
String BRAND_ACTIVE_STATUSES = "'pending','running'";
|
||||
String BRAND_TERMINAL_STATUSES = "'cancelled','failed','success'";
|
||||
|
||||
/** 拼 SQL IN 列表;排序保证渲染结果稳定(测试与日志可断言)。 */
|
||||
static String sqlList(Set<String> values) {
|
||||
return values.stream().sorted().map(value -> "'" + value + "'")
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
/**
|
||||
* 任务设备原始行:近 N 天创建过任务、设备号非空(biz_file_task ∪ brand_crawl_tasks)。
|
||||
@@ -180,4 +201,159 @@ public interface TaskCenterQueryMapper {
|
||||
+ " GROUP BY u.deviceId"
|
||||
+ "</script>")
|
||||
List<TaskCenterDeviceRow> countRunningTasksByDevice(@Param("userIds") List<Long> userIds);
|
||||
|
||||
/** 状态归一化:CANCELED 并入 CANCELLED(历史脏值),前端只认一套。 */
|
||||
String STATUS_T = "CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END";
|
||||
|
||||
/** 品牌表状态本是小写,统一 UPPER 输出。 */
|
||||
String STATUS_B = "CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END";
|
||||
|
||||
/**
|
||||
* 店铺筛选:四个来源任一命中即可(去重靠 EXISTS,不需要 JOIN 放大行数)。
|
||||
* 四张表都有 task_id 索引,子查询由外层行驱动;request_json 兜底不参与筛选。
|
||||
*/
|
||||
String SHOP_FILTER_T = "<if test='shop != null'>"
|
||||
+ " AND (EXISTS (SELECT 1 FROM biz_publish_file p"
|
||||
+ " WHERE p.task_id = t.id AND p.shop_name LIKE CONCAT('%', #{shop}, '%'))"
|
||||
+ " OR EXISTS (SELECT 1 FROM biz_task_result_item i"
|
||||
+ " WHERE i.task_id = t.id AND i.scope_key LIKE CONCAT('%', #{shop}, '%'))"
|
||||
+ " OR EXISTS (SELECT 1 FROM biz_file_result f"
|
||||
+ " WHERE f.task_id = t.id AND f.source_filename LIKE CONCAT('%', #{shop}, '%'))"
|
||||
+ " OR EXISTS (SELECT 1 FROM biz_task_progress_snapshot s"
|
||||
+ " WHERE s.task_id = t.id AND s.current_scope_key LIKE CONCAT('%', #{shop}, '%')))"
|
||||
+ "</if>";
|
||||
|
||||
/** biz_file_task 分支筛选(账号范围 + 业务/状态/设备/时间范围 + 店铺)。 */
|
||||
String TASK_FILTER_T = "<if test='userIds != null'> AND t.user_id IN"
|
||||
+ " <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if>"
|
||||
+ "<if test='moduleType != null'> AND UPPER(t.module_type) = #{moduleType}</if>"
|
||||
+ "<if test='status != null'> AND " + STATUS_T + " = #{status}</if>"
|
||||
+ "<if test='deviceId != null'> AND t.device_id = #{deviceId}</if>"
|
||||
+ "<if test='startTime != null'> AND t.created_at >= #{startTime}</if>"
|
||||
+ "<if test='endTime != null'> AND t.created_at <= #{endTime}</if>"
|
||||
+ SHOP_FILTER_T;
|
||||
|
||||
/**
|
||||
* 品牌分支筛选:业务名固定 BRAND(选了别的业务则该分支恒空);品牌任务不存店铺,
|
||||
* 带店铺筛选时直接排除——否则品牌 id 与 biz id 撞号会被 EXISTS 误判命中。
|
||||
*/
|
||||
String TASK_FILTER_B = "<if test='userIds != null'> AND b.user_id IN"
|
||||
+ " <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if>"
|
||||
+ "<if test='moduleType != null'> AND 'BRAND' = #{moduleType}</if>"
|
||||
+ "<if test='status != null'> AND " + STATUS_B + " = #{status}</if>"
|
||||
+ "<if test='deviceId != null'> AND b.device_id = #{deviceId}</if>"
|
||||
+ "<if test='startTime != null'> AND b.created_at >= #{startTime}</if>"
|
||||
+ "<if test='endTime != null'> AND b.created_at <= #{endTime}</if>"
|
||||
+ "<if test='shop != null'> AND 1 = 0</if>";
|
||||
|
||||
/**
|
||||
* 任务列表行:biz_file_task(12 个客户端模块)∪ brand_crawl_tasks。
|
||||
* 品牌行 task_no 合成 {@code brand-<id>},终态时用 updated_at 充当 finished_at。
|
||||
*/
|
||||
String TASK_LIST_ROWS = "(SELECT t.id AS id, t.task_no AS taskNo, UPPER(t.module_type) AS moduleType,"
|
||||
+ " " + STATUS_T + " AS status, t.user_id AS userId, t.device_id AS deviceId,"
|
||||
+ " t.created_at AS createdAt, t.finished_at AS finishedAt, t.updated_at AS updatedAt,"
|
||||
+ " t.source_file_count AS totalCount, t.success_file_count AS successCount,"
|
||||
+ " t.failed_file_count AS failedCount, 0 AS brand"
|
||||
+ " FROM biz_file_task t"
|
||||
+ " WHERE t.module_type IN (" + CLIENT_MODULE_TYPES + ")"
|
||||
+ TASK_FILTER_T
|
||||
+ " UNION ALL"
|
||||
+ " SELECT b.id, CONCAT('brand-', b.id), 'BRAND', " + STATUS_B + ", b.user_id, b.device_id,"
|
||||
+ " b.created_at, CASE WHEN b.status IN (" + BRAND_TERMINAL_STATUSES + ")"
|
||||
+ " THEN b.updated_at END, b.updated_at,"
|
||||
+ " b.progress_total, b.progress_current, 0, 1"
|
||||
+ " FROM brand_crawl_tasks b"
|
||||
+ " WHERE 1 = 1"
|
||||
+ TASK_FILTER_B
|
||||
+ ")";
|
||||
|
||||
@Select("<script>"
|
||||
+ "SELECT m.* FROM " + TASK_LIST_ROWS + " m"
|
||||
+ " ORDER BY m.createdAt DESC, m.id DESC"
|
||||
+ " LIMIT #{limit} OFFSET #{offset}"
|
||||
+ "</script>")
|
||||
List<TaskCenterTaskRow> selectTaskPage(@Param("userIds") List<Long> userIds,
|
||||
@Param("moduleType") String moduleType,
|
||||
@Param("status") String status,
|
||||
@Param("deviceId") String deviceId,
|
||||
@Param("shop") String shop,
|
||||
@Param("startTime") LocalDateTime startTime,
|
||||
@Param("endTime") LocalDateTime endTime,
|
||||
@Param("limit") long limit,
|
||||
@Param("offset") long offset);
|
||||
|
||||
@Select("<script>"
|
||||
+ "SELECT COUNT(*) FROM " + TASK_LIST_ROWS + " m"
|
||||
+ "</script>")
|
||||
long countTaskPage(@Param("userIds") List<Long> userIds,
|
||||
@Param("moduleType") String moduleType,
|
||||
@Param("status") String status,
|
||||
@Param("deviceId") String deviceId,
|
||||
@Param("shop") String shop,
|
||||
@Param("startTime") LocalDateTime startTime,
|
||||
@Param("endTime") LocalDateTime endTime);
|
||||
|
||||
/** 账号名筛选 → 用户号(与可见账号集合求交由服务层负责)。 */
|
||||
@Select("SELECT id FROM users WHERE username LIKE CONCAT('%', #{keyword}, '%') LIMIT 500")
|
||||
List<Long> selectUserIdsByUsername(@Param("keyword") String keyword);
|
||||
|
||||
@Select("<script>"
|
||||
+ "SELECT id AS userId, username FROM users WHERE id IN"
|
||||
+ " <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach>"
|
||||
+ "</script>")
|
||||
List<TaskCenterUserRow> selectUsernames(@Param("userIds") List<Long> userIds);
|
||||
|
||||
/** 设备展示名(不限窗口:设备名与任务时间无关)。 */
|
||||
@Select("<script>"
|
||||
+ "SELECT d.device_id AS deviceId, MAX(d.device_name) AS deviceName"
|
||||
+ " FROM device_log_file d WHERE d.device_id IN"
|
||||
+ " <foreach collection='deviceIds' item='did' open='(' separator=',' close=')'>#{did}</foreach>"
|
||||
+ " GROUP BY d.device_id"
|
||||
+ "</script>")
|
||||
List<TaskCenterDeviceRow> selectDeviceNames(@Param("deviceIds") List<String> deviceIds);
|
||||
|
||||
/** 店铺来源一:上架结果文件(shop_name)。 */
|
||||
@Select("<script>"
|
||||
+ "SELECT p.task_id AS taskId, p.shop_name AS shop FROM biz_publish_file p"
|
||||
+ " WHERE p.task_id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>"
|
||||
+ " AND p.shop_name IS NOT NULL AND p.shop_name != ''"
|
||||
+ " GROUP BY p.task_id, p.shop_name"
|
||||
+ "</script>")
|
||||
List<TaskCenterShopRow> selectPublishShops(@Param("taskIds") List<Long> taskIds);
|
||||
|
||||
/** 店铺来源二:任务结果分组(scope_key)。 */
|
||||
@Select("<script>"
|
||||
+ "SELECT i.task_id AS taskId, i.scope_key AS shop FROM biz_task_result_item i"
|
||||
+ " WHERE i.task_id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>"
|
||||
+ " AND i.scope_key IS NOT NULL AND i.scope_key != ''"
|
||||
+ " GROUP BY i.task_id, i.scope_key"
|
||||
+ "</script>")
|
||||
List<TaskCenterShopRow> selectResultItemShops(@Param("taskIds") List<Long> taskIds);
|
||||
|
||||
/** 店铺来源三:结果文件名(跟价/定时匹配的文件名里带店铺)。 */
|
||||
@Select("<script>"
|
||||
+ "SELECT f.task_id AS taskId, f.source_filename AS shop FROM biz_file_result f"
|
||||
+ " WHERE f.task_id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>"
|
||||
+ " AND f.source_filename IS NOT NULL AND f.source_filename != ''"
|
||||
+ " GROUP BY f.task_id, f.source_filename"
|
||||
+ "</script>")
|
||||
List<TaskCenterShopRow> selectFileResultShops(@Param("taskIds") List<Long> taskIds);
|
||||
|
||||
/** 进度快照(同时充当店铺来源四:current_scope_key)。按 (taskId, moduleType) 由服务层匹配。 */
|
||||
@Select("<script>"
|
||||
+ "SELECT s.task_id AS taskId, s.module_type AS moduleType, s.total_count AS totalCount,"
|
||||
+ " s.success_count AS successCount, s.failed_count AS failedCount, s.pending_count AS pendingCount,"
|
||||
+ " s.current_scope_key AS currentScopeKey, s.message AS message"
|
||||
+ " FROM biz_task_progress_snapshot s"
|
||||
+ " WHERE s.task_id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>"
|
||||
+ "</script>")
|
||||
List<TaskCenterSnapshotRow> selectSnapshots(@Param("taskIds") List<Long> taskIds);
|
||||
|
||||
/** 店铺兜底:请求体 items[].shopName(只在前面四个来源都为空时按需拉取,避免列表查询带大 JSON 列)。 */
|
||||
@Select("<script>"
|
||||
+ "SELECT t.id AS id, t.request_json AS requestJson FROM biz_file_task t"
|
||||
+ " WHERE t.id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>"
|
||||
+ "</script>")
|
||||
List<TaskCenterTaskRow> selectRequestJson(@Param("taskIds") List<Long> taskIds);
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/** 任务 → 店铺候选行(publish_file.shop_name / result_item.scope_key / file_result.source_filename / 快照 scope)。 */
|
||||
@Data
|
||||
public class TaskCenterShopRow {
|
||||
|
||||
private Long taskId;
|
||||
private String shop;
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/** 进度快照行(biz_task_progress_snapshot),按 (taskId, moduleType) 与列表行匹配。 */
|
||||
@Data
|
||||
public class TaskCenterSnapshotRow {
|
||||
|
||||
private Long taskId;
|
||||
private String moduleType;
|
||||
private Long totalCount;
|
||||
private Long successCount;
|
||||
private Long failedCount;
|
||||
private Long pendingCount;
|
||||
private String currentScopeKey;
|
||||
private String message;
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** 任务列表查询条件(账号/业务/状态/设备/店铺/时间范围 + 分页)。 */
|
||||
@Data
|
||||
public class TaskCenterTaskQuery {
|
||||
|
||||
/** 账号名模糊匹配(users.username LIKE),命中后与可见账号求交。 */
|
||||
private String username;
|
||||
private String moduleType;
|
||||
/** 大写状态:PENDING/RUNNING/SCHEDULED/SUCCESS/FAILED/CANCELLED。 */
|
||||
private String status;
|
||||
private String deviceId;
|
||||
/** 店铺模糊匹配(跨 4 个来源的 EXISTS)。 */
|
||||
private String shop;
|
||||
/** 任务创建时间(含),ISO-8601。 */
|
||||
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
|
||||
private LocalDateTime startTime;
|
||||
/** 任务创建时间(含),ISO-8601。 */
|
||||
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
|
||||
private LocalDateTime endTime;
|
||||
private Long page;
|
||||
private Long pageSize;
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 任务列表行:两表 UNION 的扁平行(biz_file_task ∪ brand_crawl_tasks)。
|
||||
*
|
||||
* <p>{@code brand} 区分来源表:品牌任务 id 与 biz 表 id 可能撞号,快照匹配必须带上 moduleType。</p>
|
||||
*/
|
||||
@Data
|
||||
public class TaskCenterTaskRow {
|
||||
|
||||
private Long id;
|
||||
private String taskNo;
|
||||
private String moduleType;
|
||||
/** 已归一化的大写状态(CANCELED 归并到 CANCELLED)。 */
|
||||
private String status;
|
||||
private Long userId;
|
||||
private String deviceId;
|
||||
private LocalDateTime createdAt;
|
||||
/** 品牌表无 finished_at,终态时取 updated_at。 */
|
||||
private LocalDateTime finishedAt;
|
||||
private LocalDateTime updatedAt;
|
||||
/** 文件计数(biz 表 source/success/failed_file_count;品牌表 progress_total/progress_current)。 */
|
||||
private Long totalCount;
|
||||
private Long successCount;
|
||||
private Long failedCount;
|
||||
private boolean brand;
|
||||
/** 仅店铺兜底查询填充(列表主查询不拉大 JSON 列)。 */
|
||||
private String requestJson;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/** 账号展示名行(users)。 */
|
||||
@Data
|
||||
public class TaskCenterUserRow {
|
||||
|
||||
private Long userId;
|
||||
private String username;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 任务列表分页结果。 */
|
||||
@Data
|
||||
public class TaskCenterTaskPageVo {
|
||||
|
||||
private List<TaskCenterTaskRowVo> items;
|
||||
private long total;
|
||||
private long page;
|
||||
private long pageSize;
|
||||
|
||||
public static TaskCenterTaskPageVo empty(long page, long pageSize) {
|
||||
TaskCenterTaskPageVo vo = new TaskCenterTaskPageVo();
|
||||
vo.setItems(List.of());
|
||||
vo.setTotal(0);
|
||||
vo.setPage(page);
|
||||
vo.setPageSize(pageSize);
|
||||
return vo;
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** 任务列表行(客户端任务页签)。 */
|
||||
@Data
|
||||
public class TaskCenterTaskRowVo {
|
||||
|
||||
private Long id;
|
||||
private String taskNo;
|
||||
private String moduleType;
|
||||
/** 中文业务名(TaskModuleRegistry.labels())。 */
|
||||
private String moduleLabel;
|
||||
/** 大写状态;品牌任务同样大写输出。 */
|
||||
private String status;
|
||||
private Long userId;
|
||||
private String username;
|
||||
private String deviceId;
|
||||
/** device_log_file 的设备名;查不到时前端按 deviceId 截断显示。 */
|
||||
private String deviceName;
|
||||
private boolean webDevice;
|
||||
/** 店铺:主来源的第一个店铺名(多店铺时前端显示「共 N 个」)。 */
|
||||
private String shopName;
|
||||
private long shopCount;
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime finishedAt;
|
||||
private long progressTotal;
|
||||
private long progressSuccess;
|
||||
private long progressFailed;
|
||||
/** 当前处理对象(快照 current_scope_key)。 */
|
||||
private String currentScope;
|
||||
private String message;
|
||||
/** 进行中且超过 10 分钟无更新。 */
|
||||
private boolean stuck;
|
||||
}
|
||||
+302
@@ -1,9 +1,20 @@
|
||||
package com.nanri.aiimage.modules.taskcenter.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.module.TaskModuleRegistry;
|
||||
import com.nanri.aiimage.modules.taskcenter.mapper.TaskCenterQueryMapper;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterDeviceRow;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterShopRow;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterSnapshotRow;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskQuery;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskRow;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterUserRow;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.vo.TaskCenterDeviceVo;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.vo.TaskCenterOverviewVo;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.vo.TaskCenterTaskPageVo;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.vo.TaskCenterTaskRowVo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -11,9 +22,15 @@ import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 客户端任务管理的只读查询。时间口径:卡住 10 分钟、在线 5 分钟、
|
||||
@@ -27,6 +44,13 @@ public class TaskCenterQueryService {
|
||||
private static final long ONLINE_WINDOW_MINUTES = 5;
|
||||
private static final long TASK_DEVICE_WINDOW_DAYS = 30;
|
||||
private static final long LOG_DEVICE_WINDOW_DAYS = 7;
|
||||
private static final long DEFAULT_PAGE_SIZE = 20;
|
||||
private static final long MAX_PAGE_SIZE = 100;
|
||||
|
||||
/** 进行中状态与 mapper SQL 同源,避免「计数说在跑、卡住判定说不在跑」。 */
|
||||
private static final Set<String> ACTIVE_STATUSES = TaskCenterQueryMapper.ACTIVE_STATUS_SET;
|
||||
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
private final TaskCenterQueryMapper queryMapper;
|
||||
|
||||
@@ -94,4 +118,282 @@ public class TaskCenterQueryService {
|
||||
return vo;
|
||||
});
|
||||
}
|
||||
|
||||
/** 任务列表:两表 UNION 分页 + 当页行的账号/设备名/店铺/进度装配。 */
|
||||
public TaskCenterTaskPageVo tasks(TaskCenterScopeService.ScopeContext scope, TaskCenterTaskQuery query) {
|
||||
long page = query.getPage() == null || query.getPage() < 1 ? 1 : query.getPage();
|
||||
long pageSize = query.getPageSize() == null || query.getPageSize() < 1 ? DEFAULT_PAGE_SIZE
|
||||
: Math.min(query.getPageSize(), MAX_PAGE_SIZE);
|
||||
|
||||
List<Long> userIds = resolveUserIds(scope, query.getUsername());
|
||||
if (userIds != null && userIds.isEmpty()) {
|
||||
return TaskCenterTaskPageVo.empty(page, pageSize);
|
||||
}
|
||||
|
||||
long total = queryMapper.countTaskPage(userIds, query.getModuleType(), query.getStatus(),
|
||||
query.getDeviceId(), query.getShop(), query.getStartTime(), query.getEndTime());
|
||||
if (total <= 0) {
|
||||
return TaskCenterTaskPageVo.empty(page, pageSize);
|
||||
}
|
||||
|
||||
List<TaskCenterTaskRow> rows = queryMapper.selectTaskPage(userIds, query.getModuleType(),
|
||||
query.getStatus(), query.getDeviceId(), query.getShop(), query.getStartTime(),
|
||||
query.getEndTime(), pageSize, (page - 1) * pageSize);
|
||||
if (rows == null || rows.isEmpty()) {
|
||||
return TaskCenterTaskPageVo.empty(page, pageSize);
|
||||
}
|
||||
|
||||
TaskCenterTaskPageVo vo = new TaskCenterTaskPageVo();
|
||||
vo.setItems(assembleRows(rows, collectShops(rows)));
|
||||
vo.setTotal(total);
|
||||
vo.setPage(page);
|
||||
vo.setPageSize(pageSize);
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 账号名 → 可见账号集合:超管用命中的全部账号,非超管与可见集合求交。
|
||||
* 未传账号名时返回 {@code scope.sqlUserIds()}(超管为 null = 不裁剪)。
|
||||
*/
|
||||
private List<Long> resolveUserIds(TaskCenterScopeService.ScopeContext scope, String username) {
|
||||
if (username == null || username.isBlank()) {
|
||||
return scope.sqlUserIds();
|
||||
}
|
||||
List<Long> matched = safe(queryMapper.selectUserIdsByUsername(username.trim())).stream()
|
||||
.filter(Objects::nonNull).toList();
|
||||
if (matched.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
if (scope.allUsers()) {
|
||||
return matched;
|
||||
}
|
||||
List<Long> hit = matched.stream().filter(scope.visibleUserIds()::contains).toList();
|
||||
return hit.isEmpty() ? List.of() : hit;
|
||||
}
|
||||
|
||||
private List<TaskCenterTaskRowVo> assembleRows(List<TaskCenterTaskRow> rows, Map<Long, List<String>> shops) {
|
||||
List<String> deviceIds = rows.stream().map(TaskCenterTaskRow::getDeviceId)
|
||||
.filter(id -> id != null && !id.isBlank()).distinct().toList();
|
||||
List<Long> userIds = rows.stream().map(TaskCenterTaskRow::getUserId)
|
||||
.filter(id -> id != null && id > 0).distinct().toList();
|
||||
List<Long> rowIds = rows.stream().map(TaskCenterTaskRow::getId)
|
||||
.filter(Objects::nonNull).distinct().toList();
|
||||
|
||||
Map<Long, String> usernames = userIds.isEmpty() ? Map.of()
|
||||
: toUserNames(safe(queryMapper.selectUsernames(userIds)));
|
||||
Map<String, String> deviceNames = deviceIds.isEmpty() ? Map.of()
|
||||
: toDeviceNames(safe(queryMapper.selectDeviceNames(deviceIds)));
|
||||
Map<String, TaskCenterSnapshotRow> snapshots = rowIds.isEmpty() ? Map.of()
|
||||
: toSnapshots(safe(queryMapper.selectSnapshots(rowIds)));
|
||||
Map<Long, List<String>> requestShops = collectRequestShops(rows, shops, snapshots);
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
List<TaskCenterTaskRowVo> items = new ArrayList<>(rows.size());
|
||||
for (TaskCenterTaskRow row : rows) {
|
||||
items.add(toRowVo(row, usernames, deviceNames, snapshots, shops, requestShops, now));
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
private static Map<Long, String> toUserNames(List<TaskCenterUserRow> rows) {
|
||||
Map<Long, String> map = new HashMap<>();
|
||||
for (TaskCenterUserRow row : rows) {
|
||||
if (row.getUserId() != null) {
|
||||
map.put(row.getUserId(), row.getUsername());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private static Map<String, String> toDeviceNames(List<TaskCenterDeviceRow> rows) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
for (TaskCenterDeviceRow row : rows) {
|
||||
if (row.getDeviceId() != null && row.getDeviceName() != null) {
|
||||
map.put(row.getDeviceId(), row.getDeviceName());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private static Map<String, TaskCenterSnapshotRow> toSnapshots(List<TaskCenterSnapshotRow> rows) {
|
||||
Map<String, TaskCenterSnapshotRow> map = new HashMap<>();
|
||||
for (TaskCenterSnapshotRow row : rows) {
|
||||
if (row.getTaskId() != null && row.getModuleType() != null) {
|
||||
map.put(snapshotKey(row.getTaskId(), row.getModuleType()), row);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/** 品牌任务 id 与 biz 表 id 可能撞号,快照必须以 (moduleType, taskId) 为键。 */
|
||||
private static String snapshotKey(Long taskId, String moduleType) {
|
||||
if (taskId == null || moduleType == null) {
|
||||
return "";
|
||||
}
|
||||
return moduleType.toUpperCase(Locale.ROOT) + "#" + taskId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 店铺候选:按来源优先级依次取第一个非空的来源
|
||||
* (上架结果文件 → 任务结果分组 → 结果文件名 → 进度快照 → 请求体 items[].shopName)。
|
||||
* 与各模块的主来源一致,未列出的模块自动降级到下一来源。品牌行不取店铺。
|
||||
*/
|
||||
private Map<Long, List<String>> collectShops(List<TaskCenterTaskRow> rows) {
|
||||
List<Long> bizIds = rows.stream().filter(row -> !row.isBrand())
|
||||
.map(TaskCenterTaskRow::getId).filter(Objects::nonNull).distinct().toList();
|
||||
if (bizIds.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
Map<Long, List<String>> shops = new HashMap<>();
|
||||
lazyMerge(bizIds, shops, missing -> toShops(safe(queryMapper.selectPublishShops(missing))));
|
||||
lazyMerge(bizIds, shops, missing -> toShops(safe(queryMapper.selectResultItemShops(missing))));
|
||||
lazyMerge(bizIds, shops, missing -> toShops(safe(queryMapper.selectFileResultShops(missing))));
|
||||
return shops;
|
||||
}
|
||||
|
||||
/** 只对仍缺店铺的任务打这个来源的批量查询;已命中的任务保持更高优先级。 */
|
||||
private void lazyMerge(List<Long> ids, Map<Long, List<String>> target,
|
||||
Function<List<Long>, Map<Long, List<String>>> source) {
|
||||
List<Long> missing = ids.stream().filter(id -> !target.containsKey(id)).toList();
|
||||
if (missing.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (Map.Entry<Long, List<String>> entry : source.apply(missing).entrySet()) {
|
||||
target.putIfAbsent(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<Long, List<String>> toShops(List<TaskCenterShopRow> rows) {
|
||||
Map<Long, List<String>> map = new HashMap<>();
|
||||
for (TaskCenterShopRow row : rows) {
|
||||
if (row.getTaskId() != null && row.getShop() != null && !row.getShop().isBlank()) {
|
||||
map.computeIfAbsent(row.getTaskId(), key -> new ArrayList<>()).add(row.getShop().trim());
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/** 兜底:前四个来源都空时批量拉一次 request_json(列表主查询刻意不拉大 JSON 列)。 */
|
||||
private Map<Long, List<String>> collectRequestShops(List<TaskCenterTaskRow> rows,
|
||||
Map<Long, List<String>> shops,
|
||||
Map<String, TaskCenterSnapshotRow> snapshots) {
|
||||
List<Long> candidates = rows.stream()
|
||||
.filter(row -> !row.isBrand() && row.getId() != null)
|
||||
.filter(row -> {
|
||||
List<String> hit = shops.get(row.getId());
|
||||
if (hit != null && !hit.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
TaskCenterSnapshotRow snapshot = snapshots.get(snapshotKey(row.getId(), row.getModuleType()));
|
||||
return snapshot == null || snapshot.getCurrentScopeKey() == null
|
||||
|| snapshot.getCurrentScopeKey().isBlank();
|
||||
})
|
||||
.map(TaskCenterTaskRow::getId).distinct().toList();
|
||||
if (candidates.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
Map<Long, List<String>> result = new HashMap<>();
|
||||
for (TaskCenterTaskRow row : safe(queryMapper.selectRequestJson(candidates))) {
|
||||
if (row.getId() == null || row.getRequestJson() == null) {
|
||||
continue;
|
||||
}
|
||||
List<String> names = extractShopNames(row.getRequestJson());
|
||||
if (!names.isEmpty()) {
|
||||
result.put(row.getId(), names);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 解析 request_json 里的 items[].shopName(去重保序,容忍结构不认识)。 */
|
||||
private static List<String> extractShopNames(String requestJson) {
|
||||
if (requestJson.isBlank()) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
JsonNode items = JSON.readTree(requestJson).path("items");
|
||||
if (!items.isArray()) {
|
||||
return List.of();
|
||||
}
|
||||
LinkedHashSet<String> names = new LinkedHashSet<>();
|
||||
for (JsonNode item : items) {
|
||||
String shopName = item.path("shopName").asText(null);
|
||||
if (shopName != null && !shopName.isBlank()) {
|
||||
names.add(shopName.trim());
|
||||
}
|
||||
}
|
||||
return List.copyOf(names);
|
||||
} catch (JsonProcessingException ex) {
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
private TaskCenterTaskRowVo toRowVo(TaskCenterTaskRow row,
|
||||
Map<Long, String> usernames,
|
||||
Map<String, String> deviceNames,
|
||||
Map<String, TaskCenterSnapshotRow> snapshots,
|
||||
Map<Long, List<String>> shops,
|
||||
Map<Long, List<String>> requestShops,
|
||||
LocalDateTime now) {
|
||||
TaskCenterTaskRowVo vo = new TaskCenterTaskRowVo();
|
||||
vo.setId(row.getId());
|
||||
vo.setTaskNo(row.getTaskNo());
|
||||
vo.setModuleType(row.getModuleType());
|
||||
vo.setModuleLabel(TaskModuleRegistry.labelOf(row.getModuleType()));
|
||||
vo.setStatus(row.getStatus());
|
||||
vo.setUserId(row.getUserId());
|
||||
vo.setUsername(row.getUserId() == null ? null : usernames.get(row.getUserId()));
|
||||
vo.setDeviceId(row.getDeviceId());
|
||||
vo.setDeviceName(row.getDeviceId() == null ? null : deviceNames.get(row.getDeviceId()));
|
||||
vo.setWebDevice(row.getDeviceId() != null && row.getDeviceId().startsWith("web-"));
|
||||
vo.setCreatedAt(row.getCreatedAt());
|
||||
vo.setFinishedAt(row.getFinishedAt());
|
||||
vo.setStuck(isStuck(row, now));
|
||||
|
||||
TaskCenterSnapshotRow snapshot = snapshots.get(snapshotKey(row.getId(), row.getModuleType()));
|
||||
if (snapshot != null) {
|
||||
vo.setProgressTotal(nullToZero(snapshot.getTotalCount()));
|
||||
vo.setProgressSuccess(nullToZero(snapshot.getSuccessCount()));
|
||||
vo.setProgressFailed(nullToZero(snapshot.getFailedCount()));
|
||||
vo.setCurrentScope(snapshot.getCurrentScopeKey());
|
||||
vo.setMessage(snapshot.getMessage());
|
||||
} else {
|
||||
vo.setProgressTotal(nullToZero(row.getTotalCount()));
|
||||
vo.setProgressSuccess(nullToZero(row.getSuccessCount()));
|
||||
vo.setProgressFailed(nullToZero(row.getFailedCount()));
|
||||
}
|
||||
|
||||
// 品牌检测按关键字执行、不落店铺;品牌行的 current_scope_key 是品牌名,不能当店铺用。
|
||||
if (!row.isBrand()) {
|
||||
List<String> rowShops = shops.get(row.getId());
|
||||
if ((rowShops == null || rowShops.isEmpty()) && snapshot != null
|
||||
&& snapshot.getCurrentScopeKey() != null && !snapshot.getCurrentScopeKey().isBlank()) {
|
||||
rowShops = List.of(snapshot.getCurrentScopeKey().trim());
|
||||
}
|
||||
if (rowShops == null || rowShops.isEmpty()) {
|
||||
rowShops = requestShops.get(row.getId());
|
||||
}
|
||||
if (rowShops != null && !rowShops.isEmpty()) {
|
||||
vo.setShopName(rowShops.get(0));
|
||||
vo.setShopCount(rowShops.size());
|
||||
}
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
private static boolean isStuck(TaskCenterTaskRow row, LocalDateTime now) {
|
||||
if (row.getStatus() == null || !ACTIVE_STATUSES.contains(row.getStatus())) {
|
||||
return false;
|
||||
}
|
||||
LocalDateTime lastSeen = row.getUpdatedAt() == null ? row.getCreatedAt() : row.getUpdatedAt();
|
||||
return lastSeen != null && lastSeen.isBefore(now.minusMinutes(STUCK_IDLE_MINUTES));
|
||||
}
|
||||
|
||||
private static long nullToZero(Long value) {
|
||||
return value == null ? 0L : value;
|
||||
}
|
||||
|
||||
private static <T> List<T> safe(List<T> rows) {
|
||||
return rows == null ? List.of() : rows;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user