fix(jd): 选品/建单归属闸从「只认建单人」改为「可见数据权限分组」作用域

客户实况(阿华112336,2026-11-25):上架页分类「3店」徽标写 3 个商品,展开列表
「该分类下暂无归属你账号的商品」、已选 0、上架按钮永久置灰——这 3 个商品是同组同事
(阿华普通账户测试)采的,服务端旧口径按 biz_jd_product.user_id 严格等于登录人过滤,
而分类下拉徽标数的是 V203 归属表行数,两套口径对不上,分类里永远选不到商品。

口径改:商品可见范围与分类下拉、徽标同源——本账号可见数据权限分组
(JdCategoryScopeService.resolveByUserId:自己带的组+所属组长的组+管理链上溯祖先组,
公司维隔离)下的分类归属行。组内谁采的都能由组内成员上架;跨公司/跨组仍 fail-closed。

改法:分类与可见组合成同一条 V203 EXISTS(SQL 侧下推),不先取 id 集再回主表;
商品上架页(/api/jd-publish/products)按可见作用域分页,建单归属闸改读同一份可见集
(resolveVisibleProductIds,归属表按 product_id IN + owner_group_id IN 查,空集不查库
直接 403)。后管只读页口径不变(visibleGroupIds 为空=不过滤)。配额闸语义未动。

判据:JdProductQueryServiceUserPageTest 7 支(作用域下推/同一支谓词/空作用域不查库/
页码钳制)、JdProductScopeSqlPinTest 2 支(XML 快照钉住 EXISTS 与 owner_group_id IN,
禁 p.user_id)、JdPublishTaskServiceTest 三支建单归属闸(同组同事放行、跨组 403 点名、
可见分组空不查归属表);AdminJdProductControllerTest 27 处调用点补参。逆向验证过:
把谓词改回 p.user_id、把可见组条件删掉、把建单闸改回 Objects.equals 三刀各自当场红。
This commit is contained in:
2026-11-25 21:36:26 +08:00
parent 4f1367ef69
commit fce4c7322c
15 changed files with 412 additions and 142 deletions
@@ -0,0 +1,69 @@
package com.nanri.aiimage.modules.jd;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 选品作用域 SQL 绊线(2026-11-25 客户实况「分类里 3 个商品、选品列表 0 条」)。
*
* <p>行为用例({@code JdProductQueryServiceUserPageTest})用 mock 的 mapper,判的是服务层
* 把 {@code visibleGroupIds}/{@code groupId} 传下去了;但「谓词到底有没有下推进 SQL」只在
* XML 里。旧口径的两套口径 bug 正是 XML/服务层各写一套造成的——分类徽标数归属行全量、
* 选品列表按 {@code user_id} 只认建单人。因此这里钉 XML 本身:</p>
* <p>① {@code visibleGroupIds} 分支必须在 {@code biz_jd_product_group_assign} 上按
* {@code owner_group_id IN (...)} 过滤;② 同一支里带 {@code category_id} 条件(分类与可见组
* 落在同一归属行,不拿别组分类凑数);③ 旧单分类分支保留在 {@code visibleGroupIds} 为空
* 时(后管只读页行为不变)。</p>
*/
class JdProductScopeSqlPinTest {
private static String mapperXml() throws IOException {
try (InputStream in = JdProductScopeSqlPinTest.class.getClassLoader()
.getResourceAsStream("mapper/jd/JdProductMapper.xml")) {
assertThat(in).as("JdProductMapper.xml 必须在 classpath 上(构建产物缺件=发布事故)")
.isNotNull();
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
}
@Test
void 选品作用域谓词必须按可见分组下推且与分类同支() throws IOException {
String xml = mapperXml();
String block = slice(xml, "visibleGroupIds != null and visibleGroupIds.size() > 0",
"visibleGroupIds == null or visibleGroupIds.size() == 0");
assertThat(block)
.as("可见分组必须落成 owner_group_id IN (...) 的 EXISTS")
.contains("biz_jd_product_group_assign")
.contains("owner_group_id IN");
assertThat(block)
.as("分类与可见组必须是同一行归属(同一条 EXISTS 里判 category_id)")
.contains("a.category_id = #{groupId}");
assertThat(block)
.as("可见组不得退化成按建单人过滤(本轮口径改的正题:同组同事采的商品可见)")
.doesNotContain("p.user_id");
}
@Test
void 旧单分类分支保留给后管只读页() throws IOException {
String xml = mapperXml();
String block = slice(xml, "visibleGroupIds == null or visibleGroupIds.size() == 0",
"<!-- 分类状态快筛");
assertThat(block)
.as("visibleGroupIds 为空时仍按 category_id 等值形过滤(后管 groupId 档行为逐字不变)")
.contains("biz_jd_product_group_assign")
.contains("a.category_id = #{groupId}");
}
/** 取两段标记之间的文本(第二段标记不存在时取到 EOF)。 */
private static String slice(String text, String from, String to) {
int start = text.indexOf(from);
assertThat(start).as("找不到起点标记:%s", from).isGreaterThanOrEqualTo(0);
int end = text.indexOf(to, start + from.length());
return end < 0 ? text.substring(start) : text.substring(start, end);
}
}
@@ -737,14 +737,14 @@ class AdminJdProductControllerTest {
JdProductMapper mapper = mock(JdProductMapper.class);
JdProductQueryService service = queryService(mapper);
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), isNull())).thenReturn(1L);
any(), any(), any(), any(), isNull(), isNull())).thenReturn(1L);
// page=0 / size=null ⇒ 归一为第 1 页、默认 20 条;offset = 0。
service.page(0L, null, null, null, null, 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(), isNull(), isNull(), isNull(), isNull(), isNull(), offset.capture(), limit.capture());
isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), offset.capture(), limit.capture());
assertEquals(0L, offset.getValue());
assertEquals(PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT, limit.getValue());
@@ -753,7 +753,7 @@ class AdminJdProductControllerTest {
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(), any(), any(), any(), any(), isNull(), offsetMax.capture(), limitMax.capture());
any(), any(), any(), any(), any(), any(), isNull(), isNull(), offsetMax.capture(), limitMax.capture());
assertEquals(9_999_000L, offsetMax.getValue(), "(page-1)*page_size 最大 9_999_000,钳制后不得溢出变负");
assertEquals(1000, limitMax.getValue(), "page_size 上限已随 v1.7 放宽到 1000(对齐原型档位)");
}
@@ -768,7 +768,7 @@ class AdminJdProductControllerTest {
() -> service.page(1L, 20L, null, null, null, null, null, null, null, null, from, to, null, null, null));
assertTrue(ex.getMessage().contains("开始时间不能晚于结束时间"), "倒挂时间按业务异常拒绝");
verify(mapper, never()).countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), isNull());
any(), any(), any(), any(), isNull(), isNull());
}
@Test
@@ -776,18 +776,18 @@ class AdminJdProductControllerTest {
JdProductMapper mapper = mock(JdProductMapper.class);
JdProductQueryService service = queryService(mapper);
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), isNull())).thenReturn(1L);
any(), any(), any(), any(), isNull(), isNull())).thenReturn(1L);
service.page(1L, 20L, null, " SKU-1 ", " 连衣裙 ", List.of(" 艺考形体服 "), 7L,
null, null, null, null, null, null, null, null);
verify(mapper).countProducts(null, "SKU-1", "连衣裙", List.of("艺考形体服"), 7L,
null, null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null, null);
verify(mapper).selectProductPage(null, "SKU-1", "连衣裙", List.of("艺考形体服"), 7L,
null, null, null, null, null, null, null, null, 0L, 20);
null, null, null, null, null, null, null, null, null, 0L, 20);
service.page(1L, 20L, null, " ", null, null, 0L, null, null, null, null, null, null, null, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, null, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, null, null, null);
verify(mapper).selectProductPage(null, null, null, null, null, null, null, null,
null, null, null, null, null, 0L, 20);
null, null, null, null, null, null, 0L, 20);
}
/**
@@ -800,20 +800,20 @@ class AdminJdProductControllerTest {
JdProductMapper mapper = mock(JdProductMapper.class);
JdProductQueryService service = queryService(mapper);
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), isNull())).thenReturn(1L);
any(), any(), any(), any(), isNull(), isNull())).thenReturn(1L);
service.page(1L, 20L, null, null, null,
java.util.Arrays.asList(" 女装 ", "女装", "", null, " 汉服/旗袍 "),
null, null, null, null, null, null, null, null, null);
verify(mapper).countProducts(null, null, null, List.of("女装", "汉服/旗袍"),
null, null, null, null, null, null, null, null, null);
null, null, null, null, null, null, null, null, null, null);
verify(mapper).selectProductPage(null, null, null, List.of("女装", "汉服/旗袍"),
null, null, null, null, null, null, null, null, null, 0L, 20);
null, null, null, null, null, null, null, null, null, null, 0L, 20);
// 全空白/空集合=不过滤该面(与「全部类目」清选同口径,不得下发空 IN)
service.page(1L, 20L, null, null, null, java.util.Arrays.asList(" ", ""),
null, null, null, null, null, null, null, null, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, null, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, null, null, null);
Path xml = findJdProductMapperXml();
String xmlText = stripComments(Files.readString(xml)).replaceAll("(?s)<!--.*?-->", "");
@@ -851,18 +851,18 @@ class AdminJdProductControllerTest {
JdProductMapper mapper = mock(JdProductMapper.class);
JdProductQueryService service = queryService(mapper);
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), isNull())).thenReturn(1L);
any(), any(), any(), any(), isNull(), isNull())).thenReturn(1L);
service.page(1L, 20L, null, null, null, null, null, null, null, null, null, null,
Boolean.TRUE, GROUP_ID, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, true, GROUP_ID, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, true, GROUP_ID, null, null);
verify(mapper).selectProductPage(null, null, null, null, null, null, null, null,
null, null, true, GROUP_ID, null, 0L, 20);
null, null, true, GROUP_ID, null, null, 0L, 20);
service.page(1L, 20L, null, null, null, null, null, null, null, null, null, null,
Boolean.FALSE, GROUP_ID, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, false, GROUP_ID, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, false, GROUP_ID, null, null);
verify(mapper).selectProductPage(null, null, null, null, null, null, null, null,
null, null, false, GROUP_ID, null, 0L, 20);
null, null, false, GROUP_ID, null, null, 0L, 20);
// ③ 控制器直传:classified 原样进服务(owner_group_id 仍按作用域收敛后的值)。
JdProductQueryService queryService = mock(JdProductQueryService.class);
@@ -896,19 +896,19 @@ class AdminJdProductControllerTest {
JdProductMapper mapper = mock(JdProductMapper.class);
JdProductQueryService service = queryService(mapper);
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), isNull())).thenReturn(1L);
any(), any(), any(), any(), isNull(), isNull())).thenReturn(1L);
// ① 三件新面原样直传:keyword/shopName trim 归一,价格 BigDecimal 原样。
service.page(1L, 20L, " 连衣裙 ", null, null, null, 7L, " 测试店铺 ",
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null, null);
verify(mapper).countProducts("连衣裙", null, null, null, 7L, "测试店铺",
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null, null);
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null, null, null);
verify(mapper).selectProductPage("连衣裙", null, null, null, 7L, "测试店铺",
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null, null, 0L, 20);
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null, null, null, 0L, 20);
// ② 空白 keyword/shopName 归 null(不过滤该面)。
service.page(1L, 20L, " ", null, null, null, 0L, " ", null, null, null, null, null, null, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, null, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, null, null, null);
// ③ 价格倒挂按业务异常拒绝(同同步起止倒挂形状)。
BusinessException ex = assertThrows(BusinessException.class,
@@ -916,7 +916,7 @@ class AdminJdProductControllerTest {
new BigDecimal("200"), new BigDecimal("100"), null, null, null, null, null));
assertTrue(ex.getMessage().contains("价格区间最小值不能大于最大值"), "倒挂价格区间按业务异常拒绝");
verify(mapper, times(2)).countProducts(any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), any(), any(), isNull());
any(), any(), any(), any(), any(), any(), isNull(), isNull());
}
/**
@@ -1170,9 +1170,9 @@ class AdminJdProductControllerTest {
JdProductEntity categorized = productEntity(1L);
JdProductEntity missing = productEntity(2L);
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), isNull())).thenReturn(2L);
any(), any(), any(), any(), isNull(), isNull())).thenReturn(2L);
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), isNull(), anyLong(), anyInt())).thenReturn(List.of(categorized, missing));
any(), any(), any(), any(), isNull(), isNull(), anyLong(), anyInt())).thenReturn(List.of(categorized, missing));
when(assignMapper.selectList(any())).thenReturn(List.of(
assign(1L, GROUP_ID, 3L), assign(2L, GROUP_ID, 99L)));
JdProductGroupEntity category = new JdProductGroupEntity();
@@ -1197,9 +1197,9 @@ class AdminJdProductControllerTest {
JdProductQueryService service = queryService(mapper, mock(JdProductImageMapper.class),
mock(JdProductSkuMapper.class), mock(JdProductGroupMapper.class), assignMapper);
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), isNull())).thenReturn(1L);
any(), any(), any(), any(), isNull(), isNull())).thenReturn(1L);
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), isNull(), anyLong(), anyInt())).thenReturn(List.of(productEntity(1L)));
any(), any(), any(), any(), isNull(), isNull(), anyLong(), anyInt())).thenReturn(List.of(productEntity(1L)));
JdProductPageVo vo = service.page(1L, 20L, null, null, null, null, null, null, null, null,
null, null, null, null, null);
@@ -21,16 +21,24 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 用户端「商品上架」选品分页(v1.5 Phase 4,AC-9 owner 过滤的服务端半边)。
* 用户端「商品上架」选品分页(v1.5 Phase 4;<b>2026-11-25 作用域改口径</b>)。
*
* <p>判据四件:① 归属条件恒带(user_id 来自会话出口,跨用户商品 0 返回);② 分类过滤走
* V203 归属表两步下推(先取 product_id 集再 in 主键,不往 wrapper 夹原生 SQL);③ 分类空集
* 直接回空页不查主表;④ 页码/页宽钳制照管理面同出口(MAX_PAGE/MAX_PAGE_SIZE,铁律 15)。</p>
* <p>旧判据是「owner 条件恒带(user_id=当前账号),跨用户商品 0 返回」——客户实况把它打穿:
* 分类「3店」写着 3 个商品(徽标数的是 V203 归属行),列表却回 0 条(那 3 行是同一分组的
* 同事采的),上架按钮永久置灰。新判据:</p>
* <p>① 选品作用域=可见数据权限分组({@code JdCategoryScope#visibleGroupIds},与分类下拉
* 同源),谓词下推进 mapper 的 {@code visibleGroupIds}(不按 user_id 过滤,同组同事采集的
* 商品可见可上架);② 分类过滤与可见组合成<b>同一支</b>谓词(同一个 {@code groupId} 参数
* 交给 SQL 侧的 EXISTS,不许 Java 侧先取 product_id 集);③ 可见组为空直接回空页不查主表;
* ④ 页码/页宽钳制照管理面同出口(MAX_PAGE/MAX_PAGE_SIZE,铁律 15)。</p>
*/
@ExtendWith(MockitoExtension.class)
class JdProductQueryServiceUserPageTest {
@@ -73,80 +81,78 @@ class JdProductQueryServiceUserPageTest {
return row;
}
private com.nanri.aiimage.modules.jd.model.entity.JdProductGroupAssignEntity assign(long productId) {
com.nanri.aiimage.modules.jd.model.entity.JdProductGroupAssignEntity row =
new com.nanri.aiimage.modules.jd.model.entity.JdProductGroupAssignEntity();
row.setProductId(productId);
row.setCategoryId(3L);
row.setOwnerGroupId(2L);
return row;
/** countProducts 的 14 个形参全 matcher(返回总数 1 行)。 */
private void stubCount(long total) {
when(jdProductMapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), any(), any())).thenReturn(total);
}
/** selectProductPage 的 16 个形参全 matcher。 */
private void stubPage(List<JdProductEntity> rows) {
when(jdProductMapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), any(), any(), org.mockito.ArgumentMatchers.anyLong(),
anyInt())).thenReturn(rows);
}
@Test
void 选品分页只回归属当前用户的行() {
when(jdProductMapper.selectCount(any())).thenReturn(1L);
when(jdProductMapper.selectList(any())).thenReturn(List.of(product(11L)));
void 选品分页按可见分组作用域下推_不按建单人过滤() {
stubCount(1L);
stubPage(List.of(product(11L)));
JdProductPageVo vo = service.pageForUser(7L, null, 1L, 20L);
JdProductPageVo vo = service.pageInScope(List.of(11L, 12L), 2L, null, 1L, 20L);
ArgumentCaptor<LambdaQueryWrapper<JdProductEntity>> captor =
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(jdProductMapper).selectCount(captor.capture());
captor.getValue().getSqlSegment(); // MP 惯例:先求值一次才铺出参数
assertThat(captor.getValue().getSqlSegment()).as("归属条件恒在主查询里").contains("user_id");
assertThat(captor.getValue().getParamNameValuePairs().values()).contains(7L);
// 作用域全在 SQL 侧:可见组集合+公司进 mapper,owner_group_id 过滤由 XML 的 EXISTS 承担;
// 这里恰好证明了「没有 user_id 条件」——旧实现按 user_id 过滤,同组同事采的商品恒 0 条。
verify(jdProductMapper).countProducts(isNull(), isNull(), isNull(), isNull(),
eq(0L), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
eq(2L), eq(List.of(11L, 12L)));
assertThat(vo.getTotal()).isEqualTo(1L);
assertThat(vo.getItems()).hasSize(1);
assertThat(vo.getItems().get(0).getTitle()).isEqualTo("测试商品 11");
// 没选分类不查归属表
// 归属表由 SQL 侧 EXISTS 读,Java 侧不再先取 product_id 集
verify(jdProductGroupAssignMapper, never()).selectList(any());
}
@Test
void 分类过滤_先取归属表产品集再进主查询() {
when(jdProductGroupAssignMapper.selectList(any()))
.thenReturn(List.of(assign(11L), assign(12L), assign(11L)));
when(jdProductMapper.selectCount(any())).thenReturn(1L);
when(jdProductMapper.selectList(any())).thenReturn(List.of(product(12L)));
void 分类过滤与可见组合成同一支谓词() {
stubCount(0L);
JdProductPageVo vo = service.pageForUser(7L, 3L, 1L, 20L);
JdProductPageVo vo = service.pageInScope(List.of(11L), 2L, 3L, 1L, 20L);
ArgumentCaptor<LambdaQueryWrapper<JdProductEntity>> captor =
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(jdProductMapper).selectCount(captor.capture());
captor.getValue().getSqlSegment();
assertThat(captor.getValue().getSqlSegment())
.as("分类集以 IN 下推进主查询(owner 条件仍然恒在)")
.contains("user_id").contains("IN");
assertThat(captor.getValue().getParamNameValuePairs().values())
.contains(7L, 11L, 12L);
assertThat(vo.getItems()).hasSize(1);
verify(jdProductMapper).countProducts(isNull(), isNull(), isNull(), isNull(),
eq(3L), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
eq(2L), eq(List.of(11L)));
assertThat(vo.getTotal()).isZero();
assertThat(vo.getItems()).isEmpty();
verify(jdProductMapper, never()).selectProductPage(any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), any(), any(), any(), any(),
org.mockito.ArgumentMatchers.anyLong(), anyInt());
}
@Test
void 分类空集直接回空页不查主表() {
when(jdProductGroupAssignMapper.selectList(any())).thenReturn(List.of());
JdProductPageVo vo = service.pageForUser(7L, 3L, 1L, 20L);
void 可见分组为空直接回空页不查主表() {
JdProductPageVo vo = service.pageInScope(List.of(), 2L, 3L, 1L, 20L);
assertThat(vo.getTotal()).isZero();
assertThat(vo.getItems()).isEmpty();
verify(jdProductMapper, never()).selectCount(any());
verify(jdProductMapper, never()).selectList(any());
verify(jdProductMapper, never()).countProducts(any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), any(), any(), any(), any());
JdProductPageVo nullScope = service.pageInScope(null, 2L, 3L, 1L, 20L);
assertThat(nullScope.getTotal()).as("作用域解析不出(null)同样回空页,不给全平台")
.isZero();
}
@Test
void 页码页宽钳制_离谱页码不喂负下标() {
when(jdProductMapper.selectCount(any())).thenReturn(0L);
stubCount(0L);
JdProductPageVo zero = service.pageForUser(7L, null, 0L, 5000L);
JdProductPageVo zero = service.pageInScope(List.of(11L), 2L, null, 0L, 5000L);
assertThat(zero.getPage()).as("页码下限钳到 1").isEqualTo(1L);
assertThat(zero.getPageSize()).as("页宽上限钳到 1000(v1.7 对齐原型档位放宽,与后管只读页同出口)")
.isEqualTo(1000L);
JdProductPageVo huge = service.pageForUser(7L, null, Long.MAX_VALUE, 20L);
JdProductPageVo huge = service.pageInScope(List.of(11L), 2L, null, Long.MAX_VALUE, 20L);
assertThat(huge.getPage()).as("页码上限钳到 10000(乘积不溢出,铁律 15 同形)").isEqualTo(10000L);
verify(jdProductMapper, never()).selectList(any());
}
/**
@@ -202,9 +208,9 @@ class JdProductQueryServiceUserPageTest {
JdProductEntity listRow = product(22L);
listRow.setManualLocked(1);
when(jdProductMapper.selectList(any())).thenReturn(List.of(listRow));
when(jdProductMapper.selectCount(any())).thenReturn(1L);
JdProductPageVo page = service.pageForUser(7L, null, 1L, 20L);
stubCount(1L);
stubPage(List.of(listRow));
JdProductPageVo page = service.pageInScope(List.of(11L), 2L, 3L, 1L, 20L);
assertThat(page.getItems().get(0).getManualLocked()).as("列表 VO 带回人工锁").isEqualTo(1);
}
}
}
@@ -5,6 +5,7 @@ import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.mapper.AdminUserMapper;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import com.nanri.aiimage.modules.jd.mapper.JdProductGroupAssignMapper;
import com.nanri.aiimage.modules.jd.mapper.JdProductImageMapper;
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
import com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper;
@@ -12,6 +13,7 @@ import com.nanri.aiimage.modules.jd.mapper.JdPublishItemMapper;
import com.nanri.aiimage.modules.jd.model.dto.JdPublishTaskCreateRequest;
import com.nanri.aiimage.modules.jd.model.dto.JdPublishTaskCreateRequest.PriceRule;
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
import com.nanri.aiimage.modules.jd.model.entity.JdProductGroupAssignEntity;
import com.nanri.aiimage.modules.jd.model.entity.JdPublishItemEntity;
import com.nanri.aiimage.modules.jd.model.vo.JdPublishTaskVo;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
@@ -71,10 +73,28 @@ class JdPublishTaskServiceTest {
private TaskHeartbeatPositionService taskHeartbeatPositionService;
@Mock
private JdPublishTaskItemsResetSpiImpl itemsReset;
@Mock
private JdCategoryScopeService jdCategoryScopeService;
@Mock
private JdProductGroupAssignMapper jdProductGroupAssignMapper;
@InjectMocks
private JdPublishTaskService service;
@org.junit.jupiter.api.BeforeAll
static void initMybatisPlusTableInfo() {
// LambdaQueryWrapper/LambdaUpdateWrapper 需要 MP 的 lambda cache(单类点跑时无人预热,
// 整量跑才靠别的用例带上——这里显式初始化,点跑与整量同结果)。
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,
FileTaskEntity.class);
com.baomidou.mybatisplus.core.metadata.TableInfoHelper.initTableInfo(assistant,
JdProductGroupAssignEntity.class);
}
private JdProductEntity product(long id, long userId, String title) {
JdProductEntity product = new JdProductEntity();
product.setId(id);
@@ -98,11 +118,33 @@ class JdPublishTaskServiceTest {
return request;
}
/** 可见作用域桩:userId 的可见数据权限分组=groupIds(2026-11-25 起建单归属闸读它)。 */
private void stubVisibleGroups(long userId, Long companyId, long... groupIds) {
when(jdCategoryScopeService.resolveByUserId(userId)).thenReturn(
new JdCategoryScopeService.JdCategoryScope(false, companyId,
java.util.Arrays.stream(groupIds).boxed().toList(), List.of(), List.of()));
}
/** 归属行桩:这些商品落在可见分组的分类归属行上(category_id 非空)。 */
private void stubAssign(long groupId, long... productIds) {
List<JdProductGroupAssignEntity> rows = java.util.Arrays.stream(productIds)
.mapToObj(id -> {
JdProductGroupAssignEntity row = new JdProductGroupAssignEntity();
row.setProductId(id);
row.setOwnerGroupId(groupId);
row.setCategoryId(11L);
return row;
}).toList();
when(jdProductGroupAssignMapper.selectList(any())).thenReturn(rows);
}
@Test
void 建单成功且通道显式写LOCAL并落快照行() {
when(adminUserMapper.selectById(7L)).thenReturn(new AdminUserEntity());
when(jdProductMapper.selectBatchIds(List.of(101L, 102L)))
.thenReturn(List.of(product(101L, 7L, "连衣裙A"), product(102L, 7L, "连衣裙B")));
stubVisibleGroups(7L, 2L, 11L);
stubAssign(11L, 101L, 102L);
when(fileTaskMapper.insert(any(FileTaskEntity.class))).thenAnswer(invocation -> {
invocation.getArgument(0, FileTaskEntity.class).setId(34400L);
return 1;
@@ -139,6 +181,8 @@ class JdPublishTaskServiceTest {
void 快照行带改价折算与库存() {
when(adminUserMapper.selectById(7L)).thenReturn(new AdminUserEntity());
when(jdProductMapper.selectBatchIds(List.of(101L))).thenReturn(List.of(product(101L, 7L, "连衣裙A")));
stubVisibleGroups(7L, 2L, 11L);
stubAssign(11L, 101L);
when(fileTaskMapper.insert(any(FileTaskEntity.class))).thenAnswer(invocation -> {
invocation.getArgument(0, FileTaskEntity.class).setId(34401L);
return 1;
@@ -186,21 +230,65 @@ class JdPublishTaskServiceTest {
}
@Test
void 选了他人商品建单被拒并点名() {
void 同组同事采集的商品可以建单() {
// 2026-11-25 客户实况:分类「3店」3 个商品是同一分组的同事采的,登录人 7 选不到、上架按钮
// 永久置灰。作用域口径下「组内谁采的都能由组内成员上架」——归属闸看的是分类归属行,
// 不是 biz_jd_product.user_id。
when(adminUserMapper.selectById(7L)).thenReturn(new AdminUserEntity());
when(jdProductMapper.selectBatchIds(List.of(101L)))
.thenReturn(List.of(product(101L, 8L, "别人的连衣裙")));
.thenReturn(List.of(product(101L, 8L, "同事采集的连衣裙")));
stubVisibleGroups(7L, 2L, 11L);
stubAssign(11L, 101L);
when(fileTaskMapper.insert(any(FileTaskEntity.class))).thenAnswer(invocation -> {
invocation.getArgument(0, FileTaskEntity.class).setId(34402L);
return 1;
});
when(jdPublishItemMapper.insert(any(JdPublishItemEntity.class))).thenReturn(1);
JdPublishTaskCreateRequest request = validRequest(7L);
request.setProductIds(List.of(101L));
JdPublishTaskVo vo = service.createTask(request, null);
assertThat(vo.getTaskId()).isEqualTo(34402L);
verify(fileTaskMapper).insert(any(FileTaskEntity.class));
verify(jdPublishItemMapper).insert(any(JdPublishItemEntity.class));
}
@Test
void 勾选商品不在可见分组内建单被拒并点名() {
when(adminUserMapper.selectById(7L)).thenReturn(new AdminUserEntity());
when(jdProductMapper.selectBatchIds(List.of(101L)))
.thenReturn(List.of(product(101L, 8L, "别组采集的连衣裙")));
stubVisibleGroups(7L, 2L, 11L);
// 归属表没有落在可见分组上的行(跨组/跨公司商品):可见集为空 ⇒ 业务码 403。
when(jdProductGroupAssignMapper.selectList(any())).thenReturn(List.of());
JdPublishTaskCreateRequest request = validRequest(7L);
request.setProductIds(List.of(101L));
assertThatThrownBy(() -> service.createTask(request, null))
.isInstanceOf(BusinessException.class)
.as("AC-9 归属半边:选他人商品业务码拒绝")
.as("作用域归属闸:不在可见分组分类归属行上的商品业务码拒绝")
.extracting("code")
.isEqualTo(403);
verify(fileTaskMapper, never()).insert(any(FileTaskEntity.class));
verify(jdPublishItemMapper, never()).insert(any(JdPublishItemEntity.class));
}
@Test
void 可见分组为空建单被拒并不查归属表() {
when(adminUserMapper.selectById(7L)).thenReturn(new AdminUserEntity());
when(jdProductMapper.selectBatchIds(List.of(101L)))
.thenReturn(List.of(product(101L, 7L, "自己的连衣裙")));
stubVisibleGroups(7L, 2L);
JdPublishTaskCreateRequest request = validRequest(7L);
request.setProductIds(List.of(101L));
assertThatThrownBy(() -> service.createTask(request, null))
.isInstanceOf(BusinessException.class)
.extracting("code")
.isEqualTo(403);
verify(jdProductGroupAssignMapper, never()).selectList(any());
verify(fileTaskMapper, never()).insert(any(FileTaskEntity.class));
}
@Test
void 商品缺行建单被拒() {
when(adminUserMapper.selectById(7L)).thenReturn(new AdminUserEntity());
@@ -429,6 +517,8 @@ class JdPublishTaskServiceTest {
second.setShopId("2002");
second.setShopName("家居日用数据库店");
when(jdProductMapper.selectBatchIds(List.of(101L, 102L))).thenReturn(List.of(first, second));
stubVisibleGroups(7L, 2L, 11L);
stubAssign(11L, 101L, 102L);
assertThatThrownBy(() -> service.createTask(validRequest(7L), null))
.isInstanceOf(BusinessException.class)
@@ -448,6 +538,8 @@ class JdPublishTaskServiceTest {
second.setShopId("228346061");
second.setShopName("家居日用数据库店");
when(jdProductMapper.selectBatchIds(List.of(101L, 102L))).thenReturn(List.of(first, second));
stubVisibleGroups(7L, 2L, 11L);
stubAssign(11L, 101L, 102L);
when(fileTaskMapper.insert(any(FileTaskEntity.class))).thenAnswer(invocation -> {
invocation.getArgument(0, FileTaskEntity.class).setId(34410L);
return 1;
@@ -475,6 +567,8 @@ class JdPublishTaskServiceTest {
nameOnly.setShopId(null);
nameOnly.setShopName("家居日用数据库店");
when(jdProductMapper.selectBatchIds(List.of(101L, 102L))).thenReturn(List.of(withId, nameOnly));
stubVisibleGroups(7L, 2L, 11L);
stubAssign(11L, 101L, 102L);
when(fileTaskMapper.insert(any(FileTaskEntity.class))).thenAnswer(invocation -> {
invocation.getArgument(0, FileTaskEntity.class).setId(34411L);
return 1;