diff --git a/admin-frontend-vue/src/pages/account/shop-group-api.ts b/admin-frontend-vue/src/pages/account/shop-group-api.ts index 379f4b693..1c482259e 100644 --- a/admin-frontend-vue/src/pages/account/shop-group-api.ts +++ b/admin-frontend-vue/src/pages/account/shop-group-api.ts @@ -12,7 +12,7 @@ import { export const SHOP_GROUPS_ENDPOINT = '/api/admin/shop-manages/groups' export async function fetchShopGroups(companyId?: number | null): Promise { - const { data } = await http.get(SHOP_GROUPS_ENDPOINT, { params: { company_id: companyId } }) + const { data } = await http.get(SHOP_GROUPS_ENDPOINT, { params: companyId != null ? { company_id: companyId } : {} }) return parseShopGroupList(data) } diff --git a/admin-frontend-vue/src/pages/shop/ShopManagePage.vue b/admin-frontend-vue/src/pages/shop/ShopManagePage.vue index b872d7cf3..21cfb7d00 100644 --- a/admin-frontend-vue/src/pages/shop/ShopManagePage.vue +++ b/admin-frontend-vue/src/pages/shop/ShopManagePage.vue @@ -104,7 +104,8 @@ async function load() { } // 切公司 = 重新取数:列表与分组下拉共用这一个 watch(少一个 = 那半边的 tab 是装饰品)。 -watch(() => companyTab.activeId, () => { void loadGroups(); void load() }) +// groupId 必须一起清掉:上一家选中的分组在新一家根本不存在,留着会把切 tab 后的首屏筛成空列表。 +watch(() => companyTab.activeId, () => { filter.groupId = null; void loadGroups(); void load() }) function apply() { page.value = 1 diff --git a/admin-frontend-vue/src/pages/shop/shop-manage-api.ts b/admin-frontend-vue/src/pages/shop/shop-manage-api.ts index 7395dbcca..22d4794a7 100644 --- a/admin-frontend-vue/src/pages/shop/shop-manage-api.ts +++ b/admin-frontend-vue/src/pages/shop/shop-manage-api.ts @@ -38,7 +38,7 @@ export const SHOP_MANAGE_GROUPS_ENDPOINT = '/api/admin/shop-manages/groups' /** 分组下拉:打的是与分组管理页同一个 GET /groups,所以它也必须跟着公司 tab 收窄, * 否则店铺列表窄了、下拉里还选得到别家的组。 */ export async function fetchShopManageGroups(companyId?: number | null): Promise { - const { data } = await http.get(SHOP_MANAGE_GROUPS_ENDPOINT, { params: { company_id: companyId } }) + const { data } = await http.get(SHOP_MANAGE_GROUPS_ENDPOINT, { params: companyId != null ? { company_id: companyId } : {} }) return parseShopManageGroups(data) } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupService.java index 6b01114e1..142307975 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupService.java @@ -48,10 +48,6 @@ public class ShopManageGroupService { return list(null, null); } - public List list(Long createdById) { - return list(createdById, null); - } - public List list(Long createdById, List companyMemberIds) { LambdaQueryWrapper qw = new LambdaQueryWrapper() .orderByAsc(ShopManageGroupEntity::getId); @@ -65,7 +61,7 @@ public class ShopManageGroupService { if (companyMemberIds.isEmpty()) { qw.eq(ShopManageGroupEntity::getId, -1L); } else { - // 公司条件同样自成一组(G2 护栏钉的是 AND (…) 这个形状;平铺 IN 与它在语义上等价)。 + // 同样自成一组:上面那支 and() 里已经混着 OR,这条若平铺下去,AND 优先只会作用在 OR 的右半边。 qw.and(w -> w.in(ShopManageGroupEntity::getCreatedById, companyMemberIds)); } } diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupServiceTest.java new file mode 100644 index 000000000..c7f8afd8b --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/shopkey/service/ShopManageGroupServiceTest.java @@ -0,0 +1,111 @@ +package com.nanri.aiimage.modules.shopkey.service; + +import com.baomidou.mybatisplus.core.MybatisConfiguration; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; +import com.nanri.aiimage.common.mapper.ShopManageGroupMapper; +import com.nanri.aiimage.common.model.entity.ShopManageGroupEntity; +import org.apache.ibatis.builder.MapperBuilderAssistant; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Task 11 分组侧(计划 Step 1 点名的新建文件):{@code ShopManageGroupService.list} 叠公司条件时, + * 既有那条 {@code created_by_id = ? OR user_id = ?} 必须整块留在括号里。 + * + *

这是本任务唯一一处「错了不报错、只是多放行」的形状:MP 的 normal 段按顺序拼 AND/OR,而 MySQL 里 + * AND 优先 ⇒ 平铺成 {@code created_by_id = ? OR user_id = ? AND created_by_id IN (...)} 时,第一个 + * {@code created_by_id = ?} 整块逃出公司筛选,别家的分组照样列出来,接口还是 200。

+ * + *

断言口径与 {@code ShopManageServiceTest} 同一条:结构(括号、OR、IN)从 + * {@code getSqlSegment()} 读,值从 {@code getParamNameValuePairs()} 读——MP 在 segment 里只放 + * {@code #{ew.paramNameValuePairs.MPGENVALn}} 占位,写 {@code segment.contains("11")} 是恒假断言。 + * 反向钉(没给成员集不许叠条件)与它的正样本写在同一支用例里,两支护一支持平。

+ */ +@ExtendWith(MockitoExtension.class) +class ShopManageGroupServiceTest { + + @Mock + private ShopManageGroupMapper groupMapper; + + /** 只给 groupMapper 打桩;其余构造参数留 null——本文件三支用例都只走 list() 的第一段查询。 */ + @InjectMocks + private ShopManageGroupService groupService; + + /** getSqlSegment() 解析 lambda 靠 TableInfo 缓存,单测没有 Spring 容器建它,不初始化就抛 "can not find lambda cache"。 */ + @BeforeEach + void initTableInfo() { + TableInfoHelper.initTableInfo( + new MapperBuilderAssistant(new MybatisConfiguration(), ""), ShopManageGroupEntity.class); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + private List> captureAll(int expectedInvocations) { + ArgumentCaptor captor = ArgumentCaptor.forClass(LambdaQueryWrapper.class); + verify(groupMapper, times(expectedInvocations)).selectList(captor.capture()); + // captor 是 raw 的(ArgumentCaptor 对泛型参数只能按原始类型捕),这里过 raw List 而不是逐个 cast。 + return (List) captor.getAllValues(); + } + + @Test + @DisplayName("分组:list 叠公司必须把既有那条平铺 OR 整个套进括号(否则 created_by_id=? 那半边逃出筛选)") + void groupListWrapsTheOrBeforeNarrowing() { + when(groupMapper.selectList(any())).thenReturn(List.of()); + + groupService.list(5L, List.of(11L)); + + LambdaQueryWrapper q = captureAll(1).get(0); + String segment = q.getSqlSegment(); + assertThat(segment).as("OR 组必须被 and() 包住").contains("(created_by_id").contains(" OR "); + assertThat(segment.lastIndexOf("AND (")) + .as("公司条件必须自成一组,不能与 OR 平铺") + .isGreaterThan(segment.indexOf("created_by_id")); + assertThat(q.getParamNameValuePairs().values()).contains(11L); + } + + @Test + @DisplayName("分组:成员集为空 ⇒ 走 id = -1 恒空,绝不渲染 IN ()(MySQL 语法错)") + void groupListEmptyMemberSetUsesSentinel() { + when(groupMapper.selectList(any())).thenReturn(List.of()); + + groupService.list(5L, List.of()); + + LambdaQueryWrapper q = captureAll(1).get(0); + assertThat(q.getSqlSegment()).doesNotContain("IN ("); + assertThat(q.getParamNameValuePairs().values()) + .as("空集必须落到恒不命中的哨兵 id 上,而不是退化成「不加条件」= 看全量") + .contains(-1L); + } + + @Test + @DisplayName("分组:只有给了成员集才叠公司条件——正反两向写在同一支用例里,反向钉不许独自成立") + void companyLegAppearsOnlyWhenMemberSetGiven() { + when(groupMapper.selectList(any())).thenReturn(List.of()); + + groupService.list(null, null); + groupService.list(null, List.of(11L, 12L)); + + List> both = captureAll(2); + LambdaQueryWrapper unfiltered = both.get(0); + LambdaQueryWrapper narrowed = both.get(1); + assertThat(unfiltered.getSqlSegment()).doesNotContain("IN ("); + assertThat(unfiltered.getParamNameValuePairs().values()).doesNotContain(11L, 12L); + // 正样本:上一支的 doesNotContain 只有在这一支真的会渲染 IN 时才有意义。 + assertThat(narrowed.getSqlSegment()).contains("IN ("); + assertThat(narrowed.getParamNameValuePairs().values()).contains(11L, 12L); + } +} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/shopkey/service/ShopManageServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/shopkey/service/ShopManageServiceTest.java index cce862652..23a718bc3 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/shopkey/service/ShopManageServiceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/shopkey/service/ShopManageServiceTest.java @@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.nanri.aiimage.common.exception.BusinessException; -import com.nanri.aiimage.common.model.entity.ShopManageGroupEntity; import com.nanri.aiimage.common.security.ShopCredentialCryptoService; import com.nanri.aiimage.modules.shopkey.mapper.ShopManageMapper; import com.nanri.aiimage.modules.shopkey.model.entity.ShopManageEntity; @@ -50,8 +49,6 @@ class ShopManageServiceTest { void initTableInfo() { TableInfoHelper.initTableInfo( new MapperBuilderAssistant(new MybatisConfiguration(), ""), ShopManageEntity.class); - TableInfoHelper.initTableInfo( - new MapperBuilderAssistant(new MybatisConfiguration(), ""), ShopManageGroupEntity.class); } @Test @@ -89,6 +86,22 @@ class ShopManageServiceTest { assertThat(captor.getValue().getParamNameValuePairs().values()).contains(11L, 12L); } + @Test + @DisplayName("读:超管选了一家没有成员的公司 ⇒ 走 id = -1 恒空,不许退化成不加条件") + void superAdminPageWithEmptyMemberSetStaysEmpty() { + when(shopManageMapper.selectCount(any())).thenReturn(0L); + + service.page(1L, 15L, null, null, 1L, true, List.of()); + + ArgumentCaptor> captor = + ArgumentCaptor.forClass(LambdaQueryWrapper.class); + verify(shopManageMapper).selectCount(captor.capture()); + assertThat(captor.getValue().getSqlSegment()) + .as("空集渲染 IN () 是 MySQL 语法错;退化成不加条件则是把全量放出来") + .doesNotContain("IN ("); + assertThat(captor.getValue().getParamNameValuePairs().values()).contains(-1L); + } + @Test @DisplayName("凭据:非超管读「创建人不认识、分组也不认识」的行 ⇒ 403(§6.1 列表筛了按 id 照读)") void credentialByIdRejectsRowOutsideBothVisibleSets() { @@ -113,7 +126,7 @@ class ShopManageServiceTest { } @Test - @DisplayName("凭据:超管不查可见集(今天不筛、tab 也不是安全边界;短路写了就红在 NPE 上)") + @DisplayName("凭据:超管不查可见集(今天不筛、tab 也不是安全边界;删掉短路就红在 403)") void credentialByIdStaysUnfilteredForSuperAdmin() { when(shopManageMapper.selectById(79L)).thenReturn(shop(79L, 999L, 7L)); @@ -139,6 +152,13 @@ class ShopManageServiceTest { .doesNotContain("company_id") .doesNotContain("companyId"); } - assertThat(SourceScan.methodBody(text, "page")).contains("company_id"); + // 取样自检必须钉「绑定」本身:methodBody 含注解,而 @Operation 的文案里就写着 company_id, + // 只断 contains("company_id") 会在参数被删掉后照样绿——删掉绑定后 G2 的 A/B 两支也都不再触发。 + assertThat(SourceScan.methodBody(text, "page")) + .as("page 的 company_id 绑定一旦消失,这一整页的 tab 就成了装饰品,且没有任何尺子会红") + .contains("@RequestParam(name = \"company_id\""); + assertThat(SourceScan.methodBody(text, "listGroups")) + .as("listGroups 同样钉在自己的注解上,不是文件里另一处的字面量") + .contains("@RequestParam(name = \"company_id\""); } }