feat(jd-database): 分组下拉 label 前缀公司名→组长账号名(2026-11-21 用户口径「这里不是选公司 是选超管/管理员」)

平台超管跨组下拉前缀从「公司名 / 组名」(2026-11-16 口径,company 表)换成「组长账号名 / 组名」:
组长=biz_shop_manage_group.created_by_id 优先、user_id 兜底(selectLeaderIdsByMemberUserId 的
COALESCE 同形),显示名=users.username;触发规则镜像「跨多组长才加前缀」(2026-11-16
「单公司不啰嗦」规则同形),单组长/组长账号已删只显组名。companyNames()/CompanyMapper 注入
随之退役;2026-11-16 空 Map.get(null) NPE 线上实录的防线注释随 leaderNames() 平移。

判据:JdCategoryScopeServiceTest 18 支全绿(组长前缀四支:跨组长带/单组长不带/缺组长不带/
createdById 优先 userId 兜底;公司超管/admin 档不查组长表;mvn -o test-compile rc0)。
⏳ 发版:scope label 属后端改动,须发 jar(随卡片分类条同窗)。
This commit is contained in:
2026-11-21 20:22:17 +08:00
parent 067e7e5ba3
commit 9991aaaf7d
2 changed files with 105 additions and 77 deletions
@@ -7,8 +7,6 @@ import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.model.entity.ShopManageGroupEntity;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.DataReadScope;
import com.nanri.aiimage.modules.company.mapper.CompanyMapper;
import com.nanri.aiimage.modules.company.model.entity.CompanyEntity;
import com.nanri.aiimage.modules.jd.model.vo.JdCategoryGroupOptionVo;
import com.nanri.aiimage.modules.jd.model.vo.JdCategoryScopeVo;
import org.junit.jupiter.api.Test;
@@ -35,7 +33,10 @@ import static org.mockito.Mockito.when;
* 管理员(组长)=自己带的组可写、自己所属组的组长带的分组只读;
* 普通号=与管理员同可见、<b>零可写</b>(能开弹窗看,不能增删改分类、不能归类)。
* 另钉三条收敛语义:不可见组读 403(不做空结果降级)、无分组上下文读回 null(页面照常可用)、
* 写侧在「无可写组 / 多可写组未选 / 该组不可写 / 组行不存在」四种形状下的文案。</p>
* 写侧在「无可写组 / 多可写组未选 / 该组不可写 / 组行不存在」四种形状下的文案。
* 下拉 label 前缀(2026-11-21 用户口径「这里不是选公司 是选超管/管理员」):平台超管跨多个
* 组长时拼「组长账号名 / 组名」(组长=created_by_id 优先、user_id 兜底),单组长/缺组长
* 只显示组名;公司名不再参与。</p>
*/
@ExtendWith(MockitoExtension.class)
class JdCategoryScopeServiceTest {
@@ -51,8 +52,6 @@ class JdCategoryScopeServiceTest {
private AdminUserMapper adminUserMapper;
@Mock
private ShopManageGroupMapper shopManageGroupMapper;
@Mock
private CompanyMapper companyMapper;
@InjectMocks
private JdCategoryScopeService service;
@@ -60,19 +59,14 @@ class JdCategoryScopeServiceTest {
// ---------------------------------------------------------------- 四档矩阵
@Test
void 平台超管_可见并可写全部公司全部组且组名带公司前缀() {
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(20L, 2L, "B组")));
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));
ledGroup(10L, 1L, "A组", 101L), ledGroup(20L, 2L, "B组", 102L)));
when(adminUserMapper.selectBatchIds(any())).thenReturn(List.of(
leader(101L, "张组长"), leader(102L, "李组长")));
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
@@ -81,7 +75,7 @@ class JdCategoryScopeServiceTest {
assertThat(scope.writableGroupIds()).containsExactly(10L, 20L);
assertThat(scope.defaultGroupId()).isEqualTo(10L);
assertThat(scope.groups()).extracting(JdCategoryGroupOptionVo::getLabel)
.containsExactly("甲公司 / A组", "乙公司 / B组");
.containsExactly("张组长 / A组", "李组长 / B组");
assertThat(scope.groups()).allMatch(JdCategoryGroupOptionVo::isEditable);
assertThat(scope.canRead(20L)).isTrue();
assertThat(scope.canWrite(20L)).isTrue();
@@ -89,8 +83,8 @@ class JdCategoryScopeServiceTest {
/**
* 2026-11-17 用户口径「同理京东那边也是」:京东侧下拉只认京东档(platform='jd')分组。
* 亚马逊档与 NULL 档(未翻档的存量)一律滤掉,可写集跟着收缩;过滤后只剩一家公司 ⇒
* 组名不带公司前缀(既有「单公司不啰嗦」口径不变)。
* 亚马逊档与 NULL 档(未翻档的存量)一律滤掉,可写集跟着收缩;过滤后只剩一个无组长
* 信息的组 ⇒ 组名不带前缀。
*/
@Test
void 平台超管_只显示京东档分组_亚马逊档与NULL档不进下拉() {
@@ -101,10 +95,6 @@ class JdCategoryScopeServiceTest {
group(10L, 1L, "A组"),
amazonGroup(20L, 2L, "B组"),
groupOfPlatform(30L, 3L, "C组", null)));
CompanyEntity first = new CompanyEntity();
first.setId(1L);
first.setCompanyName("甲公司");
when(companyMapper.selectBatchIds(any())).thenReturn(List.of(first));
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
@@ -132,7 +122,8 @@ class JdCategoryScopeServiceTest {
assertThat(scope.writableGroupIds()).containsExactly(10L);
assertThat(scope.groups()).extracting(JdCategoryGroupOptionVo::getLabel)
.containsExactly("A组");
verify(companyMapper, never()).selectBatchIds(any());
// 组长前缀是平台超管专属(非平台超管不查组长账号表)。
verify(adminUserMapper, never()).selectBatchIds(any());
}
@Test
@@ -166,8 +157,8 @@ class JdCategoryScopeServiceTest {
assertThat(scope.writableGroupIds()).containsExactly(10L, 11L);
assertThat(scope.groups()).extracting(JdCategoryGroupOptionVo::getLabel)
.containsExactly("A组", "B组");
// 公司超管的公司维只用来取组,不随 scope 下发公司字段给前端(label 里已拼好)。
verify(companyMapper, never()).selectBatchIds(any());
// 公司超管的公司维只用来取组;组长前缀是平台超管专属,不随 scope 下发公司字段给前端。
verify(adminUserMapper, never()).selectBatchIds(any());
}
@Test
@@ -291,14 +282,15 @@ class JdCategoryScopeServiceTest {
}
/**
* 平台超管 + 全部组的归属公司都是 NULL(V153 留下的存量形状:组长账号已不存在)。
* 平台超管 + 全部组都没有组长信息(created_by_id/user_id 均空:组长账号已不存在的存量形状)。
*
* <p>2026-11-16 22:00 线上实录:这种数据的整页 500——{@code companyNames()} 在「一个公司 id
* <p>2026-11-16 22:00 线上实录的教训平移:当时的 {@code companyNames()} 在「一个公司 id
* 都取不到」时返回 {@code Map.of()},`Map.of().get(null)` 抛 NullPointerException,
* 列表/分类/scope 三个端点同时挂。判据:不炸、组名不加前缀、也不去查公司表。</p>
* 平台超管看到的整个京东数据库 500。现 {@code leaderNames()} 同形状——判据:不炸、
* 组名不加前缀、也不去查组长账号表。</p>
*/
@Test
void 平台超管_组归属公司全为NULL时不炸且组名不带前缀() {
void 平台超管_组无组长信息时不炸且组名不带前缀() {
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
when(adminAuthSupport.currentRole(operator)).thenReturn("super_admin");
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(true);
@@ -311,21 +303,19 @@ class JdCategoryScopeServiceTest {
assertThat(scope.writableGroupIds()).containsExactly(10L, 11L);
assertThat(scope.groups()).extracting(JdCategoryGroupOptionVo::getLabel)
.containsExactly("A组", "B组");
verify(companyMapper, never()).selectBatchIds(any());
verify(adminUserMapper, never()).selectBatchIds(any());
}
/** 可见组只跨一家公司(现网就该是这个形状):不加公司前缀,组名保持干净。 */
/** 可见组只跨一个组长(现网单公司常就是这个形状):不加组长前缀,组名保持干净。 */
@Test
void 平台超管_只有一家公司时组名不带公司前缀() {
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, 1L, "B组"), groupWithoutCompany(12L, "C组")));
CompanyEntity company = new CompanyEntity();
company.setId(1L);
company.setCompanyName("甲公司");
when(companyMapper.selectBatchIds(any())).thenReturn(List.of(company));
ledGroup(10L, 1L, "A组", 101L), ledGroup(11L, 1L, "B组", 101L),
groupWithoutCompany(12L, "C组")));
when(adminUserMapper.selectBatchIds(any())).thenReturn(List.of(leader(101L, "张组长")));
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
@@ -333,26 +323,42 @@ class JdCategoryScopeServiceTest {
.containsExactly("A组", "B组", "C组");
}
/** 跨两家公司才加前缀;缺公司归属的那一组只显示组名(不许把 null 拼进去)。 */
/** 跨两个组长才加前缀;缺组长信息的那一组只显示组名(不许把 null 拼进去)。 */
@Test
void 平台超管_跨公司时带前缀且缺归属的只显示组名() {
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));
ledGroup(10L, 1L, "A组", 101L), ledGroup(11L, 2L, "B组", 102L),
groupWithoutCompany(12L, "C组")));
when(adminUserMapper.selectBatchIds(any())).thenReturn(List.of(
leader(101L, "张组长"), leader(102L, "李组长")));
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
assertThat(scope.groups()).extracting(JdCategoryGroupOptionVo::getLabel)
.containsExactly("甲公司 / A组", "乙公司 / B组", "C组");
.containsExactly("张组长 / A组", "李组长 / B组", "C组");
}
/** 组长 id 解析:created_by_id 新口径优先,user_id 旧字段兜底(selectLeaderIdsByMemberUserId 同形)。 */
@Test
void 平台超管_组长id按createdById优先userId兜底() {
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
when(adminAuthSupport.currentRole(operator)).thenReturn("super_admin");
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(true);
ShopManageGroupEntity bothSet = ledGroup(10L, 1L, "新口径组", 101L);
bothSet.setUserId(999L);
ShopManageGroupEntity oldOnly = group(11L, 1L, "旧口径组");
oldOnly.setUserId(102L);
when(shopManageGroupMapper.selectAllGroups()).thenReturn(List.of(bothSet, oldOnly));
when(adminUserMapper.selectBatchIds(any())).thenReturn(List.of(
leader(101L, "张组长"), leader(102L, "老组长")));
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
assertThat(scope.groups()).extracting(JdCategoryGroupOptionVo::getLabel)
.containsExactly("张组长 / 新口径组", "老组长 / 旧口径组");
}
@Test
@@ -401,7 +407,21 @@ class JdCategoryScopeServiceTest {
return group;
}
/** 归属公司为 NULL 的组(V153 口径:组长账号已不存在,归属无从追溯)。 */
/** 带组长(created_by_id 新口径)的组:label 前缀判据用。 */
private static ShopManageGroupEntity ledGroup(long id, long companyId, String name, long leaderId) {
ShopManageGroupEntity group = group(id, companyId, name);
group.setCreatedById(leaderId);
return group;
}
private static AdminUserEntity leader(long id, String username) {
AdminUserEntity leader = new AdminUserEntity();
leader.setId(id);
leader.setUsername(username);
return leader;
}
/** 归属公司与组长信息均为 NULL 的组(组长账号已不存在的存量形状)。 */
private static ShopManageGroupEntity groupWithoutCompany(long id, String name) {
ShopManageGroupEntity group = new ShopManageGroupEntity();
group.setId(id);