fix(jd): selectAllGroups 补 company_id——平台超管 500 的真因在这里

上一笔(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` 按新规则补「单公司不加前缀」「跨公司带前缀且缺归属只显示组名」。
This commit is contained in:
2026-11-16 22:22:21 +08:00
parent cac5b8ae7d
commit 47fc0e0850
4 changed files with 89 additions and 8 deletions
@@ -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)。
*
* <p><b>为什么钉 SQL 而不是钉行为</b>:线上报的是「京东商品列表加载失败:服务器内部错误」,
* 三个端点同时 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 的单元用例也全都绿。
*
* <p>判据:投影里少了 {@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;
}
}
@@ -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