feat(jd): 京东数据库分组改店铺分类+左右分栏弹窗人工归类

This commit is contained in:
2026-11-15 21:46:26 +08:00
parent 91cf5dfaf7
commit e175e85973
18 changed files with 1022 additions and 144 deletions
@@ -90,6 +90,9 @@ class AdminJdProductControllerTest {
assertTrue(source.contains("@DeleteMapping"), "删除端点必须落在 @DeleteMapping");
assertTrue(source.contains("\"/batch-delete\""), "批量删除端点必须是 POST /batch-delete");
assertTrue(source.contains("@Valid"), "批量删除请求体必须过 @Valid(ids 非空)");
// 批量归类端点(店铺分类弹窗,2026-11-15):POST /batch-assign-group,归入/移出分类。
assertTrue(source.contains("\"/batch-assign-group\""),
"批量归类端点必须是 POST /batch-assign-group(店铺分类弹窗人工归类)");
// PUT 旧绊线(「不得有 PUT:商品行只能被采集链回传覆盖」)已随后台「编辑商品」特性放行
// (AdminJdProductController 的 @PutMapping("/{id}",JdProductUpdateService 承接);商品行
// 采集回传覆盖链不变,人工改名只走这条 PUT。分组改排序仍落 AdminJdProductGroupController。
@@ -125,7 +128,7 @@ class AdminJdProductControllerTest {
void 分页参数snake且带默认值() throws IOException {
String source = stripComments(Files.readString(CONTROLLER));
for (String param : List.of("page_size", "shop_name", "product_code", "title_keyword",
"group_id", "synced_from", "synced_to", "company_id")) {
"group_id", "ungrouped", "synced_from", "synced_to", "company_id")) {
assertTrue(source.contains(param), "查询参数必须 snake_case:" + param);
}
// 商品状态筛选框已随 2026-11-15 客户口径从前端下线 ⇒ 服务端同名参数一起撤(反向钉住防回潜)。
@@ -193,13 +196,13 @@ class AdminJdProductControllerTest {
pageVo.setTotal(0L);
pageVo.setPage(1L);
pageVo.setPageSize((long) PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT);
when(queryService.page(1L, 20L, 5L, null, null, null, null, null, null, null)).thenReturn(pageVo);
when(queryService.page(1L, 20L, 5L, null, null, null, null, null, null, null, null)).thenReturn(pageVo);
ApiResponse<JdProductPageVo> response = controller.page(1L, 20L, null, null, null, null, null,
null, null, 5L, request);
null, null, null, 5L, request);
verify(dataReadScope).superAdminScopedCompanyId(operator, 5L);
verify(queryService).page(1L, 20L, 5L, null, null, null, null, null, null, null);
verify(queryService).page(1L, 20L, 5L, null, null, null, null, null, null, null, null);
assertEquals(Boolean.TRUE, response.isSuccess(), "拒绝走 403 业务码,放行必须是 success 信封(B80-03)");
assertEquals(pageVo, response.getData());
}
@@ -321,23 +324,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(), 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, 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());
isNull(), isNull(), isNull(), 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, 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());
any(), any(), any(), offsetMax.capture(), limitMax.capture());
assertEquals(999_900L, offsetMax.getValue(), "(page-1)*page_size 最大 999_900,钳制后不得溢出变负");
assertEquals(100, limitMax.getValue());
}
@@ -351,9 +354,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, 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(), any(), any(), any());
}
@Test
@@ -362,17 +365,68 @@ 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(), any(), any(), any())).thenReturn(0L);
// 超管显式传 5 ⇒ 按公司 5 筛(收窄);groupId ≤0 按没选分组处理;文本两侧空白剥掉。
// 类目(京麦末级类目名)走同一过滤面:传值原样直传、空白归 null。
service.page(1L, 20L, 5L, " 店铺甲 ", " SKU-1 ", " 连衣裙 ", " 艺考形体服 ", 0L, null, null);
service.page(1L, 20L, 5L, " 店铺甲 ", " SKU-1 ", " 连衣裙 ", " 艺考形体服 ", 0L, null, null, null);
verify(mapper).countProducts(eq(5L), eq("店铺甲"), eq("SKU-1"), eq("连衣裙"),
eq("艺考形体服"), isNull(), isNull(), isNull());
eq("艺考形体服"), isNull(), isNull(), isNull(), isNull());
// 全量(scopedCompanyId=null)⇒ companyId 不过滤。
service.page(1L, 20L, null, null, null, null, null, null, null, null);
service.page(1L, 20L, null, null, null, null, null, null, null, null, null);
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
isNull(), isNull(), isNull());
isNull(), isNull(), isNull(), isNull());
// 未分类范围(店铺分类弹窗「未分类」页签):ungrouped=true 原样下推到 SQL 侧。
service.page(1L, 20L, null, null, null, null, null, null, Boolean.TRUE, null, null);
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
isNull(), eq(Boolean.TRUE), isNull(), isNull());
// ungrouped=false / 缺省按不过滤处理(Filter 归一成 null,不进 IS NULL 分支);
// 与②「全量」那发同形状,累计 2 次。
service.page(1L, 20L, null, null, null, null, null, null, Boolean.FALSE, null, null);
verify(mapper, times(2)).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
isNull(), isNull(), isNull(), isNull());
}
@Test
void 批量归类端点走同一鉴权正门且参数透传() {
JdProductQueryService queryService = mock(JdProductQueryService.class);
JdProductUpdateService updateService = mock(JdProductUpdateService.class);
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
DataReadScope dataReadScope = mock(DataReadScope.class);
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), updateService, authSupport,
mock(PermissionMenuService.class), dataReadScope);
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");
when(updateService.assignGroup(List.of(11L, 12L), 3L)).thenReturn(2);
com.nanri.aiimage.modules.jd.model.dto.JdProductBatchAssignGroupRequest body =
new com.nanri.aiimage.modules.jd.model.dto.JdProductBatchAssignGroupRequest();
body.setIds(List.of(11L, 12L));
body.setGroupId(3L);
ApiResponse<Integer> response = controller.batchAssignGroup(body, request);
// 公共库写侧没有公司维度:不收 company_id、DataReadScope 不参与。
verifyNoInteractions(dataReadScope);
verify(updateService).assignGroup(List.of(11L, 12L), 3L);
assertEquals(Boolean.TRUE, response.isSuccess(), "放行必须是 success 信封(B80-03)");
assertEquals(2, response.getData(), "回执=实际更新的行数");
// 业务失败(目标分类不存在)文案原样透出,不吞。
when(updateService.assignGroup(List.of(11L), 404L))
.thenThrow(new BusinessException("分类不存在,请刷新分类后重试"));
com.nanri.aiimage.modules.jd.model.dto.JdProductBatchAssignGroupRequest ghostBody =
new com.nanri.aiimage.modules.jd.model.dto.JdProductBatchAssignGroupRequest();
ghostBody.setIds(List.of(11L));
ghostBody.setGroupId(404L);
BusinessException ex = assertThrows(BusinessException.class,
() -> controller.batchAssignGroup(ghostBody, request));
assertTrue(ex.getMessage().contains("分类不存在"));
}
@Test
@@ -532,8 +586,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(), any(), any(), any())).thenReturn(2L);
when(mapper.selectProductPage(any(), any(), any(), 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();
@@ -541,7 +595,7 @@ class AdminJdProductControllerTest {
group.setName("秋冬款");
when(groupMapper.selectBatchIds(any())).thenReturn(List.of(group));
JdProductPageVo vo = service.page(1L, 20L, 5L, null, null, null, null, null, null, null);
JdProductPageVo vo = service.page(1L, 20L, 5L, null, null, null, null, null, null, null, null);
assertEquals("秋冬款", vo.getItems().get(0).getGroupName(), "行集 group_id 一次取齐分组名(无逐行查询)");
assertNull(vo.getItems().get(1).getGroupName(), "分组已删=不猜名字(铁律 30),前端展示 -");
@@ -64,7 +64,7 @@ class JdProductGroupServiceTest {
BusinessException ex = assertThrows(BusinessException.class,
() -> service.create(" 秋冬款 ", 2));
assertEquals(409, ex.getCode(), "重名冲突=409(语义正确的资源冲突码,仓内 DeviceLogService 有先例)");
assertTrue(ex.getMessage().contains("同名分组"), "重名必须给可读文案,不拿 DB 唯一键异常撞 500");
assertTrue(ex.getMessage().contains("同名分类"), "重名必须给可读文案,不拿 DB 唯一键异常撞 500(2026-11-15 起界面措辞「分类」)");
verify(groupMapper, never()).insert(any(JdProductGroupEntity.class));
}
@@ -115,7 +115,7 @@ class JdProductGroupServiceTest {
BusinessException ex = assertThrows(BusinessException.class,
() -> service.rename(1L, "新名字", null));
assertTrue(ex.getMessage().contains("分组不存在"));
assertTrue(ex.getMessage().contains("分类不存在"));
verify(groupMapper, never()).updateById(any(JdProductGroupEntity.class));
}
@@ -172,17 +172,95 @@ class JdProductUpdateServiceTest {
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
assertThat(service.update(PRODUCT_ID, body)).isEqualTo(PRODUCT_ID);
// 分组不存在 ⇒ 拒绝且零写。
// 分组不存在 ⇒ 拒绝且零写(2026-11-15 起界面措辞「分类」)。
JdProductUpdateRequest ghost = mainFieldsOnly();
ghost.setGroupId(404L);
when(jdProductGroupMapper.selectById(404L)).thenReturn(null);
assertThatThrownBy(() -> service.update(PRODUCT_ID, ghost))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("分组不存在");
.hasMessageContaining("分类不存在");
// update 只剩成功挂组那 1 次:ghost 分支零写点(同用例前半段已成功写过一次,不能按 never 数)。
verify(jdProductMapper, org.mockito.Mockito.times(1)).update(isNull(), any(Wrapper.class));
}
// ---------- 批量归类(店铺分类弹窗,2026-11-15) ----------
/** 取批量归类落下的 SET 键值对(paramNameValuePairs),供断言 groupId/ids 落值。 */
@SuppressWarnings("unchecked")
private Map<String, Object> capturedAssignValues() {
ArgumentCaptor<Wrapper<JdProductEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
verify(jdProductMapper).update(isNull(), captor.capture());
LambdaUpdateWrapper<JdProductEntity> wrapper = (LambdaUpdateWrapper<JdProductEntity>) captor.getValue();
return (Map<String, Object>) ReflectionTestUtils.getField(wrapper, "paramNameValuePairs");
}
@Test
void 批量归类成功_置分类并落人工编辑锁() {
JdProductGroupEntity group = new JdProductGroupEntity();
group.setId(7L);
when(jdProductGroupMapper.selectById(7L)).thenReturn(group);
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(3);
// 入参含重复 id:去重后 1/2/3 三行
int updated = service.assignGroup(List.of(1L, 1L, 2L, 3L), 7L);
assertThat(updated).isEqualTo(3);
ArgumentCaptor<Wrapper<JdProductEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
verify(jdProductMapper).update(isNull(), captor.capture());
LambdaUpdateWrapper<JdProductEntity> wrapper = (LambdaUpdateWrapper<JdProductEntity>) captor.getValue();
// SET 段必须同时带 group_id 与 manual_locked(归类即人工干预:置锁后覆盖清理不再删行)
assertThat(wrapper.getSqlSet()).contains("group_id").contains("manual_locked");
// WHERE 段(IN ids)惰性构参:先触发 getSqlSegment 再读参数表,IN 值才落进 paramNameValuePairs。
wrapper.getSqlSegment();
Map<String, Object> values = capturedAssignValues();
assertThat(values.values()).contains(7L, 1L, 2L, 3L);
}
@Test
void 批量归类目标分类不存在拒绝且零写() {
when(jdProductGroupMapper.selectById(404L)).thenReturn(null);
assertThatThrownBy(() -> service.assignGroup(List.of(1L, 2L), 404L))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("分类不存在");
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
}
@Test
void 批量归类空清单与超上限可读拒绝() {
assertThatThrownBy(() -> service.assignGroup(List.of(), null))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("请先选择要归类的商品");
assertThatThrownBy(() -> service.assignGroup(List.of(0L, -1L), null))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("请先选择要归类的商品");
List<Long> tooMany = new java.util.ArrayList<>();
for (long i = 1; i <= 201; i++) {
tooMany.add(i);
}
assertThatThrownBy(() -> service.assignGroup(tooMany, null))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("单次最多归类 200");
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
}
@Test
void 移出分类_groupId置空仍落人工编辑锁() {
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(2);
int updated = service.assignGroup(List.of(5L, 6L), null);
assertThat(updated).isEqualTo(2);
ArgumentCaptor<Wrapper<JdProductEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
verify(jdProductMapper).update(isNull(), captor.capture());
LambdaUpdateWrapper<JdProductEntity> wrapper = (LambdaUpdateWrapper<JdProductEntity>) captor.getValue();
// groupId=null 也显式落 SET(group_id=NULL=回到未分类),锁照落。
assertThat(wrapper.getSqlSet()).contains("group_id").contains("manual_locked");
// 移出分类没有目标组:不查分组表。
verify(jdProductGroupMapper, never()).selectById(any());
}
@Test
void 属性重建只覆写tabs段其余顶层键保留() {
givenOwnedRow();