From 47fc0e08505b95c56d18b12e60ff3a54ae7c6408 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:22:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(jd):=20selectAllGroups=20=E8=A1=A5=20compan?= =?UTF-8?q?y=5Fid=E2=80=94=E2=80=94=E5=B9=B3=E5=8F=B0=E8=B6=85=E7=AE=A1=20?= =?UTF-8?q?500=20=E7=9A=84=E7=9C=9F=E5=9B=A0=E5=9C=A8=E8=BF=99=E9=87=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上一笔(7067d538)按堆栈把 NPE 挡在 assemble 里,但那一层只是表象。真因是这条查询: `SELECT g.id AS id, g.group_name AS groupName` 没投影 `company_id`,于是平台超管这条路上 每一行的 `companyId` 都是 null ⇒ `companyNames()` 收到空集合返回 `Map.of()` ⇒ 不可变空 Map 取 null 键抛 NullPointerException。现网 21 个分组里 19 个其实有归属公司(company_id=1), 是我们自己的投影把它们读成了空——这也解释了为什么「非超管一切正常、只有超管整页 500」。 - SQL 补 `g.company_id AS companyId`; - 公司前缀改为**只在可见组跨不止一家公司时**才加:现网就一家公司,给每个组都挂同一个公司名 只是噪声;单公司/缺归属一律只显示组名(顺带保证不会再对空 Map 取 null 键); - 新增 `ShopManageGroupScopeSqlTest` 钉投影(这是「组归属公司」的唯一来源,少了就复发); `JdCategoryScopeServiceTest` 按新规则补「单公司不加前缀」「跨公司带前缀且缺归属只显示组名」。 --- .../jd/service/JdCategoryScopeService.java | 10 +++- .../mapper/common/ShopManageGroupMapper.xml | 2 +- .../mapper/ShopManageGroupScopeSqlTest.java | 55 +++++++++++++++++++ .../service/JdCategoryScopeServiceTest.java | 30 ++++++++-- 4 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 backend-java/src/test/java/com/nanri/aiimage/common/mapper/ShopManageGroupScopeSqlTest.java 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 0d0852f07..6b2a30d8d 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 @@ -208,14 +208,18 @@ public class JdCategoryScopeService { private JdCategoryScope assemble(boolean platformSuperAdmin, Long companyId, Map visible, Set writable) { Map companyNames = platformSuperAdmin ? companyNames(visible.values()) : Map.of(); + // 公司前缀只在「可见组跨越不止一家公司」时才加:现网就一家公司时给每个组都挂 + // 同一个公司名只是噪声(2026-11-16 用户口径「样式统一/别啰嗦」)。单公司或缺归属时 + // 一律只显示组名——顺带保证了这里永远不会对空 Map 取 null 键。 + boolean prefixCompany = platformSuperAdmin && companyNames.size() > 1; List options = new ArrayList<>(visible.size()); for (ShopManageGroupEntity group : visible.values()) { String groupName = group.getGroupName() == null ? "" : group.getGroupName().trim(); String label = groupName; - if (platformSuperAdmin) { + if (prefixCompany) { // 归属公司可为 NULL(V153 口径:组长账号已不存在、归属无从追溯),而 companyNames() - // 在「一个公司 id 都取不到」时返回的是 Map.of()——对不可变空 Map 调 get(null) 会抛 - // NullPointerException(2026-11-16 22:00 线上实录:全空归属时整个京东数据库 500, + // 在「一个公司 id 都取不到」时返回 Map.of()——对不可变空 Map 调 get(null) 会抛 + // NullPointerException(2026-11-16 22:00 线上实录:平台超管看到的整个京东数据库 500, // 列表/分类/scope 三端点同时挂)。故先判空再取,取不到就只显示组名。 Long groupCompanyId = group.getCompanyId(); String companyName = groupCompanyId == null || groupCompanyId <= 0 diff --git a/backend-java/src/main/resources/mapper/common/ShopManageGroupMapper.xml b/backend-java/src/main/resources/mapper/common/ShopManageGroupMapper.xml index 7a485f47c..ce8193309 100644 --- a/backend-java/src/main/resources/mapper/common/ShopManageGroupMapper.xml +++ b/backend-java/src/main/resources/mapper/common/ShopManageGroupMapper.xml @@ -57,7 +57,7 @@ ORDER BY accessible_group_id ASC diff --git a/backend-java/src/test/java/com/nanri/aiimage/common/mapper/ShopManageGroupScopeSqlTest.java b/backend-java/src/test/java/com/nanri/aiimage/common/mapper/ShopManageGroupScopeSqlTest.java new file mode 100644 index 000000000..c3bd99040 --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/common/mapper/ShopManageGroupScopeSqlTest.java @@ -0,0 +1,55 @@ +package com.nanri.aiimage.common.mapper; + +import com.baomidou.mybatisplus.core.MybatisConfiguration; +import org.apache.ibatis.builder.xml.XMLMapperBuilder; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.Configuration; +import org.junit.jupiter.api.Test; + +import java.io.InputStream; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * 京东分类「当前分组」下拉的组清单投影(V203 线上 500 的绊线,2026-11-16)。 + * + *

为什么钉 SQL 而不是钉行为:线上报的是「京东商品列表加载失败:服务器内部错误」, + * 三个端点同时 500,堆栈指向 {@code JdCategoryScopeService.assemble} 的 + * {@code companyNames.get(group.getCompanyId())}。真因不在那行,而在这条查询—— + * {@code selectAllGroups} 只投影了 {@code id, group_name},于是每一行的 {@code companyId} + * 都是 {@code null}:公司名映射因此是空 {@code Map.of()},而不可变空 Map 取 null 键会抛 + * NullPointerException。平台超管是唯一走这条投影的档位,所以只有超管整页炸,非超管一切正常, + * mock 掉 mapper 的单元用例也全都绿。 + * + *

判据:投影里少了 {@code company_id},本格直接红——那条查询是「组归属公司」的唯一来源。 + */ +class ShopManageGroupScopeSqlTest { + + private static final String XML = "mapper/common/ShopManageGroupMapper.xml"; + private static final String NS = "com.nanri.aiimage.common.mapper.ShopManageGroupMapper"; + + @Test + void 平台超管的组清单必须投影归属公司() throws Exception { + String sql = render("selectAllGroups"); + + assertEquals("SELECT g.id AS id, g.group_name AS groupName, g.company_id AS companyId" + + " FROM biz_shop_manage_group g ORDER BY g.id ASC", sql, + "投影变了:少了 company_id 就是 2026-11-16 那次线上 500(公司名映射为空、" + + "组标签前缀失效);多了列则要把 assemble 的跨公司前缀判据一起复核"); + } + + private static String render(String id) throws Exception { + MappedStatement statement = parse().getMappedStatement(NS + "." + id); + return statement.getBoundSql(Map.of()).getSql().replaceAll("\\s+", " ").trim(); + } + + private static Configuration parse() throws Exception { + MybatisConfiguration configuration = new MybatisConfiguration(); + try (InputStream in = Resources.getResourceAsStream(XML)) { + new XMLMapperBuilder(in, configuration, XML, configuration.getSqlFragments()).parse(); + } + return configuration; + } +} 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 39632e44e..5c396cfd2 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 @@ -249,14 +249,14 @@ class JdCategoryScopeServiceTest { verify(companyMapper, never()).selectBatchIds(any()); } - /** 部分组没归属公司(NULL)、部分有:有归属的带公司前缀,没归属的只显示组名。 */ + /** 可见组只跨一家公司(现网就该是这个形状):不加公司前缀,组名保持干净。 */ @Test - void 平台超管_组归属公司部分为NULL时只给有归属的加前缀() { + void 平台超管_只有一家公司时组名不带公司前缀() { 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组"))); + group(10L, 1L, "A组"), group(11L, 1L, "B组"), groupWithoutCompany(12L, "C组"))); CompanyEntity company = new CompanyEntity(); company.setId(1L); company.setCompanyName("甲公司"); @@ -265,7 +265,29 @@ class JdCategoryScopeServiceTest { JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator); assertThat(scope.groups()).extracting(JdCategoryGroupOptionVo::getLabel) - .containsExactly("甲公司 / A组", "B组"); + .containsExactly("A组", "B组", "C组"); + } + + /** 跨两家公司才加前缀;缺公司归属的那一组只显示组名(不许把 null 拼进去)。 */ + @Test + void 平台超管_跨公司时带前缀且缺归属的只显示组名() { + 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组"), group(11L, 2L, "B组"), groupWithoutCompany(12L, "C组"))); + CompanyEntity first = new CompanyEntity(); + first.setId(1L); + first.setCompanyName("甲公司"); + CompanyEntity second = new CompanyEntity(); + second.setId(2L); + second.setCompanyName("乙公司"); + when(companyMapper.selectBatchIds(any())).thenReturn(List.of(first, second)); + + JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator); + + assertThat(scope.groups()).extracting(JdCategoryGroupOptionVo::getLabel) + .containsExactly("甲公司 / A组", "乙公司 / B组", "C组"); } @Test