feat(appearance-patent): 外观专利数据库主管列+导出+分组筛选——后管只读页补「所属主管」读侧现算列(owner 是主管取自己、普通账户取创建人,铁律 30 读不到不猜名)、GET /export 与分页共用同一过滤面与正门出 xlsx(SXSSF,上限 5 万行超限拒)、分组筛选 group_id 按 selectUserIdsByGroupId 解析「组长本人+名下子账户」整集 SQL 下推(铁律 20,选项复用既有 GET /shop-manages/groups 不下新端点);前端加主管列/分组下拉/导出按钮(blob 落盘+业务错误信封解包),wiring 护栏补导出发送口钉与 watch token(25 处发送口);棘轮 1073→1074 一枚记名,「无侵权」白名单登记导出上限文案

This commit is contained in:
2026-11-11 02:03:25 +08:00
parent 9b4404d027
commit 6f85f17c58
16 changed files with 1007 additions and 90 deletions
@@ -318,7 +318,13 @@ class CatchSilenceRatchetTest {
// 上层把原 401/403 原样抛回(回退失败必须显形,不吞)。
// 本批复核办法:上限压 0/下限抬 99999 单跑一次,实测 total 1073、rethrow 406、
// notRethrow 611、logOnly 193、empty 56(EMPTY 一格没涨)。
static final int STRICT_TOTAL = 1073;
// 11-11 外观专利数据库导出抬 1073 -> 1074,一枚逐处记名:
// `AppearancePatentCleanItemQueryService.writeExportWorkbook`:SXSSF 落盘 catch Exception
// → BusinessException「导出外观专利数据库失败,请稍后重试」(RETHROW 形状,rethrow 406 -> 407,
// 本格与 NOT_RETHROW/LOG_ONLY/EMPTY 都不动)。误差方向:导出请求的 IO 失败必须给用户
// 可读业务错误而不是 500 裸堆栈;反向对照——改成往外抛 IOException=前端拿到 Spring 兜底文案,
// 改成吞掉返回空字节=用户拿到 0 字节 xlsx 还以为导出成功(比报错更贵)。
static final int STRICT_TOTAL = 1074;
/**
* 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。
* 这一格只能涨不能跌——它是"异常会往外走"的总量,把它钉成"只许减"就等于拦着别人修吞异常。
@@ -3,6 +3,8 @@ package com.nanri.aiimage.modules.appearancepatent.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.api.PageDefaults;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.mapper.AdminUserMapper;
import com.nanri.aiimage.common.mapper.ShopManageGroupMapper;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.DataReadScope;
@@ -13,28 +15,42 @@ import com.nanri.aiimage.modules.appearancepatent.model.vo.AppearancePatentClean
import com.nanri.aiimage.modules.appearancepatent.service.AppearancePatentCleanItemQueryService;
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -195,13 +211,13 @@ class AdminAppearancePatentCleanItemsControllerTest {
pageVo.setTotal(0L);
pageVo.setPage(1L);
pageVo.setPageSize((long) PageDefaults.CLEAN_ITEMS_PAGE_SIZE_INT);
when(queryService.page(1L, 10L, null, null, null, null, null, 5L)).thenReturn(pageVo);
when(queryService.page(1L, 10L, null, null, null, null, null, null, 5L)).thenReturn(pageVo);
ApiResponse<AppearancePatentCleanItemPageVo> response =
controller.page(1L, 10L, null, null, null, null, null, 5L, request);
controller.page(1L, 10L, null, null, null, null, null, null, 5L, request);
verify(dataReadScope).readCompanyIdOrOwn(operator, 5L);
verify(queryService).page(1L, 10L, null, null, null, null, null, 5L);
verify(queryService).page(1L, 10L, null, null, null, null, null, null, 5L);
assertEquals(Boolean.TRUE, response.isSuccess(), "拒绝走 403 业务码,放行必须是 success 信封(B80-03)");
assertEquals(pageVo, response.getData());
}
@@ -209,24 +225,25 @@ class AdminAppearancePatentCleanItemsControllerTest {
@Test
void 查询服务分页钳制防溢出() {
AppearancePatentCleanItemMapper mapper = mock(AppearancePatentCleanItemMapper.class);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(mapper);
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any())).thenReturn(1L);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(
mapper, mock(AdminUserMapper.class), mock(ShopManageGroupMapper.class));
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any(), any())).thenReturn(1L);
// page=0 / size=null ⇒ 归一为第 1 页、默认 10 条;offset = 0。
service.page(0L, null, null, null, null, null, null, null);
// page=0 / size=null ⇒ 归一为第 1 页、默认 10 条;offset = 0(groupId 不传=不做分组过滤)。
service.page(0L, null, null, null, null, null, null, null, null);
ArgumentCaptor<Long> offset = ArgumentCaptor.forClass(Long.class);
ArgumentCaptor<Integer> limit = ArgumentCaptor.forClass(Integer.class);
verify(mapper).selectCleanItemPage(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
offset.capture(), limit.capture());
isNull(), offset.capture(), limit.capture());
assertEquals(0L, offset.getValue());
assertEquals(PageDefaults.CLEAN_ITEMS_PAGE_SIZE_INT, limit.getValue());
// page=Long.MAX_VALUE / size=500 ⇒ 钳到 10_000 页、100 条;乘积 999_900 不得变负(铁律 15,CR-18 同值)。
service.page(Long.MAX_VALUE, 500L, null, null, null, null, null, null);
service.page(Long.MAX_VALUE, 500L, null, null, null, null, null, null, null);
ArgumentCaptor<Long> offsetMax = ArgumentCaptor.forClass(Long.class);
ArgumentCaptor<Integer> limitMax = ArgumentCaptor.forClass(Integer.class);
verify(mapper, org.mockito.Mockito.times(2)).selectCleanItemPage(any(), any(), any(), any(), any(), any(),
offsetMax.capture(), limitMax.capture());
any(), offsetMax.capture(), limitMax.capture());
assertEquals(999_900L, offsetMax.getValue(), "(page-1)*page_size 最大 999_900,钳制后不得溢出变负");
assertEquals(100, limitMax.getValue());
}
@@ -234,32 +251,77 @@ class AdminAppearancePatentCleanItemsControllerTest {
@Test
void 查询服务公司过滤原样直传() {
AppearancePatentCleanItemMapper mapper = mock(AppearancePatentCleanItemMapper.class);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(mapper);
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any())).thenReturn(0L);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(
mapper, mock(AdminUserMapper.class), mock(ShopManageGroupMapper.class));
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any(), any())).thenReturn(0L);
// 超管显式传 5 ⇒ 按公司 5 筛(收窄);null ⇒ 全量(AC-4 前半句)。
service.page(1L, 10L, null, null, null, null, null, 5L);
verify(mapper).countCleanItems(eq(5L), isNull(), isNull(), isNull(), isNull(), isNull());
service.page(1L, 10L, null, null, null, null, null, null);
verify(mapper).countCleanItems(isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
service.page(1L, 10L, null, null, null, null, null, null, 5L);
verify(mapper).countCleanItems(eq(5L), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
service.page(1L, 10L, null, null, null, null, null, null, null);
verify(mapper).countCleanItems(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), isNull());
}
@Test
void 查询服务日期倒挂拒绝() {
AppearancePatentCleanItemMapper mapper = mock(AppearancePatentCleanItemMapper.class);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(mapper);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(
mapper, mock(AdminUserMapper.class), mock(ShopManageGroupMapper.class));
BusinessException ex = assertThrows(BusinessException.class,
() -> service.page(1L, 10L, null, null, null,
LocalDate.of(2026, 11, 9), LocalDate.of(2026, 11, 1), null));
LocalDate.of(2026, 11, 9), LocalDate.of(2026, 11, 1), null, null));
assertTrue(ex.getMessage().contains("开始日期不能晚于结束日期"),
"倒挂日期按业务异常拒绝(同去重页文案口径)");
verify(mapper, org.mockito.Mockito.never()).countCleanItems(any(), any(), any(), any(), any(), any());
verify(mapper, org.mockito.Mockito.never()).countCleanItems(any(), any(), any(), any(), any(), any(), any());
// 导出与分页共用同一过滤面校验(同一句文案,同一个拒绝时机:不碰 mapper)。
BusinessException exportEx = assertThrows(BusinessException.class,
() -> service.export(null, null, null,
LocalDate.of(2026, 11, 9), LocalDate.of(2026, 11, 1), null, null));
assertTrue(exportEx.getMessage().contains("开始日期不能晚于结束日期"),
"导出端点必须同样拒绝倒挂日期,不得绕过分页页签的校验口径");
}
@Test
void 分组筛选按组长名下用户集下推SQL且空集短路() {
AppearancePatentCleanItemMapper mapper = mock(AppearancePatentCleanItemMapper.class);
AdminUserMapper adminUserMapper = mock(AdminUserMapper.class);
ShopManageGroupMapper shopManageGroupMapper = mock(ShopManageGroupMapper.class);
AppearancePatentCleanItemQueryService service =
new AppearancePatentCleanItemQueryService(mapper, adminUserMapper, shopManageGroupMapper);
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any(), any())).thenReturn(2L);
when(shopManageGroupMapper.selectUserIdsByGroupId(9L)).thenReturn(List.of(9L, 33L));
// 选了分组 9 ⇒ 解析出的「组长本人+名下子账户」整集传给 mapper(SQL 侧下推,铁律 20)。
service.page(1L, 10L, null, null, null, null, null, 9L, 5L);
verify(shopManageGroupMapper).selectUserIdsByGroupId(9L);
verify(mapper).countCleanItems(eq(5L), isNull(), isNull(), isNull(), isNull(), isNull(),
eq(List.of(9L, 33L)));
// 分组读不出任何用户(如分组已被删)⇒ 结果必为空:短路不碰 mapper(IN () 不是合法 SQL)。
when(shopManageGroupMapper.selectUserIdsByGroupId(66L)).thenReturn(List.of());
AppearancePatentCleanItemPageVo empty = service.page(1L, 10L, null, null, null, null, null, 66L, 5L);
assertEquals(0L, empty.getTotal());
assertTrue(empty.getItems().isEmpty(), "分组空集=空页,不碰 mapper");
// 此刻 selectCleanItemPage 仍只有分组 9 那一页的 1 次调用(空集短路没有追加取数)。
verify(mapper, org.mockito.Mockito.times(1)).selectCleanItemPage(any(), any(), any(), any(), any(), any(),
any(), anyLong(), anyInt());
// 导出同口径:空集只给表头(不追加取数)。
byte[] bytes = service.export(null, null, null, null, null, 66L, 5L);
assertNotNull(bytes);
verify(mapper, org.mockito.Mockito.times(1)).selectCleanItemPage(any(), any(), any(), any(), any(), any(),
any(), anyLong(), anyInt());
// groupId ≤ 0 按没选分组处理(不当第三档):分组解析不新增调用(此前 9L×1+66L×2=3 次)。
service.page(1L, 10L, null, null, null, null, null, 0L, 5L);
verify(shopManageGroupMapper, org.mockito.Mockito.times(3)).selectUserIdsByGroupId(anyLong());
}
@Test
void 行字段与VO搬运一致不加工() {
AppearancePatentCleanItemMapper mapper = mock(AppearancePatentCleanItemMapper.class);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(mapper);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(
mapper, mock(AdminUserMapper.class), mock(ShopManageGroupMapper.class));
CleanItemEntity entity = new CleanItemEntity();
entity.setId(11L);
entity.setResultId(901L);
@@ -279,11 +341,11 @@ class AdminAppearancePatentCleanItemsControllerTest {
entity.setSourceFilename("list.xlsx");
LocalDateTime syncedAt = LocalDateTime.of(2026, 11, 10, 10, 30);
entity.setSyncedAt(syncedAt);
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any())).thenReturn(1L);
when(mapper.selectCleanItemPage(any(), any(), any(), any(), any(), any(), anyLong(),
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any(), any())).thenReturn(1L);
when(mapper.selectCleanItemPage(any(), any(), any(), any(), any(), any(), any(), anyLong(),
org.mockito.ArgumentMatchers.anyInt())).thenReturn(List.of(entity));
AppearancePatentCleanItemPageVo vo = service.page(1L, 10L, 33179L, null, null, null, null, 5L);
AppearancePatentCleanItemPageVo vo = service.page(1L, 10L, 33179L, null, null, null, null, null, 5L);
assertEquals(1L, vo.getTotal());
AppearancePatentCleanItemVo row = vo.getItems().get(0);
// 读侧只搬运不加工:标题两侧空格原样保留(AC-1「与 payload 一致」,后端不 trim 是与行值同口径)。
@@ -297,6 +359,204 @@ class AdminAppearancePatentCleanItemsControllerTest {
assertEquals("标题含品牌词", row.getTitleReason());
assertEquals("list.xlsx", row.getSourceFilename());
assertEquals(syncedAt, row.getSyncedAt());
// 行属用户读不到(AdminUserMapper 未命中)时主管列不猜名字,留空由前端展示 -(铁律 30)。
assertNull(row.getSupervisorName(), "用户读不到=supervisorName 为空,不得编造归属");
}
// ===================== 2026-11-11 客户需求:主管列+导出 =====================
@Test
void 导出端点就位且与分页共用同一正门() throws IOException {
String source = stripComments(Files.readString(CONTROLLER));
assertTrue(source.contains("\"/export\""),
"导出必须是 GET /api/admin/appearance-patent-clean-items/export(2026-11-11 客户需求「数据得导出来」)");
// 导出仍是只读:本页禁写/删/导入的判据由「控制器与只读端点就位」一格把守(无 Post/Put/Delete),
// 导出落 GET 不破那格;导出与分页都必须各自调用 requireCleanItemsAccess+readCompanyIdOrOwn
//(私有两支各调一次,缺一支即红——导出不许绕过页签的鉴权与公司隔离)。
long gateCalls = source.split("requireCleanItemsAccess\\(request\\)", -1).length - 1;
assertTrue(gateCalls >= 2, "分页与导出两支 handler 都必须过 requireCleanItemsAccess 正门,实得 " + gateCalls);
long scopeCalls = source.split("readCompanyIdOrOwn\\(", -1).length - 1;
assertTrue(scopeCalls >= 2, "分页与导出两支 handler 都必须走 readCompanyIdOrOwn 公司隔离,实得 " + scopeCalls);
// 导出是全量导出(服务侧另有 EXPORT_MAX_ROWS 上限),不收 page/page_size:
// 全文件 page_size 只允许出现一次(分页 handler 的参数声明)。
long pageSizeParams = source.split("page_size", -1).length - 1;
assertEquals(1, pageSizeParams, "导出不收分页参数,page_size 只许出现在分页 handler 的参数声明里");
// 分组(主管=组长)筛选:group_id 在分页与导出两支都要收;选项不下新端点(复用
// 既有 GET /shop-manages/groups,与去重页/店铺管理同源)。
long groupParams = source.split("group_id", -1).length - 1;
assertTrue(groupParams >= 2, "分页与导出都必须收 group_id(主管=组长,就是分组,2026-11-11 客户口径)");
assertFalse(source.contains("\"/supervisors\""),
"选项复用既有分组端点,本控制器不得再挂 /supervisors 第二份选项源");
}
@Test
void 控制器导出走同一鉴权正门并回Excel响应() {
AppearancePatentCleanItemMapper mapper = mock(AppearancePatentCleanItemMapper.class);
AppearancePatentCleanItemQueryService queryService = mock(AppearancePatentCleanItemQueryService.class);
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
PermissionMenuService permissionMenuService = mock(PermissionMenuService.class);
DataReadScope dataReadScope = mock(DataReadScope.class);
AdminAppearancePatentCleanItemsController controller = new AdminAppearancePatentCleanItemsController(
queryService, authSupport, permissionMenuService, dataReadScope);
AdminUserEntity operator = new AdminUserEntity();
operator.setId(7L);
HttpServletRequest request = mock(HttpServletRequest.class);
when(authSupport.requireUser(request)).thenReturn(operator);
when(authSupport.currentRole(operator)).thenReturn("super_admin");
when(dataReadScope.readCompanyIdOrOwn(operator, 5L)).thenReturn(5L);
byte[] payload = "xlsx-bytes".getBytes(StandardCharsets.UTF_8);
when(queryService.export(null, null, null, null, null, null, 5L)).thenReturn(payload);
ResponseEntity<byte[]> response = controller.export(null, null, null, null, null, null, 5L, request);
verify(dataReadScope).readCompanyIdOrOwn(operator, 5L);
verify(queryService).export(null, null, null, null, null, null, 5L);
assertEquals(MediaType.parseMediaType(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
response.getHeaders().getContentType(), "导出必须是 xlsx Content-Type(浏览器按扩展名/类型落盘)");
assertTrue(response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION).contains("filename*=UTF-8''"),
"中文文件名必须带 filename* UTF-8 轨(DownloadHeaderUtil 双轨口径)");
assertArrayEquals(payload, response.getBody(), "响应体必须原样是服务侧产出的字节");
}
@Test
void 导出超上限按业务错误拒绝且不取数() {
AppearancePatentCleanItemMapper mapper = mock(AppearancePatentCleanItemMapper.class);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(
mapper, mock(AdminUserMapper.class), mock(ShopManageGroupMapper.class));
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any(), any()))
.thenReturn(AppearancePatentCleanItemQueryService.EXPORT_MAX_ROWS + 1);
BusinessException ex = assertThrows(BusinessException.class,
() -> service.export(null, null, null, null, null, null, 5L));
assertTrue(ex.getMessage().contains("超过导出上限"),
"超上限必须给「缩小筛选范围」的可读文案,不能让用户等一次注定 OOM 的全量查询");
verify(mapper, never()).selectCleanItemPage(any(), any(), any(), any(), any(), any(), any(),
anyLong(), anyInt());
}
@Test
void 分页行补所属主管按角色分档() {
AppearancePatentCleanItemMapper mapper = mock(AppearancePatentCleanItemMapper.class);
AdminUserMapper adminUserMapper = mock(AdminUserMapper.class);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(
mapper, adminUserMapper, mock(ShopManageGroupMapper.class));
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any(), any())).thenReturn(3L);
when(mapper.selectCleanItemPage(any(), any(), any(), any(), any(), any(), any(), anyLong(), anyInt()))
.thenReturn(List.of(rowEntity(1L, 33L), rowEntity(2L, 44L), rowEntity(3L, 55L)));
// owner 33=普通账户(role 空,创建人 9=主管甲);owner 44=admin 本人=主管就是自己;
// owner 55=库里读不到(users 已删)=不猜名字。
AdminUserEntity plainOwner = new AdminUserEntity();
plainOwner.setId(33L);
plainOwner.setUsername("sub-user-1");
plainOwner.setCreatedById(9L);
AdminUserEntity adminOwner = new AdminUserEntity();
adminOwner.setId(44L);
adminOwner.setUsername("leader-b");
adminOwner.setRole("admin");
AdminUserEntity creator = new AdminUserEntity();
creator.setId(9L);
creator.setUsername(" 主管甲 ");
when(adminUserMapper.selectBatchIds(anyCollection())).thenAnswer(invocation -> {
Collection<?> ids = invocation.getArgument(0);
List<AdminUserEntity> out = new ArrayList<>();
for (Object id : ids) {
if (Long.valueOf(33L).equals(id)) out.add(plainOwner);
if (Long.valueOf(44L).equals(id)) out.add(adminOwner);
if (Long.valueOf(9L).equals(id)) out.add(creator);
}
return out;
});
AppearancePatentCleanItemPageVo vo = service.page(1L, 10L, null, null, null, null, null, null, null);
assertEquals("主管甲", vo.getItems().get(0).getSupervisorName(),
"普通账户的主管=创建人(与密钥管理页「所属主管(创建人)」同口径),用户名两侧空白剥掉");
assertEquals("leader-b", vo.getItems().get(1).getSupervisorName(),
"admin 本人的单主管就是自己,不外指创建人(company_super_admin/super_admin 同支)");
assertNull(vo.getItems().get(2).getSupervisorName(),
"owner 用户读不到=不猜名字(铁律 30:读不出≠没有)");
}
@Test
void 导出xlsx行含主管列与国家中文标签() throws Exception {
AppearancePatentCleanItemMapper mapper = mock(AppearancePatentCleanItemMapper.class);
AdminUserMapper adminUserMapper = mock(AdminUserMapper.class);
AppearancePatentCleanItemQueryService service = new AppearancePatentCleanItemQueryService(
mapper, adminUserMapper, mock(ShopManageGroupMapper.class));
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any(), any())).thenReturn(2L);
CleanItemEntity deRow = rowEntity(1L, 33L);
deRow.setCountry("DE");
CleanItemEntity ukRow = rowEntity(2L, 44L);
ukRow.setCountry("UK");
ukRow.setSyncedAt(null);
when(mapper.selectCleanItemPage(any(), any(), any(), any(), any(), any(), any(), eq(0L), anyInt()))
.thenReturn(List.of(deRow, ukRow));
AdminUserEntity plainOwner = new AdminUserEntity();
plainOwner.setId(33L);
plainOwner.setUsername("sub-user-1");
plainOwner.setCreatedById(9L);
AdminUserEntity adminOwner = new AdminUserEntity();
adminOwner.setId(44L);
adminOwner.setUsername("leader-b");
adminOwner.setRole("admin");
AdminUserEntity creator = new AdminUserEntity();
creator.setId(9L);
creator.setUsername("主管甲");
when(adminUserMapper.selectBatchIds(anyCollection())).thenAnswer(invocation -> {
Collection<?> ids = invocation.getArgument(0);
List<AdminUserEntity> out = new ArrayList<>();
for (Object id : ids) {
if (Long.valueOf(33L).equals(id)) out.add(plainOwner);
if (Long.valueOf(44L).equals(id)) out.add(adminOwner);
if (Long.valueOf(9L).equals(id)) out.add(creator);
}
return out;
});
byte[] bytes = service.export(null, null, null, null, null, null, 5L);
try (XSSFWorkbook workbook = new XSSFWorkbook(new ByteArrayInputStream(bytes))) {
Sheet sheet = workbook.getSheetAt(0);
assertEquals(2, sheet.getLastRowNum(), "表头 + 2 行明细,不多不少");
Row header = sheet.getRow(0);
assertEquals("任务号", header.getCell(0).getStringCellValue());
assertEquals("所属主管", header.getCell(8).getStringCellValue(), "主管列插在结论后、同步时间前");
assertEquals("同步时间", header.getCell(9).getStringCellValue());
Row row1 = sheet.getRow(1);
assertEquals("德国", row1.getCell(2).getStringCellValue(),
"国家列落中文标签(与前端 asin-country.ts 字典同表)");
assertEquals("33179", row1.getCell(0).getStringCellValue(), "首列是任务号(owner 33 是行属用户,不上表)");
assertEquals("主管甲", row1.getCell(8).getStringCellValue(),
"普通账户 owner 的主管=创建人(创建人用户名两批查询后命中)");
Row row2 = sheet.getRow(2);
assertEquals("英国", row2.getCell(2).getStringCellValue());
assertEquals("leader-b", row2.getCell(8).getStringCellValue());
assertEquals("", row2.getCell(9).getStringCellValue(), "syncedAt 为空落空串,不出 'null' 字样");
}
}
/** 导出/分页用例共用的明细行夹具:task 归 owner,值非空便于按列对撞。 */
private static CleanItemEntity rowEntity(long id, long userId) {
CleanItemEntity entity = new CleanItemEntity();
entity.setId(id);
entity.setResultId(900 + id);
entity.setTaskId(33179L);
entity.setUserId(userId);
entity.setCompanyId(5L);
entity.setRowNo((int) id);
entity.setAsin("B0AB8CDEF" + id);
entity.setCountry("DE");
entity.setSku("SKU-" + id);
entity.setBrand("Anker");
entity.setPrice("29.99");
entity.setTitle("无侵权商品 " + id);
entity.setConclusion("无侵权");
entity.setSourceFilename("list.xlsx");
entity.setSyncedAt(LocalDateTime.of(2026, 11, 11, 10, 30));
return entity;
}
private static Path findCleanItemMapperXml() throws IOException {
@@ -71,7 +71,9 @@ class AppearancePatentCleanSyncRoundTripTest {
AppearancePatentCleanSyncService sync = new AppearancePatentCleanSyncService(
cleanItemMapper, taskChunkMapper, adminUserMapper, json, storage,
mock(org.springframework.transaction.PlatformTransactionManager.class));
AppearancePatentCleanItemQueryService query = new AppearancePatentCleanItemQueryService(cleanItemMapper);
AppearancePatentCleanItemQueryService query = new AppearancePatentCleanItemQueryService(
cleanItemMapper, adminUserMapper,
mock(com.nanri.aiimage.common.mapper.ShopManageGroupMapper.class));
FileTaskEntity task = task(33001L, 77L);
// ⓪ 出表未成功(url 未回填):不写任何行,后管查不到
@@ -84,7 +86,7 @@ class AppearancePatentCleanSyncRoundTripTest {
assertEquals(0, table.rows.get(0).getRowNo(), "row_no 按 payload 全行序号计(含被过滤行)");
// ② 后管同一查询入口立即可见(超管全量),行值与 payload 一致(AC-1)、synced_at 倒序
AppearancePatentCleanItemPageVo vo = query.page(null, null, null, null, null, null, null, null);
AppearancePatentCleanItemPageVo vo = query.page(null, null, null, null, null, null, null, null, null);
assertEquals(2L, vo.getTotal(), "后管全量 total=2");
assertEquals("B0CLEAN002", vo.getItems().get(0).getAsin(), "synced_at 倒序=后同步的在前");
assertEquals("英国", vo.getItems().get(0).getCountry());
@@ -95,8 +97,8 @@ class AppearancePatentCleanSyncRoundTripTest {
assertEquals(7L, vo.getItems().get(0).getCompanyId(), "company_id=任务 owner 公司(物化列)");
// ③ 分公司隔离:本公司(7)看得见,别家公司(9)看不见
assertEquals(2L, query.page(null, null, null, null, null, null, null, 7L).getTotal());
assertEquals(0L, query.page(null, null, null, null, null, null, null, 9L).getTotal(),
assertEquals(2L, query.page(null, null, null, null, null, null, null, null, 7L).getTotal());
assertEquals(0L, query.page(null, null, null, null, null, null, null, null, 9L).getTotal(),
"别的公司查不到本公司同步行");
// ④ 补检重新出表 → 覆盖式重写:首行翻成侵权后全库只剩 1 行(uk(result_id,row_no) 零重复)
@@ -104,20 +106,20 @@ class AppearancePatentCleanSyncRoundTripTest {
assertEquals(1, table.rows.size(), "覆盖式重写后只剩仍满足严格口径的 1 行");
assertEquals(1, table.rows.get(0).getRowNo());
assertEquals("B0CLEAN002", table.rows.get(0).getAsin());
assertEquals(1L, query.page(null, null, null, null, null, null, null, null).getTotal(),
assertEquals(1L, query.page(null, null, null, null, null, null, null, null, null).getTotal(),
"重出后后管立即可见新结果面");
// ⑤ 后管筛选面与 XML 同语义:任务号 eq、ASIN 前缀、同步日期半开区间
assertEquals(1L, query.page(null, null, 33001L, null, null, null, null, null).getTotal());
assertEquals(0L, query.page(null, null, 99999L, null, null, null, null, null).getTotal());
assertEquals(1L, query.page(null, null, null, "B0CLEAN", null, null, null, null).getTotal(),
assertEquals(1L, query.page(null, null, 33001L, null, null, null, null, null, null).getTotal());
assertEquals(0L, query.page(null, null, 99999L, null, null, null, null, null, null).getTotal());
assertEquals(1L, query.page(null, null, null, "B0CLEAN", null, null, null, null, null).getTotal(),
"ASIN 前缀命中");
assertEquals(0L, query.page(null, null, null, "B0NOPE", null, null, null, null).getTotal());
assertEquals(0L, query.page(null, null, null, "B0NOPE", null, null, null, null, null).getTotal());
LocalDate syncDay = table.lastSyncedAt.toLocalDate();
assertEquals(1L, query.page(null, null, null, null, null, syncDay, syncDay, null).getTotal(),
assertEquals(1L, query.page(null, null, null, null, null, syncDay, syncDay, null, null).getTotal(),
"同步当天闭区间命中(synced_at < 次日 0 点)");
assertEquals(0L, query.page(null, null, null, null, null,
syncDay.plusDays(1), syncDay.plusDays(1), null).getTotal(),
syncDay.plusDays(1), syncDay.plusDays(1), null, null).getTotal(),
"同步日之后的一天查不到");
assertTrue(vo.getPage() >= 1 && vo.getPageSize() >= 1, "分页归一形状随行");
}
@@ -221,13 +223,13 @@ class AppearancePatentCleanSyncRoundTripTest {
.thenAnswer(inv -> table.deleteByResultId(inv.getArgument(0, Long.class)));
when(mapper.insertBatch(any()))
.thenAnswer(inv -> table.insertBatch(inv.getArgument(0)));
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any()))
when(mapper.countCleanItems(any(), any(), any(), any(), any(), any(), any()))
.thenAnswer(inv -> table.count(inv.getArgument(0), inv.getArgument(1), inv.getArgument(2),
inv.getArgument(3), inv.getArgument(4), inv.getArgument(5)));
when(mapper.selectCleanItemPage(any(), any(), any(), any(), any(), any(), anyLong(), anyInt()))
when(mapper.selectCleanItemPage(any(), any(), any(), any(), any(), any(), any(), anyLong(), anyInt()))
.thenAnswer(inv -> table.page(inv.getArgument(0), inv.getArgument(1), inv.getArgument(2),
inv.getArgument(3), inv.getArgument(4), inv.getArgument(5),
inv.getArgument(6, Long.class), inv.getArgument(7, Integer.class)));
inv.getArgument(7, Long.class), inv.getArgument(8, Integer.class)));
return mapper;
}
@@ -171,13 +171,17 @@ class AppearancePatentChunkRowContractTest {
assertFalse(flagged.isEmpty(), "扫描空跑=判据失效:LlmClient 自身必须命中");
// 结论值唯一来源仍是 LlmClient(NO_INFRINGEMENT 常量);白名单其余条目必须是页面名/文档
// 文案,不得出现第二个"造结论"的出处。新增命中必须在此登记并说明为何不产结论值。
// 2026-11-11 登记 service/AppearancePatentCleanItemQueryService:导出超上限的业务错误文案
// 「无侵权数据共 N 行…」(页面名口径,与控制器 403 文案同族)——不产任何结论值,行集的
// conclusion 原样搬运自 payload(toVo 只搬运不加工)。
assertIterableEquals(List.of(
"client/AppearancePatentLlmClient.java",
"controller/AdminAppearancePatentCleanItemsController.java",
"model/vo/AppearancePatentCleanItemPageVo.java",
"model/vo/AppearancePatentCleanItemVo.java"), new ArrayList<>(flagged),
"「无侵权」字面量只许落在登记白名单(LlmClient=结论值单一来源;控制器/VO 命中全是"
+ "菜单名/Swagger/403 文案);同步/导出/查询侧产结论值一律引用 NO_INFRINGEMENT 常量,"
"model/vo/AppearancePatentCleanItemVo.java",
"service/AppearancePatentCleanItemQueryService.java"), new ArrayList<>(flagged),
"「无侵权」字面量只许落在登记白名单(LlmClient=结论值单一来源;控制器/VO/查询服务命中全是"
+ "菜单名/Swagger/403 文案/导出上限文案);同步/导出/查询侧产结论值一律引用 NO_INFRINGEMENT 常量,"
+ "注释里的举例不算(本判据已剥离注释)");
}