收口(去重库): 五处比对与写入口按 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:
+2
-2
@@ -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));
|
||||
|
||||
+1
-1
@@ -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)));
|
||||
|
||||
+56
-31
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user