From 9991aaaf7d70311fb7b73eac934d22e0008e723e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Sat, 21 Nov 2026 20:22:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(jd-database):=20=E5=88=86=E7=BB=84?= =?UTF-8?q?=E4=B8=8B=E6=8B=89=20label=20=E5=89=8D=E7=BC=80=E5=85=AC?= =?UTF-8?q?=E5=8F=B8=E5=90=8D=E2=86=92=E7=BB=84=E9=95=BF=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E5=90=8D=EF=BC=882026-11-21=20=E7=94=A8=E6=88=B7=E5=8F=A3?= =?UTF-8?q?=E5=BE=84=E3=80=8C=E8=BF=99=E9=87=8C=E4=B8=8D=E6=98=AF=E9=80=89?= =?UTF-8?q?=E5=85=AC=E5=8F=B8=20=E6=98=AF=E9=80=89=E8=B6=85=E7=AE=A1/?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98=E3=80=8D=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 平台超管跨组下拉前缀从「公司名 / 组名」(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(随卡片分类条同窗)。 --- .../jd/service/JdCategoryScopeService.java | 66 +++++----- .../service/JdCategoryScopeServiceTest.java | 116 ++++++++++-------- 2 files changed, 105 insertions(+), 77 deletions(-) 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 6b556ab80..fd6a342d9 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 @@ -10,8 +10,6 @@ import com.nanri.aiimage.common.security.AdminAuthSupport; import com.nanri.aiimage.common.security.DeviceSessionPolicy; import com.nanri.aiimage.common.service.AdminPlatformScope; 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 lombok.RequiredArgsConstructor; @@ -43,8 +41,11 @@ import java.util.Set; * {@code SuperAdminPredicateSingleOutletTest} 钉死在 22 处。公司超管判定用 * {@link DeviceSessionPolicy#isCompanySuperAdmin(String)}(本笔新收的出口)。

* - *

解析结果不带任何公司字段给前端:跨公司的组名由 {@link #scopeVo} 拼进 {@code label}, - * 因为 `admin-frontend-vue` 禁止 JD 两个文件出现 `company_id|companyId` 记号。

+ *

解析结果不带任何公司字段给前端:平台超管跨多个组长时,下拉 {@code label} 由 + * {@link #scopeVo} 拼成「组长账号名 / 组名」(2026-11-21 用户口径「这里不是选公司 + * 是选超管/管理员」,替代此前 2026-11-16 的「公司名 / 组名」前缀——组是谁带的比组挂在 + * 哪家公司下更值得显示);组长新口径=分组的 {@code created_by_id}、旧字段 {@code user_id} + * 兜底。`admin-frontend-vue` 禁止 JD 两个文件出现 `company_id|companyId` 记号依旧。

*/ @Service @RequiredArgsConstructor @@ -54,7 +55,6 @@ public class JdCategoryScopeService { private final DataReadScope dataReadScope; private final AdminUserMapper adminUserMapper; private final ShopManageGroupMapper shopManageGroupMapper; - private final CompanyMapper companyMapper; /** 管理端作用域(会话身份已由调用方的菜单闸解析过)。 */ public JdCategoryScope resolve(AdminUserEntity operator) { @@ -220,25 +220,24 @@ 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; + // 组长前缀只在「可见组跨越不止一个组长」时才加(镜像 2026-11-16「单公司不啰嗦」规则): + // 只有一个组长时给每个组都挂同一个名字只是噪声。单组长或缺组长信息一律只显示组名。 + Map leaderNames = platformSuperAdmin ? leaderNames(visible.values()) : Map.of(); + boolean prefixLeader = platformSuperAdmin && leaderNames.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 (prefixCompany) { - // 归属公司可为 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; + if (prefixLeader) { + // 组长信息可能缺失(组长账号已删、created_by_id/user_id 均空),而 leaderNames() + // 在「一个组长 id 都取不到」时返回 Map.of()——对不可变空 Map 调 get(null) 会抛 + // NullPointerException(2026-11-16 22:00 线上实录的 companyNames 同形坑: + // 平台超管看到的整个京东数据库 500,列表/分类/scope 三端点同时挂)。故先判空 + // 再取,取不到就只显示组名。 + Long leaderId = leaderIdOf(group); + String leaderName = leaderId == null ? null : leaderNames.get(leaderId); + label = leaderName == null || leaderName.isBlank() + ? groupName : leaderName + " / " + groupName; } options.add(new JdCategoryGroupOptionVo(group.getId(), groupName, label, writable.contains(group.getId()))); @@ -247,26 +246,35 @@ public class JdCategoryScopeService { List.copyOf(visible.keySet()), List.copyOf(writable), List.copyOf(options)); } - /** 平台超管跨公司时给组名补公司前缀(一次批量取,不逐行查询)。 */ - private Map companyNames(java.util.Collection groups) { - Set companyIds = new LinkedHashSet<>(); + /** 平台超管跨组长时给组名补组长前缀(一次批量取账号名,不逐行查询)。 */ + private Map leaderNames(java.util.Collection groups) { + Set leaderIds = new LinkedHashSet<>(); for (ShopManageGroupEntity group : groups) { - if (group.getCompanyId() != null && group.getCompanyId() > 0) { - companyIds.add(group.getCompanyId()); + Long leaderId = leaderIdOf(group); + if (leaderId != null) { + leaderIds.add(leaderId); } } - if (companyIds.isEmpty()) { + if (leaderIds.isEmpty()) { return Map.of(); } Map names = new LinkedHashMap<>(); - for (CompanyEntity company : companyMapper.selectBatchIds(companyIds)) { - if (company.getId() != null && company.getCompanyName() != null) { - names.put(company.getId(), company.getCompanyName()); + for (AdminUserEntity leader : adminUserMapper.selectBatchIds(leaderIds)) { + if (leader.getId() != null && leader.getUsername() != null) { + names.put(leader.getId(), leader.getUsername()); } } return names; } + /** 组长 id:新口径 created_by_id 优先、旧字段 user_id 兜底(selectLeaderIdsByMemberUserId 同形)。 */ + private static Long leaderIdOf(ShopManageGroupEntity group) { + if (group.getCreatedById() != null && group.getCreatedById() > 0) { + return group.getCreatedById(); + } + return group.getUserId() != null && group.getUserId() > 0 ? group.getUserId() : null; + } + /** * 解析结果(只读快照)。{@code groups} 已按组名排序并带 {@code editable};列表读侧直接用 * {@link #visibleGroupIds()}、写侧用 {@link #writableGroupIds()}。 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 c55086860..6b2828155 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 @@ -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; * 管理员(组长)=自己带的组可写、自己所属组的组长带的分组只读; * 普通号=与管理员同可见、零可写(能开弹窗看,不能增删改分类、不能归类)。 * 另钉三条收敛语义:不可见组读 403(不做空结果降级)、无分组上下文读回 null(页面照常可用)、 - * 写侧在「无可写组 / 多可写组未选 / 该组不可写 / 组行不存在」四种形状下的文案。

+ * 写侧在「无可写组 / 多可写组未选 / 该组不可写 / 组行不存在」四种形状下的文案。 + * 下拉 label 前缀(2026-11-21 用户口径「这里不是选公司 是选超管/管理员」):平台超管跨多个 + * 组长时拼「组长账号名 / 组名」(组长=created_by_id 优先、user_id 兜底),单组长/缺组长 + * 只显示组名;公司名不再参与。

*/ @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 均空:组长账号已不存在的存量形状)。 * - *

2026-11-16 22:00 线上实录:这种数据的整页 500——{@code companyNames()} 在「一个公司 id + *

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

+ * 平台超管看到的整个京东数据库 500。现 {@code leaderNames()} 同形状——判据:不炸、 + * 组名不加前缀、也不去查组长账号表。

*/ @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);