feat(jd): 京麦授权类目树按店铺存(客户端上报+后管并集读取)

后管「京东数据库 › 商品目录」的树原是 2026-11-16 从花屿巷旗舰店抓包硬编码的快照,
换店(家居日用数据库店)打开就是整棵服饰内衣/母婴/运动户外、节点全 0,264 件收纳品
全落「树外兜底」。授权类目树是按店铺的资产,改由客户端采集窗口顺手收、服务端按店铺存。

- V215__jd_shop_category_tree.sql:biz_jd_shop_category_tree(shop_id × node_id 唯一,
  存平台公共类目名,不含业务数据 ⇒ 不做公司维隔离)
- POST /api/jd-shop-category-tree:整店快照替换(先删后插,500 行一批);缺店铺 ID/
  空树/超 5000 节点一律拒且不碰库(空树拒=不静默清空已有快照)
- GET /api/jd-shop-category-tree:已上报店铺并集森林(node_id 全局同一套 ⇒ 合并去重;
  父缺失提升为根;末级 children 置 null)+店铺清单(节点数、最新采集时间)
- 鉴权 requireUserOrInternal:后管走 JWT、采集器走 X-Internal-Token(与分片回传同形)

判据 JdShopCategoryTreeServiceTest 13 支(扁平化层级/父/同级序、空名节点丢自身保子级、
四条拒绝分支不碰库、先删后插与满批切分、并集去重/父缺失提根/末级 null、店铺清单口径);
SqlLayeringConventionTest.XML_STATEMENT_TOTAL 154→156(delete+insert 两条写语句,无 select)。
This commit is contained in:
2026-11-23 21:27:41 +08:00
parent c64dbfd8d1
commit 854257712a
12 changed files with 844 additions and 1 deletions
@@ -146,7 +146,10 @@ class SqlLayeringConventionTest {
// mapper/company/CompanyMapper.xml(898e8a5e 走了注解通道,本测试当窗抓到),SQL 文本
// 未改写;逐条登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(同文件 XML_SELECT_TOTAL
// 同轮 107 → 109)。
private static final int XML_STATEMENT_TOTAL = 154;
// 154 → 156:2026-11-23 京麦授权类目树(V215)新落 mapper/jd/JdShopCategoryTreeMapper.xml
// 2 条写语句——deleteByShopId(整店快照替换第一步,1 delete)+ insertBatch(500 行一批,
// 1 insert);全部新写非迁移语句,该文件 XML_SELECT_TOTAL 不动(无 select)。
private static final int XML_STATEMENT_TOTAL = 156;
/**
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
@@ -0,0 +1,312 @@
package com.nanri.aiimage.modules.jd.service;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.jd.mapper.JdShopCategoryTreeMapper;
import com.nanri.aiimage.modules.jd.model.dto.JdShopCategoryNodeDto;
import com.nanri.aiimage.modules.jd.model.dto.JdShopCategoryTreeReportRequest;
import com.nanri.aiimage.modules.jd.model.entity.JdShopCategoryTreeEntity;
import com.nanri.aiimage.modules.jd.model.vo.JdShopCategoryNodeVo;
import com.nanri.aiimage.modules.jd.model.vo.JdShopCategoryTreeVo;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
/**
* 京麦授权类目树(V215,2026-11-23):按店铺整店快照替换 + 后管并集森林。
*
* <p>判据:flatten 的层级/父 id/同级原序;缺名字或缺 id 的节点自身不落库但子级上提
* (挂到上一层父,不丢整支);缺店铺 ID、空树、超 5000 节点一律拒且**不碰库**
* (空树拒=不静默清空已有快照);先删后插的顺序与 500 行分批;读侧按 node_id 合并
* (平台类目 id 全局同一套,多店并集去重)+父缺失提升为根+末级 children 置 null;
* 店铺清单累加节点数并取最新采集时间。</p>
*/
class JdShopCategoryTreeServiceTest {
private final JdShopCategoryTreeMapper mapper = mock(JdShopCategoryTreeMapper.class);
private final JdShopCategoryTreeService service = new JdShopCategoryTreeService(mapper);
@BeforeAll
static void initTableInfo() {
// 读侧 wrapper 用 lambda 列引用,裸单测 JVM 里不初始化就解析不了(同 JdGroupShopServiceTest)。
MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
TableInfoHelper.initTableInfo(assistant, JdShopCategoryTreeEntity.class);
}
// ===================== 上报:扁平化 =====================
@Test
void report_扁平化保留层级父子与同级顺序() {
JdShopCategoryTreeReportRequest request = request("228346061", "家居日用数据库店",
node("服饰内衣", "1315",
node("女装", "1343", node("连衣裙", "9719")),
node("男装", "1344")),
node("运动户外", "1318"));
int nodes = service.report(request);
assertEquals(5, nodes);
List<JdShopCategoryTreeEntity> rows = captureInserted(1).get(0);
assertEquals(List.of("1315", "1343", "9719", "1344", "1318"),
rows.stream().map(JdShopCategoryTreeEntity::getNodeId).collect(Collectors.toList()),
"DFS 前序落库(同级 order 即 sort_order)");
JdShopCategoryTreeEntity root = rows.get(0);
assertEquals(1, root.getNodeLevel());
assertNull(root.getParentNodeId(), "一级节点 parent 为空");
assertEquals(0, root.getSortOrder());
assertEquals("228346061", root.getShopId());
assertEquals("家居日用数据库店", root.getShopName());
assertEquals(2, rows.get(1).getNodeLevel());
assertEquals("1315", rows.get(1).getParentNodeId());
assertEquals(3, rows.get(2).getNodeLevel());
assertEquals("1343", rows.get(2).getParentNodeId());
assertEquals(1, rows.get(3).getSortOrder(), "女装已占 order 0,男装是 1");
assertEquals(1, rows.get(4).getSortOrder(), "运动户外在根层也是第 2 个");
assertNotNull(rows.get(0).getCapturedAt(), "整店快照共用一个采集时间");
assertEquals(rows.get(0).getCapturedAt(), rows.get(4).getCapturedAt());
}
@Test
void report_缺名字或id的节点自身不落库但子级上提() {
JdShopCategoryTreeReportRequest request = request("228346061", "家居日用数据库店",
node("服饰内衣", "1315",
node(null, "9999", node("保留", "1343")),
node("无id", null, node("也保留", "1344"))));
service.report(request);
List<JdShopCategoryTreeEntity> rows = captureInserted(1).get(0);
assertEquals(List.of("1315", "1343", "1344"),
rows.stream().map(JdShopCategoryTreeEntity::getNodeId).collect(Collectors.toList()),
"缺名字/缺 id 的父节点吃掉自身,但子级上提(不丢整支)");
assertEquals("1315", rows.get(1).getParentNodeId(), "上提后挂到最近的有效父(按 node_id 建父子)");
assertEquals(3, rows.get(1).getNodeLevel(),
"层级按实际深度保留:跳过的空名节点占了一级,子级不重算成 2(读侧排序仍先父后子)");
assertEquals("1315", rows.get(2).getParentNodeId());
assertEquals(3, rows.get(2).getNodeLevel(), "层级按实际深度保留(不是提一级重算)");
}
@Test
void report_字段超长截断而不是拒整棵树() {
String longName = "类".repeat(JdShopCategoryTreeService.MAX_TEXT + 10);
service.report(request("228346061", longName, node(longName, "1315")));
JdShopCategoryTreeEntity row = captureInserted(1).get(0).get(0);
assertEquals(JdShopCategoryTreeService.MAX_TEXT, row.getNodeName().length());
assertEquals(JdShopCategoryTreeService.MAX_TEXT, row.getShopName().length(),
"店铺名与节点名同口径截断(展示字段,不值得拒整棵树)");
}
// ===================== 上报:拒绝分支 =====================
@Test
void report_缺店铺ID拒绝且不碰库() {
BusinessException error = assertThrows(BusinessException.class,
() -> service.report(request(" ", "某店", node("服饰内衣", "1315"))));
assertTrue(error.getMessage().contains("店铺 ID"));
verifyNoInteractions(mapper);
}
@Test
void report_店铺ID超长拒绝() {
assertThrows(BusinessException.class,
() -> service.report(request("1".repeat(65), "某店", node("服饰内衣", "1315"))));
verifyNoInteractions(mapper);
}
@Test
void report_空树拒绝且不覆盖已有快照() {
BusinessException error = assertThrows(BusinessException.class,
() -> service.report(request("228346061", "家居日用数据库店")));
assertTrue(error.getMessage().contains("为空"));
verify(mapper, never()).deleteByShopId(anyString());
verify(mapper, never()).insertBatch(anyList());
}
@Test
void report_全是脏节点等同空树拒绝() {
assertThrows(BusinessException.class, () -> service.report(
request("228346061", "家居日用数据库店", node("", "1315"), node("有名字", ""))));
verifyNoInteractions(mapper);
}
@Test
void report_超节点上限拒绝() {
List<JdShopCategoryNodeDto> many = new ArrayList<>();
for (int index = 0; index <= JdShopCategoryTreeService.MAX_NODES; index++) {
many.add(node("类目" + index, String.valueOf(index)));
}
BusinessException error = assertThrows(BusinessException.class,
() -> service.report(request("228346061", "家居日用数据库店",
many.toArray(new JdShopCategoryNodeDto[0]))));
assertTrue(error.getMessage().contains("超出上限"));
verify(mapper, never()).deleteByShopId(anyString());
}
// ===================== 上报:先删后插 / 分批 =====================
@Test
void report_先删后插且满批切分() {
int total = JdShopCategoryTreeService.INSERT_BATCH * 2 + 200;
List<JdShopCategoryNodeDto> many = new ArrayList<>();
for (int index = 0; index < total; index++) {
many.add(node("类目" + index, String.valueOf(index)));
}
int nodes = service.report(request("228346061", "家居日用数据库店",
many.toArray(new JdShopCategoryNodeDto[0])));
assertEquals(total, nodes);
InOrder order = inOrder(mapper);
order.verify(mapper).deleteByShopId("228346061");
order.verify(mapper, times(3)).insertBatch(anyList());
List<Integer> sizes = captureInserted(3).stream().map(List::size).collect(Collectors.toList());
assertEquals(List.of(JdShopCategoryTreeService.INSERT_BATCH,
JdShopCategoryTreeService.INSERT_BATCH, 200), sizes,
"旧行全删后按 500 行一批插入,末批是余数");
order.verifyNoMoreInteractions();
}
// ===================== 读取:并集森林 =====================
@Test
void tree_按nodeId合并多店并集且父缺失提升为根() {
when(mapper.selectList(any())).thenReturn(List.of(
row(1L, "shopA", "甲店", "1315", null, "服饰内衣", 1, 0),
row(2L, "shopA", "甲店", "1343", "1315", "女装", 2, 0),
row(3L, "shopB", "乙店", "1315", null, "服饰内衣", 1, 0),
row(4L, "shopB", "乙店", "34094", "1318", "舞蹈体操鞋服/装备", 2, 0)));
JdShopCategoryTreeVo vo = service.tree();
assertEquals(2, vo.getNodes().size(), "1315 两店同 id 合并成一支;34094 的父 1318 未授权 ⇒ 提为根");
JdShopCategoryNodeVo apparel = vo.getNodes().get(0);
assertEquals("1315", apparel.getValue());
assertEquals("服饰内衣", apparel.getName());
assertEquals(1, apparel.getChildren().size(), "同 id 不因多店上报长出重复子支");
JdShopCategoryNodeVo women = apparel.getChildren().get(0);
assertEquals("1343", women.getValue());
assertNull(women.getChildren(), "末级 children 统一置 null(省报文,前端 leaf 判定同口径)");
assertEquals("34094", vo.getNodes().get(1).getValue());
}
@Test
void tree_脏行缺id或缺名跳过但店铺清单如实累计() {
when(mapper.selectList(any())).thenReturn(List.of(
row(1L, "shopA", "甲店", "1315", null, "服饰内衣", 1, 0),
row(2L, "shopA", "甲店", null, null, "缺id", 1, 1),
row(3L, "shopA", "甲店", "9999", null, null, 1, 2)));
JdShopCategoryTreeVo vo = service.tree();
assertEquals(1, vo.getNodes().size());
assertEquals(1, vo.getShops().size());
assertEquals(3, vo.getShops().get(0).getNodeCount(), "清单是覆盖情况,按行如实累计");
}
@Test
void tree_店铺清单取最新采集时间() {
LocalDateTime early = LocalDateTime.of(2026, 11, 20, 9, 0);
LocalDateTime late = LocalDateTime.of(2026, 11, 23, 18, 30);
when(mapper.selectList(any())).thenReturn(List.of(
row(1L, "shopA", "甲店", "1315", null, "服饰内衣", 1, 0, early),
row(2L, "shopA", "甲店", "1343", "1315", "女装", 2, 0, late),
row(3L, "shopB", "乙店", "1318", null, "运动户外", 1, 0, early)));
List<JdShopCategoryTreeVo.Shop> shops = service.tree().getShops();
assertEquals(2, shops.size());
assertEquals("shopA", shops.get(0).getShopId());
assertEquals("甲店", shops.get(0).getShopName());
assertEquals(2, shops.get(0).getNodeCount());
assertEquals("2026-11-23 18:30:00", shops.get(0).getCapturedAt(),
"同店多行取最新一次采集时间");
assertEquals("2026-11-20 09:00:00", shops.get(1).getCapturedAt());
}
@Test
void tree_空表返回空森林与空店铺清单() {
when(mapper.selectList(any())).thenReturn(List.of());
JdShopCategoryTreeVo vo = service.tree();
assertEquals(List.of(), vo.getNodes());
assertEquals(List.of(), vo.getShops(), "shops 为空=还没有店上报过,页面回落内置快照");
}
// ===================== 夹具 =====================
private static JdShopCategoryTreeReportRequest request(String shopId, String shopName,
JdShopCategoryNodeDto... tree) {
JdShopCategoryTreeReportRequest request = new JdShopCategoryTreeReportRequest();
request.setShopId(shopId);
request.setShopName(shopName);
request.setTree(tree.length == 0 ? List.of() : List.of(tree));
return request;
}
private static JdShopCategoryNodeDto node(String name, String value,
JdShopCategoryNodeDto... children) {
JdShopCategoryNodeDto node = new JdShopCategoryNodeDto();
node.setName(name);
node.setValue(value);
node.setChildren(children.length == 0 ? null : List.of(children));
return node;
}
@SuppressWarnings({"unchecked", "rawtypes"})
private List<List<JdShopCategoryTreeEntity>> captureInserted(int batches) {
ArgumentCaptor<List<JdShopCategoryTreeEntity>> captor =
ArgumentCaptor.forClass((Class) List.class);
verify(mapper, times(batches)).insertBatch(captor.capture());
return captor.getAllValues();
}
private static JdShopCategoryTreeEntity row(Long id, String shopId, String shopName,
String nodeId, String parentNodeId,
String nodeName, int level, int order) {
return row(id, shopId, shopName, nodeId, parentNodeId, nodeName, level, order,
LocalDateTime.of(2026, 11, 23, 12, 0));
}
private static JdShopCategoryTreeEntity row(Long id, String shopId, String shopName,
String nodeId, String parentNodeId,
String nodeName, int level, int order,
LocalDateTime capturedAt) {
JdShopCategoryTreeEntity row = new JdShopCategoryTreeEntity();
row.setId(id);
row.setShopId(shopId);
row.setShopName(shopName);
row.setNodeId(nodeId);
row.setParentNodeId(parentNodeId);
row.setNodeName(nodeName);
row.setNodeLevel(level);
row.setSortOrder(order);
row.setCapturedAt(capturedAt);
return row;
}
}