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
@@ -16,6 +16,7 @@ import com.nanri.aiimage.modules.jd.model.vo.JdPublishItemsPageVo;
import com.nanri.aiimage.modules.jd.model.vo.JdPublishProgressVo;
import com.nanri.aiimage.modules.jd.model.vo.JdPublishTaskBatchVo;
import com.nanri.aiimage.modules.jd.model.vo.JdPublishTaskVo;
import com.nanri.aiimage.modules.jd.service.JdCategoryScopeService;
import com.nanri.aiimage.modules.jd.service.JdProductQueryService;
import com.nanri.aiimage.modules.jd.service.JdPublishBoardService;
import com.nanri.aiimage.modules.jd.service.JdPublishFillAiService;
@@ -70,6 +71,7 @@ public class JdPublishController {
private final JdSizeChartAiService sizeChartAiService;
private final JdPublishFillAiService fillAiService;
private final JdPublishPermissionSupport publishPermissionSupport;
private final JdCategoryScopeService jdCategoryScopeService;
@PostMapping({"/tasks", "/tasks/target-brand"})
@Operation(summary = "创建京东商品上架任务", description = "勾选京东数据库商品(归属校验)+可选统一改价规则建单;"
@@ -189,16 +191,18 @@ public class JdPublishController {
// ------------------------------------------------------------------
// v1.5 Phase 4:用户端「商品上架」页读侧(选品分页+任务面板四端点)。
// 形状照 JdCollectController 面板段同族:dashboard/history/delete 走 resolve,
// progressBatch 走 requireOwnershipUserId+filterOwnedTaskIds;products 按会话
// 身份只回归属当前用户的商品行(AC-9 owner 过滤,跨用户商品 0 返回)。
// progressBatch 走 requireOwnershipUserId+filterOwnedTaskIds;products 按会话身份
// 解析可见数据权限分组、回归属行(2026-11-25 口径改:与分类下拉/徽标同源,同组同事
// 采集的商品也能上架;跨公司/跨组看不到——见 JdProductQueryService#pageInScope)。
// ------------------------------------------------------------------
@GetMapping("/products")
@Operation(summary = "用户端选品分页(归属过滤)", description = "「商品上架」页选品列表:只回归属当前"
+ "登录用户的京东数据库商品行(AC-9 owner 过滤,跨用户商品 0 返回),可按自定义分类过滤"
+ "(分类 id 经 V203 归属表两步下推:先取 product_id 集,再 in 主键)。")
@Operation(summary = "用户端选品分页(可见分类作用域)", description = "「商品上架」页选品列表:回归当前"
+ "账号<b>可见数据权限分组</b>下的分类归属商品行(与分类下拉/徽标同源同口径,2026-11-25 "
+ "口径改:同组同事采集的商品也能由你上架;跨公司、跨组一律看不到),可按自定义分类过滤"
+ "(分类与可见组合成同一条 V203 归属表 EXISTS,SQL 侧下推)。")
public ApiResponse<JdProductPageVo> products(
@Parameter(description = "自定义分类 id(不传=全部归属商品)")
@Parameter(description = "自定义分类 id(不传=全部可见分类下商品)")
@RequestParam(name = "category_id", required = false) Long categoryId,
@Parameter(description = "页码") @RequestParam(name = "page", defaultValue = PageDefaults.FIRST_PAGE) Long page,
@Parameter(description = "每页数量") @RequestParam(name = "page_size", defaultValue = PageDefaults.JD_PRODUCTS_PAGE_SIZE) Long pageSize,
@@ -207,7 +211,9 @@ public class JdPublishController {
HttpServletRequest httpRequest) {
userId = adminAuthSupport.requireSessionOwnerUserId(httpRequest, userId);
publishPermissionSupport.assertCanPublish(userId);
return ApiResponse.success(jdProductQueryService.pageForUser(userId, categoryId, page, pageSize));
JdCategoryScopeService.JdCategoryScope scope = jdCategoryScopeService.resolveByUserId(userId);
return ApiResponse.success(jdProductQueryService.pageInScope(scope.visibleGroupIds(),
scope.companyId(), categoryId, page, pageSize));
}
@GetMapping("/dashboard")
@@ -65,6 +65,12 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
* {@code companyId}(2026-11-23 客户口径「公司与公司之间不能互看」):行级公司谓词
* {@code p.company_id = #{companyId}},由控制器经 {@code DataReadScope#readCompanyIdOrOwn}
* 算好传入——非平台超管恒为本公司,平台超管不带公司参数时为 {@code null}(=不筛)。
* {@code visibleGroupIds}(2026-11-25 客户实况「分类里有 3 个商品、选品列表 0 条」):
* 用户端「商品上架」的选品作用域=<b>当前账号可见的数据权限分组</b>下的分类归属行
* (V203 {@code biz_jd_product_group_assign.owner_group_id IN (...)},与分类下拉同一份
* {@code JdCategoryScope#visibleGroupIds})——与分类徽标「N 个商品」同口径。非空时与
* {@code groupId} 合成<b>同一条</b> EXISTS(分类与可见组必须落在同一归属行,不拿别组
* 分类凑数);{@code null}/空=该面不过滤(后管只读页维持原口径不变)。
*/
long countProducts(@Param("keyword") String keyword,
@Param("productCode") String productCode,
@@ -78,7 +84,8 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
@Param("syncedTo") LocalDateTime syncedTo,
@Param("classified") Boolean classified,
@Param("ownerGroupId") Long ownerGroupId,
@Param("companyId") Long companyId);
@Param("companyId") Long companyId,
@Param("visibleGroupIds") List<Long> visibleGroupIds);
/**
* 商品上架配额计数({@code JdPublishQuotaService}):按 owner uid 集(管理员本人+
@@ -125,6 +132,7 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
@Param("classified") Boolean classified,
@Param("ownerGroupId") Long ownerGroupId,
@Param("companyId") Long companyId,
@Param("visibleGroupIds") List<Long> visibleGroupIds,
@Param("offset") long offset,
@Param("limit") int limit);
@@ -8,7 +8,8 @@ import java.util.List;
* 京东商品上架建单请求(v1.5 蓝图 §3 建单契约)。
*
* <p>用户在用户端「京东工具台 → 商品上架」页选自定义分类(消费 {@code GET /api/jd-collect/groups},
* B113-01 分类口唯一,AC-9)→ 分页拉该分类下归属自己的商品 → 勾选 → 可选统一改价规则 → 提交建单。
* B113-01 分类口唯一,AC-9)→ 分页拉该分类下商品(2026-11-25 起作用域=本账号可见
* 数据权限分组,同组同事采集的也能上架)→ 勾选 → 可选统一改价规则 → 提交建单。
* 服务端按 productIds 落 {@code biz_jd_publish_item} 快照行;{@code request_json} 整体进
* {@code biz_file_task} 供兜底拉取回读({@code readCreateRequest} 同形)。</p>
*/
@@ -21,7 +22,8 @@ public class JdPublishTaskCreateRequest {
/** 自定义分类 id(biz_jd_product_group.id,记录与展示用;商品勾选集才是发布范围)。 */
private Long categoryId;
/** 要上架的商品行 id 集(biz_jd_product.id,必填非空;全部必须归属当前用户,AC-9 归属半边)。 */
/** 要上架的商品行 id 集(biz_jd_product.id,必填非空;必须落在本账号可见数据权限分组的
* 分类归属行上——2026-11-25 作用域口径,跨公司/跨组商品建单当场 403)。 */
private List<Long> productIds;
/** 统一改价规则(可选;null=按原价,mode=none)。 */
@@ -128,13 +128,13 @@ public class JdProductQueryService {
filter.titleKeyword(), filter.categories(), filter.groupId(), filter.shopName(),
filter.minPrice(), filter.maxPrice(),
filter.syncedFrom(), filter.syncedTo(), filter.classified(), ownerGroupId,
scopedCompanyId);
scopedCompanyId, null);
List<JdProductEntity> rows = total <= 0 ? List.of()
: jdProductMapper.selectProductPage(filter.keyword(),
filter.productCode(), filter.titleKeyword(), filter.categories(),
filter.groupId(), filter.shopName(), filter.minPrice(), filter.maxPrice(),
filter.syncedFrom(), filter.syncedTo(), filter.classified(), ownerGroupId,
scopedCompanyId,
scopedCompanyId, null,
(safePage - 1) * safeSize, (int) safeSize);
List<JdProductListItemVo> items = rows.stream().map(this::toListItem).toList();
fillGroupNames(items, ownerGroupId);
@@ -147,34 +147,47 @@ public class JdProductQueryService {
}
/**
* 用户端「商品上架」选品分页(v1.5 Phase 4,AC-9 owner 过滤):只回归属当前用户的行,
* 可按自定义分类过滤。分类过滤不走 wrapper 原生 EXISTS(SqlLayeringConventionTest
* rawSqlInsideWrappersDoesNotGrow 钉死不涨):先从 V203 归属表取该分类的 product_id 集,
* 再 in 主键——分类规模=组内商品数,量级可控;分类空集直接回空页不查主表。
* 页码钳制与 {@link #page} 同出口(MAX_PAGE/MAX_PAGE_SIZE,铁律 15)。
* 用户端「商品上架」选品分页(v1.5 Phase 4;<b>2026-11-25 作用域改口径</b>)。
*
* <p>旧形状按 {@code biz_jd_product.user_id = 当前账号} 严格过滤(AC-9 owner 半边),
* 客户实况暴露两套口径:分类下拉与徽标来自「可见数据权限分组」(同组同事采集的商品
* 也算),选品列表只认建单人 ⇒ 阿华112336 看到「3店(3 个商品)」却选不到任何一行、
* 上架按钮永久置灰(那 3 行是同一分组的 阿华普通账户测试 采的)。</p>
*
* <p>新口径:<b>可见分组下的分类归属行</b>——与分类下拉同源
* ({@code JdCategoryScopeService#resolveByUserId} 的 {@code visibleGroupIds}),
* 与分类徽标同表同口径({@code JdProductGroupService#listForGroups} 数的是归属行、
* 不带 owner 条件)。归属(谁能看到分类)已在作用域解析里做完:普通号=自己带的组+
* 所属组组长带的组+管理链上级各账号带的组,跨公司/跨组一律进不来。谓词 SQL 侧下推
* (铁律 20,不 Java 侧对已取页 filter),分类与可见组合成同一条 EXISTS 由
* {@code JdProductMapper.countProducts}/{@code selectProductPage} 的
* {@code visibleGroupIds} 参数承载。</p>
*
* <p>可见分组为空=看不到任何分类归属行,直接回空页不查主表(防 {@code IN ()});
* 页码/页宽钳制与 {@link #page} 同出口(MAX_PAGE/MAX_PAGE_SIZE,铁律 15)。</p>
*/
public JdProductPageVo pageForUser(Long userId, Long categoryId, Long page, Long pageSize) {
public JdProductPageVo pageInScope(List<Long> visibleGroupIds, Long companyId,
Long categoryId, Long page, Long pageSize) {
long safePage = page == null || page < 1 ? 1 : Math.min(page, MAX_PAGE);
long safeSize = pageSize == null ? PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT
: Math.min(Math.max(pageSize, 1), MAX_PAGE_SIZE);
LambdaQueryWrapper<JdProductEntity> query = new LambdaQueryWrapper<JdProductEntity>()
.eq(JdProductEntity::getUserId, userId);
if (categoryId != null && categoryId > 0) {
List<Long> productIds = jdProductGroupAssignMapper.selectList(
new LambdaQueryWrapper<JdProductGroupAssignEntity>()
.select(JdProductGroupAssignEntity::getProductId)
.eq(JdProductGroupAssignEntity::getCategoryId, categoryId))
.stream().map(JdProductGroupAssignEntity::getProductId).distinct().toList();
if (productIds.isEmpty()) {
return emptyPage(safePage, safeSize);
}
query.in(JdProductEntity::getId, productIds);
List<Long> groupIds = visibleGroupIds == null ? List.of()
: visibleGroupIds.stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (groupIds.isEmpty()) {
return emptyPage(safePage, safeSize);
}
long total = jdProductMapper.selectCount(query);
long safeCategoryId = categoryId != null && categoryId > 0 ? categoryId : 0L;
long total = jdProductMapper.countProducts(null, null, null, null,
safeCategoryId, null, null, null, null, null, null, null,
companyId, groupIds);
List<JdProductListItemVo> items = total <= 0 ? List.of()
: jdProductMapper.selectList(query
.orderByDesc(JdProductEntity::getSyncedAt, JdProductEntity::getId)
.last("limit " + (safePage - 1) * safeSize + ", " + safeSize))
: jdProductMapper.selectProductPage(null, null, null, null,
safeCategoryId, null, null, null, null, null, null, null,
companyId, groupIds,
(safePage - 1) * safeSize, (int) safeSize)
.stream().map(this::toListItem).toList();
JdProductPageVo vo = new JdProductPageVo();
vo.setItems(items);
@@ -13,6 +13,7 @@ import com.nanri.aiimage.common.task.TaskStatuses;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.nanri.aiimage.config.CollectRoutingProperties;
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;
@@ -20,6 +21,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.JdProductImageEntity;
import com.nanri.aiimage.modules.jd.model.entity.JdProductSkuEntity;
import com.nanri.aiimage.modules.jd.model.entity.JdPublishItemEntity;
@@ -63,9 +65,11 @@ import java.util.stream.Collectors;
* 19899 Chrome profile,执行方没有"界面选 ONLINE"的自由度);② 建单口<b>不接</b>
* {@code requireSubmitSecret}——发布由桌面机已登录京麦的 Chrome 直连执行,无代理无 LLM
* ({@code TaskModuleRegistry} 登记 {@code Optional.empty()},铁律 27c 登记结论);
* ③ 建单前置闸门按铁律 27b 排在一切昂贵动作前:未选商品 / 选了他人商品(AC-9 归属,
* 业务码 403)/ 超配额(AC-8,{@code JdPublishQuotaService#assertWithinQuota} 原语接线)
* 逐条点名拒绝;④ 建单即按商品快照落 {@code biz_jd_publish_item} 行(V210,商品库有
* ③ 建单前置闸门按铁律 27b 排在一切昂贵动作前:未选商品 / 勾选商品不在可上架作用域
* (2026-11-25 起=本账号可见数据权限分组下的分类归属行,跨公司/跨组业务码 403;旧口径
* 「只认建单人」把同组同事采的商品全拒在门外,客户实况「分类里有 3 个、列表 0 个」)/
* 超配额(AC-8,{@code JdPublishQuotaService#assertWithinQuota} 原语接线)逐条点名拒绝;
* ④ 建单即按商品快照落 {@code biz_jd_publish_item} 行(V210,商品库有
* 清理语义,发布记录不随商品删除丢失溯源)。</p>
*
* <p>建单成功页面推送与兜底拉取共用同一份载荷组装 {@link #buildQueuePayload}:
@@ -109,6 +113,8 @@ public class JdPublishTaskService {
private final TaskHeartbeatService taskHeartbeatService;
private final TaskHeartbeatPositionService taskHeartbeatPositionService;
private final JdPublishTaskItemsResetSpiImpl itemsReset;
private final JdCategoryScopeService jdCategoryScopeService;
private final JdProductGroupAssignMapper jdProductGroupAssignMapper;
/** 下发批大小出厂值(aiimage.jd.publish.items-batch-size,服务侧钳制上限 100)。 */
@Value("${aiimage.jd.publish.items-batch-size:20}")
@@ -125,15 +131,23 @@ public class JdPublishTaskService {
AdminUserEntity operator = adminUserMapper.selectById(request.getUserId());
jdPublishQuotaService.assertWithinQuota(operator, request.getProductIds().size());
// AC-9 归属半边:按主键批量取商品行(建单勾选集上界 500,非取页分页),存在性
// 与归属一起 fail-closed——缺行与选了他人商品都当场拒,不让一半商品静默混进任务。
// 归属闸(2026-11-25 作用域改口径:选中商品必须在「本账号可见数据权限分组」的分类
// 归属行里,与选品列表/分类下拉同源同口径)。旧形状按 biz_jd_product.user_id 严格
// 等于当前账号,客户实况把同组同事采集的商品全数拒在门外(分类里有 3 个、列表 0 个、
// 上架按钮永久置灰)——分组本就是团队作业面:组内谁采的都能由组内成员上架,
// 跨公司/跨组仍 fail-closed 拒绝(可见集来自 JdCategoryScopeService,不由请求体决定)。
// 存在性先于归属:缺行与不在作用域内都当场拒,不让一半商品静默混进任务。
List<JdProductEntity> products = jdProductMapper.selectBatchIds(request.getProductIds());
if (products.size() != request.getProductIds().size()) {
throw new BusinessException("部分商品不存在或已从京东数据库清理,请刷新后重新勾选");
}
JdCategoryScopeService.JdCategoryScope scope =
jdCategoryScopeService.resolveByUserId(request.getUserId());
Set<Long> visibleProductIds = resolveVisibleProductIds(request.getProductIds(), scope);
for (JdProductEntity product : products) {
if (!Objects.equals(product.getUserId(), request.getUserId())) {
throw new BusinessException(403, "勾选的商品不属于当前账号,不能上架他人数据库的商品");
if (!visibleProductIds.contains(product.getId())) {
throw new BusinessException(403,
"勾选的商品不在你可上架的店铺分类内(跨公司/跨组商品不能上架),请刷新后重新勾选");
}
}
// 来源店铺闸(2026-11-24 客户口径「软件怎么知道要上架哪一家店」):一次任务只允许
@@ -182,6 +196,38 @@ public class JdPublishTaskService {
return vo;
}
/**
* 建单归属闸的作用域取数(2026-11-25):勾选商品里落在「可见数据权限分组的分类归属行」
* 上的 id 集。与 {@code JdProductQueryService#pageInScope} 读同一张 V203 归属表、
* 同一份 {@code JdCategoryScope#visibleGroupIds}(分类下拉/徽标/选品列表/建单四处
* 同口径,口径源只有 {@code JdCategoryScopeService} 一处)。可见集为空=一行都不放行
* (不查 SQL、不回空 {@code IN ()});归属表为空同样回空集,调用方按 403 拒。
*/
private Set<Long> resolveVisibleProductIds(List<Long> productIds,
JdCategoryScopeService.JdCategoryScope scope) {
if (productIds == null || productIds.isEmpty() || scope == null) {
return Set.of();
}
List<Long> groupIds = scope.visibleGroupIds() == null ? List.of()
: scope.visibleGroupIds().stream()
.filter(id -> id != null && id > 0)
.distinct()
.toList();
if (groupIds.isEmpty()) {
return Set.of();
}
return jdProductGroupAssignMapper.selectList(
new LambdaQueryWrapper<JdProductGroupAssignEntity>()
.select(JdProductGroupAssignEntity::getProductId)
.in(JdProductGroupAssignEntity::getProductId, productIds)
.in(JdProductGroupAssignEntity::getOwnerGroupId, groupIds)
.isNotNull(JdProductGroupAssignEntity::getCategoryId))
.stream()
.map(JdProductGroupAssignEntity::getProductId)
.filter(id -> id != null && id > 0)
.collect(Collectors.toSet());
}
/** 仅当前代次的 PENDING 任务能被页面队列中的桌面执行器认领。 */
public void activateTask(Long taskId, Long userId, int attempt, Integer protocol) {
FileTaskEntity task = requireOwnedTask(taskId, userId);
@@ -143,12 +143,30 @@
(price_min >= X AND price_max <= Y),不是「区间有交集」。 -->
<if test="minPrice != null">AND p.price_min &gt;= #{minPrice}</if>
<if test="maxPrice != null">AND p.price_max &lt;= #{maxPrice}</if>
<if test="groupId != null and groupId &gt; 0">
<!-- 选品作用域(2026-11-25 客户实况「自定义分类写着 3 个商品、选品列表却是 0 条」):
用户端「商品上架」的可见面=当前账号可见数据权限分组(JdCategoryScope#visibleGroupIds,
与分类下拉同一份解析结果)下的分类归属行。与 groupId 合成<b>同一条</b> EXISTS——
「这个分类」与「我可见的组」必须是同一行归属(不许拿别组分类名凑数);
visibleGroupIds 为 null/空时退回原来只按 category_id 的等值形状(后管只读页
groupId 档行为逐字不变)。徽标计数(JdProductGroupService.listForGroups)读的也是
这张归属表、不带 owner 条件,故与徽标天然同口径。谓词 SQL 侧下推(铁律 20)。 -->
<if test="visibleGroupIds != null and visibleGroupIds.size() > 0">
AND EXISTS (
SELECT 1 FROM biz_jd_product_group_assign a
WHERE a.product_id = p.id AND a.category_id = #{groupId}
WHERE a.product_id = p.id
<if test="groupId != null and groupId &gt; 0">AND a.category_id = #{groupId}</if>
AND a.owner_group_id IN
<foreach collection="visibleGroupIds" item="vgid" open="(" separator="," close=")">#{vgid}</foreach>
)
</if>
<if test="visibleGroupIds == null or visibleGroupIds.size() == 0">
<if test="groupId != null and groupId &gt; 0">
AND EXISTS (
SELECT 1 FROM biz_jd_product_group_assign a
WHERE a.product_id = p.id AND a.category_id = #{groupId}
)
</if>
</if>
<!-- 分类状态快筛(2026-11-17 用户口径「加个已分类和未分类的筛选」):已分类=在
「当前数据权限分组」(owner_group_id)下有 category_id 非空的归属行——与读侧
fillGroupNames 把分类列填出来的条件同形;未分类=NOT EXISTS。谓词 SQL 侧下推
@@ -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;
+8 -7
View File
@@ -40,8 +40,9 @@ import BrandApiSecretSettingsButton from '@/pages/brand/components/BrandApiSecre
*
* 六条边界(照 v1.4/v1.5 已落地的采集契约与外观专利任务面板骨架,不另造流程):
* 1. 分类选项唯一来源=jdCollect.groups 既有出口(B113-01,AC-9 源码绊线钉死:本页不得自拼
* 分类 URL);分组下的商品列表走 jdPublish.products(AC-9 owner 过滤:服务端只回归属
* 当前登录用户的行),页内翻页、勾选跨页累计;
* 分类 URL);分组下的商品列表走 jdPublish.products(2026-11-25 起作用域=本账号可见
* 数据权限分组下的分类归属行,与分类下拉/徽标同源——同组同事采集的商品也能上架,
* 跨公司/跨组看不到),页内翻页、勾选跨页累计;
* 2. 改价规则三档(none/percent/fixed,蓝图 §3 死契约),与勾选集一起进建单体;服务端按
* 同一规则折算 SKU 价(铁律 27a 改价同源);
* 3. 网页原样转交建单响应的 queuePayload;认领只由客户端与后端完成,投递失败仍可由客户端拉取;
@@ -93,7 +94,7 @@ const GROUPS_EMPTY_HINT_TEXT =
'本分组暂无分类——请联系管理员在后台「京东数据库 → 店铺分类」创建分类并归入你的分组后再上架。'
const groupsEmpty = computed(() => !groupsLoading.value && groupOptions.value.length === 0)
// ---- 选品分页(owner 过滤在服务端;勾选跨页累计在本地 Map,翻页不清空)----
// ---- 选品分页(作用域过滤在服务端:可见分组的分类归属行;勾选跨页累计在本地 Map,翻页不清空)----
const PRODUCT_PAGE_SIZE = 12
const products = ref<JdPublishProductVo[]>([])
const productTotal = ref(0)
@@ -712,7 +713,7 @@ async function startPublish() {
? { mode: 'none' as const }
: { mode: priceMode.value, value: priceValue.value as number }
// 勾选集即建单 productIds;selectedGroupId 即建单体 categoryId
// (服务端按 owner 校验,选了他人商品当场拒,AC-9)。
// (服务端按可见分类作用域校验:跨公司/跨组商品当场拒,同组同事采集的可上架)。
const categoryId = selectedGroupId.value
const productIds = selectedProductIds.value
const brand = targetBrandName.value
@@ -784,7 +785,7 @@ onUnmounted(() => {
<router-link class="tool-head__back" :to="backHref">← 返回京东工具台</router-link>
<span class="tool-head__title">商品上架</span>
<span class="tool-head__desc">
把京东数据库里你自己的商品批量上架到京麦店铺:选分类、勾商品、可选统一改价,本机已登录京麦的客户端逐个发布。
把京东数据库里的商品批量上架到京麦店铺:选分类、勾商品、可选统一改价,本机已登录京麦的客户端逐个发布。
</span>
</div>
@@ -828,7 +829,7 @@ onUnmounted(() => {
:label="`${group.name}(${group.productCount ?? 0} 个商品)`" :value="group.id" />
</el-select>
<p v-if="groupsEmpty" class="category-hint category-hint--warn">{{ GROUPS_EMPTY_HINT_TEXT }}</p>
<p v-else class="category-hint">分类由管理员在后台「京东数据库」维护;列表只显示归属你账号的商品。</p>
<p v-else class="category-hint">分类由管理员在后台「京东数据库」维护;列表显示你可见分类下的商品(同组同事采集的也能上架)。</p>
</div>
<div class="product-block" v-loading="productsLoading">
@@ -844,7 +845,7 @@ onUnmounted(() => {
@click="clearSelection">清空勾选</button>
</div>
<div v-if="!products.length && !productsLoading" class="product-empty">
{{ selectedGroupId ? '该分类下暂无归属你账号的商品' : '请先选择分类' }}
{{ selectedGroupId ? '该分类下暂无商品' : '请先选择分类' }}
</div>
<label v-for="product in products" :key="product.id" class="product-row">
<input type="checkbox" class="product-check"
+2 -1
View File
@@ -233,7 +233,8 @@ export const API_ENDPOINTS = {
createWithTargetBrand: '/api/jd-publish/tasks/target-brand',
/** 修改目标品牌后原位重试;沿用建单回执以直推本机队列。 */
retryBrand: '/api/jd-publish/tasks/{taskId}/retry-brand',
/** 用户端选品分页(AC-9 owner 过滤:只回归属当前用户的京东数据库商品行) */
/** 用户端选品分页(作用域=本账号可见数据权限分组下的分类归属行,2026-11-25 口径改:
* 同组同事采集的商品同样可上架;跨公司/跨组不可见) */
products: '/api/jd-publish/products',
/** 任务面板总览(运行中/成功/失败计数统计卡) */
dashboard: '/api/jd-publish/dashboard',
@@ -7,7 +7,8 @@ import { createTaskProgressRequestCache } from '../../../task-progress-request-c
/**
* 京东商品上架(v1.5 蓝图 docs/ITERATION_PLAN.md §3/§4 Phase 4):用户端「京东工具台 →
* 商品上架」选自定义分类 → 勾选归属自己的商品(AC-9 owner 过滤)→ 可选统一改价规则 →
* 商品上架」选自定义分类 → 勾选你可见分类下的商品(2026-11-25 起作用域=本账号可见数据
* 权限分组,同组同事采集的商品也能上架;跨公司/跨组不可见)→ 可选统一改价规则 →
* 建单(服务端落 PENDING、执行通道显式 LOCAL)→ 建单响应 queuePayload 原样投
* api.enqueue_json 推本机客户端(铁律 32 第三件,type 与 SPI QUEUE_TYPE 同值)。
*
@@ -31,8 +32,8 @@ export interface JdPublishPriceRule {
}
/**
* 建单(AC-1/AC-8/AC-9):categoryId=所选自定义分类;productIds=勾选的商品行 id 集
* (服务端按 owner 校验,选了他人商品/超配额当场业务码拒绝并点名文案)。
* 建单(AC-1/AC-8;作用域闸 2026-11-25 改口径):categoryId=所选自定义分类;productIds=勾选的商品行 id 集
* (服务端按「可见数据权限分组的分类归属行」校验,跨公司/跨组商品或超配额当场业务码拒绝并点名文案)。
* 请求体是 JSON(@RequestBody + JdPublishTaskCreateRequest),字段名 camelCase 与采集建单同口径。
*/
export function createJdPublishTask(
@@ -62,7 +63,8 @@ export function retryJdPublishBrand(taskId: number, targetBrandName: string, res
}
// ---------------------------------------------------------------------------
// 选品分页(AC-9 owner 过滤):服务端只回归属当前用户的京东数据库商品行。
// 选品分页(2026-11-25 作用域):服务端回归本账号可见数据权限分组下的分类归属商品行
// (同组同事采集的也在内;跨公司/跨组不可见——与分类下拉/徽标同源)。
// 形状照后管京东数据库列表行裁剪(选品面板只需要主字段,attr/detail 不下发)。
// ---------------------------------------------------------------------------
@@ -92,7 +94,7 @@ export interface JdPublishProductPageVo {
pageSize: number;
}
/** 选品分页:categoryId 缺省=全部归属商品(分类下拉唯一来源是 jdCollect.groups 既有出口,AC-9)。 */
/** 选品分页:categoryId 缺省=全部可见分类下商品(分类下拉唯一来源是 jdCollect.groups 既有出口,AC-9)。 */
export function fetchJdPublishProducts(categoryId: number | null, page: number, pageSize: number) {
const params: Record<string, unknown> = {
user_id: getCurrentUserId(),
+6 -2
View File
@@ -51,10 +51,14 @@ test('AC-9: JdPublishTab 分类选择消费唯一 groups 出口', () => {
assert.ok(!tab.includes('jd-collect/groups'), 'JdPublishTab 不得裸写分类 URL(B113-01 绊线)')
})
test('AC-9: 商品列表只拉归属当前用户的行(owner 过滤)', () => {
test('选品作用域:建单带 productIds,文案与作用域口径一致(2026-11-25)', () => {
const tab = read('src/pages/jd/JdPublishTab.vue', '商品上架页')
assert.ok(tab.includes('productIds') || tab.includes('productId'),
'JdPublishTab 建单提交必须携带勾选的商品 id 集合(服务端按 owner 校验,跨用户商品 0 返回)')
'JdPublishTab 建单提交必须携带勾选的商品 id 集合(服务端按可见分类作用域校验)')
// 口径改:同组同事采集的商品也能上架——文案不得再写「归属你账号」,空态与说明同步。
assert.ok(!tab.includes('归属你账号'), '文案不得再写「归属你账号」:起作用域是可见分类,不是建单人')
assert.ok(tab.includes('该分类下暂无商品'), '空态文案必须与作用域口径一致')
assert.ok(tab.includes('同组同事采集的也能上架'), '分类说明必须点名「同组同事采集的也能上架」')
})
test('建单请求体形状:categoryId + productIds + priceRule{mode,value}', () => {
@@ -440,7 +440,7 @@ it('分类拉回空=点名「本分组暂无分类」带动作句,不再静
expect(warn.text()).toContain('本分组暂无分类')
expect(warn.text()).toContain('京东数据库 → 店铺分类')
// 原静态说明只在非空态出现(v-if/v-else 互斥,同屏双份即红)。
expect(page.text()).not.toContain('列表只显示归属你账号的商品')
expect(page.text()).not.toContain('列表显示你可见分类下的商品')
// 空态下建单仍被硬闸禁用(既有 submitDisabled 口径),但界面此刻给得出原因。
expect(page.get('.collect-btn').attributes('disabled')).toBeDefined()
})