feat(jd): 整合自定义分类并保护商品数据

This commit is contained in:
2026-11-16 16:12:04 +08:00
parent 79eb7a7f16
commit 8c81546aa7
23 changed files with 708 additions and 1643 deletions
@@ -71,6 +71,11 @@ import java.util.List;
* {@code sale_status} 查询参数同窗撤掉(商品状态筛选框已从前端下线)。店铺筛选参数
* {@code shop_name} 同日客户口径「店铺不需要」同窗撤掉(前端筛选框与列下线;DB 列
* {@code biz_jd_product.shop_name} 与详情/回传链不动)。</p>
*
* <p><b>分类并入类目(2026-11-16 用户口径「分类不要了,全部整合到类目里」)</b>:
* 分页查询的 {@code ungrouped}/{@code grouped}(分类状态档)整批撤掉;独立「分类」筛选框
* 前端下线,{@code group_id} 保留、改由类目面板「自定义分类」选中驱动(存量分组数据由
* V201 清空,分组表保留作自定义分类存储)。</p>
*/
@RestController
@RequiredArgsConstructor
@@ -100,19 +105,18 @@ public class AdminJdProductController {
@Parameter(description = "商品编码(包含匹配)") @RequestParam(name = "product_code", required = false) String productCode,
@Parameter(description = "标题关键词(包含匹配)") @RequestParam(name = "title_keyword", required = false) String titleKeyword,
@Parameter(description = "类目(京麦末级类目名,eq)") @RequestParam(name = "category", required = false) String category,
@Parameter(description = "分组 id") @RequestParam(name = "group_id", required = false) Long groupId,
@Parameter(description = "只看未分类(group_id IS NULL,店铺分类弹窗「未分类」范围)")
@RequestParam(name = "ungrouped", required = false) Boolean ungrouped,
@Parameter(description = "只看已分类(group_id IS NOT NULL,页面筛选行「分类状态」档)")
@RequestParam(name = "grouped", required = false) Boolean grouped,
@Parameter(description = "自定义分类 id(类目面板「自定义分类」选中驱动)") @RequestParam(name = "group_id", required = false) Long groupId,
@Parameter(description = "同步开始时间(包含,ISO datetime)")
@RequestParam(name = "synced_from", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime syncedFrom,
@Parameter(description = "同步结束时间(包含,ISO datetime)")
@RequestParam(name = "synced_to", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime syncedTo,
HttpServletRequest request) {
requireJdAccess(request);
if (request.getParameter("grouped") != null || request.getParameter("ungrouped") != null) {
throw new BusinessException("页面版本已更新,请刷新页面后重新查询");
}
return ApiResponse.success(jdProductQueryService.page(page, pageSize,
productCode, titleKeyword, category, groupId, ungrouped, grouped, syncedFrom, syncedTo));
productCode, titleKeyword, category, groupId, syncedFrom, syncedTo));
}
@GetMapping("/categories")
@@ -42,19 +42,18 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
/**
* 后管只读页总数(Phase 5,v1.4):与 {@link #selectProductPage} 同一过滤面
* (商品编码/标题关键词/类目/分组/未分类/已分类/同步起止,全部 SQL 侧下推,
* 铁律 20)。公司维过滤已随 2026-11-16「删除掉公司 tab,参考品牌数据库」撤掉
* (商品编码/标题关键词/类目/自定义分类/同步起止,全部 SQL 侧下推,铁律 20)。
* 公司维过滤已随 2026-11-16「删除掉公司 tab,参考品牌数据库」撤掉
* (数据全平台公共);{@code groupId} 为空或 ≤0=不过滤该面;
* {@code ungrouped=true}=只看未分类行(group_id IS NULL,店铺分类弹窗「未分类」范围);
* {@code grouped=true}=只看已分类行(group_id IS NOT NULL,页面筛选行「分类状态」档)。
* {@code ungrouped}/{@code grouped} 两档(未分类/已分类)已随同日口径
* 「分类不要了,全部整合到类目里」撤掉(独立「分类」筛选框前端下线,
* {@code groupId} 改由类目面板「自定义分类」选中驱动)。
* 店铺名过滤参数已随 2026-11-15 客户口径「店铺不需要」同窗撤掉(DB 列与详情/回传链不动)。
*/
long countProducts(@Param("productCode") String productCode,
@Param("titleKeyword") String titleKeyword,
@Param("category") String category,
@Param("groupId") Long groupId,
@Param("ungrouped") Boolean ungrouped,
@Param("grouped") Boolean grouped,
@Param("syncedFrom") LocalDateTime syncedFrom,
@Param("syncedTo") LocalDateTime syncedTo);
@@ -94,8 +93,6 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
@Param("titleKeyword") String titleKeyword,
@Param("category") String category,
@Param("groupId") Long groupId,
@Param("ungrouped") Boolean ungrouped,
@Param("grouped") Boolean grouped,
@Param("syncedFrom") LocalDateTime syncedFrom,
@Param("syncedTo") LocalDateTime syncedTo,
@Param("offset") long offset,
@@ -75,15 +75,15 @@ public class JdProductQueryService {
/**
* 分页查询商品行({@code synced_at} 倒序)。
*
* <p>过滤面:商品编码 contains、标题关键词 contains、类目 eq、分组 eq、
* 未分类(group_id IS NULL,店铺分类弹窗「未分类」范围)、
* 已分类(group_id IS NOT NULL,页面筛选行「分类状态」档,2026-11-15 客户口径
* 「查询已分类的跟未分类,分类好的可以不显示」)、同步起止(datetime 闭区间)。
* <p>过滤面:商品编码 contains、标题关键词 contains、类目 eq、分组 eq(2026-11-16 起
* 由类目面板「自定义分类」选中驱动)、同步起止(datetime 闭区间)。
* 未分类/已分类两档(ungrouped/grouped)与独立「分类」筛选框随同日口径
* 「分类不要了,全部整合到类目里」整批撤掉。
* 公司维过滤已随 2026-11-16 摘公司 tab 整批撤掉(全平台公共)。</p>
*/
public JdProductPageVo page(Long page, Long pageSize,
String productCode, String titleKeyword, String category, Long groupId, Boolean ungrouped,
Boolean grouped, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
String productCode, String titleKeyword, String category, Long groupId,
LocalDateTime syncedFrom, LocalDateTime syncedTo) {
if (syncedFrom != null && syncedTo != null && syncedFrom.isAfter(syncedTo)) {
throw new BusinessException("开始时间不能晚于结束时间");
}
@@ -91,14 +91,14 @@ public class JdProductQueryService {
long safeSize = pageSize == null ? PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT
: Math.min(Math.max(pageSize, 1), MAX_PAGE_SIZE);
Filter filter = filter(productCode, titleKeyword, category,
groupId, ungrouped, grouped, syncedFrom, syncedTo);
groupId, syncedFrom, syncedTo);
long total = jdProductMapper.countProducts(filter.productCode(),
filter.titleKeyword(), filter.category(), filter.groupId(), filter.ungrouped(),
filter.grouped(), filter.syncedFrom(), filter.syncedTo());
filter.titleKeyword(), filter.category(), filter.groupId(),
filter.syncedFrom(), filter.syncedTo());
List<JdProductEntity> rows = total <= 0 ? List.of()
: jdProductMapper.selectProductPage(filter.productCode(),
filter.titleKeyword(), filter.category(), filter.groupId(), filter.ungrouped(),
filter.grouped(), filter.syncedFrom(), filter.syncedTo(),
filter.titleKeyword(), filter.category(), filter.groupId(),
filter.syncedFrom(), filter.syncedTo(),
(safePage - 1) * safeSize, (int) safeSize);
List<JdProductListItemVo> items = rows.stream().map(this::toListItem).toList();
fillGroupNames(items);
@@ -175,21 +175,16 @@ public class JdProductQueryService {
/** 服务侧过滤面归一:groupId ≤0 按没选处理,文本 trim 后空串归 null(无公司维,2026-11-16 摘)。 */
private record Filter(String productCode, String titleKeyword,
String category, Long groupId, Boolean ungrouped, Boolean grouped,
String category, Long groupId,
LocalDateTime syncedFrom, LocalDateTime syncedTo) {
}
private static Filter filter(String productCode, String titleKeyword,
String category, Long groupId, Boolean ungrouped, Boolean grouped,
String category, Long groupId,
LocalDateTime syncedFrom, LocalDateTime syncedTo) {
Long safeGroupId = groupId != null && groupId > 0 ? groupId : null;
// 未分类与其它过滤面同口径:没显式要(true)=null=不进 IS NULL 分支。
Boolean safeUngrouped = Boolean.TRUE.equals(ungrouped) ? Boolean.TRUE : null;
// 已分类同口径:true=只看 group_id IS NOT NULL 的行;与未分类同选时 SQL 侧两谓词相与=空集
// (前端下拉是单选,正常到不了这个组合)。
Boolean safeGrouped = Boolean.TRUE.equals(grouped) ? Boolean.TRUE : null;
return new Filter(trimToNull(productCode), trimToNull(titleKeyword),
trimToNull(category), safeGroupId, safeUngrouped, safeGrouped, syncedFrom, syncedTo);
trimToNull(category), safeGroupId, syncedFrom, syncedTo);
}
// ---------------------------------------------------------------- 列表/详情搬运
@@ -44,11 +44,8 @@ import java.util.Set;
* statObject 对撞落 {@code biz_jd_product_image},唯一出口
* {@link JdProductImageIngestService#registerTaskImages})→
* scope 台账合并写(断点游标 + 批次号 + 累计行数)→
* {@code isEnd=true} 时任务落 SUCCESS 并执行<b>覆盖清理</b>:同一店铺里本次没采到、
* 所属任务已不在在飞的旧行整行删掉(用户拍板 2026-11-15:重爬同一店铺直接覆盖旧数据)。
* 清理只在收尾时做且本次 {@code itemsReceived=0} 不清(B94-01 教训:载荷读不准的空回传
* 不得抹掉存量),在飞任务(PENDING/RUNNING)的行不动,删除本体走
* {@link JdProductRowDeleteService#deleteRows}(SKU/图片行级联)。</p>
* {@code isEnd=true} 时任务落 SUCCESS;由于现有回传不能证明分页完整,暂不执行覆盖清理,
* 保留历史商品并记录警告。
*
* <p>回传 payload 键集契约({@code JdIngestPayloadContractTest} 绊线钉住):顶层
* {@code batchId / cursor / items / isEnd}(+ {@code error} 失败上报),行内
@@ -168,7 +165,9 @@ public class JdResultIngestService {
int itemsReceived = mergeScopeState(scope, request, request.getItems() == null ? 0 : request.getItems().size());
if (Boolean.TRUE.equals(request.getIsEnd())) {
finalizeSuccess(task, itemsReceived);
purgeSupersededRows(task, companyId, itemsReceived);
// 客户端缺少可验证的完整分页回执,不能据此删除未回传的旧商品。
log.warn("[{}] 缺少全店分页完整性证明,跳过覆盖清理 taskId={} itemsReceived={}",
LOG_TAG, task.getId(), itemsReceived);
}
JdIngestChunkVo vo = new JdIngestChunkVo();
vo.setAccept(true);
@@ -235,39 +234,6 @@ public class JdResultIngestService {
}
}
/**
* 覆盖清理(用户拍板 2026-11-15:重爬同一店铺直接覆盖旧数据,采集完成时清)。
*
* <p>只清「本次没采到」的旧行:同公司、同店铺(shop_id 取本任务落过的行)、
* task_id 不是本任务、且所属任务已不在在飞的行(在飞任务如 PENDING/RUNNING 的行不动,
* 同店铺并发两单时先收尾的单不得抹掉仍在跑的单的行,后收尾的单会在自己的收尾时清)。
* 删除本体走 {@link JdProductRowDeleteService#deleteRows}(SKU/图片行级联,同一事务)。</p>
*
* <p>两条护栏:① 本次 {@code itemsReceived=0} 不清(B94-01 教训——载荷读不准的空回传
* 一旦清库=存量整库抹掉);② 清理失败不推翻采集结果(新数据已完整落库且任务已落
* SUCCESS),但必须留痕不静默:error 日志+scope 台账记 lastError。</p>
*/
private void purgeSupersededRows(FileTaskEntity task, Long companyId, int itemsReceived) {
if (itemsReceived <= 0) {
log.warn("[{}] 收尾回传 0 行,跳过覆盖清理(B94-01:不清旧) taskId={}", LOG_TAG, task.getId());
return;
}
try {
List<Long> staleIds = jdProductMapper.selectSupersededProductIds(companyId, task.getId());
if (staleIds.isEmpty()) {
log.info("[{}] 覆盖清理无候选行(本次已覆盖全部旧行) taskId={}", LOG_TAG, task.getId());
return;
}
int deleted = jdProductRowDeleteService.deleteRows(staleIds);
log.info("[{}] 覆盖清理完成:本次没采到的旧行已删 taskId={} candidates={} deleted={}",
LOG_TAG, task.getId(), staleIds.size(), deleted);
} catch (Exception ex) {
log.error("[{}] 覆盖清理失败(本次采集数据已完整落库,仅旧数据未清,可重爬一次补清) taskId={}",
LOG_TAG, task.getId(), ex);
recordLastError(task.getId(), "覆盖清理失败(本次采集数据已完整落库,仅旧数据未清,可重爬一次补清)");
}
}
// ---------- V199 保护闸:人工编辑锁与删除忽略 ----------
/**
@@ -0,0 +1,25 @@
-- 清空旧的京东自定义分类,商品保留。备份分类及商品原归属以支持人工恢复。
-- 必须停止所有实例的分类及商品归属写入,等待在途事务结束,直至迁移与核对完成。
-- 备份表已存在时立即失败,不覆盖或复用未知备份;失败后须先人工检查源表和备份表。
CREATE TABLE biz_jd_product_group_v201_backup LIKE biz_jd_product_group;
INSERT INTO biz_jd_product_group_v201_backup (id, company_id, name, sort_order, created_at, updated_at)
SELECT id, company_id, name, sort_order, created_at, updated_at FROM biz_jd_product_group;
CREATE TABLE biz_jd_product_group_assignment_v201_backup (
product_id BIGINT NOT NULL PRIMARY KEY,
group_id BIGINT NOT NULL
) ENGINE=InnoDB;
INSERT INTO biz_jd_product_group_assignment_v201_backup (product_id, group_id)
SELECT id, group_id FROM biz_jd_product WHERE group_id IS NOT NULL;
UPDATE biz_jd_product SET group_id = NULL WHERE group_id IS NOT NULL;
DELETE FROM biz_jd_product_group;
-- 人工回滚前停止所有分类及商品归属写入,核对主键、分类名、缺失商品、已重新归类
-- 商品与孤儿归属;有冲突须先人工处理。以下只恢复到 V200 后的状态,不能还原 V200
-- 已合并的历史同名分类。MySQL DDL 会隐式提交,失败后应先检查源表、备份表及
-- flyway_schema_history;Flyway repair 不会回滚数据。
-- INSERT INTO biz_jd_product_group (id, company_id, name, sort_order, created_at, updated_at)
-- SELECT id, company_id, name, sort_order, created_at, updated_at FROM biz_jd_product_group_v201_backup;
-- UPDATE biz_jd_product p JOIN biz_jd_product_group_assignment_v201_backup b ON b.product_id = p.id
-- SET p.group_id = b.group_id WHERE p.group_id IS NULL;
@@ -101,9 +101,9 @@
</select>
<!-- 后管只读页(Phase 5,v1.4)过滤面:商品编码/标题关键词(contains LIKE)、类目 eq、
分组 eq、未分类(group_id IS NULL,店铺分类弹窗「未分类」范围)、已分类
(group_id IS NOT NULL,页面筛选行「分类状态」档,2026-11-15 客户口径
「查询已分类的跟未分类,分类好的可以不显示」)、同步起止(datetime 闭区间)。
分组 eq(2026-11-16 起由类目面板「自定义分类」选中驱动,独立「分类」筛选框与
「分类状态」档随同日口径「分类不要了,全部整合到类目里」撤掉:ungrouped/grouped
两参数与谓词同窗删除)、同步起止(datetime 闭区间)。
公司维过滤随 2026-11-16「删除掉公司 tab,参考品牌数据库」撤掉(数据全平台公共;
行 company_id 列仍是同步来源标记,不构成可见性边界)。
sale_status 筛选随 2026-11-15 客户口径撤掉;店铺名过滤同日客户口径「店铺不需要」
@@ -114,8 +114,6 @@
<if test="titleKeyword != null and titleKeyword != ''">AND p.title LIKE CONCAT('%', #{titleKeyword}, '%')</if>
<if test="category != null and category != ''">AND p.category_path = #{category}</if>
<if test="groupId != null and groupId &gt; 0">AND p.group_id = #{groupId}</if>
<if test="ungrouped != null and ungrouped">AND p.group_id IS NULL</if>
<if test="grouped != null and grouped">AND p.group_id IS NOT NULL</if>
<if test="syncedFrom != null">AND p.synced_at &gt;= #{syncedFrom}</if>
<if test="syncedTo != null">AND p.synced_at &lt;= #{syncedTo}</if>
</sql>
@@ -132,9 +132,11 @@ class AdminJdProductControllerTest {
void 分页参数snake且带默认值() throws IOException {
String source = stripComments(Files.readString(CONTROLLER));
for (String param : List.of("page_size", "product_code", "title_keyword",
"group_id", "ungrouped", "grouped", "synced_from", "synced_to")) {
"group_id", "synced_from", "synced_to")) {
assertTrue(source.contains(param), "查询参数必须 snake_case:" + param);
}
assertFalse(source.contains("@RequestParam(name = \"ungrouped\""));
assertFalse(source.contains("@RequestParam(name = \"grouped\""));
// company_id 参数已随 2026-11-16「删除掉公司 tab」整批摘除(读侧零公司维度支另有反向钉)。
assertFalse(source.contains("company_id"), "company_id 请求参数必须已删除,不得回潜");
// 商品状态筛选框已随 2026-11-15 客户口径从前端下线 ⇒ 服务端同名参数一起撤(反向钉住防回潜)。
@@ -155,9 +157,14 @@ class AdminJdProductControllerTest {
assertTrue(mapperSource.contains("@Mapper"), "mapper 接口必须带 @Mapper");
Path xml = findJdProductMapperXml();
assertNotNull(xml, "mapper XML 必须落在 src/main/resources/mapper/**(铁律 1)");
String xmlText = stripComments(Files.readString(xml));
String xmlText = stripComments(Files.readString(xml)).replaceAll("(?s)<!--.*?-->", "");
assertTrue(xmlText.contains("countProducts"), "XML 必须有 countProducts(只读页总数)");
assertTrue(xmlText.contains("selectProductPage"), "XML 必须有 selectProductPage(只读页分页)");
assertTrue(xmlText.contains("AND p.group_id = #{groupId}"), "自定义分类必须下推 SQL");
assertFalse(xmlText.contains("ungrouped"));
assertFalse(xmlText.contains("grouped"));
assertEquals(2, xmlText.split("<include refid=\"productConds\"", -1).length - 1,
"计数和分页必须复用同一过滤条件");
assertFalse(xmlText.contains("LIMIT #{page}"),
"分页只收服务钳制后的 offset/limit,绝不 LIMIT #{page} 直拼(铁律 15)");
assertTrue(xmlText.contains("ORDER BY p.synced_at DESC, p.id DESC"),
@@ -201,17 +208,42 @@ class AdminJdProductControllerTest {
pageVo.setTotal(0L);
pageVo.setPage(1L);
pageVo.setPageSize((long) PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT);
when(queryService.page(1L, 20L, null, null, null, null, null, null, null, null)).thenReturn(pageVo);
when(queryService.page(1L, 20L, null, null, null, null, null, null)).thenReturn(pageVo);
// 2026-11-16 起请求面没有 company_id:任何授权身份看到的都是同一份全量。
ApiResponse<JdProductPageVo> response = controller.page(1L, 20L, null, null, null, null, null,
null, null, null, request);
null, request);
verify(queryService).page(1L, 20L, null, null, null, null, null, null, null, null);
verify(queryService).page(1L, 20L, null, null, null, null, null, null);
assertEquals(Boolean.TRUE, response.isSuccess(), "拒绝走 403 业务码,放行必须是 success 信封(B80-03)");
assertEquals(pageVo, response.getData());
}
@Test
void 旧分类筛选参数明确拒绝并提示刷新() {
JdProductQueryService queryService = mock(JdProductQueryService.class);
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
authSupport, mock(PermissionMenuService.class));
AdminUserEntity operator = new AdminUserEntity();
operator.setId(7L);
HttpServletRequest request = mock(HttpServletRequest.class);
when(authSupport.requireUser(request)).thenReturn(operator);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
for (String name : List.of("grouped", "ungrouped")) {
for (String value : List.of("true", "false", "")) {
when(request.getParameter(name)).thenReturn(value);
BusinessException ex = assertThrows(BusinessException.class,
() -> controller.page(1L, 20L, null, null, null, 7L, null, null, request));
assertTrue(ex.getMessage().contains("刷新页面"));
when(request.getParameter(name)).thenReturn(null);
}
}
verify(queryService, never()).page(any(), any(), any(), any(), any(), any(), any(), any());
}
@Test
void 类目下拉端点走同一鉴权正门并回全平台选项集() {
JdProductQueryService queryService = mock(JdProductQueryService.class);
@@ -321,23 +353,23 @@ class AdminJdProductControllerTest {
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
mock(com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper.class),
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(1L);
when(mapper.countProducts(any(), any(), any(), any(), any(), any())).thenReturn(1L);
// page=0 / size=null ⇒ 归一为第 1 页、默认 20 条;offset = 0。
service.page(0L, null, null, null, null, null, null, null, null, null);
service.page(0L, null, null, null, null, null, null, null);
ArgumentCaptor<Long> offset = ArgumentCaptor.forClass(Long.class);
ArgumentCaptor<Integer> limit = ArgumentCaptor.forClass(Integer.class);
verify(mapper).selectProductPage(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
isNull(), isNull(), offset.capture(), limit.capture());
offset.capture(), limit.capture());
assertEquals(0L, offset.getValue());
assertEquals(PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT, limit.getValue());
// page=Long.MAX_VALUE / size=500 ⇒ 钳到 10_000 页、100 条;乘积 999_900 不得变负(铁律 15)。
service.page(Long.MAX_VALUE, 500L, null, null, null, null, null, null, null, null);
service.page(Long.MAX_VALUE, 500L, null, null, null, null, null, null);
ArgumentCaptor<Long> offsetMax = ArgumentCaptor.forClass(Long.class);
ArgumentCaptor<Integer> limitMax = ArgumentCaptor.forClass(Integer.class);
verify(mapper, times(2)).selectProductPage(any(), any(), any(), any(), any(), any(),
any(), any(), offsetMax.capture(), limitMax.capture());
offsetMax.capture(), limitMax.capture());
assertEquals(999_900L, offsetMax.getValue(), "(page-1)*page_size 最大 999_900,钳制后不得溢出变负");
assertEquals(100, limitMax.getValue());
}
@@ -351,9 +383,9 @@ class AdminJdProductControllerTest {
LocalDateTime from = LocalDateTime.of(2026, 11, 14, 12, 0);
LocalDateTime to = LocalDateTime.of(2026, 11, 14, 8, 0);
BusinessException ex = assertThrows(BusinessException.class,
() -> service.page(1L, 20L, null, null, null, null, null, null, from, to));
() -> service.page(1L, 20L, null, null, null, null, from, to));
assertTrue(ex.getMessage().contains("开始时间不能晚于结束时间"), "倒挂时间按业务异常拒绝");
verify(mapper, never()).countProducts(any(), any(), any(), any(), any(), any(), any(), any());
verify(mapper, never()).countProducts(any(), any(), any(), any(), any(), any());
}
@Test
@@ -362,37 +394,14 @@ class AdminJdProductControllerTest {
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
mock(com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper.class),
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(0L);
when(mapper.countProducts(any(), any(), any(), any(), any(), any())).thenReturn(1L);
service.page(1L, 20L, " SKU-1 ", " 连衣裙 ", " 艺考形体服 ", 7L, null, null);
verify(mapper).countProducts("SKU-1", "连衣裙", "艺考形体服", 7L, null, null);
verify(mapper).selectProductPage("SKU-1", "连衣裙", "艺考形体服", 7L, null, null, 0L, 20);
// groupId ≤0 按没选分组处理;文本两侧空白剥掉。
// 类目(京麦末级类目名)走同一过滤面:传值原样直传、空白归 null。
service.page(1L, 20L, " SKU-1 ", " 连衣裙 ", " 艺考形体服 ", 0L, null, null, null, null);
verify(mapper).countProducts(eq("SKU-1"), eq("连衣裙"),
eq("艺考形体服"), isNull(), isNull(), isNull(), isNull(), isNull());
// 全量口径(2026-11-16 起读侧无公司维)⇒ 过滤面全 null。
service.page(1L, 20L, null, null, null, null, null, null, null, null);
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
isNull(), isNull(), isNull());
// 未分类范围(店铺分类弹窗「未分类」页签):ungrouped=true 原样下推到 SQL 侧。
service.page(1L, 20L, null, null, null, null, Boolean.TRUE, null, null, null);
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), eq(Boolean.TRUE),
isNull(), isNull(), isNull());
// ungrouped=false / 缺省按不过滤处理(Filter 归一成 null,不进 IS NULL 分支);
// 与②「全量」那发同形状,累计 2 次。
service.page(1L, 20L, null, null, null, null, Boolean.FALSE, null, null, null);
verify(mapper, times(2)).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
isNull(), isNull(), isNull());
// 已分类档(页面筛选行「分类状态」=已分类,2026-11-15 客户口径「分类好的可以不显示」反选):
// grouped=true 原样下推到 SQL 侧(group_id IS NOT NULL),ungrouped 不动。
service.page(1L, 20L, null, null, null, null, null, Boolean.TRUE, null, null);
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
eq(Boolean.TRUE), isNull(), isNull());
// grouped=false / 缺省同口径归一成 null(不进 IS NOT NULL 分支);与②「全量」累计 3 次。
service.page(1L, 20L, null, null, null, null, null, Boolean.FALSE, null, null);
verify(mapper, times(3)).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
isNull(), isNull(), isNull());
service.page(1L, 20L, " ", null, null, 0L, null, null);
verify(mapper).countProducts(null, null, null, null, null, null);
verify(mapper).selectProductPage(null, null, null, null, null, null, 0L, 20);
}
@Test
@@ -591,8 +600,8 @@ class AdminJdProductControllerTest {
grouped.setGroupId(3L);
JdProductEntity missing = productEntity(2L);
missing.setGroupId(99L);
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(2L);
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(),
when(mapper.countProducts(any(), any(), any(), any(), any(), any())).thenReturn(2L);
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(),
anyLong(), anyInt())).thenReturn(List.of(grouped, missing));
com.nanri.aiimage.modules.jd.model.entity.JdProductGroupEntity group =
new com.nanri.aiimage.modules.jd.model.entity.JdProductGroupEntity();
@@ -600,7 +609,7 @@ class AdminJdProductControllerTest {
group.setName("秋冬款");
when(groupMapper.selectBatchIds(any())).thenReturn(List.of(group));
JdProductPageVo vo = service.page(1L, 20L, null, null, null, null, null, null, null, null);
JdProductPageVo vo = service.page(1L, 20L, null, null, null, null, null, null);
assertEquals("秋冬款", vo.getItems().get(0).getGroupName(), "行集 group_id 一次取齐分组名(无逐行查询)");
assertNull(vo.getItems().get(1).getGroupName(), "分组已删=不猜名字(铁律 30),前端展示 -");
@@ -377,26 +377,49 @@ class JdProductSyncServiceTest {
// ---------- 覆盖清理(2026-11-15 用户拍板:重爬同一店铺,采集完成时清掉旧数据) ----------
private JdCollectTaskCreateRequest fullShopRequest() {
JdCollectTaskCreateRequest request = createRequest();
request.setListTabs(List.of("ONSALE", "OFFSALE"));
request.setMaxProducts(0);
return request;
}
@Test
void 成功收尾后清掉本次没采到的旧行() {
void 非完整店铺采集收尾不得查询或删除其他商品() {
stubHappyPath();
JdCollectTaskCreateRequest singleTab = createRequest();
JdCollectTaskCreateRequest category = fullShopRequest();
category.setCategoryPaths(List.of("女装"));
JdCollectTaskCreateRequest limited = fullShopRequest();
limited.setMaxProducts(10);
JdCollectTaskCreateRequest missingTabs = fullShopRequest();
missingTabs.setListTabs(null);
JdCollectTaskCreateRequest invalidLimit = fullShopRequest();
invalidLimit.setMaxProducts(-1);
for (JdCollectTaskCreateRequest config : List.of(singleTab, category, limited, missingTabs, invalidLimit)) {
when(collectTaskService.readCreateRequest(any(FileTaskEntity.class))).thenReturn(config);
JdIngestChunkRequest request = chunk(item("10036442942137", "艺考形体服女"));
request.setIsEnd(true);
assertThat(service.submitChunk(TASK_ID, request, true).isAccept()).isTrue();
}
verify(jdProductMapper, never()).selectSupersededProductIds(any(), any());
verify(jdProductRowDeleteService, never()).deleteRows(anyList());
}
@Test
void 全店请求完成但缺少分页完整性证明时不清旧行() {
stubHappyPath();
when(collectTaskService.readCreateRequest(any(FileTaskEntity.class))).thenReturn(fullShopRequest());
when(fileTaskMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
// 同店铺旧行:上次采集落的、本次没采到的商品行
when(jdProductMapper.selectSupersededProductIds(COMPANY_ID, TASK_ID)).thenReturn(List.of(901L, 902L));
when(jdProductRowDeleteService.deleteRows(anyList())).thenReturn(2);
JdIngestChunkRequest request = chunk(item("10036442942137", "艺考形体服女"));
request.setIsEnd(true);
JdIngestChunkVo vo = service.submitChunk(TASK_ID, request, true);
assertThat(vo.isAccept()).isTrue();
// 删除走行级联唯一出口(SKU/图片行随删),候选 id 原样透传
verify(jdProductRowDeleteService).deleteRows(List.of(901L, 902L));
// 删除只发生在收尾之后:先落库,后清理
org.mockito.InOrder order = inOrder(jdProductMapper, jdProductRowDeleteService);
order.verify(jdProductMapper).upsertBatch(anyList());
order.verify(jdProductMapper).selectSupersededProductIds(COMPANY_ID, TASK_ID);
order.verify(jdProductRowDeleteService).deleteRows(anyList());
verify(jdProductMapper, never()).selectSupersededProductIds(any(), any());
verify(jdProductRowDeleteService, never()).deleteRows(anyList());
}
@Test
@@ -428,26 +451,6 @@ class JdProductSyncServiceTest {
verify(jdProductRowDeleteService, never()).deleteRows(anyList());
}
@Test
void 覆盖清理失败不推翻回执且台账留痕() {
stubHappyPath();
when(fileTaskMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
when(jdProductMapper.selectSupersededProductIds(COMPANY_ID, TASK_ID))
.thenThrow(new RuntimeException("db down"));
JdIngestChunkRequest request = chunk(item("10036442942137", "艺考形体服女"));
request.setIsEnd(true);
JdIngestChunkVo vo = service.submitChunk(TASK_ID, request, true);
// 新数据已完整落库且任务已 SUCCESS:清理失败不改回执、不炸整批,但必须留痕不静默
assertThat(vo.isAccept()).isTrue();
verify(jdProductRowDeleteService, never()).deleteRows(anyList());
ArgumentCaptor<TaskScopeStateEntity> scopeCaptor = ArgumentCaptor.forClass(TaskScopeStateEntity.class);
verify(taskScopeStateMapper, times(2)).updateById(scopeCaptor.capture());
assertThat(scopeCaptor.getAllValues().stream().map(TaskScopeStateEntity::getStateJson))
.anySatisfy(json -> assertThat(json).contains("覆盖清理失败"));
}
// ---------- V199 保护闸:人工编辑锁与删除忽略 ----------
@Test