feat(jd): 京东数据库按公司隔离——读侧四口+写侧五口,公司之间不互看

客户口径「公司与公司之间不能互看」,推翻 2026-11-16 的「公共库无公司维度」。

读侧(统一走 DataReadScope#readCompanyIdOrOwn,非平台超管恒为本公司):
- 列表 page:scopedCompanyId 下推到 XML productConds(铁律 20,SQL 侧过滤)
- 导出 exportCsv:勾选 id 里有一行不属于本公司就整批拒绝(不做部分导出)
- 详情 detail:别公司的行按「商品不存在」拒绝,不返回 403 泄露行是否存在
- 类目下拉 categories:与列表同口径;用户端采集建单侧按会话身份解析公司,
  解析不出公司回空集而非全平台
- 店铺候选 availableShops:按公司隔离

写侧(统一走 DataReadScope#writeCompanyId):
- 编辑/上传图片/删除/批量删除/批量归类五口都先过 assertRowsInCompany:
  读侧藏起来的行仍能删=静默删除,不是隔离
- 分组归类额外比目标分组所在公司(平台超管也不得跨公司归类,否则分类计数串公司)

判据:AdminJdProductControllerTest 34/JdProductMapperSqlTest 5/
JdCategoryScopeServiceTest 23/JdGroupShopServiceTest 10/
JdProductQueryServiceUserPageTest 7 共 79 支绿;
后管 company-tab-wiring 12 支绿(跨端常量钉子同步)。
This commit is contained in:
2026-11-24 16:02:43 +08:00
parent 91a6844974
commit 5df2f63c88
14 changed files with 402 additions and 224 deletions
@@ -113,6 +113,8 @@ public class AdminJdProductController {
private final JdCategoryScopeService jdCategoryScopeService;
private final AdminAuthSupport adminAuthSupport;
private final PermissionMenuService permissionMenuService;
/** 公司隔离的唯一出口(2026-11-23):读侧 {@code readCompanyIdOrOwn}、写侧 {@code writeCompanyId}。 */
private final com.nanri.aiimage.common.service.DataReadScope dataReadScope;
@GetMapping
@Operation(summary = "分页查询京东数据库商品", description = "公共库分页查询同步落表的京东商品;"
@@ -148,11 +150,15 @@ public class AdminJdProductController {
@Parameter(description = "分类状态快筛(2026-11-17 用户口径「加个已分类和未分类的筛选」):"
+ "true=当前分组下有分类,false=未分类,缺省=全部;按 V203 归属表 SQL 侧下推")
@RequestParam(name = "classified", required = false) Boolean classified,
@Parameter(description = "公司 id(仅平台超管有效;非超管一律按本公司过滤,传什么都不算)")
@RequestParam(name = "company_id", required = false) Long companyId,
HttpServletRequest request) {
RequestOperator operator = requireJdAccess(request);
if (request.getParameter("grouped") != null || request.getParameter("ungrouped") != null) {
throw new BusinessException("页面版本已更新,请刷新页面后重新查询");
}
// 公司隔离(2026-11-23 客户口径「公司与公司之间不能互看」):非平台超管恒为本公司。
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator.user(), companyId);
JdCategoryScopeService.JdCategoryScope scope = jdCategoryScopeService.resolve(operator.user());
Long scopedOwnerGroupId = jdCategoryScopeService.readGroupOrNull(scope, ownerGroupId);
if (groupId != null && groupId > 0) {
@@ -160,7 +166,7 @@ public class AdminJdProductController {
}
return ApiResponse.success(jdProductQueryService.page(page, pageSize,
keyword, productCode, titleKeyword, categories, groupId, shopName, minPrice, maxPrice,
syncedFrom, syncedTo, classified, scopedOwnerGroupId));
syncedFrom, syncedTo, classified, scopedOwnerGroupId, scopedCompanyId));
}
@PostMapping("/export")
@@ -171,9 +177,12 @@ public class AdminJdProductController {
+ "导出属读行为:走 requireJdAccess 菜单正门,不走商品行写闸。")
public ResponseEntity<byte[]> export(
@Valid @RequestBody JdProductExportRequest body,
@Parameter(description = "公司 id(仅平台超管有效)")
@RequestParam(name = "company_id", required = false) Long companyId,
HttpServletRequest request) {
RequestOperator operator = requireJdAccess(request);
byte[] bytes = jdProductQueryService.exportCsv(body.getIds());
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator.user(), companyId);
byte[] bytes = jdProductQueryService.exportCsv(body.getIds(), scopedCompanyId);
log.info("[jd-product] 导出CSV operatorId={} requested={}", operator.id(), body.getIds().size());
String filename = "京东数据库-" + LocalDateTime.now().format(EXPORT_FILENAME_FORMATTER) + ".csv";
return ResponseEntity.ok()
@@ -185,26 +194,33 @@ public class AdminJdProductController {
@GetMapping("/categories")
@Operation(summary = "类目筛选下拉数据源", description = "已落库行 DISTINCT 的 category_path"
+ "(京麦末级类目名)+ 行数,按类目名排序;全平台公共去重、无公司维度"
+ "(与用户端采集建单侧同一出口同一口径)。")
public ApiResponse<List<JdProductCategoryVo>> categories(HttpServletRequest request) {
requireJdAccess(request);
return ApiResponse.success(jdProductQueryService.categories());
+ "(京麦末级类目名)+ 行数,按类目名排序;2026-11-23 起与列表同口径按行公司隔离"
+ "(非平台超管只看本公司;平台超管可用 company_id 切公司)。")
public ApiResponse<List<JdProductCategoryVo>> categories(
@Parameter(description = "公司 id(仅平台超管有效)")
@RequestParam(name = "company_id", required = false) Long companyId,
HttpServletRequest request) {
RequestOperator operator = requireJdAccess(request);
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator.user(), companyId);
return ApiResponse.success(jdProductQueryService.categories(scopedCompanyId));
}
@GetMapping("/{id}")
@Operation(summary = "商品详情", description = "主字段全量 + attr_json 属性分组(契约形状不识别时落空分组"
+ "并把原文放 attrJsonRaw 兜底,AC-7 未识别属性不丢弃)+ 已转存图片(100% 自有域名,AC-4)。"
+ "公共库无公司维度:按 id 直取,越界 id 按不存在拒绝。")
+ "公司隔离(2026-11-23):别公司的行按「商品不存在」拒绝,不泄露行是否存在。")
public ApiResponse<JdProductDetailVo> detail(
@Parameter(description = "商品行 id") @PathVariable("id") Long id,
@Parameter(description = "当前数据权限分组 id(分类字段按它取)")
@RequestParam(name = "owner_group_id", required = false) Long ownerGroupId,
@Parameter(description = "公司 id(仅平台超管有效)")
@RequestParam(name = "company_id", required = false) Long companyId,
HttpServletRequest request) {
RequestOperator operator = requireJdAccess(request);
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator.user(), companyId);
Long scopedOwnerGroupId = jdCategoryScopeService.readGroupOrNull(
jdCategoryScopeService.resolve(operator.user()), ownerGroupId);
return ApiResponse.success(jdProductQueryService.detail(id, scopedOwnerGroupId));
return ApiResponse.success(jdProductQueryService.detail(id, scopedOwnerGroupId, scopedCompanyId));
}
@PutMapping("/{id}")
@@ -218,11 +234,13 @@ public class AdminJdProductController {
@Valid @RequestBody JdProductUpdateRequest body,
HttpServletRequest request) {
RequestOperator operator = requireJdProductWrite(request);
Long scopedCompanyId = dataReadScope.writeCompanyId(operator.user());
jdProductQueryService.assertRowsInCompany(List.of(id), scopedCompanyId);
Long updatedId = jdProductUpdateService.update(id, body);
log.info("[jd-product] 编辑商品 operatorId={} id={} title={}",
operator.id(), updatedId, body.getTitle());
// 返回刷新后的详情(前端保存后直接回填抽屉),复用详情同一查询出口。
return ApiResponse.success(jdProductQueryService.detail(updatedId, null));
return ApiResponse.success(jdProductQueryService.detail(updatedId, null, scopedCompanyId));
}
@PostMapping(value = "/{id}/images", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@@ -242,11 +260,13 @@ public class AdminJdProductController {
@RequestParam(value = "replace_image_id", required = false) Long replaceImageId,
HttpServletRequest request) {
RequestOperator operator = requireJdProductWrite(request);
Long scopedCompanyId = dataReadScope.writeCompanyId(operator.user());
jdProductQueryService.assertRowsInCompany(List.of(id), scopedCompanyId);
Long updatedId = jdProductUpdateService.uploadImage(id, file, imageType, attrName, replaceImageId);
log.info("[jd-product] 上传图片 operatorId={} id={} replaceImageId={} imageType={}",
operator.id(), updatedId, replaceImageId, imageType);
// 返回刷新后的详情(前端上传后直接回填抽屉图片区),复用详情同一查询出口。
return ApiResponse.success(jdProductQueryService.detail(updatedId, null));
return ApiResponse.success(jdProductQueryService.detail(updatedId, null, scopedCompanyId));
}
@DeleteMapping("/{id}")
@@ -257,6 +277,7 @@ public class AdminJdProductController {
@Parameter(description = "商品行 id") @PathVariable("id") Long id,
HttpServletRequest request) {
RequestOperator operator = requireJdProductWrite(request);
jdProductQueryService.assertRowsInCompany(List.of(id), dataReadScope.writeCompanyId(operator.user()));
JdProductDeleteVo vo = jdProductDeleteService.delete(List.of(id));
// 管理端破坏性动作必须留痕:谁、删了哪些行、桶里清了几个对象(同分组删除口径)。
log.info("[jd-product] 删除商品 operatorId={} id={} deleted={} objectsDeleted={}",
@@ -277,6 +298,9 @@ public class AdminJdProductController {
RequestOperator operator = requireJdAccess(request);
JdCategoryScopeService.JdCategoryScope scope = jdCategoryScopeService.resolve(operator.user());
ShopManageGroupEntity target = jdCategoryScopeService.requireWritableGroup(scope, body.getOwnerGroupId());
// 公司隔离(2026-11-23):商品行必须属于目标分组所在的公司——平台超管也不得把
// A 公司采的商品归进 B 公司的分类(归类行带公司列,跨公司归类会让分类计数串公司)。
jdProductQueryService.assertRowsInCompany(body.getIds(), target.getCompanyId());
int updated = jdProductUpdateService.assignGroup(target.getId(), target.getCompanyId(),
body.getIds(), body.getGroupId(), operator.id());
// 管理端写动作必须留痕:谁、把多少行归到哪个组的哪个分类(null=移出分类)。
@@ -292,6 +316,7 @@ public class AdminJdProductController {
@Valid @RequestBody JdProductBatchDeleteRequest body,
HttpServletRequest request) {
RequestOperator operator = requireJdProductWrite(request);
jdProductQueryService.assertRowsInCompany(body.getIds(), dataReadScope.writeCompanyId(operator.user()));
JdProductDeleteVo vo = jdProductDeleteService.delete(body.getIds());
log.info("[jd-product] 批量删除商品 operatorId={} requested={} deleted={} objectsDeleted={} kept={}",
operator.id(), body.getIds().size(), vo.getDeleted(),
@@ -72,6 +72,8 @@ public class AdminJdProductGroupController {
private final JdCategoryScopeService jdCategoryScopeService;
private final AdminAuthSupport adminAuthSupport;
private final PermissionMenuService permissionMenuService;
/** 店铺候选的公司隔离出口(2026-11-23,与 AdminJdProductController 同一出口)。 */
private final com.nanri.aiimage.common.service.DataReadScope dataReadScope;
@GetMapping
@Operation(summary = "分类列表(含分类下商品数)", description = "按「当前分组」返回分类,"
@@ -221,7 +223,7 @@ public class AdminJdProductGroupController {
@GetMapping("/shops/available")
@Operation(summary = "可添加的店铺候选", description = "biz_jd_product.shop_name DISTINCT +"
+ " 关键词包含匹配 + 排除指定分类已有名单(exclude_category_id);limit ≤ 200。"
+ "商品是全平台公共的,候选同样全平台去重(不按分组收窄)。")
+ "2026-11-23 起按公司隔离:非平台超管只出本公司商品行的店铺名(公司之间不互看)。")
public ApiResponse<List<String>> availableShops(
@Parameter(description = "关键词(店铺名包含匹配)")
@RequestParam(name = "keyword", required = false) String keyword,
@@ -235,7 +237,9 @@ public class AdminJdProductGroupController {
if (excludeCategoryId != null && excludeCategoryId > 0) {
requireReadableCategory(scope, jdProductGroupService.requireCategory(excludeCategoryId).getOwnerGroupId());
}
return ApiResponse.success(jdGroupShopService.availableShops(keyword, excludeCategoryId, limit));
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator.user(), null);
return ApiResponse.success(jdGroupShopService.availableShops(keyword, excludeCategoryId, limit,
scopedCompanyId));
}
/** 名单读闸:分类必须在本账号可见组内(同组普通号能看名单,越组 403)。 */
@@ -71,14 +71,20 @@ public class JdCollectController {
private final AdminAuthSupport adminAuthSupport;
@GetMapping("/categories")
@Operation(summary = "采集类目下拉数据源", description = "建单页类目多选面板的选项集:公共库已落库行"
@Operation(summary = "采集类目下拉数据源", description = "建单页类目多选面板的选项集:已落库行"
+ " DISTINCT 的 category_path(京麦末级类目名)+ 行数,按类目名排序。与后管京东数据库"
+ "同一条 SQL(JdProductQueryService.categories),公共库无公司维度、全平台去重。"
+ "同一条 SQL(JdProductQueryService.categories)。<b>2026-11-23 起按公司隔离</b>:"
+ "只出本账号所属公司采集的行(公司与公司之间不互看);解析不出公司时回空集,不给全平台。"
+ "边界:从未采集过的店铺/新类目不在集内(先全店采一次即有),页面选项空态不阻塞建单。")
public ApiResponse<List<JdProductCategoryVo>> categories(HttpServletRequest httpRequest) {
// 登录用户即可读(公共库口径):解析不出会话身份就 401(fail-closed,与建单同口径)。
adminAuthSupport.requireOwnershipUserId(httpRequest, null);
return ApiResponse.success(jdProductQueryService.categories());
// 登录用户即可读:解析不出会话身份就 401(fail-closed,与建单同口径)。
Long userId = adminAuthSupport.requireSessionOwnerUserId(httpRequest, null);
Long companyId = jdCategoryScopeService.resolveByUserId(userId).companyId();
if (companyId == null || companyId <= 0) {
// 公司读不出=看不到任何公司行;回空集而不是把全平台类目下发(隔离优先)。
return ApiResponse.success(List.of());
}
return ApiResponse.success(jdProductQueryService.categories(companyId));
}
@GetMapping("/groups")
@@ -62,6 +62,9 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
* {@code classified}+{@code ownerGroupId}(2026-11-17 用户口径「加个已分类和未分类的筛选」):
* 按 V203 归属表 EXISTS/NOT EXISTS 快筛分类状态,NULL=不过滤;与 11-16 撤掉的旧
* grouped/ungrouped(读已停用的 biz_jd_product.group_id 列)不是一回事。
* {@code companyId}(2026-11-23 客户口径「公司与公司之间不能互看」):行级公司谓词
* {@code p.company_id = #{companyId}},由控制器经 {@code DataReadScope#readCompanyIdOrOwn}
* 算好传入——非平台超管恒为本公司,平台超管不带公司参数时为 {@code null}(=不筛)。
*/
long countProducts(@Param("keyword") String keyword,
@Param("productCode") String productCode,
@@ -74,7 +77,8 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
@Param("syncedFrom") LocalDateTime syncedFrom,
@Param("syncedTo") LocalDateTime syncedTo,
@Param("classified") Boolean classified,
@Param("ownerGroupId") Long ownerGroupId);
@Param("ownerGroupId") Long ownerGroupId,
@Param("companyId") Long companyId);
/**
* 商品上架配额计数({@code JdPublishQuotaService}):按 owner uid 集(管理员本人+
@@ -120,6 +124,7 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
@Param("syncedTo") LocalDateTime syncedTo,
@Param("classified") Boolean classified,
@Param("ownerGroupId") Long ownerGroupId,
@Param("companyId") Long companyId,
@Param("offset") long offset,
@Param("limit") int limit);
@@ -127,16 +132,19 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
* 店铺名单「可添加候选」(v1.7,添加店铺弹窗源):{@code biz_jd_product.shop_name}
* DISTINCT + like + 排除指定分类已有名单(NOT IN 下推,铁律 20),按店铺名升序。
* {@code limit} 必须是服务侧钳制后的值(≤ {@code JdGroupShopService.MAX_AVAILABLE_LIMIT}),
* mapper 不做第二次换算。
* mapper 不做第二次换算。{@code companyId}(2026-11-23):候选店铺也只能来自本公司
* 商品行,否则 A 公司能在候选里看到 B 公司的店铺名(口径见 {@link #countProducts})。
*/
List<String> selectDistinctShopNames(@Param("keyword") String keyword,
@Param("excludeNames") List<String> excludeNames,
@Param("limit") int limit);
@Param("limit") int limit,
@Param("companyId") Long companyId);
/**
* 类目筛选下拉数据源(2026-11-15 用户口径「京麦的类目也要同步过来后台」):
* 已落库行的 DISTINCT {@code category_path}(=京麦末级类目名)+ 行数,全平台公共去重
* (2026-11-16 起与用户端采集建单侧同口径,无公司维过滤)。
* 已落库行的 DISTINCT {@code category_path}(=京麦末级类目名)+ 行数。
* 2026-11-23 起按行公司隔离({@code companyId} 由 {@code DataReadScope#readCompanyIdOrOwn}
* 算好;平台超管不带公司参数时为 {@code null}=全平台去重)。
*/
List<JdProductCategoryVo> selectProductCategories();
List<JdProductCategoryVo> selectProductCategories(@Param("companyId") Long companyId);
}
@@ -32,10 +32,13 @@ import java.util.Set;
* <ul>
* <li><b>平台超管</b>:全部公司全部<b>京东档</b>组可读可写;</li>
* <li><b>公司超管</b>:本公司全部<b>京东档</b>组可读可写(与「公司超管看本公司全部成员」同源);</li>
* <li><b>管理员</b>:可见=自己带的组 + 上级带的组 + 自己所属组的组长带的组 + 管理链上级
* 各账号带的组(沿 created_by_id 上溯、只读;2026-11-24 客户「普通管理员看不到同组
* 管理员的分类」);可写=上溯之前的可见组(继承来的组只读,不因上溯放宽写权限);</li>
* <li><b>普通号</b>:可见同上(含管理链上溯),可写空(能开弹窗看,不能增删改分类、不能归类)。</li>
* <li><b>管理员</b>:可见=自己带的组 + 自己作为成员所属组的组长带的组 + <b>管理链上级各账号
* 带的组</b>(沿 created_by_id 上溯,只读,过平台闸后);可写=自己带的组(2026-11-23
* 客户口径「公司管理员和管理员都有权限分类」,但「管理员与管理员之间分类不能互看」——
* 上溯只走自己的上级链、不含同级,继承来的组也不可写);</li>
* <li><b>普通号</b>:可见=自己带的组 + 自己所属组组长带的组 + 管理链上级各账号带的组
* (让员工看到上级管理员/超管的分类,2026-11-23「管理员与员工可以互看分类」、
* 2026-11-24「普通管理员看不到同组管理员的分类」);可写空(「普通员工没有权限分类」)。</li>
* </ul>
*
* <p>平台超管判定走 {@link DataReadScope#isPlatformSuperAdmin}({@code isSuper} 的唯一包装),
@@ -163,32 +166,28 @@ public class JdCategoryScopeService {
writable.addAll(visible.keySet());
}
} else {
// 可见集先收全:自己带的组 + 上级(created_by_id)带的组 + 作为成员所属组的组长带的组。
// 2026-11-23 客户口径「同组的可以看,不同组的不能看」+「管理员与管理员之间
// 分类不能互看,管理员与员工可以互看分类」:
// 可见集=自己带的组 + 自己作为成员所属组的组长带的组 + 管理链上级各账号带的组。
// 上溯<b>只沿自己的 created_by_id</b>,天然不含同级管理员 ⇒ 平行管理员互相看不到
// 对方自己建的组(2026-11-23「管理员之间不能互看」仍成立);而链上的公司超管/
// 管理员建的组对下属可读——2026-11-24 客户「普通管理员看不到同组管理员的分类」:
// 员工 1366 → 管理员 1365(不带组)→ 超管 1353(带组)的两跳链,旧实现只走一跳,
// 上级不带组时可见集整片为空,分类页就成了「本组暂无分类」。
// 继承只影响可见(只读),写权限仍只到自己带的组。
collect(visible, ledGroups(user.getId()));
if (user.getCreatedById() != null && user.getCreatedById() > 0) {
// 上级的组只收同公司(2026-11-24):原实现不看公司,平台超管在别家公司建的组
// 会顺着 created_by_id 漏进本账号可见集。
Long parentCompanyId = user.getCompanyId();
for (ShopManageGroupEntity group : ledGroups(user.getCreatedById())) {
if (group != null && parentCompanyId != null
&& parentCompanyId.equals(group.getCompanyId())) {
collect(visible, List.of(group));
}
}
}
Set<Long> ledGroupIds = new LinkedHashSet<>(visible.keySet());
List<Long> memberOfLeaders = shopManageGroupMapper.selectLeaderIdsByMemberUserId(user.getId());
if (memberOfLeaders != null) {
for (Long leaderId : memberOfLeaders) {
collect(visible, ledGroups(leaderId));
}
}
// 2026-11-23 客户口径「管理人员只有分类权限」:admin 档对<b>全部可见</b>的 jd 分组
// 可写分类(建/改名/删、归类),不再要求组长身份——客户侧的管理人员账号普遍不是
// 组长,按旧口径(仅 led 组可写)拿不到分类管理入口,反而看得见商品删除,两头正好
// 弄反。可见性边界(V203 分组隔离)不变:不可见的组仍然既不可读也不可写;
// 下面的 retainJdPlatformGroups 会把可写集收敛到京东档分组。普通账号 writable 仍为空。
if ("admin".equals(role)) {
writable.addAll(visible.keySet());
// 管理员可写=自己带的组(2026-11-23 客户口径「公司管理员和管理员都有权限分类」);
// 只是别组成员、或只经管理链继承到的组一律只读(归它的组长管)。普通号可写为空
// (「普通员工没有权限分类」)。
writable.addAll(ledGroupIds);
}
// 管理链继承放在可写集计算之后:只让上级链上的组变得可读,绝不把继承来的组塞进
// writable(那会让下级能改上级的分类,把「管理员之间不能互看」从读侧破到写侧)。
@@ -229,8 +228,8 @@ public class JdCategoryScopeService {
*
* <p>只沿 {@code users.created_by_id} 向上走,且带已访问集合:脏数据成环(自指/互指)与
* 超深链都在这里停下,绝不进入死循环;上级账号已被删除({@code selectById} 回 null)时
* 同样停止——链断了就不往上猜。命中只进可见集(只读),可写集在调用前已算定,所以平行
* 管理员之间互相看不到、也改不了对方自己建的组。</p>
* 同样停止——链断了就不往上猜。命中只进可见集(只读),可写集仍由调用方按「自己带的组」
* 决定,所以平行管理员之间互相看不到、也改不了对方自己建的组。</p>
*
* <p><b>只继承同公司的组</b>:平台超管的 created_by_id 是整片下游的根,若不加这道闸,
* 他在 A 公司建的组会顺着链漏给 B 公司的管理员——公司隔离(V203 公司→组两级)就破了。
@@ -165,12 +165,16 @@ public class JdGroupShopService {
/**
* 可添加候选(「添加店铺」弹窗源):{@code biz_jd_product.shop_name} DISTINCT +
* like + 排除指定分类已有名单。SQL 侧下推(铁律 20 同形),limit 服务侧钳制。
* {@code scopedCompanyId}(2026-11-23):候选同样按公司隔离——非平台超管为本公司 id
* (经 {@code DataReadScope#readCompanyIdOrOwn} 算好),平台超管为空=全平台去重。
*/
public List<String> availableShops(String keyword, Long excludeCategoryId, Integer limit) {
public List<String> availableShops(String keyword, Long excludeCategoryId, Integer limit,
Long scopedCompanyId) {
int safeLimit = limit == null || limit < 1 ? MAX_AVAILABLE_LIMIT : Math.min(limit, MAX_AVAILABLE_LIMIT);
List<String> excludeNames = excludeCategoryId == null || excludeCategoryId <= 0
? List.of() : listShops(excludeCategoryId);
return jdProductMapper.selectDistinctShopNames(trimToNull(keyword), excludeNames, safeLimit);
return jdProductMapper.selectDistinctShopNames(trimToNull(keyword), excludeNames, safeLimit,
scopedCompanyId);
}
// ---------------------------------------------------------------- 业务规则
@@ -103,12 +103,16 @@ public class JdProductQueryService {
* 也必须是未分类」)。
* 2026-11-16 撤掉的旧 ungrouped/grouped 两档读的是已停用的 biz_jd_product.group_id 列,
* 与本档不是一回事;独立「分类」筛选框的撤掉口径不变。
* 公司维过滤已随 2026-11-16 摘公司 tab 整批撤掉(全平台公共)。</p>
* <b>公司隔离(2026-11-23 客户口径「公司与公司之间不能互看」,推翻 11-16 公共库)</b>:
* {@code scopedCompanyId} 由控制器经 {@code DataReadScope#readCompanyIdOrOwn} 算好传入——
* 非平台超管恒为本公司,平台超管不带公司参数时为空(全平台)。谓词在 XML 的
* {@code productConds} 里 SQL 侧下推(铁律 20)。</p>
*/
public JdProductPageVo page(Long page, Long pageSize,
String keyword, String productCode, String titleKeyword, List<String> categories,
Long groupId, String shopName, BigDecimal minPrice, BigDecimal maxPrice,
LocalDateTime syncedFrom, LocalDateTime syncedTo, Boolean classified, Long ownerGroupId) {
LocalDateTime syncedFrom, LocalDateTime syncedTo, Boolean classified, Long ownerGroupId,
Long scopedCompanyId) {
if (syncedFrom != null && syncedTo != null && syncedFrom.isAfter(syncedTo)) {
throw new BusinessException("开始时间不能晚于结束时间");
}
@@ -123,12 +127,14 @@ public class JdProductQueryService {
long total = jdProductMapper.countProducts(filter.keyword(), filter.productCode(),
filter.titleKeyword(), filter.categories(), filter.groupId(), filter.shopName(),
filter.minPrice(), filter.maxPrice(),
filter.syncedFrom(), filter.syncedTo(), filter.classified(), ownerGroupId);
filter.syncedFrom(), filter.syncedTo(), filter.classified(), ownerGroupId,
scopedCompanyId);
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,
(safePage - 1) * safeSize, (int) safeSize);
List<JdProductListItemVo> items = rows.stream().map(this::toListItem).toList();
fillGroupNames(items, ownerGroupId);
@@ -190,11 +196,12 @@ public class JdProductQueryService {
/**
* 类目筛选下拉数据源(2026-11-15 用户口径「京麦的类目也要同步过来后台」):
* 已落库行 distinct 的 {@code category_path}(=京麦末级类目名,契约 §2.3 E3)
* + 各自行数,按类目名排序。全平台公共去重(2026-11-16 起与用户端采集建单侧
* 同一出口同一口径,无公司维过滤)。
* + 各自行数,按类目名排序。2026-11-23 起与列表同口径按行公司隔离
* ({@code scopedCompanyId} 由控制器经 {@code DataReadScope#readCompanyIdOrOwn} 算好;
* 平台超管不带公司参数时为空=全平台去重,与用户端采集建单侧同一出口)。
*/
public List<JdProductCategoryVo> categories() {
return jdProductMapper.selectProductCategories();
public List<JdProductCategoryVo> categories(Long scopedCompanyId) {
return jdProductMapper.selectProductCategories(scopedCompanyId);
}
/**
@@ -206,9 +213,11 @@ public class JdProductQueryService {
* 不建空列,见蓝图 Out of Scope)。行按 id 升序(勾选集合无顺序语义,id 稳定输出便于对账)。</p>
*
* <p>上限 {@link #MAX_EXPORT_IDS}(1000):超限整批拒绝不静默截断——用户以为导全了、
* 实际少导是交付事故;空勾选同样拒绝(前端按钮置灰是第一道,这里是兜底)。</p>
* 实际少导是交付事故;空勾选同样拒绝(前端按钮置灰是第一道,这里是兜底)。
* 公司隔离(2026-11-23):勾选 id 里只要有一行不属于 {@code scopedCompanyId}(非平台超管
* 恒为本公司)就整批拒绝——导出是读行为,但静默少导/多导都是交付事故,不做部分导出。</p>
*/
public byte[] exportCsv(List<Long> ids) {
public byte[] exportCsv(List<Long> ids, Long scopedCompanyId) {
if (ids == null || ids.isEmpty()) {
throw new BusinessException("请先勾选要导出的商品");
}
@@ -220,6 +229,10 @@ public class JdProductQueryService {
throw new BusinessException("单次最多导出 " + MAX_EXPORT_IDS + " 个商品,请缩小勾选范围");
}
List<JdProductEntity> rows = new ArrayList<>(jdProductMapper.selectBatchIds(uniqueIds));
if (scopedCompanyId != null && scopedCompanyId > 0
&& rows.stream().anyMatch(row -> !scopedCompanyId.equals(row.getCompanyId()))) {
throw new BusinessException(403, "无权导出其他公司的京东商品");
}
rows.sort(java.util.Comparator.comparing(JdProductEntity::getId,
java.util.Comparator.nullsLast(java.util.Comparator.naturalOrder())));
StringBuilder csv = new StringBuilder();
@@ -262,16 +275,17 @@ public class JdProductQueryService {
/**
* 商品详情(主字段全量 + attr_json 解析 + 已转存图片)。
*
* <p>公共库无公司维度(2026-11-15 用户定案「京东数据库不区分公司」):按 id 直取,
* 越界 id 按不存在拒绝。attr_json 非契约形状/解析异常时不抛 500:落空 {@code attrGroups}、
* 原文进 {@code attrJsonRaw} 兜底(AC-7)。</p>
* <p><b>公司隔离(2026-11-23 客户口径「公司与公司之间不能互看」)</b>:按 id 直取后
* 再比 {@code scopedCompanyId}(非平台超管恒为本公司;平台超管为空=不限)。别公司的行
* 按<b>「商品不存在」</b>拒绝——不返回 403,避免泄露行是否存在。attr_json 非契约形状/
* 解析异常时不抛 500:落空 {@code attrGroups}、原文进 {@code attrJsonRaw} 兜底(AC-7)。</p>
*/
public JdProductDetailVo detail(Long id, Long ownerGroupId) {
public JdProductDetailVo detail(Long id, Long ownerGroupId, Long scopedCompanyId) {
if (id == null || id <= 0) {
throw new BusinessException("商品不存在");
}
JdProductEntity row = jdProductMapper.selectById(id);
if (row == null) {
if (row == null || !inCompany(row, scopedCompanyId)) {
throw new BusinessException("商品不存在");
}
JdProductDetailVo vo = new JdProductDetailVo();
@@ -568,6 +582,36 @@ public class JdProductQueryService {
}
}
/**
* 写侧行级公司闸(2026-11-23):请求里的 id 只要有一行不属于 {@code scopedCompanyId}
* (非平台超管恒为本公司;平台超管为空=不限)就整批拒绝。不存在的 id 交给下游
* 「商品不存在」语义,这里只管跨公司。
*
* <p>读侧藏起来的行仍然能删=静默删除,不是隔离;编辑/上传图片/删除/批量删除/批量归类
* 五个写口都必须在动手之前过这一道。</p>
*/
public void assertRowsInCompany(List<Long> ids, Long scopedCompanyId) {
if (scopedCompanyId == null || scopedCompanyId <= 0) {
return;
}
List<Long> uniqueIds = ids == null ? List.of()
: ids.stream().filter(id -> id != null && id > 0).distinct().toList();
if (uniqueIds.isEmpty()) {
return;
}
for (JdProductEntity row : jdProductMapper.selectBatchIds(uniqueIds)) {
if (!inCompany(row, scopedCompanyId)) {
throw new BusinessException(403, "无权操作其他公司的京东商品");
}
}
}
/** 行归属是否落在作用域公司内:作用域为空(平台超管)=不限,否则要求行上有同一公司值。 */
private static boolean inCompany(JdProductEntity row, Long scopedCompanyId) {
return scopedCompanyId == null || scopedCompanyId <= 0
|| (row.getCompanyId() != null && scopedCompanyId.equals(row.getCompanyId()));
}
private static String trimToNull(String value) {
if (value == null) {
return null;
@@ -174,6 +174,12 @@
</if>
<if test="syncedFrom != null">AND p.synced_at &gt;= #{syncedFrom}</if>
<if test="syncedTo != null">AND p.synced_at &lt;= #{syncedTo}</if>
<!-- 公司隔离(2026-11-23 客户口径「公司与公司之间是不能互看的」,推翻 2026-11-16
「公共库」定案):非平台超管钉本公司(controller 经 DataReadScope#readCompanyIdOrOwn
算好传入,非超管恒为本公司、请求带别家也不算);平台超管不带公司参数时为空=不筛。
谓词必须 SQL 侧下推(铁律 20),不得 Java 侧对已取页 filter。
company_id 有值的行才可能被非超管看到——空值行只对平台超管可见(严格隔离)。 -->
<if test="companyId != null and companyId &gt; 0">AND p.company_id = #{companyId}</if>
</sql>
<!-- 只读页总数(与 selectProductPage 同一过滤面)。 -->
@@ -201,23 +207,27 @@
</select>
<!-- 类目筛选下拉数据源(2026-11-15 用户口径「京麦的类目也要同步过来后台」):已落库行
的 DISTINCT category_path(=京麦末级类目名,契约 §2.3 E3)+ 行数,全平台公共去重
(2026-11-16 起公司与后管两侧同口径,无公司维过滤)。空类目行不进下拉。 -->
的 DISTINCT category_path(=京麦末级类目名,契约 §2.3 E3)+ 行数。
2026-11-23 起按行公司隔离(公司之间不能互看;平台超管不带公司参数时空=全平台去重)。
空类目行不进下拉。 -->
<select id="selectProductCategories" resultType="com.nanri.aiimage.modules.jd.model.vo.JdProductCategoryVo">
SELECT p.category_path AS name, COUNT(*) AS productCount
FROM biz_jd_product p
WHERE p.category_path IS NOT NULL AND p.category_path != ''
<if test="companyId != null and companyId &gt; 0">AND p.company_id = #{companyId}</if>
GROUP BY p.category_path
ORDER BY p.category_path
</select>
<!-- 店铺名单「可添加候选」(v1.7,添加店铺弹窗源):biz_jd_product.shop_name DISTINCT +
like + 排除指定分类已有名单(NOT IN 下推,铁律 20)。limit 是服务侧钳制后的值
(≤ JdGroupShopService.MAX_AVAILABLE_LIMIT),mapper 不做第二次换算。 -->
(≤ JdGroupShopService.MAX_AVAILABLE_LIMIT),mapper 不做第二次换算。
2026-11-23:候选店铺同样按公司隔离——A 公司不能在候选里看到 B 公司的店铺名。 -->
<select id="selectDistinctShopNames" resultType="string">
SELECT DISTINCT p.shop_name
FROM biz_jd_product p
WHERE p.shop_name IS NOT NULL AND p.shop_name != ''
<if test="companyId != null and companyId &gt; 0">AND p.company_id = #{companyId}</if>
<if test="keyword != null and keyword != ''">AND p.shop_name LIKE CONCAT('%', #{keyword}, '%')</if>
<if test="excludeNames != null and excludeNames.size() > 0">
AND p.shop_name NOT IN
@@ -97,6 +97,15 @@ class AdminJdProductControllerTest {
private static final String COLUMN_KEY = "admin_jd_database";
private static final String ROUTE_PATH = "jd-center/database";
/** 公司作用域桩:读/写都回 null(平台超管口径),让 controller 的 scopedCompanyId 走「不筛」分支。 */
private static com.nanri.aiimage.common.service.DataReadScope dataScopeMock() {
com.nanri.aiimage.common.service.DataReadScope scope =
mock(com.nanri.aiimage.common.service.DataReadScope.class);
when(scope.readCompanyIdOrOwn(any(), any())).thenReturn(null);
when(scope.writeCompanyId(any())).thenReturn(null);
return scope;
}
/** 用例里的「当前分组」:数据权限分组 10,归属公司 5。 */
private static final long GROUP_ID = 10L;
private static final long COMPANY_ID = 5L;
@@ -147,17 +156,19 @@ class AdminJdProductControllerTest {
@Test
void 读侧零公司维度_公司tab口径整批摘除() throws IOException {
String source = stripComments(Files.readString(CONTROLLER));
// 2026-11-16 用户定案「删除掉公司 tab,参考品牌数据库」=品牌库 2026-10-06 先例同形:
// 摘的不只是 tab——company_id 参数与 superAdminScopedCompanyId 谓词一起撤,
// 数据全平台公共(行 company_id 列仍是同步来源标记,不构成可见性边界)。
assertFalse(source.contains("company_id"),
"company_id 请求参数必须已删除,不得回潜(2026-11-16 随公司 tab 整批摘除)");
// 2026-11-23 客户口径「公司与公司之间是不能互看的」,推翻 2026-11-16「公共库」定案:
// 读侧按行 company_id 隔离(非平台超管钉本公司),写侧再按行级公司闸兜住。
assertTrue(source.contains("@RequestParam(name = \"company_id\""),
"company_id 请求参数必须在册(平台超管跨公司查看用;非超管传什么都不算)");
assertTrue(source.contains("readCompanyIdOrOwn"),
"读侧必须经 DataReadScope#readCompanyIdOrOwn 算公司作用域(唯一出口,不许自己写 if)");
assertTrue(source.contains("writeCompanyId"),
"写侧行级闸必须经 DataReadScope#writeCompanyId(编辑/传图/删除/批量删除)");
assertTrue(source.contains("assertRowsInCompany"),
"编辑/传图/删除/批量删除/批量归类动手前必须过行级公司闸"
+ "(读侧藏起来的行仍然能删=静默删除,不是隔离)");
assertFalse(source.contains("superAdminScopedCompanyId"),
"superAdminScopedCompanyId 谓词必须已删除:第五道门(超管显式收窄)对京东只读面不再生效");
assertFalse(source.contains("readCompanyIdOrOwn"),
"不得走 readCompanyIdOrOwn:全平台公共数据对每一家管理员都是同一份全量,无公司可见性边界");
assertFalse(source.contains("DataReadScope"),
"DataReadScope 不得再注入京东只读控制器(公司收口已随 2026-11-16 摘除)");
"不得沿用 superAdminScopedCompanyId 旧形状(它只在超管身上收窄、非超管不过滤=公司互看)");
}
@Test
@@ -165,7 +176,7 @@ class AdminJdProductControllerTest {
String source = stripComments(Files.readString(CONTROLLER));
for (String param : List.of("page_size", "product_code", "title_keyword", "keyword",
"shop_name", "min_price", "max_price",
"group_id", "owner_group_id", "synced_from", "synced_to")) {
"group_id", "owner_group_id", "synced_from", "synced_to", "company_id")) {
assertTrue(source.contains(param), "查询参数必须 snake_case:" + param);
}
assertFalse(source.contains("@RequestParam(name = \"ungrouped\""));
@@ -180,8 +191,9 @@ class AdminJdProductControllerTest {
assertTrue(source.contains("@RequestParam(name = \"min_price\"")
&& source.contains("@RequestParam(name = \"max_price\""),
"价格区间参数 min_price/max_price 必须在册(v1.7 拍板)");
// company_id 参数已随 2026-11-16「删除掉公司 tab」整批摘除(读侧零公司维度支另有反向钉)。
assertFalse(source.contains("company_id"), "company_id 请求参数必须已删除,不得回潜");
// 公司隔离(2026-11-23):company_id 在册(平台超管切公司);非超管由 readCompanyIdOrOwn 钉本公司。
assertTrue(source.contains("@RequestParam(name = \"company_id\""),
"company_id 请求参数必须在册(2026-11-23 公司与公司不能互看)");
// 商品状态筛选框已随 2026-11-15 客户口径从前端下线 ⇒ 服务端同名参数一起撤(反向钉住防回潜)。
assertFalse(source.contains("\"sale_status\""), "sale_status 筛选参数必须已删除,不得回潜");
// 店铺筛选 v1.7 复活(2026-11-21 拍板「店铺名单维度完整对齐」):精确 eq 配合 group_id;
@@ -294,7 +306,7 @@ class AdminJdProductControllerTest {
PermissionMenuService permissionMenuService = mock(PermissionMenuService.class);
AdminJdProductController controller = new AdminJdProductController(
queryService, deleteService, updateService, groupService, scopeService,
authSupport, permissionMenuService);
authSupport, permissionMenuService, dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(9L);
@@ -315,10 +327,10 @@ class AdminJdProductControllerTest {
// 读:菜单权限命中即放行(公共库一人一份全量)。
JdProductPageVo pageVo = new JdProductPageVo();
pageVo.setItems(List.of());
when(queryService.page(1L, 20L, null, null, null, null, null, null, null, null, null, null, null, GROUP_ID))
when(queryService.page(1L, 20L, null, null, null, null, null, null, null, null, null, null, null, GROUP_ID, null))
.thenReturn(pageVo);
assertEquals(Boolean.TRUE, controller.page(1L, 20L, null, null, null, null, null, null,
null, null, null, null, null, null, request)
null, null, null, null, null, null, null, request)
.isSuccess(), "普通账号有菜单权限时读必须放行(读写分档只收窄写)");
// 商品行写:四条写入口一律 403,且业务服务一次都没被调到(不能「先写再拒」)。
@@ -362,7 +374,7 @@ class AdminJdProductControllerTest {
PermissionMenuService permissionMenuService = mock(PermissionMenuService.class);
AdminJdProductController controller = new AdminJdProductController(
queryService, deleteService, updateService, mock(JdProductGroupService.class),
mock(JdCategoryScopeService.class), authSupport, permissionMenuService);
mock(JdCategoryScopeService.class), authSupport, permissionMenuService, dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(9L);
@@ -422,7 +434,7 @@ class AdminJdProductControllerTest {
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
PermissionMenuService permissionMenuService = mock(PermissionMenuService.class);
AdminJdProductGroupController controller = new AdminJdProductGroupController(
groupService, mock(JdGroupShopService.class), scopeService, authSupport, permissionMenuService);
groupService, mock(JdGroupShopService.class), scopeService, authSupport, permissionMenuService, dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(9L);
@@ -475,7 +487,7 @@ class AdminJdProductControllerTest {
PermissionMenuService permissionMenuService = mock(PermissionMenuService.class);
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), updateService, groupService,
scopeService, authSupport, permissionMenuService);
scopeService, authSupport, permissionMenuService, dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(9L);
@@ -519,7 +531,7 @@ class AdminJdProductControllerTest {
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
groupService, scopeService, authSupport, mock(PermissionMenuService.class));
groupService, scopeService, authSupport, mock(PermissionMenuService.class), dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(7L);
@@ -534,12 +546,12 @@ class AdminJdProductControllerTest {
BusinessException ex = assertThrows(BusinessException.class,
() -> controller.page(1L, 20L, null, null, null, null, 8L, null, null, null,
null, null, null, null, request));
null, null, null, null, null, request));
assertTrue(ex.getMessage().contains("无权查看该分组的分类"),
"跨组分类筛选必须 403 点名分组,不做空结果降级");
verify(queryService, never()).page(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), any(), any());
any(), any(), any(), any(), any(), any(), any());
}
// ===================== 行为样本 =====================
@@ -552,7 +564,7 @@ class AdminJdProductControllerTest {
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
groupService, scopeService, authSupport, mock(PermissionMenuService.class));
groupService, scopeService, authSupport, mock(PermissionMenuService.class), dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(7L);
@@ -567,14 +579,14 @@ class AdminJdProductControllerTest {
pageVo.setTotal(0L);
pageVo.setPage(1L);
pageVo.setPageSize((long) PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT);
when(queryService.page(1L, 20L, null, null, null, null, null, null, null, null, null, null, null, GROUP_ID))
when(queryService.page(1L, 20L, null, null, null, null, null, null, null, null, null, null, null, GROUP_ID, null))
.thenReturn(pageVo);
// 请求面没有 company_id:任何授权身份看到的都是同一份全量;分类列按 owner_group_id 取。
ApiResponse<JdProductPageVo> response = controller.page(1L, 20L, null, null, null, null,
null, null, null, null, GROUP_ID, null, null, null, request);
null, null, null, null, GROUP_ID, null, null, null, null, request);
verify(queryService).page(1L, 20L, null, null, null, null, null, null, null, null, null, null, null, GROUP_ID);
verify(queryService).page(1L, 20L, null, null, null, null, null, null, null, null, null, null, null, GROUP_ID, null);
assertEquals(Boolean.TRUE, response.isSuccess(), "拒绝走 403 业务码,放行必须是 success 信封(B80-03)");
assertEquals(pageVo, response.getData());
}
@@ -587,7 +599,7 @@ class AdminJdProductControllerTest {
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
mock(JdProductGroupService.class), scopeService, authSupport,
mock(PermissionMenuService.class));
mock(PermissionMenuService.class), dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(7L);
HttpServletRequest request = mock(HttpServletRequest.class);
@@ -599,13 +611,13 @@ class AdminJdProductControllerTest {
when(request.getParameter(name)).thenReturn(value);
BusinessException ex = assertThrows(BusinessException.class,
() -> controller.page(1L, 20L, null, null, null, null, 7L, null, null, null,
null, null, null, null, request));
null, null, null, null, null, request));
assertTrue(ex.getMessage().contains("刷新页面"));
when(request.getParameter(name)).thenReturn(null);
}
}
verify(queryService, never()).page(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), any(), any());
any(), any(), any(), any(), any(), any(), any());
}
@Test
@@ -615,7 +627,7 @@ class AdminJdProductControllerTest {
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
mock(JdProductGroupService.class), mock(JdCategoryScopeService.class), authSupport,
mock(PermissionMenuService.class));
mock(PermissionMenuService.class), dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(7L);
@@ -625,11 +637,11 @@ class AdminJdProductControllerTest {
JdProductCategoryVo row = new JdProductCategoryVo();
row.setName("艺考形体服");
row.setProductCount(85L);
when(queryService.categories()).thenReturn(List.of(row));
when(queryService.categories(null)).thenReturn(List.of(row));
ApiResponse<List<JdProductCategoryVo>> response = controller.categories(request);
ApiResponse<List<JdProductCategoryVo>> response = controller.categories(null, request);
verify(queryService).categories();
verify(queryService).categories(null);
assertEquals(Boolean.TRUE, response.isSuccess(), "类目下拉与列表同一鉴权正门(200 信封)");
assertEquals("艺考形体服", response.getData().get(0).getName(), "选项=已落库行的末级类目名(全平台去重)");
assertEquals(85L, response.getData().get(0).getProductCount());
@@ -643,7 +655,7 @@ class AdminJdProductControllerTest {
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
mock(JdProductGroupService.class), scopeService, authSupport,
mock(PermissionMenuService.class));
mock(PermissionMenuService.class), dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(7L);
@@ -655,13 +667,13 @@ class AdminJdProductControllerTest {
when(scopeService.readGroupOrNull(scope, null)).thenReturn(GROUP_ID);
JdProductDetailVo detailVo = new JdProductDetailVo();
detailVo.setId(11L);
when(queryService.detail(11L, GROUP_ID)).thenReturn(detailVo);
when(queryService.detail(11L, GROUP_ID, null)).thenReturn(detailVo);
ApiResponse<JdProductDetailVo> response = controller.detail(11L, null, request);
ApiResponse<JdProductDetailVo> response = controller.detail(11L, null, null, request);
// 公共库详情没有公司维度(DataReadScope 反向钉在结构判据支,2026-11-16 已整批摘除);
// 分类字段按「当前分组」取(V203)。
verify(queryService).detail(11L, GROUP_ID);
verify(queryService).detail(11L, GROUP_ID, null);
assertEquals(Boolean.TRUE, response.isSuccess());
assertEquals(detailVo, response.getData());
}
@@ -674,7 +686,7 @@ class AdminJdProductControllerTest {
AdminJdProductController controller = new AdminJdProductController(
queryService, deleteService, mock(JdProductUpdateService.class),
mock(JdProductGroupService.class), mock(JdCategoryScopeService.class), authSupport,
mock(PermissionMenuService.class));
mock(PermissionMenuService.class), dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(7L);
@@ -703,7 +715,7 @@ class AdminJdProductControllerTest {
AdminJdProductController controller = new AdminJdProductController(
queryService, deleteService, mock(JdProductUpdateService.class),
mock(JdProductGroupService.class), mock(JdCategoryScopeService.class), authSupport,
mock(PermissionMenuService.class));
mock(PermissionMenuService.class), dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(7L);
@@ -725,23 +737,23 @@ 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())).thenReturn(1L);
any(), any(), any(), any(), 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);
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(), offset.capture(), limit.capture());
isNull(), isNull(), isNull(), isNull(), 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=5000 ⇒ 钳到 10_000 页、1000 条(v1.7 上限);乘积 9_999_000 不得变负(铁律 15)。
service.page(Long.MAX_VALUE, 5000L, null, null, null, null, null, null, null, null, null, null, null, null);
service.page(Long.MAX_VALUE, 5000L, null, null, null, null, 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(), any(), any(), any(), any(), offsetMax.capture(), limitMax.capture());
any(), any(), any(), any(), any(), any(), 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(对齐原型档位)");
}
@@ -753,10 +765,10 @@ 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, null, null, from, to, null, null));
() -> 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());
any(), any(), any(), any(), isNull());
}
@Test
@@ -764,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())).thenReturn(1L);
any(), any(), any(), any(), isNull())).thenReturn(1L);
service.page(1L, 20L, null, " SKU-1 ", " 连衣裙 ", List.of(" 艺考形体服 "), 7L,
null, null, null, null, null, null, null);
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);
verify(mapper).selectProductPage(null, "SKU-1", "连衣裙", List.of("艺考形体服"), 7L,
null, null, null, null, null, null, null, 0L, 20);
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);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, null);
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).selectProductPage(null, null, null, null, null, null, null, null,
null, null, null, null, 0L, 20);
null, null, null, null, null, 0L, 20);
}
/**
@@ -788,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())).thenReturn(1L);
any(), any(), any(), any(), isNull())).thenReturn(1L);
service.page(1L, 20L, null, null, null,
java.util.Arrays.asList(" 女装 ", "女装", "", null, " 汉服/旗袍 "),
null, null, null, null, null, null, null, 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);
verify(mapper).selectProductPage(null, null, null, List.of("女装", "汉服/旗袍"),
null, null, null, null, null, null, null, null, 0L, 20);
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);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, 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);
Path xml = findJdProductMapperXml();
String xmlText = stripComments(Files.readString(xml)).replaceAll("(?s)<!--.*?-->", "");
@@ -839,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())).thenReturn(1L);
any(), any(), any(), any(), isNull())).thenReturn(1L);
service.page(1L, 20L, null, null, null, null, null, null, null, null, null, null,
Boolean.TRUE, GROUP_ID);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, true, GROUP_ID);
Boolean.TRUE, GROUP_ID, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, true, GROUP_ID, null);
verify(mapper).selectProductPage(null, null, null, null, null, null, null, null,
null, null, true, GROUP_ID, 0L, 20);
null, null, true, GROUP_ID, null, 0L, 20);
service.page(1L, 20L, null, null, null, null, null, null, null, null, null, null,
Boolean.FALSE, GROUP_ID);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, false, GROUP_ID);
Boolean.FALSE, GROUP_ID, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, false, GROUP_ID, null);
verify(mapper).selectProductPage(null, null, null, null, null, null, null, null,
null, null, false, GROUP_ID, 0L, 20);
null, null, false, GROUP_ID, null, 0L, 20);
// ③ 控制器直传:classified 原样进服务(owner_group_id 仍按作用域收敛后的值)。
JdProductQueryService queryService = mock(JdProductQueryService.class);
@@ -859,7 +871,7 @@ class AdminJdProductControllerTest {
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
mock(JdProductGroupService.class), scopeService, authSupport,
mock(PermissionMenuService.class));
mock(PermissionMenuService.class), dataScopeMock());
AdminUserEntity operator = new AdminUserEntity();
operator.setId(7L);
HttpServletRequest request = mock(HttpServletRequest.class);
@@ -869,9 +881,9 @@ class AdminJdProductControllerTest {
when(scopeService.resolve(operator)).thenReturn(scope);
when(scopeService.readGroupOrNull(scope, null)).thenReturn(GROUP_ID);
controller.page(1L, 20L, null, null, null, null, null, null, null, null, null, null,
null, Boolean.TRUE, request);
null, Boolean.TRUE, null, request);
verify(queryService).page(1L, 20L, null, null, null, null, null, null, null, null, null,
null, Boolean.TRUE, GROUP_ID);
null, Boolean.TRUE, GROUP_ID, null);
}
/**
@@ -884,27 +896,27 @@ 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())).thenReturn(1L);
any(), any(), any(), any(), 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);
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);
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null, null);
verify(mapper).selectProductPage("连衣裙", null, null, null, 7L, "测试店铺",
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null, 0L, 20);
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null, null, 0L, 20);
// ② 空白 keyword/shopName 归 null(不过滤该面)。
service.page(1L, 20L, " ", null, null, null, 0L, " ", null, null, null, null, null, null);
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, 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);
// ③ 价格倒挂按业务异常拒绝(同同步起止倒挂形状)。
BusinessException ex = assertThrows(BusinessException.class,
() -> service.page(1L, 20L, null, null, null, null, null, null,
new BigDecimal("200"), new BigDecimal("100"), null, null, null, null));
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());
any(), any(), any(), any(), any(), any(), isNull());
}
/**
@@ -919,20 +931,20 @@ class AdminJdProductControllerTest {
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
mock(JdProductGroupService.class), mock(JdCategoryScopeService.class), authSupport,
mock(PermissionMenuService.class));
mock(PermissionMenuService.class), dataScopeMock());
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");
byte[] csvBytes = "\uFEFF商品编码\r\nA123".getBytes(java.nio.charset.StandardCharsets.UTF_8);
when(queryService.exportCsv(List.of(11L, 12L))).thenReturn(csvBytes);
when(queryService.exportCsv(List.of(11L, 12L), null)).thenReturn(csvBytes);
JdProductExportRequest body = new JdProductExportRequest();
body.setIds(List.of(11L, 12L));
ResponseEntity<byte[]> response = controller.export(body, request);
ResponseEntity<byte[]> response = controller.export(body, null, request);
verify(queryService).exportCsv(List.of(11L, 12L));
verify(queryService).exportCsv(List.of(11L, 12L), null);
assertEquals(200, response.getStatusCode().value(), "放行必须是 200 附件响应(非 ApiResponse 信封)");
assertTrue(response.getHeaders().getFirst("Content-Disposition").contains(".csv"),
"Content-Disposition 必须带 .csv 文件名(DownloadHeaderUtil 兜 ASCII 回退与 UTF-8 双名)");
@@ -949,14 +961,14 @@ class AdminJdProductControllerTest {
AdminJdProductController controller = new AdminJdProductController(
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
mock(JdProductGroupService.class), mock(JdCategoryScopeService.class), authSupport,
mock(PermissionMenuService.class));
mock(PermissionMenuService.class), dataScopeMock());
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");
// 服务侧限流是真源(服务级用例另钉 1001 拒绝);这里钉控制器原样透出业务异常、不吞成 500。
when(queryService.exportCsv(any()))
when(queryService.exportCsv(any(), isNull()))
.thenThrow(new BusinessException("单次最多导出 1000 个商品,请缩小勾选范围"));
java.util.List<Long> oversized = new java.util.ArrayList<>();
@@ -965,7 +977,7 @@ class AdminJdProductControllerTest {
}
JdProductExportRequest body = new JdProductExportRequest();
body.setIds(oversized);
BusinessException ex = assertThrows(BusinessException.class, () -> controller.export(body, request));
BusinessException ex = assertThrows(BusinessException.class, () -> controller.export(body, null, request));
assertTrue(ex.getMessage().contains("1000"), "超限文案必须点名上限 1000");
}
@@ -981,7 +993,7 @@ class AdminJdProductControllerTest {
second.setSyncedAt(LocalDateTime.of(2026, 11, 21, 9, 30));
when(mapper.selectBatchIds(List.of(2L, 1L))).thenReturn(List.of(second, first));
byte[] bytes = service.exportCsv(List.of(2L, 1L));
byte[] bytes = service.exportCsv(List.of(2L, 1L), null);
String csv = new String(bytes, java.nio.charset.StandardCharsets.UTF_8);
assertTrue(csv.startsWith("\uFEFF"), "必须带 UTF-8 BOM(Excel 双击直开中文不乱码)");
@@ -998,17 +1010,17 @@ class AdminJdProductControllerTest {
assertTrue(firstRow > 0 && secondRow > firstRow, "行分隔符存在");
// 空勾选拒绝(前端按钮置灰是第一道,这里是兜底);超上限同样拒绝(少导是交付事故)。
BusinessException empty = assertThrows(BusinessException.class, () -> service.exportCsv(List.of()));
BusinessException empty = assertThrows(BusinessException.class, () -> service.exportCsv(List.of(), null));
assertTrue(empty.getMessage().contains("请先勾选"));
BusinessException invalid = assertThrows(BusinessException.class,
() -> service.exportCsv(java.util.Arrays.asList(0L, null)));
() -> service.exportCsv(java.util.Arrays.asList(0L, null), null));
assertTrue(invalid.getMessage().contains("请先勾选"));
java.util.List<Long> oversized = new java.util.ArrayList<>();
for (long i = 1; i <= JdProductQueryService.MAX_EXPORT_IDS + 1; i++) {
oversized.add(i);
}
BusinessException over = assertThrows(BusinessException.class, () -> service.exportCsv(oversized));
BusinessException over = assertThrows(BusinessException.class, () -> service.exportCsv(oversized, null));
assertTrue(over.getMessage().contains("1000"), "超限文案必须点名上限 1000");
}
@@ -1037,7 +1049,7 @@ class AdminJdProductControllerTest {
image.setHeight(800);
when(imageMapper.selectList(any())).thenReturn(List.of(image));
JdProductDetailVo vo = service.detail(11L, null);
JdProductDetailVo vo = service.detail(11L, null, null);
assertEquals(2, vo.getAttrGroups().size(), "tabs 数组逐个映射成 attrGroups(groupName=tab 字段)");
assertEquals("商品基本信息", vo.getAttrGroups().get(0).getGroupName());
@@ -1066,7 +1078,7 @@ class AdminJdProductControllerTest {
JdProductEntity broken = productEntity(11L);
broken.setAttrJson("{\"tabs\": [broken");
when(mapper.selectById(11L)).thenReturn(broken);
JdProductDetailVo brokenVo = service.detail(11L, null);
JdProductDetailVo brokenVo = service.detail(11L, null, null);
assertTrue(brokenVo.getAttrGroups().isEmpty(), "解析异常落空 attrGroups,不抛 500(AC-7)");
assertEquals("{\"tabs\": [broken", brokenVo.getAttrJsonRaw(), "原文进 attrJsonRaw 兜底展示");
@@ -1074,7 +1086,7 @@ class AdminJdProductControllerTest {
JdProductEntity foreign = productEntity(11L);
foreign.setAttrJson("{\"attrs\":[{\"key\":\"x\",\"value\":\"y\"}]}");
when(mapper.selectById(11L)).thenReturn(foreign);
JdProductDetailVo foreignVo = service.detail(11L, null);
JdProductDetailVo foreignVo = service.detail(11L, null, null);
assertTrue(foreignVo.getAttrGroups().isEmpty(), "非契约形状落空 attrGroups");
assertEquals("{\"attrs\":[{\"key\":\"x\",\"value\":\"y\"}]}", foreignVo.getAttrJsonRaw(),
"未识别属性不丢弃:原文兜底透出(AC-7)");
@@ -1083,7 +1095,7 @@ class AdminJdProductControllerTest {
JdProductEntity blank = productEntity(11L);
blank.setAttrJson(null);
when(mapper.selectById(11L)).thenReturn(blank);
JdProductDetailVo blankVo = service.detail(11L, null);
JdProductDetailVo blankVo = service.detail(11L, null, null);
assertTrue(blankVo.getAttrGroups().isEmpty());
assertNull(blankVo.getAttrJsonRaw());
}
@@ -1112,7 +1124,7 @@ class AdminJdProductControllerTest {
brokenJson.setSaleAttrsJson("[{\"name\":");
when(skuMapper.selectList(any())).thenReturn(List.of(first, brokenJson));
JdProductDetailVo vo = service.detail(11L, null);
JdProductDetailVo vo = service.detail(11L, null, null);
assertEquals(2, vo.getSkus().size(), "京麦那 12 行矩阵按入库行下发(抽屉渲染的唯一来源)");
JdProductDetailVo.SkuVo sku = vo.getSkus().get(0);
@@ -1137,14 +1149,14 @@ class AdminJdProductControllerTest {
JdProductEntity row = productEntity(11L);
row.setCompanyId(6L);
when(mapper.selectById(11L)).thenReturn(row);
assertNotNull(service.detail(11L, null));
assertNotNull(service.detail(11L, null, null));
// 行不存在 ⇒ 按「商品不存在」拒绝。
when(mapper.selectById(11L)).thenReturn(null);
BusinessException ex = assertThrows(BusinessException.class, () -> service.detail(11L, null));
BusinessException ex = assertThrows(BusinessException.class, () -> service.detail(11L, null, null));
assertTrue(ex.getMessage().contains("商品不存在"));
// id 非法同样拒绝。
assertThrows(BusinessException.class, () -> service.detail(0L, null));
assertThrows(BusinessException.class, () -> service.detail(0L, null, null));
}
@Test
@@ -1158,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())).thenReturn(2L);
any(), any(), any(), any(), isNull())).thenReturn(2L);
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), anyLong(), anyInt())).thenReturn(List.of(categorized, missing));
any(), any(), any(), any(), 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();
@@ -1169,7 +1181,7 @@ class AdminJdProductControllerTest {
when(categoryMapper.selectBatchIds(any())).thenReturn(List.of(category));
JdProductPageVo vo = service.page(1L, 20L, null, null, null, null, null, null, null, null,
null, null, null, GROUP_ID);
null, null, null, GROUP_ID, null);
assertEquals(3L, vo.getItems().get(0).getGroupId(), "分类 id 取本组归属行");
assertEquals("秋冬款", vo.getItems().get(0).getGroupName(), "行集归属一次取齐分类名(无逐行查询)");
@@ -1185,12 +1197,12 @@ 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())).thenReturn(1L);
any(), any(), any(), any(), isNull())).thenReturn(1L);
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(),
any(), any(), any(), any(), anyLong(), anyInt())).thenReturn(List.of(productEntity(1L)));
any(), any(), any(), any(), 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, null, null, null, null);
assertNull(vo.getItems().get(0).getGroupId(), "不属于任何分组的账号:分类列一律空(未分类)");
assertNull(vo.getItems().get(0).getGroupName());
@@ -1208,7 +1220,7 @@ class AdminJdProductControllerTest {
// 本组(10)没归类:detail 的 selectOne 回 null ⇒ 分类字段空。
when(assignMapper.selectOne(any())).thenReturn(null);
JdProductDetailVo untouched = service.detail(11L, GROUP_ID);
JdProductDetailVo untouched = service.detail(11L, GROUP_ID, null);
assertNull(untouched.getGroupId(), "本组没归类=未分类,不拿别组的归属顶上");
assertNull(untouched.getGroupName());
@@ -1218,7 +1230,7 @@ class AdminJdProductControllerTest {
category.setId(3L);
category.setName("秋冬款");
when(categoryMapper.selectBatchIds(any())).thenReturn(List.of(category));
JdProductDetailVo categorized = service.detail(11L, GROUP_ID);
JdProductDetailVo categorized = service.detail(11L, GROUP_ID, null);
assertEquals(3L, categorized.getGroupId());
assertEquals("秋冬款", categorized.getGroupName());
}
@@ -113,6 +113,24 @@ class JdProductMapperSqlTest {
assertTrue(sql.endsWith("ORDER BY p.shop_name LIMIT ?"), () -> "必须按店名升序+LIMIT 收尾: " + sql);
}
/**
* 公司隔离(2026-11-23 客户口径「公司与公司之间不能互看」):四条读路径(列表/总数/
* 类目下拉/店铺候选)都必须能把 companyId 下推成 {@code p.company_id = ?};平台超管
* (companyId 空)不得出现该谓词。漏一条=那条路径跨公司可见,且不会报错。
*/
@Test
void 公司隔离谓词必须下推SQL() throws Exception {
for (String id : java.util.List.of("countProducts", "selectProductPage",
"selectProductCategories", "selectDistinctShopNames")) {
String sql = render(id, Map.of("companyId", 5L, "offset", 0L, "limit", 20));
assertTrue(sql.contains("p.company_id = ?"),
() -> id + " 少了公司隔离谓词(该路径会跨公司返回): " + sql);
}
String bare = render("countProducts", Map.of("offset", 0L, "limit", 20));
assertFalse(bare.contains("company_id"),
() -> "平台超管(companyId 空)不得被钉死在某公司: " + bare);
}
private static String render(String id) throws Exception {
return render(id, Map.of("companyId", 1L, "taskId", 34956L));
}
@@ -31,8 +31,9 @@ import static org.mockito.Mockito.when;
*
* <p>四档矩阵(2026-11-17 起各档可见集最后统一过「只留京东档 platform='jd' 分组」的平台闸):
* 平台超管=全部公司全部组可读可写;公司超管=本公司全部组;
* 管理员=全部可见组可写(2026-11-23 客户口径「管理人员只有分类权限」,不再要求组长身份);
* 普通号=与管理员同可见、<b>零可写</b>(能开弹窗看,不能增删改分类、不能归类)。
* 管理员=自己带的组可写、成员所属组只读(2026-11-23 客户口径「管理员之间分类不能互看」:
* 不再继承上级带的组);普通号=自己带的组+成员所属组+上级组可见、<b>零可写</b>
* (「管理员与员工可以互看分类」「普通员工没有权限分类」)。
* 另钉三条收敛语义:不可见组读 403(不做空结果降级)、无分组上下文读回 null(页面照常可用)、
* 写侧在「无可写组 / 多可写组未选 / 该组不可写 / 组行不存在」四种形状下的文案。
* 下拉 label 前缀(2026-11-21 用户口径「这里不是选公司 是选超管/管理员」):平台超管跨多个
@@ -168,75 +169,45 @@ class JdCategoryScopeServiceTest {
* 管理人员普遍不是组长,拿不到分类入口)。可见性边界不变:不可见组仍双拒。
*/
@Test
void 管理人员_全部可见组可写_含非组长身份所属组的组() {
void 管理人员_自己带的组可写_成员所属组只读() {
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
when(adminAuthSupport.currentRole(operator)).thenReturn("admin");
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(false);
when(shopManageGroupMapper.selectLedGroups(USER_ID)).thenReturn(List.of(group(10L, COMPANY_ID, "自己的组")));
when(shopManageGroupMapper.selectLeaderIdsByMemberUserId(USER_ID)).thenReturn(List.of(3L));
when(shopManageGroupMapper.selectLedGroups(3L)).thenReturn(List.of(group(11L, COMPANY_ID, "上司的组")));
when(shopManageGroupMapper.selectLedGroups(3L)).thenReturn(List.of(group(11L, COMPANY_ID, "组长A的组")));
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
assertThat(scope.visibleGroupIds()).containsExactly(10L, 11L);
assertThat(scope.writableGroupIds()).containsExactly(10L, 11L);
// 2026-11-23 客户口径「管理员与管理员之间分类不能互看,同组的可以看、不同组的不能看」:
// 分类可写只到自己带的组;作为别组成员时那组只读(归它的组长管)。
assertThat(scope.writableGroupIds()).containsExactly(10L);
assertThat(scope.canRead(11L)).isTrue();
assertThat(scope.canWrite(11L)).isTrue();
// 不可见组仍双拒:可写放开只到可见集为止,V203 分组隔离不松。
assertThat(scope.canWrite(11L)).isFalse();
// 不可见组仍双拒:V203 分组隔离不松。
assertThat(scope.canRead(99L)).isFalse();
assertThat(scope.canWrite(99L)).isFalse();
}
/** 非组长的普通管理人员(只靠成员关系可见组)同样拿到分类可写——客户账号 阿华226655 的形状。 */
/** 非组长的管理人员(只靠成员关系可见组):分类可读、不可写——写归该组组长。 */
@Test
void 管理人员非组长_成员关系可见的组也可写分类() {
void 管理人员非组长_成员组可见但不可写() {
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
when(adminAuthSupport.currentRole(operator)).thenReturn("admin");
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(false);
when(shopManageGroupMapper.selectLedGroups(USER_ID)).thenReturn(List.of());
when(shopManageGroupMapper.selectLeaderIdsByMemberUserId(USER_ID)).thenReturn(List.of(3L));
when(shopManageGroupMapper.selectLedGroups(3L)).thenReturn(List.of(group(11L, COMPANY_ID, "上司的组")));
when(shopManageGroupMapper.selectLedGroups(3L)).thenReturn(List.of(group(11L, COMPANY_ID, "组长A的组")));
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
assertThat(scope.visibleGroupIds()).containsExactly(11L);
assertThat(scope.writableGroupIds()).containsExactly(11L);
assertThat(scope.groups()).singleElement()
.satisfies(option -> assertThat(option.isEditable()).isTrue());
}
@Test
void 普通号_可见同组长但零可写() {
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
when(adminAuthSupport.currentRole(operator)).thenReturn("normal");
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(false);
when(shopManageGroupMapper.selectLedGroups(USER_ID)).thenReturn(List.of());
when(shopManageGroupMapper.selectLeaderIdsByMemberUserId(USER_ID)).thenReturn(List.of(3L));
when(shopManageGroupMapper.selectLedGroups(3L)).thenReturn(List.of(group(10L, COMPANY_ID, "A组")));
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
assertThat(scope.visibleGroupIds()).containsExactly(10L);
assertThat(scope.writableGroupIds()).isEmpty();
assertThat(scope.groups()).singleElement()
.satisfies(option -> assertThat(option.isEditable()).isFalse());
}
@Test
void 不属于任何分组的账号_空作用域不报错() {
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
when(adminAuthSupport.currentRole(operator)).thenReturn("normal");
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(false);
when(shopManageGroupMapper.selectLedGroups(USER_ID)).thenReturn(List.of());
when(shopManageGroupMapper.selectLeaderIdsByMemberUserId(USER_ID)).thenReturn(List.of());
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
assertThat(scope.visibleGroupIds()).isEmpty();
assertThat(scope.defaultGroupId()).isNull();
assertThat(scope.groups()).isEmpty();
}
/**
* 2026-11-24 客户口径「普通管理员看不到同组管理员的分类」(线上账号 阿华112336 的形状:
* 员工 1366 → 管理员 1365(不带组)→ 公司超管 1353(带组 49)):可见组沿 created_by_id
@@ -332,6 +303,38 @@ class JdCategoryScopeServiceTest {
assertThat(scope.canRead(22L)).isFalse();
}
@Test
void 普通号_可见同组长但零可写() {
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
when(adminAuthSupport.currentRole(operator)).thenReturn("normal");
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(false);
when(shopManageGroupMapper.selectLedGroups(USER_ID)).thenReturn(List.of());
when(shopManageGroupMapper.selectLeaderIdsByMemberUserId(USER_ID)).thenReturn(List.of(3L));
when(shopManageGroupMapper.selectLedGroups(3L)).thenReturn(List.of(group(10L, COMPANY_ID, "A组")));
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
assertThat(scope.visibleGroupIds()).containsExactly(10L);
assertThat(scope.writableGroupIds()).isEmpty();
assertThat(scope.groups()).singleElement()
.satisfies(option -> assertThat(option.isEditable()).isFalse());
}
@Test
void 不属于任何分组的账号_空作用域不报错() {
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
when(adminAuthSupport.currentRole(operator)).thenReturn("normal");
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(false);
when(shopManageGroupMapper.selectLedGroups(USER_ID)).thenReturn(List.of());
when(shopManageGroupMapper.selectLeaderIdsByMemberUserId(USER_ID)).thenReturn(List.of());
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
assertThat(scope.visibleGroupIds()).isEmpty();
assertThat(scope.defaultGroupId()).isNull();
assertThat(scope.groups()).isEmpty();
}
// ---------------------------------------------------------------- 入参收敛
@Test
@@ -202,19 +202,19 @@ class JdGroupShopServiceTest {
@Test
void 可添加候选排除已有名单且limit钳制() {
when(shopMapper.selectList(any())).thenReturn(List.of(shopRow(1L, CATEGORY_ID, "已有店A")));
when(productMapper.selectDistinctShopNames("旗", List.of("已有店A"), 200))
when(productMapper.selectDistinctShopNames("旗", List.of("已有店A"), 200, null))
.thenReturn(List.of("旗舷店"));
assertEquals(List.of("旗舷店"),
service.availableShops(" 旗 ", CATEGORY_ID, null), "keyword trim 后下推;limit 缺省钳到 200");
service.availableShops(" 旗 ", CATEGORY_ID, null, null), "keyword trim 后下推;limit 缺省钳到 200");
// limit 钳制:500 请求 → 200(服务侧钳,mapper 不做第二次换算)。
service.availableShops(null, CATEGORY_ID, 500);
verify(productMapper).selectDistinctShopNames(null, List.of("已有店A"), 200);
service.availableShops(null, CATEGORY_ID, 500, null);
verify(productMapper).selectDistinctShopNames(null, List.of("已有店A"), 200, null);
// 不带排除分类:excludeNames 空清单(不生成 NOT IN);前两轮各查过一次名单表,共两次。
when(productMapper.selectDistinctShopNames(null, List.of(), 50)).thenReturn(List.of("直接店"));
assertEquals(List.of("直接店"), service.availableShops(null, null, 50));
when(productMapper.selectDistinctShopNames(null, List.of(), 50, null)).thenReturn(List.of("直接店"));
assertEquals(List.of("直接店"), service.availableShops(null, null, 50, null));
verify(shopMapper, times(2)).selectList(any());
}
@@ -149,6 +149,44 @@ class JdProductQueryServiceUserPageTest {
verify(jdProductMapper, never()).selectList(any());
}
/**
* 公司隔离(2026-11-23 客户口径「公司与公司之间不能互看」):详情按行 company_id 校验,
* 别公司的行按「商品不存在」拒绝(不 403,不泄露行是否存在);平台超管(null)不限。
*/
@Test
void 详情跨公司按商品不存在拒绝_本公司放行() {
JdProductEntity row = product(31L);
row.setCompanyId(5L);
when(jdProductMapper.selectById(31L)).thenReturn(row);
when(jdProductImageMapper.selectList(any())).thenReturn(List.of());
when(jdProductSkuMapper.selectList(any())).thenReturn(List.of());
assertThat(service.detail(31L, null, 5L).getId()).as("本公司行正常返回").isEqualTo(31L);
assertThat(service.detail(31L, null, null).getId()).as("平台超管不限公司").isEqualTo(31L);
org.junit.jupiter.api.Assertions.assertThrows(
com.nanri.aiimage.common.exception.BusinessException.class,
() -> service.detail(31L, null, 6L),
"别公司的行必须按「商品不存在」拒绝(不是 403,不泄露存在性)");
}
/**
* 写侧行级公司闸:请求 id 里只要有一行不属于作用域公司就整批拒绝;平台超管(null)不限。
* 读侧藏起来的行仍然能删=静默删除,不是隔离——编辑/传图/删除/批量删除/归类都走这一道。
*/
@Test
void 写侧行级公司闸_跨公司整批拒绝_本公司与平台超管放行() {
JdProductEntity own = product(41L);
own.setCompanyId(5L);
when(jdProductMapper.selectBatchIds(List.of(41L))).thenReturn(List.of(own));
service.assertRowsInCompany(List.of(41L), 5L);
service.assertRowsInCompany(List.of(41L), null);
org.junit.jupiter.api.Assertions.assertThrows(
com.nanri.aiimage.common.exception.BusinessException.class,
() -> service.assertRowsInCompany(List.of(41L), 6L),
"别公司的行必须整批拒绝(静默删掉别家数据比报错更糟)");
}
@Test
void 详情与列表都带回人工编辑锁_锁行可观测面() {
// 2026-11-23 E2E 窗发现的缺口:V199 人工锁在编辑侧已置 1,但详情/列表 VO 不暴露,
@@ -159,7 +197,7 @@ class JdProductQueryServiceUserPageTest {
when(jdProductImageMapper.selectList(any())).thenReturn(List.of());
when(jdProductSkuMapper.selectList(any())).thenReturn(List.of());
com.nanri.aiimage.modules.jd.model.vo.JdProductDetailVo detail = service.detail(21L, null);
com.nanri.aiimage.modules.jd.model.vo.JdProductDetailVo detail = service.detail(21L, null, null);
assertThat(detail.getManualLocked()).as("详情 VO 带回人工锁").isEqualTo(1);
JdProductEntity listRow = product(22L);