feat(jd-collect): 任务面板改版——后端 dashboard/history/progress-batch/delete 四端点+前端左按钮右 TaskCenterPanel(历史/断点续传/中断/重试走通用原位复位)
This commit is contained in:
+59
@@ -1,18 +1,25 @@
|
||||
package com.nanri.aiimage.modules.jd.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.api.PageDefaults;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdCollectTaskBatchRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdCollectTaskCreateRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdIngestChunkRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectDashboardVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectHistoryVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectProgressVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectTaskBatchVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectTaskVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdIngestChunkVo;
|
||||
import com.nanri.aiimage.modules.jd.service.JdCollectBoardService;
|
||||
import com.nanri.aiimage.modules.jd.service.JdCollectTaskService;
|
||||
import com.nanri.aiimage.modules.jd.service.JdResultIngestService;
|
||||
import com.nanri.aiimage.modules.task.model.dto.TaskHeartbeatRequest;
|
||||
import com.nanri.aiimage.modules.task.model.vo.TaskHeartbeatVo;
|
||||
import com.nanri.aiimage.modules.task.service.TaskHeartbeatPositionService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskHeartbeatService;
|
||||
import com.nanri.aiimage.modules.task.service.TaskProgressOwnershipSupport;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -20,6 +27,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -45,11 +53,62 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
public class JdCollectController {
|
||||
|
||||
private final JdCollectTaskService collectTaskService;
|
||||
private final JdCollectBoardService boardService;
|
||||
private final JdResultIngestService ingestService;
|
||||
private final TaskHeartbeatService taskHeartbeatService;
|
||||
private final TaskHeartbeatPositionService taskHeartbeatPositionService;
|
||||
private final TaskProgressOwnershipSupport progressOwnershipSupport;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
|
||||
@GetMapping("/dashboard")
|
||||
@Operation(summary = "查询京东采集任务总览", description = "当前用户的运行中、成功、失败任务数。"
|
||||
+ "任务面板统计卡数据源,页面进入时请求一次即可,不需要持续轮询。")
|
||||
public ApiResponse<JdCollectDashboardVo> dashboard(
|
||||
@Parameter(description = "当前用户 ID", required = true, example = "1")
|
||||
@RequestParam("user_id") Long userId,
|
||||
HttpServletRequest httpRequest) {
|
||||
userId = adminAuthSupport.resolveOwnershipUserId(httpRequest, userId);
|
||||
return ApiResponse.success(boardService.dashboard(userId));
|
||||
}
|
||||
|
||||
@GetMapping("/history")
|
||||
@Operation(summary = "查询京东采集任务历史", description = "当前用户最近的任务历史(活跃任务排前),"
|
||||
+ "含店铺名、任务状态、已落商品行数、断点游标与终态原因。任务面板历史层数据源。")
|
||||
public ApiResponse<JdCollectHistoryVo> history(
|
||||
@Parameter(description = "当前用户 ID", required = true, example = "1")
|
||||
@RequestParam("user_id") Long userId,
|
||||
@Parameter(description = "历史记录条数,默认 50,最大 100", example = "50")
|
||||
@RequestParam(value = "limit", required = false, defaultValue = PageDefaults.DEFAULT_RESULT_LIMIT) Integer limit,
|
||||
HttpServletRequest httpRequest) {
|
||||
userId = adminAuthSupport.resolveOwnershipUserId(httpRequest, userId);
|
||||
int safeLimit = limit == null ? 100 : Math.min(Math.max(1, limit), 100);
|
||||
return ApiResponse.success(boardService.history(userId, safeLimit));
|
||||
}
|
||||
|
||||
@PostMapping("/tasks/progress/batch")
|
||||
@Operation(summary = "批量查询任务进度", description = "任务面板只对活跃任务调用该接口,建议 6 秒一次。"
|
||||
+ "归属先由公共出口按会话身份过滤,返回任务头 + 最新游标进度。")
|
||||
public ApiResponse<JdCollectTaskBatchVo> progressBatch(@Valid @RequestBody JdCollectTaskBatchRequest request,
|
||||
HttpServletRequest httpRequest) {
|
||||
return ApiResponse.success(boardService.progressBatch(progressOwnershipSupport
|
||||
.filterOwnedTaskIds(request.getTaskIds(),
|
||||
adminAuthSupport.requireOwnershipUserId(httpRequest, request.getUserId()))));
|
||||
}
|
||||
|
||||
@DeleteMapping("/tasks/{taskId}")
|
||||
@Operation(summary = "删除任务", description = "删除当前用户的一条京东采集任务,同时清理京东数据库"
|
||||
+ "该任务落下的商品/图片/SKU 行与断点游标(铁律 32 数据面)。")
|
||||
public ApiResponse<Void> deleteTask(
|
||||
@Parameter(description = "任务 ID", required = true, example = "34398")
|
||||
@PathVariable Long taskId,
|
||||
@Parameter(description = "当前用户 ID,必须与创建任务的用户一致", required = true, example = "1")
|
||||
@RequestParam("user_id") Long userId,
|
||||
HttpServletRequest httpRequest) {
|
||||
userId = adminAuthSupport.resolveOwnershipUserId(httpRequest, userId);
|
||||
boardService.deleteTask(taskId, userId);
|
||||
return ApiResponse.success(null);
|
||||
}
|
||||
|
||||
@PostMapping("/tasks")
|
||||
@Operation(summary = "创建京东店铺采集任务", description = "选择店铺 + 商品 Tab(在售/已下架)+ 采集范围建单;"
|
||||
+ "任务落 PENDING、执行通道显式 LOCAL(本机紫鸟执行)。返回 taskId/taskNo/collectChannel。")
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.nanri.aiimage.modules.jd.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东采集批量进度请求(形状照 {@code AppearancePatentTaskBatchRequest})。
|
||||
* {@code userId} 可省略:归属过滤主体是 {@code TaskProgressOwnershipSupport.filterOwnedTaskIds}
|
||||
* 按会话身份判定,请求体里的 userId 只作 admin 代客的归属提示。
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东采集批量进度请求")
|
||||
public class JdCollectTaskBatchRequest {
|
||||
|
||||
@Schema(description = "要查询的任务 ID 列表(只查活跃任务,建议 6 秒一批)", required = true)
|
||||
@NotEmpty(message = "taskIds 不能为空")
|
||||
private List<Long> taskIds;
|
||||
|
||||
@Schema(description = "当前用户 ID(归属过滤用,可省略)")
|
||||
private Long userId;
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 京东采集任务面板总览(v1.5 用户端「任务中心」统计卡数据源)。
|
||||
*
|
||||
* <p>形状照外观专利 {@code AppearancePatentDashboardVo}:四张卡各一个数,
|
||||
* {@code pendingTaskCount} 实际存 RUNNING 数(页面文案「运行中任务」),
|
||||
* {@code processedTaskCount} = 成功 + 失败。页面进入时请求一次即可,不需要持续轮询。
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东采集任务总览:运行中/已结束/成功/失败任务数")
|
||||
public class JdCollectDashboardVo {
|
||||
|
||||
@Schema(description = "运行中任务数(RUNNING)", example = "1")
|
||||
private Long pendingTaskCount;
|
||||
|
||||
@Schema(description = "已结束任务数(成功+失败)", example = "5")
|
||||
private Long processedTaskCount;
|
||||
|
||||
@Schema(description = "成功任务数", example = "4")
|
||||
private Long successTaskCount;
|
||||
|
||||
@Schema(description = "失败任务数", example = "1")
|
||||
private Long failedTaskCount;
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东采集历史行(v1.5 用户端「任务中心」卡片数据源)。
|
||||
*
|
||||
* <p>形状照外观专利 {@code AppearancePatentHistoryItemVo} 裁剪:京东采集<b>没有</b>
|
||||
* {@code biz_file_result}/{@code biz_task_file_job}(交付物直接落京东数据库,无结果文件),
|
||||
* 所以没有 downloadUrl/fileStatus/fileProgress 那一层;行数与游标改从
|
||||
* {@code biz_jd_product} 计数与 {@code biz_task_scope_state} 取。时间一律 ISO 字符串
|
||||
* ({@code LocalDateTime.toString()}),与外观专利前端格式化口径一致。
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东采集历史行:任务状态 + 行数 + 游标进度 + 终态原因")
|
||||
public class JdCollectHistoryItemVo {
|
||||
|
||||
@Schema(description = "结果记录 ID(京东采集无结果文件,恒空)")
|
||||
private Long resultId;
|
||||
|
||||
@Schema(description = "任务 ID", example = "34398")
|
||||
private Long taskId;
|
||||
|
||||
@Schema(description = "任务号", example = "JD_SHOP_COLLECT-1973...")
|
||||
private String taskNo;
|
||||
|
||||
@Schema(description = "任务状态(biz_file_task.status 原值:PENDING/RUNNING/SUCCESS/FAILED)")
|
||||
private String taskStatus;
|
||||
|
||||
@Schema(description = "任务是否成功结束")
|
||||
private Boolean success;
|
||||
|
||||
@Schema(description = "终态原因(FAILED 时为客户端上报的真实原因)")
|
||||
private String errorMessage;
|
||||
|
||||
@Schema(description = "建单时选择的店铺名(建单载荷 shopName)", example = "花屿巷旗舰店")
|
||||
private String shopName;
|
||||
|
||||
@Schema(description = "建单时选择的商品 Tab(ONSALE/OFFSALE)")
|
||||
private List<String> listTabs;
|
||||
|
||||
@Schema(description = "已落京东数据库的商品行数(biz_jd_product 按 task_id 计数)", example = "1407")
|
||||
private Integer rowCount;
|
||||
|
||||
@Schema(description = "已回传商品数(scope 游标 itemsReceived)", example = "1407")
|
||||
private Integer itemsReceived;
|
||||
|
||||
@Schema(description = "列表总数(客户端抓首页时写入游标,没拿到过为空)")
|
||||
private Integer total;
|
||||
|
||||
@Schema(description = "当前采集页(断点游标)")
|
||||
private Integer page;
|
||||
|
||||
@Schema(description = "当前采集 Tab(ONSALE/OFFSALE)")
|
||||
private String listTab;
|
||||
|
||||
@Schema(description = "已收到的分片批次数")
|
||||
private Integer batches;
|
||||
|
||||
@Schema(description = "最近一次回传/合并写时间(ISO)")
|
||||
private String updatedAt;
|
||||
|
||||
@Schema(description = "建单时间(ISO)")
|
||||
private String createdAt;
|
||||
|
||||
@Schema(description = "结束时间(ISO)")
|
||||
private String finishedAt;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** 京东采集历史列表(形状照 {@code AppearancePatentHistoryVo})。 */
|
||||
@Data
|
||||
@Schema(description = "京东采集历史列表")
|
||||
public class JdCollectHistoryVo {
|
||||
|
||||
@Schema(description = "历史行(活跃任务排前,其余按最近回传时间倒序)")
|
||||
private List<JdCollectHistoryItemVo> items = new ArrayList<>();
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** 京东采集批量进度响应(形状照 {@code AppearancePatentTaskBatchVo})。 */
|
||||
@Data
|
||||
@Schema(description = "京东采集批量进度:items + 查不到的任务 ID")
|
||||
public class JdCollectTaskBatchVo {
|
||||
|
||||
@Schema(description = "命中的任务条目")
|
||||
private List<JdCollectTaskDetailVo> items = new ArrayList<>();
|
||||
|
||||
@Schema(description = "归属校验后被剔除或不存在/不属于本模块的任务 ID")
|
||||
private List<Long> missingTaskIds = new ArrayList<>();
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东采集批量进度条目(形状照 {@code AppearancePatentTaskDetailVo}:
|
||||
* {@code task} 任务头 + {@code items} 单元素历史行,前端任务面板按同一形状消费)。
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东采集批量进度条目:任务头 + 最新进度")
|
||||
public class JdCollectTaskDetailVo {
|
||||
|
||||
@Schema(description = "任务头")
|
||||
private JdCollectTaskItemVo task;
|
||||
|
||||
@Schema(description = "进度行(单元素,取该任务最新一条历史形状)")
|
||||
private List<JdCollectHistoryItemVo> items = new ArrayList<>();
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/** 京东采集批量进度里的任务头(形状照 {@code AppearancePatentTaskItemVo})。 */
|
||||
@Data
|
||||
@Schema(description = "京东采集任务头(批量进度用)")
|
||||
public class JdCollectTaskItemVo {
|
||||
|
||||
@Schema(description = "任务 ID", example = "34398")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "任务号")
|
||||
private String taskNo;
|
||||
|
||||
@Schema(description = "任务状态(biz_file_task.status 原值)")
|
||||
private String status;
|
||||
|
||||
@Schema(description = "终态原因")
|
||||
private String errorMessage;
|
||||
|
||||
@Schema(description = "最近更新时间(ISO)")
|
||||
private String updatedAt;
|
||||
}
|
||||
+306
@@ -0,0 +1,306 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.task.TaskStatuses;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductImageMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdCollectTaskCreateRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectDashboardVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectHistoryItemVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectHistoryVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectTaskBatchVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectTaskDetailVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdCollectTaskItemVo;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductImageEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductSkuEntity;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||
import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
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.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 京东采集任务面板服务(v1.5 用户端「任务中心」:总览/历史/批量进度/删除)。
|
||||
*
|
||||
* <p>形状照外观专利的 {@code AppearancePatentProgressSupport}+{@code AppearancePatentTaskRecordSupport}
|
||||
* 裁剪:京东采集<b>没有</b> {@code biz_file_result}/{@code biz_task_file_job}(交付物直接落
|
||||
* 京东数据库,无结果文件、无下载),所以历史行的「行数」改从 {@code biz_jd_product} 按
|
||||
* {@code task_id} 计数、进度从 {@code biz_task_scope_state} 游标取;删除面清理
|
||||
* {@code biz_jd_product(_image/_sku)}+scope 行+任务主行(铁律 32 数据面)。
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class JdCollectBoardService {
|
||||
|
||||
private static final int MAX_HISTORY_LIMIT = 100;
|
||||
|
||||
private final FileTaskMapper fileTaskMapper;
|
||||
private final TaskScopeStateMapper taskScopeStateMapper;
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductImageMapper jdProductImageMapper;
|
||||
private final JdProductSkuMapper jdProductSkuMapper;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
/** 查询当前用户的面板总览(运行中/成功/失败三档计数,已结束=成功+失败)。 */
|
||||
public JdCollectDashboardVo dashboard(Long userId) {
|
||||
JdCollectDashboardVo vo = new JdCollectDashboardVo();
|
||||
long running = countTask(userId, TaskStatuses.FileTask.RUNNING);
|
||||
long success = countTask(userId, TaskStatuses.FileTask.SUCCESS);
|
||||
long failed = countTask(userId, TaskStatuses.FileTask.FAILED);
|
||||
vo.setPendingTaskCount(running);
|
||||
vo.setSuccessTaskCount(success);
|
||||
vo.setFailedTaskCount(failed);
|
||||
vo.setProcessedTaskCount(success + failed);
|
||||
return vo;
|
||||
}
|
||||
|
||||
private long countTask(Long userId, String status) {
|
||||
return fileTaskMapper.selectCount(new LambdaQueryWrapper<FileTaskEntity>()
|
||||
.eq(FileTaskEntity::getModuleType, JdCollectTaskService.MODULE_TYPE)
|
||||
.eq(FileTaskEntity::getUserId, userId)
|
||||
.eq(FileTaskEntity::getStatus, status));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询当前用户最近的任务历史(活跃任务排前,其余按最近回传时间倒序)。
|
||||
* 行数与游标批量回查(每任务一次聚合查询会打爆历史面板,100 行上限 × 2 条批量 SQL)。
|
||||
*/
|
||||
public JdCollectHistoryVo history(Long userId, Integer limit) {
|
||||
int safeLimit = Math.max(1, Math.min(limit == null ? 50 : limit, MAX_HISTORY_LIMIT));
|
||||
List<FileTaskEntity> tasks = fileTaskMapper.selectList(new LambdaQueryWrapper<FileTaskEntity>()
|
||||
.eq(FileTaskEntity::getModuleType, JdCollectTaskService.MODULE_TYPE)
|
||||
.eq(FileTaskEntity::getUserId, userId)
|
||||
.orderByDesc(FileTaskEntity::getCreatedAt)
|
||||
.last("limit " + safeLimit));
|
||||
tasks.sort(Comparator
|
||||
.comparing((FileTaskEntity task) -> !isActive(task))
|
||||
.thenComparing(FileTaskEntity::getUpdatedAt,
|
||||
Comparator.nullsLast(Comparator.reverseOrder()))
|
||||
.thenComparing(FileTaskEntity::getId, Comparator.reverseOrder()));
|
||||
|
||||
List<Long> taskIds = tasks.stream().map(FileTaskEntity::getId).toList();
|
||||
Map<Long, Integer> rowCounts = countProductsByTask(taskIds);
|
||||
Map<Long, TaskScopeStateEntity> scopes = findScopeRows(taskIds);
|
||||
|
||||
JdCollectHistoryVo vo = new JdCollectHistoryVo();
|
||||
for (FileTaskEntity task : tasks) {
|
||||
vo.getItems().add(toHistoryItem(task, rowCounts.get(task.getId()),
|
||||
scopes.get(task.getId())));
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
private static boolean isActive(FileTaskEntity task) {
|
||||
return TaskStatuses.FileTask.ACTIVE.contains(task.getStatus());
|
||||
}
|
||||
|
||||
/** 批量数行:一条 GROUP BY 出全部任务的商品行数(Mapper 层聚合,Java 不逐行数)。 */
|
||||
private Map<Long, Integer> countProductsByTask(List<Long> taskIds) {
|
||||
Map<Long, Integer> counts = new HashMap<>();
|
||||
if (taskIds.isEmpty()) {
|
||||
return counts;
|
||||
}
|
||||
List<Map<String, Object>> rows = jdProductMapper.selectMaps(new QueryWrapper<JdProductEntity>()
|
||||
.select("task_id", "count(*) as cnt")
|
||||
.in("task_id", taskIds)
|
||||
.groupBy("task_id"));
|
||||
for (Map<String, Object> row : rows) {
|
||||
Long taskId = JdScopeStateReader.asNullableInt(row.get("task_id")) == null
|
||||
? null : Long.valueOf(String.valueOf(row.get("task_id")));
|
||||
if (taskId != null) {
|
||||
counts.put(taskId, JdScopeStateReader.asInt(row.get("cnt")));
|
||||
}
|
||||
}
|
||||
return counts;
|
||||
}
|
||||
|
||||
private Map<Long, TaskScopeStateEntity> findScopeRows(List<Long> taskIds) {
|
||||
Map<Long, TaskScopeStateEntity> scopes = new HashMap<>();
|
||||
if (taskIds.isEmpty()) {
|
||||
return scopes;
|
||||
}
|
||||
List<TaskScopeStateEntity> rows = taskScopeStateMapper.selectList(
|
||||
new LambdaQueryWrapper<TaskScopeStateEntity>()
|
||||
.eq(TaskScopeStateEntity::getModuleType, JdCollectTaskService.MODULE_TYPE)
|
||||
.in(TaskScopeStateEntity::getTaskId, taskIds));
|
||||
for (TaskScopeStateEntity row : rows) {
|
||||
scopes.putIfAbsent(row.getTaskId(), row);
|
||||
}
|
||||
return scopes;
|
||||
}
|
||||
|
||||
/** 组装历史行:任务头 + 建单载荷(店铺名/Tab,宽松读) + 行数 + 游标进度。 */
|
||||
private JdCollectHistoryItemVo toHistoryItem(FileTaskEntity task, Integer rowCount,
|
||||
TaskScopeStateEntity scope) {
|
||||
JdCollectHistoryItemVo item = new JdCollectHistoryItemVo();
|
||||
item.setTaskId(task.getId());
|
||||
item.setTaskNo(task.getTaskNo());
|
||||
item.setTaskStatus(task.getStatus());
|
||||
item.setSuccess(TaskStatuses.FileTask.SUCCESS.equals(task.getStatus()));
|
||||
item.setErrorMessage(task.getErrorMessage());
|
||||
JdCollectTaskCreateRequest request = readCreateRequestQuiet(task);
|
||||
if (request != null) {
|
||||
item.setShopName(request.getShopName());
|
||||
item.setListTabs(request.getListTabs());
|
||||
}
|
||||
item.setRowCount(rowCount == null ? 0 : rowCount);
|
||||
fillProgress(item, task, scope);
|
||||
if (task.getCreatedAt() != null) {
|
||||
item.setCreatedAt(task.getCreatedAt().toString());
|
||||
}
|
||||
if (task.getFinishedAt() != null) {
|
||||
item.setFinishedAt(task.getFinishedAt().toString());
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
/** 历史行宽松读建单载荷:载荷缺失/损坏时只少店铺名,不把历史面板打成红条。 */
|
||||
private JdCollectTaskCreateRequest readCreateRequestQuiet(FileTaskEntity task) {
|
||||
if (task.getRequestJson() == null || task.getRequestJson().isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return objectMapper.readValue(task.getRequestJson(), JdCollectTaskCreateRequest.class);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[jd-collect] 建单载荷解析失败,历史行少店铺名 taskId={} err={}",
|
||||
task.getId(), ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 填游标进度(与 {@code JdCollectTaskService#queryProgress} 同口径:任务行+scope 行两处真源,
|
||||
* itemsReceived 取两源较大值,total/page 没拿到过为空)。
|
||||
*/
|
||||
private void fillProgress(JdCollectHistoryItemVo item, FileTaskEntity task,
|
||||
TaskScopeStateEntity scope) {
|
||||
item.setItemsReceived(JdScopeStateReader.nz(task.getSuccessFileCount()));
|
||||
item.setBatches(0);
|
||||
if (scope != null) {
|
||||
Map<String, Object> state = JdScopeStateReader.parse(objectMapper, scope.getStateJson(),
|
||||
message -> log.warn("[jd-collect] {}", message));
|
||||
Map<String, Object> cursor = JdScopeStateReader.asMap(state.get("cursor"));
|
||||
item.setItemsReceived(Math.max(JdScopeStateReader.nz(task.getSuccessFileCount()),
|
||||
JdScopeStateReader.asInt(state.get("itemsReceived"))));
|
||||
item.setTotal(JdScopeStateReader.asNullableInt(cursor.get("total")));
|
||||
item.setPage(JdScopeStateReader.asNullableInt(cursor.get("page")));
|
||||
Object tab = cursor.get("tab");
|
||||
item.setListTab(tab == null ? null : String.valueOf(tab));
|
||||
item.setBatches(scope.getReceivedChunkCount() == null ? 0 : scope.getReceivedChunkCount());
|
||||
}
|
||||
if (scope != null && scope.getUpdatedAt() != null) {
|
||||
item.setUpdatedAt(scope.getUpdatedAt().toString());
|
||||
} else if (task.getUpdatedAt() != null) {
|
||||
item.setUpdatedAt(task.getUpdatedAt().toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量查任务进度(前端轮询专用,归属已由 {@code TaskProgressOwnershipSupport} 过滤)。
|
||||
* 形状照外观专利:每任务一条 {@code {task, items:[最新进度行]}},查不到的进 missingTaskIds。
|
||||
*/
|
||||
public JdCollectTaskBatchVo progressBatch(List<Long> taskIds) {
|
||||
Set<Long> normalized = new LinkedHashSet<>();
|
||||
if (taskIds != null) {
|
||||
for (Long id : taskIds) {
|
||||
if (id != null && id > 0) {
|
||||
normalized.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
JdCollectTaskBatchVo vo = new JdCollectTaskBatchVo();
|
||||
if (normalized.isEmpty()) {
|
||||
return vo;
|
||||
}
|
||||
List<FileTaskEntity> tasks = fileTaskMapper.selectList(
|
||||
new LambdaQueryWrapper<FileTaskEntity>().in(FileTaskEntity::getId, normalized));
|
||||
List<Long> hitIds = new ArrayList<>();
|
||||
for (FileTaskEntity task : tasks) {
|
||||
if (!JdCollectTaskService.MODULE_TYPE.equals(task.getModuleType())) {
|
||||
continue;
|
||||
}
|
||||
hitIds.add(task.getId());
|
||||
vo.getItems().add(toTaskDetail(task));
|
||||
}
|
||||
for (Long id : normalized) {
|
||||
if (!hitIds.contains(id)) {
|
||||
vo.getMissingTaskIds().add(id);
|
||||
}
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
private JdCollectTaskDetailVo toTaskDetail(FileTaskEntity task) {
|
||||
JdCollectTaskItemVo taskItem = new JdCollectTaskItemVo();
|
||||
taskItem.setId(task.getId());
|
||||
taskItem.setTaskNo(task.getTaskNo());
|
||||
taskItem.setStatus(task.getStatus());
|
||||
taskItem.setErrorMessage(task.getErrorMessage());
|
||||
taskItem.setUpdatedAt(task.getUpdatedAt() == null ? null : task.getUpdatedAt().toString());
|
||||
|
||||
JdCollectTaskDetailVo detail = new JdCollectTaskDetailVo();
|
||||
detail.setTask(taskItem);
|
||||
Map<Long, Integer> rowCounts = countProductsByTask(List.of(task.getId()));
|
||||
detail.getItems().add(toHistoryItem(task, rowCounts.get(task.getId()), findScopeRow(task.getId())));
|
||||
return detail;
|
||||
}
|
||||
|
||||
private TaskScopeStateEntity findScopeRow(Long taskId) {
|
||||
return taskScopeStateMapper.selectOne(new LambdaQueryWrapper<TaskScopeStateEntity>()
|
||||
.eq(TaskScopeStateEntity::getTaskId, taskId)
|
||||
.eq(TaskScopeStateEntity::getModuleType, JdCollectTaskService.MODULE_TYPE)
|
||||
.last("limit 1"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除任务(铁律 32 数据面):京东数据库三表按 {@code task_id} 清 + scope 行 + 任务主行。
|
||||
* 事务内只做 DB 删除;京东采集没有 OSS 载荷/任务缓存需要 afterCommit 清理。
|
||||
* 删行顺序照 {@code JdProductRowDeleteService#deleteRows}:先 sku/image 子行再主行。
|
||||
*/
|
||||
@Transactional
|
||||
public void deleteTask(Long taskId, Long userId) {
|
||||
FileTaskEntity task = fileTaskMapper.selectById(taskId);
|
||||
if (task == null || !JdCollectTaskService.MODULE_TYPE.equals(task.getModuleType())
|
||||
|| (userId != null && task.getUserId() != null && !Objects.equals(userId, task.getUserId()))) {
|
||||
throw new BusinessException("任务不存在");
|
||||
}
|
||||
List<Long> productIds = jdProductMapper.selectList(
|
||||
new LambdaQueryWrapper<JdProductEntity>()
|
||||
.select(JdProductEntity::getId)
|
||||
.eq(JdProductEntity::getTaskId, taskId))
|
||||
.stream().map(JdProductEntity::getId).toList();
|
||||
if (!productIds.isEmpty()) {
|
||||
jdProductSkuMapper.delete(new LambdaQueryWrapper<JdProductSkuEntity>()
|
||||
.in(JdProductSkuEntity::getProductId, productIds));
|
||||
jdProductImageMapper.delete(new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.in(JdProductImageEntity::getProductId, productIds));
|
||||
}
|
||||
int products = jdProductMapper.delete(new LambdaQueryWrapper<JdProductEntity>()
|
||||
.eq(JdProductEntity::getTaskId, taskId));
|
||||
taskScopeStateMapper.delete(new LambdaQueryWrapper<TaskScopeStateEntity>()
|
||||
.eq(TaskScopeStateEntity::getTaskId, taskId)
|
||||
.eq(TaskScopeStateEntity::getModuleType, JdCollectTaskService.MODULE_TYPE));
|
||||
fileTaskMapper.deleteById(taskId);
|
||||
log.info("[jd-collect] 删除任务 taskId={} userId={} 商品行={}", taskId, userId, products);
|
||||
}
|
||||
}
|
||||
+8
-55
@@ -22,7 +22,6 @@ import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -234,15 +233,17 @@ public class JdCollectTaskService {
|
||||
if (scope == null) {
|
||||
// scope 行缺失(历史/异常建单)时只回任务级状态,进度按 0 计,不抛错——
|
||||
// 进度查询是展示面,不该因为一行辅助表缺失把页面打成红条。
|
||||
vo.setItemsReceived(nz(task.getSuccessFileCount()));
|
||||
vo.setItemsReceived(JdScopeStateReader.nz(task.getSuccessFileCount()));
|
||||
vo.setBatches(0);
|
||||
return vo;
|
||||
}
|
||||
Map<String, Object> state = parseScopeState(scope.getStateJson());
|
||||
Map<String, Object> cursor = asMap(state.get("cursor"));
|
||||
vo.setItemsReceived(Math.max(nz(task.getSuccessFileCount()), asInt(state.get("itemsReceived"))));
|
||||
vo.setTotal(asNullableInt(cursor.get("total")));
|
||||
vo.setPage(asNullableInt(cursor.get("page")));
|
||||
Map<String, Object> state = JdScopeStateReader.parse(objectMapper, scope.getStateJson(),
|
||||
message -> log.warn("[jd-collect] scope 状态 JSON 解析失败,进度按 0 展示 taskId 相关行: {}", message));
|
||||
Map<String, Object> cursor = JdScopeStateReader.asMap(state.get("cursor"));
|
||||
vo.setItemsReceived(Math.max(JdScopeStateReader.nz(task.getSuccessFileCount()),
|
||||
JdScopeStateReader.asInt(state.get("itemsReceived"))));
|
||||
vo.setTotal(JdScopeStateReader.asNullableInt(cursor.get("total")));
|
||||
vo.setPage(JdScopeStateReader.asNullableInt(cursor.get("page")));
|
||||
Object tab = cursor.get("tab");
|
||||
vo.setListTab(tab == null ? null : String.valueOf(tab));
|
||||
vo.setBatches(scope.getReceivedChunkCount() == null ? 0 : scope.getReceivedChunkCount());
|
||||
@@ -251,52 +252,4 @@ public class JdCollectTaskService {
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
/** 读 scope 状态 JSON;损坏时返回空对象并留痕(查询面不因一行脏 JSON 抛错)。 */
|
||||
private Map<String, Object> parseScopeState(String stateJson) {
|
||||
if (stateJson == null || stateJson.isBlank()) {
|
||||
return new LinkedHashMap<>();
|
||||
}
|
||||
try {
|
||||
Map<String, Object> parsed = objectMapper.readValue(stateJson,
|
||||
objectMapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, Object.class));
|
||||
return parsed == null ? new LinkedHashMap<>() : parsed;
|
||||
} catch (Exception ex) {
|
||||
log.warn("[jd-collect] scope 状态 JSON 解析失败,进度按 0 展示 taskId 相关行: {}", ex.getMessage());
|
||||
return new LinkedHashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Map<String, Object> asMap(Object raw) {
|
||||
return raw instanceof Map ? new LinkedHashMap<>((Map<String, Object>) raw) : new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
private static int asInt(Object raw) {
|
||||
Integer value = asNullableInt(raw);
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 宽松读整数:数字类型直接用,字符串只认 1~9 位十进制(int 范围内必然不溢出)。
|
||||
* 不拿 {@code NumberFormatException} 当控制流——脏值在进度展示面是正常的空结果,
|
||||
* 不该为它添一个 catch(catch 棘轮同形判据)。
|
||||
*/
|
||||
private static Integer asNullableInt(Object raw) {
|
||||
if (raw instanceof Number number) {
|
||||
return number.intValue();
|
||||
}
|
||||
if (raw == null) {
|
||||
return null;
|
||||
}
|
||||
String text = String.valueOf(raw).trim();
|
||||
if (!text.matches("-?\\d{1,9}")) {
|
||||
return null;
|
||||
}
|
||||
return Integer.valueOf(text);
|
||||
}
|
||||
|
||||
private static int nz(Integer value) {
|
||||
return value == null ? 0 : Math.max(0, value);
|
||||
}
|
||||
}
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* scope 状态 JSON 的宽松读取工具(进度/历史/批量进度三个展示面共用)。
|
||||
*
|
||||
* <p>从 {@code JdCollectTaskService} 的私有小工具收口而来:展示面不因一行脏 JSON 抛错,
|
||||
* 读不出的键一律按空值展示(不编造 0 以外的数)。解析只认 1~9 位十进制(int 范围内必然
|
||||
* 不溢出),不拿 {@code NumberFormatException} 当控制流——脏值在进度展示面是正常的空结果。
|
||||
*/
|
||||
final class JdScopeStateReader {
|
||||
|
||||
private JdScopeStateReader() {
|
||||
}
|
||||
|
||||
/** 读 scope 状态 JSON;损坏时返回空对象并留痕(调用方日志口径自定)。 */
|
||||
static Map<String, Object> parse(ObjectMapper objectMapper, String stateJson,
|
||||
java.util.function.Consumer<String> warn) {
|
||||
if (stateJson == null || stateJson.isBlank()) {
|
||||
return new LinkedHashMap<>();
|
||||
}
|
||||
try {
|
||||
Map<String, Object> parsed = objectMapper.readValue(stateJson,
|
||||
objectMapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, Object.class));
|
||||
return parsed == null ? new LinkedHashMap<>() : parsed;
|
||||
} catch (Exception ex) {
|
||||
warn.accept("scope 状态 JSON 解析失败,进度按 0 展示: " + ex.getMessage());
|
||||
return new LinkedHashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static Map<String, Object> asMap(Object raw) {
|
||||
return raw instanceof Map ? new LinkedHashMap<>((Map<String, Object>) raw) : new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
static int asInt(Object raw) {
|
||||
Integer value = asNullableInt(raw);
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
|
||||
static Integer asNullableInt(Object raw) {
|
||||
if (raw instanceof Number number) {
|
||||
return number.intValue();
|
||||
}
|
||||
if (raw == null) {
|
||||
return null;
|
||||
}
|
||||
String text = String.valueOf(raw).trim();
|
||||
if (!text.matches("-?\\d{1,9}")) {
|
||||
return null;
|
||||
}
|
||||
return Integer.valueOf(text);
|
||||
}
|
||||
|
||||
static int nz(Integer value) {
|
||||
return value == null ? 0 : Math.max(0, value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user