fix(jd): 平台超管在「组归属公司全为 NULL」时整个京东数据库 500

线上实录(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时只给有归属的加前缀`。
This commit is contained in:
2026-11-16 22:06:46 +08:00
parent 61ab844e5d
commit 7067d5380d
2 changed files with 58 additions and 1 deletions
@@ -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;
}
@@ -225,6 +225,49 @@ class JdCategoryScopeServiceTest {
verify(adminUserMapper, org.mockito.Mockito.times(1)).selectById(anyLong());
}
/**
* 平台超管 + 全部组的归属公司都是 NULL(V153 留下的存量形状:组长账号已不存在)。
*
* <p>2026-11-16 22:00 线上实录:这种数据的整页 500——{@code companyNames()} 在「一个公司 id
* 都取不到」时返回 {@code Map.of()},`Map.of().get(null)` 抛 NullPointerException,
* 列表/分类/scope 三个端点同时挂。判据:不炸、组名不加前缀、也不去查公司表。</p>
*/
@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<Long> visible, List<Long> writable,
List<Long> optionGroupIds) {