收口(去重库): 五处比对与写入口按 company_id 认公司,不再跨公司去重

读侧(列表/导出)早已按公司收窄,但"去重"这个动作本身一直查全平台:
selectExistingDataValues 是 `WHERE data_value IN (...)`,一个租户条件都没有。
库里唯一键自 V145 起就是 uk_company_data_value(company_id, data_value)——
同值本来就允许归两家,是代码没跟上库的口径。

五处一起收口(第 5 处由编译器抓出,正是 V145 头注释 §1.2 点名的采集侧):
去重执行 ASIN 过滤 / 导入预检"已存在就跳过" / 手工新增与编辑的重名校验
(文案改「本公司已存在该数据」)/ 删除导入(超管不选分组时原先一次删掉全平台
同名行)/ 采集结果过滤(CollectDataBatchQuery 直连 mapper,改为经 dedupe 侧推导)。

超管代导入的归属分裂一并修掉:前端两个导入接口把公司 tab 发下去——原先
group_id 落别家的组、company_id 落超管自己那一家。公司身份一律在请求线程上由
DataReadScope.readCompanyIdOrOwn 解析后传入(导入与去重跑在虚拟线程,那时已无
请求上下文),service 侧 requireScopedCompanyId 只判空不兜底。

删两条零调用者的跨公司通道(selectAllDataValues/listComparableValues 全表读、
deleteFromExcel 两个同步重载)。反转一条反向钉:comparableValueLookupRemainsGlobal
原显式断言"比对不参与公司收窄"(多公司一期的半套遗留),现为 IsScopedToOneCompany。

三把尺子随之改动,均带出处:tsv 基准行搬家(三个标量双双不动)、前端发送口分母
17→18 组/21→22 处、CatchSilenceRatchet 重抛下限 387→386(随死代码离开树那处)。

验证:后端全量 4198 例 0 失败/0 错误/7 跳过;前端 1778 例全绿;变异自证
6/6 全部 KILLED,注入文件按 sha256 还原并复跑绿。
现网只读探针:misfiled=0、同值跨家=0 ⇒ 存量比对结果不变,行为改变只在将来
A 家导入 B 家已有 ASIN 那一刻;两口径差额 1,452 行(0.26%)全属公司 1、
全源于 10-05 账号硬删。
This commit is contained in:
2026-10-09 17:57:37 +08:00
parent 974d0c77d0
commit 5ccf3c896f
22 changed files with 393 additions and 187 deletions
@@ -71,7 +71,10 @@ class CatchSilenceRatchetTest {
// 随死代码一起离开树;实测重抛数 387、不重抛与只 log 两数未涨 ⇒ 没有任何一处"异常会往外走"
// 被改回吞掉。这一格的本意是"重抛只许涨不许跌",所以调基线必须带着这样的逐处出处;
// 拿"总数变小了"当理由不算——那种情况下真正要看的是不重抛那一侧有没有涨。
static final int RETHROW_FLOOR = 387;
// 10-09 再下调 387 -> 386,出处点名:去重库按公司收口那一笔删掉了 DedupeTotalDataService.deleteFromExcel
// 的两个零调用者重载,其中 `throw new BusinessException("删除 Excel 匹配数据失败", e)` 是 RETHROW 形状,
// 随死代码一起离开树;实测重抛 386、不重抛与只 log 两数未涨(那两格仍绿)=> 没有任何一处「异常会往外走」被改回吞掉。
static final int RETHROW_FLOOR = 386;
static final int STRICT_NOT_RETHROW = 559;
static final int STRICT_LOG_ONLY = 172; // 严格口径:块内每条顶层语句都是 log 调用才算
static final int STRICT_EMPTY = 56;
@@ -119,7 +119,7 @@ class CollectDataBrandRecheckInjectorTest {
String payload = new ObjectMapper().writeValueAsString(List.of(row("B01", "Nike"), row("B02", "Nike")));
when(transientPayloadStorageService.resolvePayload(eq("ptr1"), anyString())).thenReturn(payload);
CollectDataResultRowVo kept = row("B01", "Nike");
when(collectDataBatchQuery.filter(anyList())).thenReturn(
when(collectDataBatchQuery.filter(anyList(), any())).thenReturn(
new CollectDataBatchQuery.FilterResult(List.of(kept), 1, 0));
when(transientPayloadStorageService.storeResultPayload(eq("COLLECT_DATA"), eq(100L), eq("hash1"),
eq("recheck-chunk-1"), anyString())).thenReturn("ptr2");
@@ -179,7 +179,7 @@ class CollectDataBrandRecheckInjectorTest {
when(transientPayloadStorageService.resolvePayload(eq("ptr1"), anyString())).thenReturn(
"[{\"asin\":\"B01\",\"brand\":\"Nike\"}]");
// 补测窗口内该 ASIN 已被其他任务采走:复查全滤掉
when(collectDataBatchQuery.filter(anyList())).thenReturn(
when(collectDataBatchQuery.filter(anyList(), any())).thenReturn(
new CollectDataBatchQuery.FilterResult(List.of(), 1, 0));
GroupOutcome outcome = injector.apply(task(100L), Map.of("nike", RecheckVerdict.CLEAN));
@@ -145,7 +145,7 @@ class CollectDataServiceTxBoundaryTest {
}).when(taskChunkMapper).insert(any(TaskChunkEntity.class));
CollectDataResultRowVo row = new CollectDataResultRowVo();
row.setAsin("B0COLLECT1");
lenient().when(collectDataBatchQuery.filter(any())).thenReturn(
lenient().when(collectDataBatchQuery.filter(any(), any())).thenReturn(
new CollectDataBatchQuery.FilterResult(List.of(row), 0, 0));
lenient().when(brandBatchFilter.filter(any())).thenReturn(
new CollectDataBrandBatchFilter.BrandBatchOutcome(List.of(), List.of(), List.of(row)));
@@ -1,7 +1,7 @@
package com.nanri.aiimage.modules.collectdata.util;
import com.nanri.aiimage.modules.collectdata.model.vo.CollectDataResultRowVo;
import com.nanri.aiimage.modules.dedupe.mapper.DedupeTotalDataMapper;
import com.nanri.aiimage.modules.dedupe.service.DedupeTotalDataService;
import com.nanri.aiimage.modules.invalidasin.mapper.InvalidAsinDataMapper;
import com.nanri.aiimage.modules.invalidasin.model.entity.InvalidAsinDataEntity;
import org.junit.jupiter.api.BeforeEach;
@@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -28,25 +29,36 @@ import static org.mockito.Mockito.when;
*/
class CollectDataBatchQueryTest {
private DedupeTotalDataMapper dedupeMapper;
/** 采集任务归属账号:比对集由它推导,「推不出公司就不比对」那条闸门钉在 dedupe 侧的用例里。 */
private static final Long OWNER_USER_ID = 7L;
private DedupeTotalDataService dedupeService;
private InvalidAsinDataMapper invalidAsinMapper;
private CollectDataBatchQuery query;
@BeforeEach
void setUp() {
dedupeMapper = mock(DedupeTotalDataMapper.class);
dedupeService = mock(DedupeTotalDataService.class);
invalidAsinMapper = mock(InvalidAsinDataMapper.class);
query = new CollectDataBatchQuery(dedupeMapper, invalidAsinMapper);
query = new CollectDataBatchQuery(dedupeService, invalidAsinMapper);
}
/**
* 生产入口已改成 {@code filter(rows, ownerUserId)};本类逐支测的是过滤语义,
* 公司归属由这一层统一喂,不逐支重复——归属本身由下面那两支新用例专钉。
*/
private List<CollectDataResultRowVo> filterByExistingAsin(List<CollectDataResultRowVo> rows) {
return query.filter(rows, OWNER_USER_ID).kept();
}
@Test
void test_task_045_asin_brand_normal_default_path() {
// 正常输入:去重表已存在的 ASIN 被过滤,无效品牌表中的品牌被过滤,其余保留。
when(dedupeMapper.selectExistingDataValues(anyList()))
.thenReturn(List.of("B000000001"));
when(dedupeService.findExistingComparableValuesForOwner(anyList(), any()))
.thenReturn(Set.of("B000000001"));
when(invalidAsinMapper.selectList(any())).thenReturn(List.of(entity("Zara")));
List<CollectDataResultRowVo> out = query.filterByExistingAsin(List.of(
List<CollectDataResultRowVo> out = filterByExistingAsin(List.of(
row("B000000001", "Nike"),
row("B000000002", "Zara"),
row("B000000003", "H&M")
@@ -68,14 +80,14 @@ class CollectDataBatchQueryTest {
for (int i = 0; i < 300; i++) {
rows.add(row("B" + String.format("%09d", i + 1), "bad"));
}
List<String> existing = new ArrayList<>();
Set<String> existing = new java.util.HashSet<>();
for (int i = 0; i < 100; i++) {
existing.add("D" + String.format("%09d", i + 1));
}
when(dedupeMapper.selectExistingDataValues(anyList())).thenReturn(existing);
when(dedupeService.findExistingComparableValuesForOwner(anyList(), any())).thenReturn(existing);
when(invalidAsinMapper.selectList(any())).thenReturn(List.of(entity("bad")));
List<CollectDataResultRowVo> out = query.filterByExistingAsin(rows);
List<CollectDataResultRowVo> out = filterByExistingAsin(rows);
assertEquals(200, out.size(), "100 行去重 + 300 行无效品牌 = 过滤 400 行");
assertEquals("D000000101", out.get(0).getAsin(), "输出首行保持输入顺序");
@@ -87,51 +99,51 @@ class CollectDataBatchQueryTest {
@Test
void test_task_045_asin_brand_normal_repeated_operation_is_idempotent() {
// 幂等:同一输入重复执行输出一致,两次都发批量查询。
when(dedupeMapper.selectExistingDataValues(anyList()))
.thenReturn(List.of("B000000001"));
when(dedupeService.findExistingComparableValuesForOwner(anyList(), any()))
.thenReturn(Set.of("B000000001"));
when(invalidAsinMapper.selectList(any())).thenReturn(List.of(entity("bad")));
List<CollectDataResultRowVo> rows = List.of(
row("B000000001", "bad"),
row("B000000002", "good")
);
List<CollectDataResultRowVo> first = query.filterByExistingAsin(rows);
List<CollectDataResultRowVo> second = query.filterByExistingAsin(rows);
List<CollectDataResultRowVo> first = filterByExistingAsin(rows);
List<CollectDataResultRowVo> second = filterByExistingAsin(rows);
assertEquals(first, second, "重复执行输出一致");
assertEquals(1, first.size());
verify(dedupeMapper, times(2)).selectExistingDataValues(anyList());
verify(dedupeService, times(2)).findExistingComparableValuesForOwner(anyList(), any());
verify(invalidAsinMapper, times(2)).selectList(any());
}
@Test
void test_task_045_asin_brand_boundary_empty_input() {
// 空输入:空列表/无 ASIN 无品牌的行集合不触发数据库查询,安全跳过。
List<CollectDataResultRowVo> empty = query.filterByExistingAsin(List.of());
List<CollectDataResultRowVo> empty = filterByExistingAsin(List.of());
assertEquals(0, empty.size(), "空列表返回空");
List<CollectDataResultRowVo> blankRows = query.filterByExistingAsin(List.of(
List<CollectDataResultRowVo> blankRows = filterByExistingAsin(List.of(
row("", ""), row(null, null)
));
assertEquals(2, blankRows.size(), "无 ASIN 无品牌行不触发查询且全保留");
verify(dedupeMapper, never()).selectExistingDataValues(anyList());
verify(dedupeService, never()).findExistingComparableValuesForOwner(anyList(), any());
verify(invalidAsinMapper, never()).selectList(any());
}
@Test
void test_task_045_asin_brand_boundary_single_item() {
// 单元素:单行命中/未命中均正确,不依赖批量路径。
when(dedupeMapper.selectExistingDataValues(anyList())).thenReturn(List.of());
when(dedupeService.findExistingComparableValuesForOwner(anyList(), any())).thenReturn(Set.of());
when(invalidAsinMapper.selectList(any())).thenReturn(List.of());
List<CollectDataResultRowVo> out = query.filterByExistingAsin(List.of(
List<CollectDataResultRowVo> out = filterByExistingAsin(List.of(
row("B000000001", "solo")
));
assertEquals(1, out.size(), "单行未命中保留");
assertEquals("solo", out.get(0).getBrand());
when(dedupeMapper.selectExistingDataValues(anyList())).thenReturn(List.of("B000000001"));
List<CollectDataResultRowVo> hit = query.filterByExistingAsin(List.of(
when(dedupeService.findExistingComparableValuesForOwner(anyList(), any())).thenReturn(Set.of("B000000001"));
List<CollectDataResultRowVo> hit = filterByExistingAsin(List.of(
row("B000000001", "solo")
));
assertEquals(0, hit.size(), "单行命中去重表被过滤");
@@ -144,26 +156,26 @@ class CollectDataBatchQueryTest {
for (int i = 0; i < 5000; i++) {
rows.add(row("B" + String.format("%09d", i), "brand-" + i));
}
when(dedupeMapper.selectExistingDataValues(anyList())).thenReturn(List.of());
when(dedupeService.findExistingComparableValuesForOwner(anyList(), any())).thenReturn(Set.of());
when(invalidAsinMapper.selectList(any())).thenReturn(List.of());
List<CollectDataResultRowVo> out = query.filterByExistingAsin(rows);
List<CollectDataResultRowVo> out = filterByExistingAsin(rows);
assertEquals(5000, out.size(), "超大集合全量保留");
verify(dedupeMapper).selectExistingDataValues(anyList());
verify(dedupeService).findExistingComparableValuesForOwner(anyList(), any());
verify(invalidAsinMapper).selectList(any());
}
@Test
void test_task_045_asin_brand_invalid_input_rejected() {
// 非法参数:null 行安全跳过;数据库返回 null 集合安全处理。
when(dedupeMapper.selectExistingDataValues(anyList())).thenReturn(null);
when(dedupeService.findExistingComparableValuesForOwner(anyList(), any())).thenReturn(null);
when(invalidAsinMapper.selectList(any())).thenReturn(null);
List<CollectDataResultRowVo> rows = new ArrayList<>();
rows.add(null);
rows.add(row("B000000001", " Nike "));
List<CollectDataResultRowVo> out = query.filterByExistingAsin(rows);
List<CollectDataResultRowVo> out = filterByExistingAsin(rows);
assertEquals(1, out.size(), "null 行不丢弃");
assertEquals("B000000001", out.get(0).getAsin(), "正常行保留");
@@ -173,26 +185,39 @@ class CollectDataBatchQueryTest {
@Test
void test_task_045_asin_brand_dependency_failure_releases_resources() {
// 依赖失败:去重表查询抛错时异常向上传播;恢复后再次执行成功,不残留状态。
when(dedupeMapper.selectExistingDataValues(anyList()))
when(dedupeService.findExistingComparableValuesForOwner(anyList(), any()))
.thenThrow(new RuntimeException("db down"))
.thenReturn(List.of("B000000001"));
.thenReturn(Set.of("B000000001"));
when(invalidAsinMapper.selectList(any())).thenReturn(List.of());
boolean threw = false;
try {
query.filterByExistingAsin(List.of(row("B000000001", "nike")));
filterByExistingAsin(List.of(row("B000000001", "nike")));
} catch (RuntimeException ex) {
threw = "db down".equals(ex.getMessage());
}
assertTrue(threw, "去重查询失败向上抛错");
List<CollectDataResultRowVo> out = query.filterByExistingAsin(List.of(
List<CollectDataResultRowVo> out = filterByExistingAsin(List.of(
row("B000000001", "nike"), row("B000000002", "adidas")
));
assertEquals(1, out.size(), "恢复后过滤正确");
assertEquals("B000000002", out.get(0).getAsin(), "恢复后命中逻辑仍生效");
}
/**
* 采集结果按去重库过滤时,比对集必须由「任务归属账号」那一家推导。
* 这一支钉的是参数真的传下去了:换成常量或漏传,下面这条 eq 就红。
*/
@Test
void comparisonSetIsRequestedForTheTaskOwnerAccount() {
when(dedupeService.findExistingComparableValuesForOwner(anyList(), any())).thenReturn(Set.of());
filterByExistingAsin(List.of(row("B000000001", "nike")));
verify(dedupeService).findExistingComparableValuesForOwner(List.of("B000000001"), OWNER_USER_ID);
}
private static CollectDataResultRowVo row(String asin, String brand) {
CollectDataResultRowVo row = new CollectDataResultRowVo();
row.setAsin(asin);
@@ -15,7 +15,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* {@code insertBatchIgnore} 生成 SQL 的审计:去重库写侧必须显式带 {@code company_id}。
* 去重库两条自定义语句的 SQL 审计:写侧 {@code insertBatchIgnore} 必须显式落 {@code company_id},
* 比对侧 {@code selectExistingDataValues} 必须按 {@code company_id} 收窄。
*
* <p>为什么必须单独有一支:服务层测试用 mock 捕获的是实体对象,看不到最终 SQL——本表写侧
* 靠的是注解里的列清单(不是 MyBatis-Plus 的实体字段推导),把 {@code company_id} 从清单里漏掉
@@ -43,6 +44,31 @@ class DedupeTotalDataMapperSqlTest {
assertFalse(sql.contains("$"), "不得出现 ${} 拼接面: " + sql);
}
/**
* 比对集的租户谓词审计。断的是最终 SQL 而不是方法签名:这条从 @SelectProvider 迁到 XML 之后,
* 少写一个 {@code AND company_id = #{companyId}} 在 Java 侧照样编译通过、照样返回行,
* 而后果是「别家导入的 ASIN 被当成已存在」——去重执行少行、导入静默跳过。
*/
@Test
void selectExistingValuesAreScopedToOneCompany() throws Exception {
Configuration configuration = MapperXmlSupport.configurationWith(DedupeTotalDataMapper.class);
MappedStatement statement = configuration.getMappedStatement(
DedupeTotalDataMapper.class.getName() + ".selectExistingDataValues");
BoundSql boundSql = statement.getBoundSql(java.util.Map.of(
"values", List.of("B000000001", "B000000002"), "companyId", 3L));
String sql = boundSql.getSql().replaceAll("\\s+", " ");
assertTrue(sql.contains("WHERE company_id = ?"),
"比对集必须按 company_id 收窄,否则一次去重会看见全平台: " + sql);
assertTrue(sql.contains("data_value IN ("),
"值集合仍按 IN 批量比对(收窄不该把它拆成逐行查): " + sql);
assertEquals(3, boundSql.getParameterMappings().size(),
"2 个值 + 1 个公司 = 3 个占位符: " + sql);
assertEquals("companyId", boundSql.getParameterMappings().get(0).getProperty(),
"第一个占位符就是公司(谓词排在 IN 之前,走 uk_company_data_value 前缀): " + sql);
assertFalse(sql.contains("$"), "不得出现 ${} 拼接面: " + sql);
}
private static BoundSql boundSqlFor(int rowCount) throws Exception {
// SQL 已从注解迁到 mapper XML(档 B 口径),原文与语句都要从 XML 那侧取
String script = MapperXmlSupport.statementBody(DedupeTotalDataMapper.class, "insertBatchIgnore");
@@ -69,6 +69,8 @@ class DedupeRunProgressResumeTest {
@Mock
private DedupeTotalDataService dedupeTotalDataService;
@Mock
private com.nanri.aiimage.common.service.CompanyScopeSupport companyScopeSupport;
@Mock
private StringRedisTemplate redisTemplate;
@Mock
private ValueOperations<String, String> valueOperations;
@@ -102,7 +104,7 @@ class DedupeRunProgressResumeTest {
return true;
});
return new DedupeRunService(fileTaskMapper, fileResultMapper, storageProperties,
ossStorageService, dedupeTotalDataService, redisTemplate, objectMapper,
ossStorageService, dedupeTotalDataService, companyScopeSupport, redisTemplate, objectMapper,
new InstanceMetadata("server-110"));
}
@@ -425,7 +427,7 @@ class DedupeRunProgressResumeTest {
// 异步 worker 是否来得及读这个目录取决于线程时序,只做兜底、不参与断言
lenient().when(storageProperties.getLocalTempDir()).thenReturn("D:/definitely-not-a-real-dedupe-dir/xx");
DedupeRunService service = new DedupeRunService(fileTaskMapper, fileResultMapper, storageProperties,
ossStorageService, dedupeTotalDataService, redisTemplate, objectMapper,
ossStorageService, dedupeTotalDataService, companyScopeSupport, redisTemplate, objectMapper,
new InstanceMetadata("server-110"));
DedupeRunProgressVo submitted = service.submitRun(request(1));
@@ -25,6 +25,8 @@ import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
@@ -144,6 +146,20 @@ class DedupeRunServiceCleanRulesTest {
return rows.stream().map(DedupeCandidateRow::asinValue).toList();
}
/**
* 去重执行拿哪一家的库比对:提交人自己那一家。
* 摘掉 DedupeRunService 里往下传的 companyId,这支会红(比对收到的是 null 或别的值)。
*/
@Test
void comparisonSetIsTakenForTheSubmittingAccountCompanyOnly(@TempDir Path tempDir) throws Exception {
runClean(tempDir, List.<String[]>of(
new String[]{"1", "B0AAAAAA01", "英国", "10.00", "FBM", "品牌A", "在售", "2026-09-01"},
new String[]{"1_2", "B0AAAAAA02", "英国", "11.00", "FBM", "品牌A", "在售", "2026-09-01"}),
List.of("id", "ASIN", "国家", "价格", "品牌"));
verify(dedupeTotalDataService).findExistingComparableValues(any(), eq(1L));
}
private File runClean(Path tempDir, List<String[]> rows, List<String> selectedColumns) throws Exception {
File input = tempDir.resolve("input.xlsx").toFile();
File output = tempDir.resolve("output.xlsx").toFile();
@@ -151,8 +167,8 @@ class DedupeRunServiceCleanRulesTest {
DedupeRunService service = new DedupeRunService(
fileTaskMapper, fileResultMapper, storageProperties, ossStorageService, dedupeTotalDataService,
null, null, new com.nanri.aiimage.config.InstanceMetadata("test-instance"));
when(dedupeTotalDataService.findExistingComparableValues(any())).thenReturn(Set.of());
null, null, null, new com.nanri.aiimage.config.InstanceMetadata("test-instance"));
when(dedupeTotalDataService.findExistingComparableValues(any(), any())).thenReturn(Set.of());
when(dedupeTotalDataService.normalizeComparableValueOrBlank(any()))
.thenAnswer(invocation -> {
String value = invocation.getArgument(0);
@@ -160,7 +176,7 @@ class DedupeRunServiceCleanRulesTest {
});
ReflectionTestUtils.invokeMethod(service, "cleanExcelByStreamRules",
input, output, selectedColumns, false, true, true);
input, output, selectedColumns, false, true, true, 1L);
return output;
}
@@ -44,6 +44,7 @@ import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -77,7 +78,6 @@ class DedupeTotalDataServiceTest {
void createRecordsUploaderIdentity() {
AdminUserEntity uploader = user(23L, "normal", "member-a");
when(adminUserMapper.selectById(23L)).thenReturn(uploader);
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class))).thenReturn(1L);
stubWritableGroup(23L, 7L);
when(dedupeTotalDataMapper.selectOne(any())).thenReturn(null);
when(dedupeTotalDataMapper.insert(any(DedupeTotalDataEntity.class))).thenAnswer(invocation -> {
@@ -98,7 +98,7 @@ class DedupeTotalDataServiceTest {
DedupeTotalDataCreateRequest request = new DedupeTotalDataCreateRequest();
request.setDataValue(" b012345678 ");
request.setGroupId(7L);
DedupeTotalDataItemVo item = service.create(request, 23L);
DedupeTotalDataItemVo item = service.create(request, 1L, 23L);
ArgumentCaptor<DedupeTotalDataEntity> captor = ArgumentCaptor.forClass(DedupeTotalDataEntity.class);
verify(dedupeTotalDataMapper).insert(captor.capture());
@@ -184,13 +184,12 @@ class DedupeTotalDataServiceTest {
@Test
void duplicateExcelValueKeepsOriginalUploader() throws Exception {
when(adminUserMapper.selectById(23L)).thenReturn(user(23L, "normal", "member-a"));
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class))).thenReturn(1L);
stubWritableGroup(23L, 7L);
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678")))
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"), 1L))
.thenReturn(List.of("B012345678"));
MockMultipartFile file = asinWorkbook("B012345678");
var result = service.importFromExcel(file, 7L, 23L);
var result = service.importFromExcel(file, 7L, 1L, 23L);
assertEquals(0, result.getInsertedCount());
assertEquals(1, result.getSkippedCount());
@@ -199,17 +198,15 @@ class DedupeTotalDataServiceTest {
@Test
void importRejectsOperatorWithoutCompany() throws Exception {
AdminUserEntity noCompany = user(23L, "normal", "member-a");
noCompany.setCompanyId(null);
when(adminUserMapper.selectById(23L)).thenReturn(noCompany);
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class)))
.thenThrow(new BusinessException(403, "当前账号没有所属公司,无法按公司读写数据"));
when(adminUserMapper.selectById(23L)).thenReturn(user(23L, "normal", "member-a"));
// 公司身份由 controller 在请求线程上算好传进来(超管认 tab、非超管认自己那一家);
// service 这道闸门钉的是「没算出来就不许写」,它是最后那道。
BusinessException exception = assertThrows(
BusinessException.class,
() -> service.importFromExcel(asinWorkbook("B012345678"), 7L, 23L));
() -> service.importFromExcel(asinWorkbook("B012345678"), 7L, null, 23L));
assertEquals("当前账号没有所属公司,无法按公司读写数据", exception.getMessage());
assertEquals("缺少归属公司:请先在页面上方选择公司,再操作去重数据库", exception.getMessage());
verify(dedupeTotalDataMapper, never()).insertBatchIgnore(any());
verify(dedupeTotalDataMapper, never()).insert(any(DedupeTotalDataEntity.class));
}
@@ -217,13 +214,12 @@ class DedupeTotalDataServiceTest {
@Test
void importReadsAndNormalizesCountryColumn() throws Exception {
when(adminUserMapper.selectById(23L)).thenReturn(user(23L, "normal", "member-a"));
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class))).thenReturn(1L);
stubWritableGroup(23L, 7L);
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"))).thenReturn(List.of());
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"), 1L)).thenReturn(List.of());
when(dedupeTotalDataMapper.insertBatchIgnore(any())).thenReturn(1);
MockMultipartFile file = asinCountryWorkbook("B012345678", "德国, US,United Kingdom");
var result = service.importFromExcel(file, 7L, 23L);
var result = service.importFromExcel(file, 7L, 1L, 23L);
assertEquals(1, result.getInsertedCount());
ArgumentCaptor<List<DedupeTotalDataEntity>> captor = ArgumentCaptor.forClass(List.class);
@@ -237,12 +233,11 @@ class DedupeTotalDataServiceTest {
@Test
void importKeepsCountryBlankWhenHeaderMissing() throws Exception {
when(adminUserMapper.selectById(23L)).thenReturn(user(23L, "normal", "member-a"));
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class))).thenReturn(1L);
stubWritableGroup(23L, 7L);
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"))).thenReturn(List.of());
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"), 1L)).thenReturn(List.of());
when(dedupeTotalDataMapper.insertBatchIgnore(any())).thenReturn(1);
var result = service.importFromExcel(asinWorkbook("B012345678"), 7L, 23L);
var result = service.importFromExcel(asinWorkbook("B012345678"), 7L, 1L, 23L);
assertEquals(1, result.getInsertedCount());
ArgumentCaptor<List<DedupeTotalDataEntity>> captor = ArgumentCaptor.forClass(List.class);
@@ -274,17 +269,16 @@ class DedupeTotalDataServiceTest {
@Test
void concurrentDuplicateDuringImportIsSkipped() throws Exception {
when(adminUserMapper.selectById(23L)).thenReturn(user(23L, "normal", "member-a"));
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class))).thenReturn(1L);
stubWritableGroup(23L, 7L);
when(dedupeTotalDataMapper.selectOne(any())).thenReturn(null);
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"))).thenReturn(List.of());
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"), 1L)).thenReturn(List.of());
when(transactionManager.getTransaction(any())).thenReturn(transactionStatus);
when(dedupeTotalDataMapper.insertBatchIgnore(any()))
.thenThrow(new DuplicateKeyException("duplicate batch"));
when(dedupeTotalDataMapper.insert(any(DedupeTotalDataEntity.class)))
.thenThrow(new DuplicateKeyException("duplicate"));
var result = service.importFromExcel(asinWorkbook("b012345678"), 7L, 23L);
var result = service.importFromExcel(asinWorkbook("b012345678"), 7L, 1L, 23L);
assertEquals(0, result.getInsertedCount());
assertEquals(1, result.getSkippedCount());
@@ -360,18 +354,83 @@ class DedupeTotalDataServiceTest {
verify(dedupeTotalDataMapper, never()).selectCount(any());
}
/**
* 比对集只认一家。这条曾经叫 comparableValueLookupRemainsGlobal,显式钉着「不参与公司收窄」
* (2026-10 多公司一期)——那是半套发布的遗留:库里 uk_company_data_value 自 V145 起就允许
* 同值归两家,代码却仍拿全平台当比对集。2026-10-09 按控制方定案反转。
*/
@Test
void comparableValueLookupRemainsGlobal() {
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678")))
void comparableValueLookupIsScopedToOneCompany() {
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"), 1L))
.thenReturn(List.of("b012345678"));
Set<String> values = service.findExistingComparableValues(List.of(" b012345678 "));
Set<String> values = service.findExistingComparableValues(List.of(" b012345678 "), 1L);
assertEquals(Set.of("B012345678"), values);
verify(adminUserMapper, never()).selectById(any(Long.class));
verify(shopManageGroupMapper, never()).selectManagedMemberUserIds(any(Long.class));
}
/**
* 删除导入的谓词必须带公司,且**对平台超管也一样**:allUsers 那一站原本只按分组裁,
* 超管不选分组时删除条件只剩一个 data_value IN——同名 ASIN 在几家库里就删几次。
* 这条走真 wrapper(不是 mock 的返回值):MP 的 IN 段要先把 getSqlSegment 求值一次才铺出来。
*/
@Test
void deleteImportNeverReachesAnotherCompanyRows() {
when(transactionManager.getTransaction(any())).thenReturn(transactionStatus);
when(dedupeTotalDataMapper.delete(any())).thenReturn(1);
service.deleteBatchByDataValues(List.of("B012345678"),
new DedupeTotalDataService.AccessScope(true, java.util.Set.of(), java.util.Set.of(), false),
null, 3L);
ArgumentCaptor<LambdaQueryWrapper<DedupeTotalDataEntity>> captor =
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(dedupeTotalDataMapper).delete(captor.capture());
LambdaQueryWrapper<DedupeTotalDataEntity> wrapper = captor.getValue();
wrapper.getSqlSegment();
assertThat(wrapper.getSqlSegment()).contains("company_id");
assertThat(wrapper.getParamNameValuePairs().values()).contains(3L);
}
/**
* 「数据已存在」只在本公司内成立。这条钉的是重名查询的 wrapper:摘掉 company_id 那一刀,
* B 家导入过的 ASIN 会让 A 家的「新增」报已存在,而 A 家库里根本没有那行。
*/
@Test
void createChecksDuplicatesInsideOneCompanyOnly() {
when(adminUserMapper.selectById(23L)).thenReturn(user(23L, "normal", "member-a"));
stubWritableGroup(23L, 7L);
when(dedupeTotalDataMapper.selectOne(any())).thenReturn(null);
when(dedupeTotalDataMapper.insert(any(DedupeTotalDataEntity.class))).thenAnswer(invocation -> {
DedupeTotalDataEntity entity = invocation.getArgument(0);
entity.setId(91L);
return 1;
});
when(dedupeTotalDataMapper.selectById(91L)).thenReturn(data(91L, 23L));
DedupeTotalDataCreateRequest request = new DedupeTotalDataCreateRequest();
request.setDataValue("B012345678");
request.setGroupId(7L);
service.create(request, 3L, 23L);
ArgumentCaptor<LambdaQueryWrapper<DedupeTotalDataEntity>> captor =
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(dedupeTotalDataMapper).selectOne(captor.capture());
captor.getValue().getSqlSegment();
assertThat(captor.getValue().getSqlSegment()).contains("company_id");
assertThat(captor.getValue().getParamNameValuePairs().values()).contains(3L);
}
/** 没有归属公司就不比对:宁可炸,也不给出一条"比对集是空的"与"一家都没命中"同形的路径。 */
@Test
void comparableValueLookupRefusesACompanylessComparison() {
assertThrows(BusinessException.class,
() -> service.findExistingComparableValues(List.of("B012345678"), null));
verify(dedupeTotalDataMapper, never()).selectExistingDataValues(any(), any());
}
@Test
void exportUsesScopedUsernameAndProducesWorkbook() throws Exception {
when(adminUserMapper.selectById(10L)).thenReturn(user(10L, "admin", "leader"));
@@ -3,7 +3,7 @@ AdminUserMapper selectCompanySuperAdminEarliestExpiry Select PURE SELECT MIN(exp
BrandCrawlTaskMapper selectExpiredTerminalTaskIds Select PURE SELECT id FROM brand_crawl_tasks\nWHERE status IN ('success', 'failed', 'cancelled')\n AND updated_at < #{cutoff}\nORDER BY id ASC\nLIMIT #{batchSize}
CollectDataItemMapper insertBatch Insert PURE <script>\nINSERT INTO biz_collect_data_item\n (task_id, row_index, source_file_key, source_filename, keyword, status_value, extra_json, created_at)\nVALUES\n<foreach collection="rows" item="row" separator=",">\n (#{row.taskId}, #{row.rowIndex}, #{row.sourceFileKey}, #{row.sourceFilename},\n #{row.keyword}, #{row.statusValue}, #{row.extraJson}, #{row.createdAt})\n</foreach>\n</script>
DedupeTotalDataMapper insertBatchIgnore Insert PURE <script>\nINSERT IGNORE INTO biz_dedupe_total_data\n (data_value, country, group_id, uploader_user_id, uploader_username, company_id)\nVALUES\n<foreach collection="rows" item="row" separator=",">\n (#{row.dataValue}, #{row.country}, #{row.groupId}, #{row.uploaderUserId}, #{row.uploaderUsername}, #{row.companyId})\n</foreach>\n</script>
DedupeTotalDataMapper selectAllDataValues Select PURE SELECT data_value FROM biz_dedupe_total_data
DedupeTotalDataMapper selectExistingDataValues Select PURE <script>\nSELECT data_value FROM biz_dedupe_total_data\nWHERE company_id = #{companyId}\n AND data_value IN\n<foreach collection="values" item="value" open="(" separator="," close=")">#{value}</foreach>\n</script>
DeviceLogFileMapper deleteOlderThanBatch Delete PURE DELETE FROM device_log_file\nWHERE log_date < #{cutoff}\nLIMIT #{batchSize}
FileResultMapper selectRowsUpdatedAtByTaskIds Select PURE <script>\nSELECT task_id AS taskId, MAX(updated_at) AS latestRowAt, COUNT(*) AS rowCount\nFROM biz_file_result\nWHERE module_type = #{moduleType}\n AND task_id IN\n <foreach item="taskId" collection="taskIds" open="(" separator="," close=")">#{taskId}</foreach>\nGROUP BY task_id\n</script>
FileTaskMapper selectExpiredTerminalTaskIds Select PURE SELECT id FROM biz_file_task\nWHERE module_type = #{moduleType}\n AND status IN ('SUCCESS', 'FAILED', 'CANCELLED', 'CANCELED')\n AND updated_at < #{cutoff}\nORDER BY id ASC\nLIMIT #{batchSize}
Can't render this file because it contains an unexpected character in line 1 and column 222.