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>