From 7067d5380d36ca2d424814de210b5b52cebd5761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Mon, 16 Nov 2026 22:06:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(jd):=20=E5=B9=B3=E5=8F=B0=E8=B6=85=E7=AE=A1?= =?UTF-8?q?=E5=9C=A8=E3=80=8C=E7=BB=84=E5=BD=92=E5=B1=9E=E5=85=AC=E5=8F=B8?= =?UTF-8?q?=E5=85=A8=E4=B8=BA=20NULL=E3=80=8D=E6=97=B6=E6=95=B4=E4=B8=AA?= =?UTF-8?q?=E4=BA=AC=E4=B8=9C=E6=95=B0=E6=8D=AE=E5=BA=93=20500?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 线上实录(2026-11-16 22:00):`JdCategoryScopeService.assemble` 抛 NullPointerException, `/api/admin/jd-products`(列表)、`/api/admin/jd-product-groups`(分类列表)、 `/api/admin/jd-product-groups/scope` 三个端点同时 500,页面提示「京东商品列表加载失败: 服务器内部错误」,分类下拉与「店铺分类」弹窗全空。 真因不是 V203 的 SQL,而是「不可变空 Map 上取 null 键」:`companyNames()` 在 「一个公司 id 都取不到」时返回 `Map.of()`,而 `biz_shop_manage_group.company_id` 按 V153 口径允许为 NULL(组长账号已不存在、归属无从追溯),于是 `Map.of().get(null)` 抛 NPE。 平台超管(跨公司视角)是唯一会读这张映射的档位,所以只有超管看到整页炸。 改法:组归属公司为空/非正时直接不取映射,只显示组名(有归属的照旧带「公司 / 组」前缀)。 新增 2 支判据:`平台超管_组归属公司全为NULL时不炸且组名不带前缀`(复现线上形状, 并断言不去查公司表)、`平台超管_组归属公司部分为NULL时只给有归属的加前缀`。 --- .../jd/service/JdCategoryScopeService.java | 8 ++- .../service/JdCategoryScopeServiceTest.java | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdCategoryScopeService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdCategoryScopeService.java index a016f8262..0d0852f07 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdCategoryScopeService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/service/JdCategoryScopeService.java @@ -213,7 +213,13 @@ public class JdCategoryScopeService { String groupName = group.getGroupName() == null ? "" : group.getGroupName().trim(); String label = groupName; if (platformSuperAdmin) { - String companyName = companyNames.get(group.getCompanyId()); + // 归属公司可为 NULL(V153 口径:组长账号已不存在、归属无从追溯),而 companyNames() + // 在「一个公司 id 都取不到」时返回的是 Map.of()——对不可变空 Map 调 get(null) 会抛 + // NullPointerException(2026-11-16 22:00 线上实录:全空归属时整个京东数据库 500, + // 列表/分类/scope 三端点同时挂)。故先判空再取,取不到就只显示组名。 + Long groupCompanyId = group.getCompanyId(); + String companyName = groupCompanyId == null || groupCompanyId <= 0 + ? null : companyNames.get(groupCompanyId); label = companyName == null || companyName.isBlank() ? groupName : companyName + " / " + groupName; } diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdCategoryScopeServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdCategoryScopeServiceTest.java index b2903f5cd..39632e44e 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdCategoryScopeServiceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/jd/service/JdCategoryScopeServiceTest.java @@ -225,6 +225,49 @@ class JdCategoryScopeServiceTest { verify(adminUserMapper, org.mockito.Mockito.times(1)).selectById(anyLong()); } + /** + * 平台超管 + 全部组的归属公司都是 NULL(V153 留下的存量形状:组长账号已不存在)。 + * + *

2026-11-16 22:00 线上实录:这种数据的整页 500——{@code companyNames()} 在「一个公司 id + * 都取不到」时返回 {@code Map.of()},`Map.of().get(null)` 抛 NullPointerException, + * 列表/分类/scope 三个端点同时挂。判据:不炸、组名不加前缀、也不去查公司表。

+ */ + @Test + void 平台超管_组归属公司全为NULL时不炸且组名不带前缀() { + AdminUserEntity operator = user(USER_ID, COMPANY_ID); + when(adminAuthSupport.currentRole(operator)).thenReturn("super_admin"); + when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(true); + when(shopManageGroupMapper.selectAllGroups()).thenReturn(List.of( + groupWithoutCompany(10L, "A组"), groupWithoutCompany(11L, "B组"))); + + JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator); + + assertThat(scope.visibleGroupIds()).containsExactly(10L, 11L); + assertThat(scope.writableGroupIds()).containsExactly(10L, 11L); + assertThat(scope.groups()).extracting(JdCategoryGroupOptionVo::getLabel) + .containsExactly("A组", "B组"); + verify(companyMapper, never()).selectBatchIds(any()); + } + + /** 部分组没归属公司(NULL)、部分有:有归属的带公司前缀,没归属的只显示组名。 */ + @Test + void 平台超管_组归属公司部分为NULL时只给有归属的加前缀() { + AdminUserEntity operator = user(USER_ID, COMPANY_ID); + when(adminAuthSupport.currentRole(operator)).thenReturn("super_admin"); + when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(true); + when(shopManageGroupMapper.selectAllGroups()).thenReturn(List.of( + group(10L, 1L, "A组"), groupWithoutCompany(11L, "B组"))); + CompanyEntity company = new CompanyEntity(); + company.setId(1L); + company.setCompanyName("甲公司"); + when(companyMapper.selectBatchIds(any())).thenReturn(List.of(company)); + + JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator); + + assertThat(scope.groups()).extracting(JdCategoryGroupOptionVo::getLabel) + .containsExactly("甲公司 / A组", "B组"); + } + @Test void scope回执_带平台超管标记与每组可写标记() { AdminUserEntity operator = user(USER_ID, COMPANY_ID); @@ -261,6 +304,14 @@ class JdCategoryScopeServiceTest { return group; } + /** 归属公司为 NULL 的组(V153 口径:组长账号已不存在,归属无从追溯)。 */ + private static ShopManageGroupEntity groupWithoutCompany(long id, String name) { + ShopManageGroupEntity group = new ShopManageGroupEntity(); + group.setId(id); + group.setGroupName(name); + return group; + } + /** 直接构作用域(跳过解析):可见组 / 可写组 / 下拉项三者按入参给。 */ private static JdCategoryScopeService.JdCategoryScope scope(List visible, List writable, List optionGroupIds) {