feat(jd-collect): 任务面板改版——后端 dashboard/history/progress-batch/delete 四端点+前端左按钮右 TaskCenterPanel(历史/断点续传/中断/重试走通用原位复位)

This commit is contained in:
2026-11-15 01:56:37 +08:00
parent 55f4f17480
commit 45d0989ada
21 changed files with 1494 additions and 378 deletions
@@ -0,0 +1,194 @@
package com.nanri.aiimage.modules.jd.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessException;
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.vo.JdCollectHistoryItemVo;
import com.nanri.aiimage.modules.jd.model.vo.JdCollectTaskBatchVo;
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 org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 京东采集任务面板服务(v1.5 任务面板四端点)。
*
* <p>判据四件:① 总览三档计数(已结束=成功+失败);② 历史行读真源(biz_jd_product 行数
* + scope 游标),活跃任务排前;③ 批量进度按模块过滤,非本模块任务进 missingTaskIds
* 不冒充面板条目;④ 删除面校验归属后才清京东数据库三表+scope 行+任务主行(铁律 32)。
*/
@ExtendWith(MockitoExtension.class)
class JdCollectBoardServiceTest {
@Mock
private FileTaskMapper fileTaskMapper;
@Mock
private TaskScopeStateMapper taskScopeStateMapper;
@Mock
private JdProductMapper jdProductMapper;
@Mock
private JdProductImageMapper jdProductImageMapper;
@Mock
private JdProductSkuMapper jdProductSkuMapper;
@Spy
private ObjectMapper objectMapper = new ObjectMapper();
@InjectMocks
private JdCollectBoardService service;
@org.junit.jupiter.api.BeforeEach
void setUp() {
// LambdaQueryWrapper 需要 MP 的 lambda cache(JdProductDeleteServiceTest 同款初始化)。
com.baomidou.mybatisplus.core.MybatisConfiguration configuration =
new com.baomidou.mybatisplus.core.MybatisConfiguration();
org.apache.ibatis.builder.MapperBuilderAssistant assistant =
new org.apache.ibatis.builder.MapperBuilderAssistant(configuration, "");
com.baomidou.mybatisplus.core.metadata.TableInfoHelper.initTableInfo(assistant,
com.nanri.aiimage.modules.jd.model.entity.JdProductEntity.class);
com.baomidou.mybatisplus.core.metadata.TableInfoHelper.initTableInfo(assistant,
com.nanri.aiimage.modules.jd.model.entity.JdProductImageEntity.class);
com.baomidou.mybatisplus.core.metadata.TableInfoHelper.initTableInfo(assistant,
com.nanri.aiimage.modules.jd.model.entity.JdProductSkuEntity.class);
com.baomidou.mybatisplus.core.metadata.TableInfoHelper.initTableInfo(assistant,
com.nanri.aiimage.modules.task.model.entity.FileTaskEntity.class);
com.baomidou.mybatisplus.core.metadata.TableInfoHelper.initTableInfo(assistant,
com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity.class);
}
private FileTaskEntity task(long id, long userId, String status) {
FileTaskEntity task = new FileTaskEntity();
task.setId(id);
task.setModuleType(JdCollectTaskService.MODULE_TYPE);
task.setUserId(userId);
task.setTaskNo("JD-" + id);
task.setStatus(status);
task.setSuccessFileCount(0);
task.setRequestJson("{\"userId\":" + userId + ",\"shopName\":\"花屿巷旗舰店\","
+ "\"listTabs\":[\"ONSALE\"],\"maxProducts\":0}");
task.setCreatedAt(LocalDateTime.of(2026, 11, 15, 10, 0));
task.setUpdatedAt(LocalDateTime.of(2026, 11, 15, 10, 1));
return task;
}
private TaskScopeStateEntity scope(long taskId, String stateJson, Integer batches) {
TaskScopeStateEntity scope = new TaskScopeStateEntity();
scope.setTaskId(taskId);
scope.setModuleType(JdCollectTaskService.MODULE_TYPE);
scope.setStateJson(stateJson);
scope.setReceivedChunkCount(batches);
scope.setUpdatedAt(LocalDateTime.of(2026, 11, 15, 10, 2));
return scope;
}
@Test
void 总览三档计数与已结束数() {
when(fileTaskMapper.selectCount(any())).thenReturn(2L, 3L, 1L);
var vo = service.dashboard(7L);
assertThat(vo.getPendingTaskCount()).isEqualTo(2L);
assertThat(vo.getSuccessTaskCount()).isEqualTo(3L);
assertThat(vo.getFailedTaskCount()).isEqualTo(1L);
assertThat(vo.getProcessedTaskCount()).as("已结束=成功+失败").isEqualTo(4L);
}
@Test
void 历史行读真源_行数来自京东数据库计数与游标进度() {
FileTaskEntity running = task(34398L, 7L, "RUNNING");
FileTaskEntity failed = task(34400L, 7L, "FAILED");
// selectList 先出 history 主查询(按 createdAt 倒序 limit),乱序返回验证排序
when(fileTaskMapper.selectList(any())).thenReturn(new java.util.ArrayList<>(List.of(failed, running)));
when(jdProductMapper.selectMaps(any())).thenReturn(List.of(
Map.of("task_id", 34398L, "cnt", 120L),
Map.of("task_id", 34400L, "cnt", 88L)));
when(taskScopeStateMapper.selectList(any())).thenReturn(List.of(
scope(34398L, "{\"cursor\":{\"total\":300,\"page\":3},\"itemsReceived\":120}", 4),
scope(34400L, "{\"cursor\":{},\"itemsReceived\":88}", 3)));
var vo = service.history(7L, 50);
assertThat(vo.getItems()).hasSize(2);
// 活跃(RUNNING)排前:失败任务往后靠
assertThat(vo.getItems().get(0).getTaskId()).isEqualTo(34398L);
JdCollectHistoryItemVo first = vo.getItems().get(0);
assertThat(first.getShopName()).as("店铺名来自建单载荷").isEqualTo("花屿巷旗舰店");
assertThat(first.getRowCount()).as("行数只来自 biz_jd_product 计数").isEqualTo(120);
assertThat(first.getItemsReceived()).isEqualTo(120);
assertThat(first.getTotal()).isEqualTo(300);
assertThat(first.getPage()).isEqualTo(3);
assertThat(first.getBatches()).isEqualTo(4);
assertThat(vo.getItems().get(1).getTaskStatus()).isEqualTo("FAILED");
}
@Test
void 批量进度按模块过滤_非本模块任务进missingTaskIds() {
FileTaskEntity jd = task(88L, 7L, "RUNNING");
FileTaskEntity patent = task(89L, 7L, "RUNNING");
patent.setModuleType("APPEARANCE_PATENT");
when(fileTaskMapper.selectList(any())).thenReturn(List.of(jd, patent));
when(jdProductMapper.selectMaps(any())).thenReturn(List.of(Map.of("task_id", 88L, "cnt", 5L)));
when(taskScopeStateMapper.selectOne(any())).thenReturn(
scope(88L, "{\"cursor\":{\"total\":10},\"itemsReceived\":5}", 1));
JdCollectTaskBatchVo vo = service.progressBatch(List.of(88L, 89L, 0L, 88L));
assertThat(vo.getItems()).hasSize(1);
assertThat(vo.getItems().get(0).getTask().getId()).isEqualTo(88L);
assertThat(vo.getItems().get(0).getItems().get(0).getRowCount()).isEqualTo(5);
assertThat(vo.getMissingTaskIds())
.as("非本模块任务不冒充面板条目;非法/重复 id 归一化后不影响")
.containsExactly(89L);
}
@Test
void 删除任务_归属不符一律拒_不碰京东数据库() {
FileTaskEntity other = task(88L, 9L, "FAILED");
when(fileTaskMapper.selectById(88L)).thenReturn(other);
assertThatThrownBy(() -> service.deleteTask(88L, 7L))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("任务不存在");
verify(jdProductMapper, never()).delete(any());
verify(fileTaskMapper, never()).deleteById(any(java.io.Serializable.class));
}
@Test
void 删除任务_先清子行再清主行_scope行与任务行一并收掉() {
FileTaskEntity mine = task(88L, 7L, "FAILED");
when(fileTaskMapper.selectById(88L)).thenReturn(mine);
com.nanri.aiimage.modules.jd.model.entity.JdProductEntity product =
new com.nanri.aiimage.modules.jd.model.entity.JdProductEntity();
product.setId(5001L);
when(jdProductMapper.selectList(any())).thenReturn(List.of(product));
when(jdProductMapper.delete(any())).thenReturn(1);
service.deleteTask(88L, 7L);
// sku/image 子行 → 主行 → scope → 任务行,五次删除一次不落
verify(jdProductSkuMapper, times(1)).delete(any());
verify(jdProductImageMapper, times(1)).delete(any());
verify(jdProductMapper, times(1)).delete(any());
verify(taskScopeStateMapper, times(1)).delete(any());
verify(fileTaskMapper, times(1)).deleteById(any(java.io.Serializable.class));
}
}