feat(jd-database): v1.7 对齐原型后端半边——V214 店铺名单+合并搜索/价格区间/店铺谓词+CSV 导出
This commit is contained in:
@@ -156,6 +156,23 @@ class JdSchemaContractTest {
|
||||
assertTrue(sql.contains("迁移 V210"), "头注释必须是 `-- 迁移 V210…`(MigrationInventoryTest 口径)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void V214_分类店铺名单_同分类唯一键形状() {
|
||||
String sql = read(DB_DIR.resolve("V214__jd_group_shop.sql"));
|
||||
assertTrue(sql.contains("biz_jd_group_shop"), "缺名单表");
|
||||
for (String column : List.of("category_id", "shop_name", "created_by", "created_at")) {
|
||||
assertFalse(linesContaining(sql, "`" + column + "`").isEmpty(), "V214 缺列: " + column);
|
||||
}
|
||||
// 同分类重名拒绝(服务层先查给 409,DB 唯一键兜并发)。
|
||||
assertTrue(sql.contains("uk_category_shop"), "同分类店铺重名唯一键缺失");
|
||||
assertTrue(linesContaining(sql, "uk_category_shop").stream()
|
||||
.anyMatch(line -> line.contains("category_id") && line.contains("shop_name")),
|
||||
"唯一键必须是 (category_id, shop_name)");
|
||||
assertTrue(linesContaining(sql, "`shop_name`").stream().anyMatch(line -> line.contains("NOT NULL")),
|
||||
"shop_name 必须 NOT NULL(空店铺名没有语义,铁律 30 同形)");
|
||||
assertTrue(sql.contains("迁移 V214"), "头注释必须是 `-- 迁移 V214…`(MigrationInventoryTest 口径)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 配置_独立图片桶不落默认桶() {
|
||||
String appYml = read(RESOURCES.resolve("application.yml"));
|
||||
|
||||
+251
-61
@@ -13,6 +13,7 @@ import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdProductBatchAssignGroupRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdProductBatchDeleteRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdProductExportRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdProductGroupSaveRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdProductUpdateRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
@@ -26,6 +27,7 @@ import com.nanri.aiimage.modules.jd.model.vo.JdProductDeleteVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductDetailVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductPageVo;
|
||||
import com.nanri.aiimage.modules.jd.service.JdCategoryScopeService;
|
||||
import com.nanri.aiimage.modules.jd.service.JdGroupShopService;
|
||||
import com.nanri.aiimage.modules.jd.service.JdProductDeleteService;
|
||||
import com.nanri.aiimage.modules.jd.service.JdProductGroupService;
|
||||
import com.nanri.aiimage.modules.jd.service.JdProductQueryService;
|
||||
@@ -35,6 +37,7 @@ import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -107,8 +110,9 @@ class AdminJdProductControllerTest {
|
||||
assertTrue(source.contains("/api/admin/jd-products"), "端点必须挂在 /api/admin/jd-products");
|
||||
assertTrue(source.contains("@GetMapping"), "查询端点必须落在 @GetMapping");
|
||||
assertTrue(source.contains("\"/{id}\""), "详情端点必须是 GET /{id}");
|
||||
// 导出端点已随 2026-11-15 客户口径「导出也不用」下线(前端导出发送口同窗撤)——反向钉住防回潜。
|
||||
assertFalse(source.contains("\"/export\""), "导出端点必须已删除,不得回潜");
|
||||
// 导出端点 v1.7 恢复(2026-11-21 拍板「只恢复 CSV」):POST /export 按勾选 id 导出,
|
||||
// 2026-11-15 的下线口径只对当年的 xlsx 形状有效(历史见 git)。
|
||||
assertTrue(source.contains("\"/export\""), "导出端点必须复活(v1.7 拍板「只恢复 CSV」)");
|
||||
// 删除入口(用户口径「后台删除数据的时候也要把桶对应的文件删除」):DELETE /{id} 单行
|
||||
// + POST /batch-delete 批量,先删桶、全成功才删库行(JdProductDeleteService 判据另钉)。
|
||||
assertTrue(source.contains("@DeleteMapping"), "删除端点必须落在 @DeleteMapping");
|
||||
@@ -159,7 +163,8 @@ class AdminJdProductControllerTest {
|
||||
@Test
|
||||
void 分页参数snake且带默认值() throws IOException {
|
||||
String source = stripComments(Files.readString(CONTROLLER));
|
||||
for (String param : List.of("page_size", "product_code", "title_keyword",
|
||||
for (String param : List.of("page_size", "product_code", "title_keyword", "keyword",
|
||||
"shop_name", "min_price", "max_price",
|
||||
"group_id", "owner_group_id", "synced_from", "synced_to")) {
|
||||
assertTrue(source.contains(param), "查询参数必须 snake_case:" + param);
|
||||
}
|
||||
@@ -169,12 +174,20 @@ class AdminJdProductControllerTest {
|
||||
// 那两档读已停用的 biz_jd_product.group_id 列,语义不同,旧参数仍必须被拒收)。
|
||||
assertTrue(source.contains("@RequestParam(name = \"classified\""),
|
||||
"分类状态快筛参数 classified 必须在册(2026-11-17 用户口径)");
|
||||
// v1.7 合并搜索(2026-11-21 对齐原型单搜索框):keyword 必须在册(OR 语义后端做)。
|
||||
assertTrue(source.contains("@RequestParam(name = \"keyword\""),
|
||||
"合并搜索参数 keyword 必须在册(v1.7 拍板)");
|
||||
assertTrue(source.contains("@RequestParam(name = \"min_price\"")
|
||||
&& source.contains("@RequestParam(name = \"max_price\""),
|
||||
"价格区间参数 min_price/max_price 必须在册(v1.7 拍板)");
|
||||
// company_id 参数已随 2026-11-16「删除掉公司 tab」整批摘除(读侧零公司维度支另有反向钉)。
|
||||
assertFalse(source.contains("company_id"), "company_id 请求参数必须已删除,不得回潜");
|
||||
// 商品状态筛选框已随 2026-11-15 客户口径从前端下线 ⇒ 服务端同名参数一起撤(反向钉住防回潜)。
|
||||
assertFalse(source.contains("\"sale_status\""), "sale_status 筛选参数必须已删除,不得回潜");
|
||||
// 店铺筛选同日客户口径「店铺不需要」下线 ⇒ shop_name 参数同窗撤(反向钉住防回潜)。
|
||||
assertFalse(source.contains("shop_name"), "shop_name 筛选参数必须已删除,不得回潜(店铺列/详情仍保留)");
|
||||
// 店铺筛选 v1.7 复活(2026-11-21 拍板「店铺名单维度完整对齐」):精确 eq 配合 group_id;
|
||||
// 2026-11-15 撤掉的是筛选框(旧口径注释的历史事实保留在 git)。
|
||||
assertTrue(source.contains("@RequestParam(name = \"shop_name\""),
|
||||
"店铺精确过滤参数 shop_name 必须在册(v1.7 随名单维度复活)");
|
||||
assertTrue(source.contains("PageDefaults"), "page/page_size 默认值必须取 PageDefaults"
|
||||
+ "(page_size 默认 20=PageDefaults.JD_PRODUCTS_PAGE_SIZE);"
|
||||
+ "page 钳制防 (page-1)*page_size 溢出的行为样本见查询服务钳制支(铁律 15)");
|
||||
@@ -220,11 +233,17 @@ class AdminJdProductControllerTest {
|
||||
assertTrue(source.contains(seam), "分组鉴权闸缺接缝 " + seam + "(与商品页同款)");
|
||||
}
|
||||
// V203 起写权限不再收窄到平台超管:本组管理员 / 公司超管 / 平台超管都可写(作用域服务判)。
|
||||
for (String verb : List.of("@PostMapping", "@PutMapping", "@DeleteMapping")) {
|
||||
for (String verb : List.of("@PostMapping", "@PutMapping", "@DeleteMapping", "@PatchMapping")) {
|
||||
assertTrue(source.contains(verb), "分组 CRUD 必须有 " + verb);
|
||||
}
|
||||
assertEquals(5, count(source, "requireJdAccess(request)"),
|
||||
"列表/作用域/新建/改名/删除五条路由都走同一菜单正门(写权限在作用域服务里判)");
|
||||
// v1.7 店铺名单五条端点(V214):GET/POST/DELETE /{id}/shops、PATCH /{id}/shops/rename、
|
||||
// GET /shops/available——读走 canRead、写走 canWrite(与分类同一道闸)。
|
||||
for (String route : List.of("\"/{id}/shops\"", "\"/{id}/shops/rename\"", "\"/shops/available\"")) {
|
||||
assertTrue(source.contains(route), "店铺名单端点缺失:" + route);
|
||||
}
|
||||
assertEquals(10, count(source, "requireJdAccess(request)"),
|
||||
"列表/作用域/新建/改名/删除五条分类路由+名单五条路由,共十处同一菜单正门"
|
||||
+ "(写权限在作用域服务里判)");
|
||||
assertFalse(source.contains("requirePlatformSuperAdminWrite"),
|
||||
"分类写权限已改由 JdCategoryScopeService 判「该组可写」,超管专属闸必须已摘除");
|
||||
assertTrue(source.contains("requireWritableGroup(") && source.contains("canWrite("),
|
||||
@@ -234,6 +253,11 @@ class AdminJdProductControllerTest {
|
||||
assertTrue(source.contains("仅该分组的管理员可以修改它的分类"),
|
||||
"非本组管理员写分类的 403 文案必须点名分组管理员");
|
||||
assertTrue(source.contains("\"owner_group_id\""), "列表必须收 owner_group_id(当前分组)");
|
||||
// 名单写端点必须显式过 requireWritableCategory(同分类改名/删除同一道闸,不另开门)。
|
||||
assertTrue(count(source, "requireWritableCategory(scope, ownerGroupId)") >= 3,
|
||||
"名单添加/移除/改名三条写路由必须逐条过 requireWritableCategory(V214)");
|
||||
assertTrue(source.contains("requireReadableCategory("),
|
||||
"名单读端点必须过可见组闸(canRead,同组普通号能看名单)");
|
||||
}
|
||||
|
||||
// ===================== 写权限分档(2026-11-17:超管级+管理员可写,普通账号只读) =====================
|
||||
@@ -245,8 +269,9 @@ class AdminJdProductControllerTest {
|
||||
assertEquals(4, count(source, "requireJdProductWrite(request)"),
|
||||
"编辑/上传图片/单行删除/批量删除四条商品行写路由必须逐条走管理员级写闸"
|
||||
+ "(归类不在此列:V203 起归本组管理员可做)");
|
||||
assertEquals(5, count(source, "requireJdAccess(request)"),
|
||||
"列表/类目/详情/归类四条读+组内写路由,与写闸内部一处,共五处菜单权限正门");
|
||||
assertEquals(6, count(source, "requireJdAccess(request)"),
|
||||
"列表/类目/详情/归类四条读+组内写路由与导出,加写闸内部一处,共六处菜单权限正门"
|
||||
+ "(v1.7 导出走读闸:导出属读行为,不走商品行写闸)");
|
||||
assertTrue(source.contains("仅超级管理员或管理员可以编辑或删除京东数据库数据"),
|
||||
"普通账号写商品的 403 文案必须点名超管与管理员(2026-11-17 口径)");
|
||||
assertTrue(source.contains("operator.superAdmin()"),
|
||||
@@ -289,8 +314,10 @@ class AdminJdProductControllerTest {
|
||||
// 读:菜单权限命中即放行(公共库一人一份全量)。
|
||||
JdProductPageVo pageVo = new JdProductPageVo();
|
||||
pageVo.setItems(List.of());
|
||||
when(queryService.page(1L, 20L, null, null, null, null, null, null, null, GROUP_ID)).thenReturn(pageVo);
|
||||
assertEquals(Boolean.TRUE, controller.page(1L, 20L, null, null, null, null, null, null, null, null, request)
|
||||
when(queryService.page(1L, 20L, null, null, null, null, null, null, null, null, null, null, null, GROUP_ID))
|
||||
.thenReturn(pageVo);
|
||||
assertEquals(Boolean.TRUE, controller.page(1L, 20L, null, null, null, null, null, null,
|
||||
null, null, null, null, null, null, request)
|
||||
.isSuccess(), "普通账号有菜单权限时读必须放行(读写分档只收窄写)");
|
||||
|
||||
// 商品行写:四条写入口一律 403,且业务服务一次都没被调到(不能「先写再拒」)。
|
||||
@@ -391,7 +418,7 @@ class AdminJdProductControllerTest {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
PermissionMenuService permissionMenuService = mock(PermissionMenuService.class);
|
||||
AdminJdProductGroupController controller = new AdminJdProductGroupController(
|
||||
groupService, scopeService, authSupport, permissionMenuService);
|
||||
groupService, mock(JdGroupShopService.class), scopeService, authSupport, permissionMenuService);
|
||||
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(9L);
|
||||
@@ -502,11 +529,13 @@ class AdminJdProductControllerTest {
|
||||
when(groupService.requireCategory(8L)).thenReturn(category(8L, 20L));
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> controller.page(1L, 20L, null, null, null, 8L, null, null, null, null, request));
|
||||
() -> controller.page(1L, 20L, null, null, null, null, 8L, null, null, null,
|
||||
null, null, null, null, request));
|
||||
assertTrue(ex.getMessage().contains("无权查看该分组的分类"),
|
||||
"跨组分类筛选必须 403 点名分组,不做空结果降级");
|
||||
|
||||
verify(queryService, never()).page(any(), any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
verify(queryService, never()).page(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
// ===================== 行为样本 =====================
|
||||
@@ -534,13 +563,14 @@ class AdminJdProductControllerTest {
|
||||
pageVo.setTotal(0L);
|
||||
pageVo.setPage(1L);
|
||||
pageVo.setPageSize((long) PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT);
|
||||
when(queryService.page(1L, 20L, null, null, null, null, null, null, null, GROUP_ID)).thenReturn(pageVo);
|
||||
when(queryService.page(1L, 20L, null, null, null, null, null, null, null, null, null, null, null, GROUP_ID))
|
||||
.thenReturn(pageVo);
|
||||
|
||||
// 请求面没有 company_id:任何授权身份看到的都是同一份全量;分类列按 owner_group_id 取。
|
||||
ApiResponse<JdProductPageVo> response = controller.page(1L, 20L, null, null, null, null,
|
||||
GROUP_ID, null, null, null, request);
|
||||
null, null, null, null, GROUP_ID, null, null, null, request);
|
||||
|
||||
verify(queryService).page(1L, 20L, null, null, null, null, null, null, null, GROUP_ID);
|
||||
verify(queryService).page(1L, 20L, null, null, null, null, null, null, null, null, null, null, null, GROUP_ID);
|
||||
assertEquals(Boolean.TRUE, response.isSuccess(), "拒绝走 403 业务码,放行必须是 success 信封(B80-03)");
|
||||
assertEquals(pageVo, response.getData());
|
||||
}
|
||||
@@ -564,12 +594,14 @@ class AdminJdProductControllerTest {
|
||||
for (String value : List.of("true", "false", "")) {
|
||||
when(request.getParameter(name)).thenReturn(value);
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> controller.page(1L, 20L, null, null, null, 7L, null, null, null, null, request));
|
||||
() -> controller.page(1L, 20L, null, null, null, null, 7L, null, null, null,
|
||||
null, null, null, null, request));
|
||||
assertTrue(ex.getMessage().contains("刷新页面"));
|
||||
when(request.getParameter(name)).thenReturn(null);
|
||||
}
|
||||
}
|
||||
verify(queryService, never()).page(any(), any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
verify(queryService, never()).page(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -688,25 +720,26 @@ class AdminJdProductControllerTest {
|
||||
void 查询服务分页钳制防溢出() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductQueryService service = queryService(mapper);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(1L);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(1L);
|
||||
|
||||
// page=0 / size=null ⇒ 归一为第 1 页、默认 20 条;offset = 0。
|
||||
service.page(0L, null, null, null, null, null, null, null, null, null);
|
||||
service.page(0L, null, null, null, null, null, null, null, null, null, null, null, null, null);
|
||||
ArgumentCaptor<Long> offset = ArgumentCaptor.forClass(Long.class);
|
||||
ArgumentCaptor<Integer> limit = ArgumentCaptor.forClass(Integer.class);
|
||||
verify(mapper).selectProductPage(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), isNull(), offset.capture(), limit.capture());
|
||||
isNull(), isNull(), isNull(), isNull(), isNull(), isNull(), offset.capture(), limit.capture());
|
||||
assertEquals(0L, offset.getValue());
|
||||
assertEquals(PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT, limit.getValue());
|
||||
|
||||
// page=Long.MAX_VALUE / size=500 ⇒ 钳到 10_000 页、100 条;乘积 999_900 不得变负(铁律 15)。
|
||||
service.page(Long.MAX_VALUE, 500L, null, null, null, null, null, null, null, null);
|
||||
// page=Long.MAX_VALUE / size=5000 ⇒ 钳到 10_000 页、1000 条(v1.7 上限);乘积 9_999_000 不得变负(铁律 15)。
|
||||
service.page(Long.MAX_VALUE, 5000L, null, null, null, null, null, null, null, null, null, null, null, null);
|
||||
ArgumentCaptor<Long> offsetMax = ArgumentCaptor.forClass(Long.class);
|
||||
ArgumentCaptor<Integer> limitMax = ArgumentCaptor.forClass(Integer.class);
|
||||
verify(mapper, times(2)).selectProductPage(any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), offsetMax.capture(), limitMax.capture());
|
||||
assertEquals(999_900L, offsetMax.getValue(), "(page-1)*page_size 最大 999_900,钳制后不得溢出变负");
|
||||
assertEquals(100, limitMax.getValue());
|
||||
any(), any(), any(), any(), any(), any(), offsetMax.capture(), limitMax.capture());
|
||||
assertEquals(9_999_000L, offsetMax.getValue(), "(page-1)*page_size 最大 9_999_000,钳制后不得溢出变负");
|
||||
assertEquals(1000, limitMax.getValue(), "page_size 上限已随 v1.7 放宽到 1000(对齐原型档位)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -716,23 +749,29 @@ class AdminJdProductControllerTest {
|
||||
LocalDateTime from = LocalDateTime.of(2026, 11, 14, 12, 0);
|
||||
LocalDateTime to = LocalDateTime.of(2026, 11, 14, 8, 0);
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.page(1L, 20L, null, null, null, null, from, to, null, null));
|
||||
() -> service.page(1L, 20L, null, null, null, null, null, null, null, null, from, to, null, null));
|
||||
assertTrue(ex.getMessage().contains("开始时间不能晚于结束时间"), "倒挂时间按业务异常拒绝");
|
||||
verify(mapper, never()).countProducts(any(), any(), any(), any(), any(), any(), any(), any());
|
||||
verify(mapper, never()).countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 查询服务过滤面原样直传且空串归null() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductQueryService service = queryService(mapper);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(1L);
|
||||
service.page(1L, 20L, " SKU-1 ", " 连衣裙 ", List.of(" 艺考形体服 "), 7L, null, null, null, null);
|
||||
verify(mapper).countProducts("SKU-1", "连衣裙", List.of("艺考形体服"), 7L, null, null, null, null);
|
||||
verify(mapper).selectProductPage("SKU-1", "连衣裙", List.of("艺考形体服"), 7L, null, null, null, null, 0L, 20);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(1L);
|
||||
service.page(1L, 20L, null, " SKU-1 ", " 连衣裙 ", List.of(" 艺考形体服 "), 7L,
|
||||
null, null, null, null, null, null, null);
|
||||
verify(mapper).countProducts(null, "SKU-1", "连衣裙", List.of("艺考形体服"), 7L,
|
||||
null, null, null, null, null, null, null);
|
||||
verify(mapper).selectProductPage(null, "SKU-1", "连衣裙", List.of("艺考形体服"), 7L,
|
||||
null, null, null, null, null, null, null, 0L, 20);
|
||||
|
||||
service.page(1L, 20L, " ", null, null, 0L, null, null, null, null);
|
||||
verify(mapper).countProducts(null, null, null, null, null, null, null, null);
|
||||
verify(mapper).selectProductPage(null, null, null, null, null, null, null, null, 0L, 20);
|
||||
service.page(1L, 20L, null, " ", null, null, 0L, null, null, null, null, null, null, null);
|
||||
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, null);
|
||||
verify(mapper).selectProductPage(null, null, null, null, null, null, null, null,
|
||||
null, null, null, null, 0L, 20);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -744,19 +783,21 @@ class AdminJdProductControllerTest {
|
||||
void 类目多值必须逐项归一并IN下推() throws IOException {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductQueryService service = queryService(mapper);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(1L);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(1L);
|
||||
|
||||
service.page(1L, 20L, null, null,
|
||||
service.page(1L, 20L, null, null, null,
|
||||
java.util.Arrays.asList(" 女装 ", "女装", "", null, " 汉服/旗袍 "),
|
||||
null, null, null, null, null);
|
||||
verify(mapper).countProducts(null, null, List.of("女装", "汉服/旗袍"),
|
||||
null, null, null, null, null);
|
||||
verify(mapper).selectProductPage(null, null, List.of("女装", "汉服/旗袍"),
|
||||
null, null, null, null, null, 0L, 20);
|
||||
null, null, null, null, null, null, null, null);
|
||||
verify(mapper).countProducts(null, null, null, List.of("女装", "汉服/旗袍"),
|
||||
null, null, null, null, null, null, null, null);
|
||||
verify(mapper).selectProductPage(null, null, null, List.of("女装", "汉服/旗袍"),
|
||||
null, null, null, null, null, null, null, null, 0L, 20);
|
||||
|
||||
// 全空白/空集合=不过滤该面(与「全部类目」清选同口径,不得下发空 IN)
|
||||
service.page(1L, 20L, null, null, java.util.Arrays.asList(" ", ""), null, null, null, null, null);
|
||||
verify(mapper).countProducts(null, null, null, null, null, null, null, null);
|
||||
service.page(1L, 20L, null, null, null, java.util.Arrays.asList(" ", ""),
|
||||
null, null, null, null, null, null, null, null);
|
||||
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, null);
|
||||
|
||||
Path xml = findJdProductMapperXml();
|
||||
String xmlText = stripComments(Files.readString(xml)).replaceAll("(?s)<!--.*?-->", "");
|
||||
@@ -793,14 +834,19 @@ class AdminJdProductControllerTest {
|
||||
// ① 服务直传:classified 与 ownerGroupId 成对进 mapper(计数与分页同一过滤面)。
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductQueryService service = queryService(mapper);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(1L);
|
||||
service.page(1L, 20L, null, null, null, null, null, null, Boolean.TRUE, GROUP_ID);
|
||||
verify(mapper).countProducts(null, null, null, null, null, null, true, GROUP_ID);
|
||||
verify(mapper).selectProductPage(null, null, null, null, null, null, true, GROUP_ID, 0L, 20);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(1L);
|
||||
service.page(1L, 20L, null, null, null, null, null, null, null, null, null, null,
|
||||
Boolean.TRUE, GROUP_ID);
|
||||
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, true, GROUP_ID);
|
||||
verify(mapper).selectProductPage(null, null, null, null, null, null, null, null,
|
||||
null, null, true, GROUP_ID, 0L, 20);
|
||||
|
||||
service.page(1L, 20L, null, null, null, null, null, null, Boolean.FALSE, GROUP_ID);
|
||||
verify(mapper).countProducts(null, null, null, null, null, null, false, GROUP_ID);
|
||||
verify(mapper).selectProductPage(null, null, null, null, null, null, false, GROUP_ID, 0L, 20);
|
||||
service.page(1L, 20L, null, null, null, null, null, null, null, null, null, null,
|
||||
Boolean.FALSE, GROUP_ID);
|
||||
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, false, GROUP_ID);
|
||||
verify(mapper).selectProductPage(null, null, null, null, null, null, null, null,
|
||||
null, null, false, GROUP_ID, 0L, 20);
|
||||
|
||||
// ③ 控制器直传:classified 原样进服务(owner_group_id 仍按作用域收敛后的值)。
|
||||
JdProductQueryService queryService = mock(JdProductQueryService.class);
|
||||
@@ -818,8 +864,148 @@ class AdminJdProductControllerTest {
|
||||
JdCategoryScopeService.JdCategoryScope scope = writableScope(GROUP_ID, COMPANY_ID);
|
||||
when(scopeService.resolve(operator)).thenReturn(scope);
|
||||
when(scopeService.readGroupOrNull(scope, null)).thenReturn(GROUP_ID);
|
||||
controller.page(1L, 20L, null, null, null, null, null, null, null, Boolean.TRUE, request);
|
||||
verify(queryService).page(1L, 20L, null, null, null, null, null, null, Boolean.TRUE, GROUP_ID);
|
||||
controller.page(1L, 20L, null, null, null, null, null, null, null, null, null, null,
|
||||
null, Boolean.TRUE, request);
|
||||
verify(queryService).page(1L, 20L, null, null, null, null, null, null, null, null, null,
|
||||
null, Boolean.TRUE, GROUP_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* v1.7(2026-11-21 对齐原型):合并搜索 keyword(product_code OR title)+ 价格区间
|
||||
* min/max_price + 店铺精确过滤 shop_name 三件新过滤面——服务侧逐项归一后原样直传
|
||||
* mapper(谓词在 XML 的 productConds 段,铁律 20 同形);价格倒挂按业务异常拒绝。
|
||||
*/
|
||||
@Test
|
||||
void 合并搜索价格区间店铺过滤归一后直传且价格倒挂拒绝() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductQueryService service = queryService(mapper);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(1L);
|
||||
|
||||
// ① 三件新面原样直传:keyword/shopName trim 归一,价格 BigDecimal 原样。
|
||||
service.page(1L, 20L, " 连衣裙 ", null, null, null, 7L, " 测试店铺 ",
|
||||
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null);
|
||||
verify(mapper).countProducts("连衣裙", null, null, null, 7L, "测试店铺",
|
||||
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null);
|
||||
verify(mapper).selectProductPage("连衣裙", null, null, null, 7L, "测试店铺",
|
||||
new BigDecimal("100"), new BigDecimal("200"), null, null, null, null, 0L, 20);
|
||||
|
||||
// ② 空白 keyword/shopName 归 null(不过滤该面)。
|
||||
service.page(1L, 20L, " ", null, null, null, 0L, " ", null, null, null, null, null, null);
|
||||
verify(mapper).countProducts(null, null, null, null, null, null, null, null, null, null, null, null);
|
||||
|
||||
// ③ 价格倒挂按业务异常拒绝(同同步起止倒挂形状)。
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.page(1L, 20L, null, null, null, null, null, null,
|
||||
new BigDecimal("200"), new BigDecimal("100"), null, null, null, null));
|
||||
assertTrue(ex.getMessage().contains("价格区间最小值不能大于最大值"), "倒挂价格区间按业务异常拒绝");
|
||||
verify(mapper, times(2)).countProducts(any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
/**
|
||||
* v1.7 导出端点(2026-11-21 拍板「只恢复 CSV」):POST /export 按勾选 id 导出,
|
||||
* 读闸 requireJdAccess(导出属读行为,不走商品行写闸),200 信封改
|
||||
* {@code ResponseEntity<byte[]>}+Content-Disposition(外观专利导出同款形状)。
|
||||
*/
|
||||
@Test
|
||||
void 导出CSV端点走菜单正门并把字节装进附件响应() {
|
||||
JdProductQueryService queryService = mock(JdProductQueryService.class);
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
|
||||
mock(JdProductGroupService.class), mock(JdCategoryScopeService.class), authSupport,
|
||||
mock(PermissionMenuService.class));
|
||||
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");
|
||||
byte[] csvBytes = "\uFEFF商品编码\r\nA123".getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
when(queryService.exportCsv(List.of(11L, 12L))).thenReturn(csvBytes);
|
||||
|
||||
JdProductExportRequest body = new JdProductExportRequest();
|
||||
body.setIds(List.of(11L, 12L));
|
||||
ResponseEntity<byte[]> response = controller.export(body, request);
|
||||
|
||||
verify(queryService).exportCsv(List.of(11L, 12L));
|
||||
assertEquals(200, response.getStatusCode().value(), "放行必须是 200 附件响应(非 ApiResponse 信封)");
|
||||
assertTrue(response.getHeaders().getFirst("Content-Disposition").contains(".csv"),
|
||||
"Content-Disposition 必须带 .csv 文件名(DownloadHeaderUtil 兜 ASCII 回退与 UTF-8 双名)");
|
||||
assertTrue(response.getHeaders().getFirst("Content-Type").startsWith("text/csv"),
|
||||
"Content-Type 必须是 text/csv(Excel 直开形状)");
|
||||
assertEquals(csvBytes.length, response.getHeaders().getContentLength());
|
||||
}
|
||||
|
||||
/** 导出超限整批拒绝(不静默截断——少导是交付事故),文案点名上限 1000。 */
|
||||
@Test
|
||||
void 导出勾选超上限整批拒绝() {
|
||||
JdProductQueryService queryService = mock(JdProductQueryService.class);
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class),
|
||||
mock(JdProductGroupService.class), mock(JdCategoryScopeService.class), authSupport,
|
||||
mock(PermissionMenuService.class));
|
||||
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");
|
||||
// 服务侧限流是真源(服务级用例另钉 1001 拒绝);这里钉控制器原样透出业务异常、不吞成 500。
|
||||
when(queryService.exportCsv(any()))
|
||||
.thenThrow(new BusinessException("单次最多导出 1000 个商品,请缩小勾选范围"));
|
||||
|
||||
java.util.List<Long> oversized = new java.util.ArrayList<>();
|
||||
for (long i = 1; i <= 1001; i++) {
|
||||
oversized.add(i);
|
||||
}
|
||||
JdProductExportRequest body = new JdProductExportRequest();
|
||||
body.setIds(oversized);
|
||||
BusinessException ex = assertThrows(BusinessException.class, () -> controller.export(body, request));
|
||||
assertTrue(ex.getMessage().contains("1000"), "超限文案必须点名上限 1000");
|
||||
}
|
||||
|
||||
/** 导出服务侧:BOM+中文表头+RFC 4180 转义+id 稳定排序;空/超限在服务层同样拒绝。 */
|
||||
@Test
|
||||
void 导出CSV带BOM中文表头转义与稳定排序() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductQueryService service = queryService(mapper);
|
||||
|
||||
JdProductEntity first = productEntity(1L);
|
||||
first.setTitle("测试连衣裙,带逗号\"带引号\"");
|
||||
JdProductEntity second = productEntity(2L);
|
||||
second.setSyncedAt(LocalDateTime.of(2026, 11, 21, 9, 30));
|
||||
when(mapper.selectBatchIds(List.of(2L, 1L))).thenReturn(List.of(second, first));
|
||||
|
||||
byte[] bytes = service.exportCsv(List.of(2L, 1L));
|
||||
String csv = new String(bytes, java.nio.charset.StandardCharsets.UTF_8);
|
||||
|
||||
assertTrue(csv.startsWith("\uFEFF"), "必须带 UTF-8 BOM(Excel 双击直开中文不乱码)");
|
||||
assertTrue(csv.contains("商品编码,标题,价格下限,价格上限,总库存,SKU数,信息分,品牌,货号,店铺,类目,状态,同步时间,主图"),
|
||||
"中文表头必须按 v1.7 列序在册");
|
||||
// RFC 4180 转义:含逗号/引号的标题整体加引号、内部引号翻倍,不串列。
|
||||
assertTrue(csv.contains("\"测试连衣裙,带逗号\"\"带引号\"\"\""),
|
||||
"含逗号/引号的标题必须加引号且内部引号翻倍");
|
||||
assertTrue(csv.contains("2026-11-21 09:30:00"), "同步时间列必须是秒级格式");
|
||||
// 行按 id 升序(勾选集合无顺序语义)。
|
||||
assertTrue(csv.indexOf("A123,") < csv.lastIndexOf("A123,"), "两行都在");
|
||||
int firstRow = csv.indexOf("\r\n" + "A123,");
|
||||
int secondRow = csv.indexOf("\r\n", firstRow + 1);
|
||||
assertTrue(firstRow > 0 && secondRow > firstRow, "行分隔符存在");
|
||||
|
||||
// 空勾选拒绝(前端按钮置灰是第一道,这里是兜底);超上限同样拒绝(少导是交付事故)。
|
||||
BusinessException empty = assertThrows(BusinessException.class, () -> service.exportCsv(List.of()));
|
||||
assertTrue(empty.getMessage().contains("请先勾选"));
|
||||
BusinessException invalid = assertThrows(BusinessException.class,
|
||||
() -> service.exportCsv(java.util.Arrays.asList(0L, null)));
|
||||
assertTrue(invalid.getMessage().contains("请先勾选"));
|
||||
|
||||
java.util.List<Long> oversized = new java.util.ArrayList<>();
|
||||
for (long i = 1; i <= JdProductQueryService.MAX_EXPORT_IDS + 1; i++) {
|
||||
oversized.add(i);
|
||||
}
|
||||
BusinessException over = assertThrows(BusinessException.class, () -> service.exportCsv(oversized));
|
||||
assertTrue(over.getMessage().contains("1000"), "超限文案必须点名上限 1000");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -967,9 +1153,10 @@ class AdminJdProductControllerTest {
|
||||
|
||||
JdProductEntity categorized = productEntity(1L);
|
||||
JdProductEntity missing = productEntity(2L);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(2L);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(2L);
|
||||
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
anyLong(), anyInt())).thenReturn(List.of(categorized, missing));
|
||||
any(), any(), any(), any(), anyLong(), anyInt())).thenReturn(List.of(categorized, missing));
|
||||
when(assignMapper.selectList(any())).thenReturn(List.of(
|
||||
assign(1L, GROUP_ID, 3L), assign(2L, GROUP_ID, 99L)));
|
||||
JdProductGroupEntity category = new JdProductGroupEntity();
|
||||
@@ -977,7 +1164,8 @@ class AdminJdProductControllerTest {
|
||||
category.setName("秋冬款");
|
||||
when(categoryMapper.selectBatchIds(any())).thenReturn(List.of(category));
|
||||
|
||||
JdProductPageVo vo = service.page(1L, 20L, null, null, null, null, null, null, null, GROUP_ID);
|
||||
JdProductPageVo vo = service.page(1L, 20L, null, null, null, null, null, null, null, null,
|
||||
null, null, null, GROUP_ID);
|
||||
|
||||
assertEquals(3L, vo.getItems().get(0).getGroupId(), "分类 id 取本组归属行");
|
||||
assertEquals("秋冬款", vo.getItems().get(0).getGroupName(), "行集归属一次取齐分类名(无逐行查询)");
|
||||
@@ -992,11 +1180,13 @@ class AdminJdProductControllerTest {
|
||||
JdProductGroupAssignMapper assignMapper = mock(JdProductGroupAssignMapper.class);
|
||||
JdProductQueryService service = queryService(mapper, mock(JdProductImageMapper.class),
|
||||
mock(JdProductSkuMapper.class), mock(JdProductGroupMapper.class), assignMapper);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(1L);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), any(), any())).thenReturn(1L);
|
||||
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
anyLong(), anyInt())).thenReturn(List.of(productEntity(1L)));
|
||||
any(), any(), any(), any(), anyLong(), anyInt())).thenReturn(List.of(productEntity(1L)));
|
||||
|
||||
JdProductPageVo vo = service.page(1L, 20L, null, null, null, null, null, null, null, null);
|
||||
JdProductPageVo vo = service.page(1L, 20L, null, null, null, null, null, null, null, null,
|
||||
null, null, null, null);
|
||||
|
||||
assertNull(vo.getItems().get(0).getGroupId(), "不属于任何分组的账号:分类列一律空(未分类)");
|
||||
assertNull(vo.getItems().get(0).getGroupName());
|
||||
|
||||
+57
-1
@@ -61,9 +61,65 @@ class JdProductMapperSqlTest {
|
||||
() -> "占位符数变了(companyId + taskId): " + sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* v1.7(2026-11-21 对齐原型《商品数据商城》)新筛选谓词必须 SQL 侧下推(铁律 20):
|
||||
* keyword=product_code LIKE <b>OR</b> title LIKE(OR 语义必须在 SQL 段,Java 侧 filter 即红)、
|
||||
* 价格区间=商品区间完全落在筛选区间内(price_min >= X AND price_max <= Y)、
|
||||
* 店铺=精确 eq(点店铺=该分类归属 ∩ 该店铺)。
|
||||
*/
|
||||
@Test
|
||||
void v17_合并搜索价格区间店铺谓词全部下推SQL() throws Exception {
|
||||
String sql = render("countProducts", Map.of(
|
||||
"keyword", "连衣裙",
|
||||
"productCode", "A123",
|
||||
"titleKeyword", "碎花裙",
|
||||
"groupId", 7L,
|
||||
"shopName", "测试店铺",
|
||||
"minPrice", new java.math.BigDecimal("100"),
|
||||
"maxPrice", new java.math.BigDecimal("200")));
|
||||
|
||||
assertTrue(sql.contains("(p.product_code LIKE CONCAT('%', ?, '%') OR p.title LIKE CONCAT('%', ?, '%'))"),
|
||||
() -> "keyword 必须 OR 下推在同一个括号对里(拆成 AND 或 Java 侧 filter 都算回潜): " + sql);
|
||||
assertTrue(sql.contains("p.product_code LIKE CONCAT('%', ?, '%')")
|
||||
&& sql.contains("p.title LIKE CONCAT('%', ?, '%')"),
|
||||
() -> "旧两参数的独立 LIKE 谓词必须保留(兼容在飞前端): " + sql);
|
||||
assertTrue(sql.contains("p.price_min >= ?") && sql.contains("p.price_max <= ?"),
|
||||
() -> "价格区间必须是「商品区间完全落在筛选区间内」(有交集语义即改坏了口径): " + sql);
|
||||
assertTrue(sql.contains("p.shop_name = ?"),
|
||||
() -> "店铺过滤必须精确 eq(LIKE 模糊会把名单点选变搜索面): " + sql);
|
||||
assertTrue(sql.contains("a.category_id = ?"),
|
||||
() -> "自定义分类谓词必须仍在(v1.7 未动存量过滤面): " + sql);
|
||||
|
||||
// 空过滤面:一个新谓词都不许出现(空串/null 归一在服务侧,XML 不兜)。
|
||||
String bare = render("countProducts", Map.of("offset", 0L, "limit", 20));
|
||||
assertFalse(bare.contains("LIKE"), () -> "空过滤面不得残留 LIKE 谓词: " + bare);
|
||||
assertFalse(bare.contains("shop_name"), () -> "空过滤面不得残留店铺谓词: " + bare);
|
||||
assertFalse(bare.contains("price_min"), () -> "空过滤面不得残留价格谓词: " + bare);
|
||||
}
|
||||
|
||||
/** 店铺名单「可添加候选」:DISTINCT + like + NOT IN 排除已有名单 + LIMIT(服务侧钳制值)。 */
|
||||
@Test
|
||||
void v17_店铺候选查询必须distinct下推与排除已有名单() throws Exception {
|
||||
String sql = render("selectDistinctShopNames", Map.of(
|
||||
"keyword", "旗",
|
||||
"excludeNames", java.util.List.of("已有店A", "已有店B"),
|
||||
"limit", 200));
|
||||
|
||||
assertTrue(sql.startsWith("SELECT DISTINCT p.shop_name FROM biz_jd_product p WHERE"),
|
||||
() -> "候选源必须是 biz_jd_product.shop_name DISTINCT: " + sql);
|
||||
assertTrue(sql.contains("p.shop_name LIKE CONCAT('%', ?, '%')"), () -> "关键词必须 like 下推: " + sql);
|
||||
assertTrue(sql.contains("p.shop_name NOT IN ( ? , ? )"),
|
||||
() -> "排除已有名单必须 NOT IN 下推(Java 侧过滤即红,铁律 20): " + sql);
|
||||
assertTrue(sql.endsWith("ORDER BY p.shop_name LIMIT ?"), () -> "必须按店名升序+LIMIT 收尾: " + sql);
|
||||
}
|
||||
|
||||
private static String render(String id) throws Exception {
|
||||
return render(id, Map.of("companyId", 1L, "taskId", 34956L));
|
||||
}
|
||||
|
||||
private static String render(String id, Map<String, Object> params) throws Exception {
|
||||
MappedStatement statement = parse().getMappedStatement(NS + "." + id);
|
||||
return statement.getBoundSql(Map.of("companyId", 1L, "taskId", 34956L))
|
||||
return statement.getBoundSql(params)
|
||||
.getSql().replaceAll("\\s+", " ").trim();
|
||||
}
|
||||
|
||||
|
||||
+232
@@ -0,0 +1,232 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdGroupShopMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdGroupShopEntity;
|
||||
import com.nanri.aiimage.testutil.UpdateWrapperProbe;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
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.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* v1.7(V214,2026-11-21 拍板「店铺名单维度完整对齐」原型《商品数据商城》):
|
||||
* 分类店铺名单业务规则。
|
||||
*
|
||||
* <p>判据:批量添加 trim/去空/去重/上限 100/INSERT IGNORE 幂等(全重复=0 不报错)、
|
||||
* 移除不存在按业务错误拒绝、改名同分类查重 409+老名必须存在、可添加候选
|
||||
* DISTINCT+like+排除已有名单+limit 钳制 200、分类列表带名单一次批量取。
|
||||
* <b>读写作用域(谁能看/改哪个分类的名单)不在本类</b>——控制器按分类归属过
|
||||
* JdCategoryScopeService 的闸(与分类改名/删除同口径)。</p>
|
||||
*/
|
||||
class JdGroupShopServiceTest {
|
||||
|
||||
private static final long CATEGORY_ID = 3L;
|
||||
private static final long OPERATOR_ID = 9L;
|
||||
|
||||
private final JdGroupShopMapper shopMapper = mock(JdGroupShopMapper.class);
|
||||
private final JdProductMapper productMapper = mock(JdProductMapper.class);
|
||||
private final JdGroupShopService service = new JdGroupShopService(shopMapper, productMapper);
|
||||
|
||||
@BeforeAll
|
||||
static void initTableInfo() {
|
||||
// 探针要读 Lambda wrapper 的 WHERE 段:裸单测 JVM 里 lambda 列缓存不初始化就解析不了
|
||||
// (JdProductGroupServiceTest 同款)。
|
||||
MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
||||
TableInfoHelper.initTableInfo(assistant, JdGroupShopEntity.class);
|
||||
}
|
||||
|
||||
// ===================== 批量添加 =====================
|
||||
|
||||
@Test
|
||||
void 批量添加归一去重后落INSERTIGNORE并带操作者() {
|
||||
when(shopMapper.insertIgnoreBatch(anyLong(), anyList(), anyLong())).thenReturn(2);
|
||||
|
||||
int added = service.addShops(CATEGORY_ID,
|
||||
java.util.Arrays.asList(" 测试店铺 ", "测试店铺", "", null, "绿屿街道店"), OPERATOR_ID);
|
||||
|
||||
assertEquals(2, added, "回执=实际新增行数(幂等跳过不计)");
|
||||
verify(shopMapper).insertIgnoreBatch(CATEGORY_ID, List.of("测试店铺", "绿屿街道店"), OPERATOR_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 批量添加空名单与全空白直接拒绝() {
|
||||
BusinessException empty = assertThrows(BusinessException.class,
|
||||
() -> service.addShops(CATEGORY_ID, List.of(), OPERATOR_ID));
|
||||
assertTrue(empty.getMessage().contains("店铺名称列表不能为空"));
|
||||
verifyNoInteractions(shopMapper);
|
||||
|
||||
BusinessException blank = assertThrows(BusinessException.class,
|
||||
() -> service.addShops(CATEGORY_ID, java.util.Arrays.asList(" ", "", null), OPERATOR_ID));
|
||||
assertTrue(blank.getMessage().contains("有效店铺名称不能为空"));
|
||||
verifyNoInteractions(shopMapper);
|
||||
|
||||
BusinessException ghost = assertThrows(BusinessException.class,
|
||||
() -> service.addShops(0L, List.of("测试店铺"), OPERATOR_ID));
|
||||
assertTrue(ghost.getMessage().contains("分类不存在"));
|
||||
verifyNoInteractions(shopMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 批量添加超上限拒绝且不落库() {
|
||||
List<String> tooMany = new java.util.ArrayList<>();
|
||||
for (int i = 0; i < JdGroupShopService.MAX_ADD_SHOPS + 1; i++) {
|
||||
tooMany.add("店铺" + i);
|
||||
}
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.addShops(CATEGORY_ID, tooMany, OPERATOR_ID));
|
||||
assertTrue(ex.getMessage().contains("100"), "超限文案必须点名上限 100");
|
||||
verify(shopMapper, never()).insertIgnoreBatch(anyLong(), anyList(), anyLong());
|
||||
}
|
||||
|
||||
// ===================== 移除 =====================
|
||||
|
||||
@Test
|
||||
void 移除限定该分类该店铺且不存在按业务错误拒绝() {
|
||||
when(shopMapper.delete(any())).thenReturn(1);
|
||||
assertEquals(1, service.removeShop(CATEGORY_ID, " 测试店铺 "));
|
||||
|
||||
ArgumentCaptor<Wrapper<JdGroupShopEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
|
||||
verify(shopMapper).delete(captor.capture());
|
||||
var whereEq = UpdateWrapperProbe.whereEq(captor.getValue());
|
||||
assertEquals(CATEGORY_ID, ((Number) whereEq.get("category_id")).longValue(),
|
||||
"移除谓词必须限定该分类(别家分类的名单不许动)");
|
||||
assertEquals("测试店铺", whereEq.get("shop_name"), "移除谓词必须精确店铺名");
|
||||
|
||||
when(shopMapper.delete(any())).thenReturn(0);
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.removeShop(CATEGORY_ID, "不存在的店"));
|
||||
assertTrue(ex.getMessage().contains("该分类下不存在此店铺"), "移除 0 行=按不存在拒绝(不静默成功)");
|
||||
}
|
||||
|
||||
// ===================== 改名 =====================
|
||||
|
||||
@Test
|
||||
void 改名撞同分类已有名409且不落库() {
|
||||
when(shopMapper.selectOne(any())).thenReturn(shopRow(11L, CATEGORY_ID, "旧名"));
|
||||
when(shopMapper.selectCount(any())).thenReturn(1L);
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.renameShop(CATEGORY_ID, "旧名", "已有名"));
|
||||
assertEquals(409, ex.getCode(), "同分类重名=409(资源冲突语义)");
|
||||
assertTrue(ex.getMessage().contains("该分类下已存在同名店铺"));
|
||||
verify(shopMapper, never()).updateById(any(JdGroupShopEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 改名老名不存在拒绝与原名相同拒绝() {
|
||||
when(shopMapper.selectOne(any())).thenReturn(null);
|
||||
BusinessException missing = assertThrows(BusinessException.class,
|
||||
() -> service.renameShop(CATEGORY_ID, "幽灵店", "新名"));
|
||||
assertTrue(missing.getMessage().contains("该分类下不存在此店铺"));
|
||||
verify(shopMapper, never()).updateById(any(JdGroupShopEntity.class));
|
||||
|
||||
BusinessException same = assertThrows(BusinessException.class,
|
||||
() -> service.renameShop(CATEGORY_ID, "同名", " 同名 "));
|
||||
assertTrue(same.getMessage().contains("新店铺名称不能与原名称相同"));
|
||||
verifyNoInteractions(productMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 改名成功只更新名单表本身() {
|
||||
when(shopMapper.selectOne(any())).thenReturn(shopRow(11L, CATEGORY_ID, "旧名"));
|
||||
when(shopMapper.selectCount(any())).thenReturn(0L);
|
||||
|
||||
service.renameShop(CATEGORY_ID, "旧名", " 新名 ");
|
||||
|
||||
ArgumentCaptor<JdGroupShopEntity> captor = ArgumentCaptor.forClass(JdGroupShopEntity.class);
|
||||
verify(shopMapper).updateById(captor.capture());
|
||||
assertEquals("新名", captor.getValue().getShopName());
|
||||
assertEquals(CATEGORY_ID, captor.getValue().getCategoryId(), "改名不得把分类改走");
|
||||
// 无原型 assignment 同步链:商品行一次都不查(名单改名不碰 biz_jd_product)。
|
||||
verifyNoInteractions(productMapper);
|
||||
}
|
||||
|
||||
// ===================== 查询 =====================
|
||||
|
||||
@Test
|
||||
void 名单按店名升序且空分类回空() {
|
||||
when(shopMapper.selectList(any())).thenReturn(List.of(
|
||||
shopRow(1L, CATEGORY_ID, "B店"), shopRow(2L, CATEGORY_ID, "A店")));
|
||||
|
||||
assertEquals(List.of("B店", "A店"), service.listShops(CATEGORY_ID),
|
||||
"排序由 wrapper 决定,服务不重排(selectList 顺序即返回顺序)");
|
||||
assertTrue(service.listShops(null).isEmpty(), "越界分类 id 回空(不查库)");
|
||||
assertTrue(service.listShops(0L).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void 分类列表带名单按行集id一次批量取() {
|
||||
when(shopMapper.selectList(any())).thenReturn(List.of(
|
||||
shopRow(1L, CATEGORY_ID, "B店"), shopRow(2L, CATEGORY_ID, "A店")));
|
||||
|
||||
Map<Long, List<String>> byCategory = service.listShopsByCategoryIds(
|
||||
java.util.Arrays.asList(CATEGORY_ID, CATEGORY_ID, null, 0L, 99L));
|
||||
|
||||
assertEquals(List.of("B店", "A店"), byCategory.get(CATEGORY_ID), "一次批量取(无逐行查询)");
|
||||
assertTrue(byCategory.getOrDefault(99L, List.of()).isEmpty(), "无名单分类=空,不猜");
|
||||
assertTrue(service.listShopsByCategoryIds(List.of()).isEmpty(), "空清单不生成 IN ()");
|
||||
assertTrue(service.listShopsByCategoryIds(null).isEmpty());
|
||||
|
||||
ArgumentCaptor<Wrapper<JdGroupShopEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
|
||||
verify(shopMapper).selectList(captor.capture());
|
||||
captor.getValue().getSqlSegment();
|
||||
assertTrue(captor.getValue().getSqlSegment().toUpperCase().contains(" IN "),
|
||||
"多分类走 IN 谓词:实得 " + captor.getValue().getSqlSegment());
|
||||
}
|
||||
|
||||
// ===================== 可添加候选 =====================
|
||||
|
||||
@Test
|
||||
void 可添加候选排除已有名单且limit钳制() {
|
||||
when(shopMapper.selectList(any())).thenReturn(List.of(shopRow(1L, CATEGORY_ID, "已有店A")));
|
||||
when(productMapper.selectDistinctShopNames("旗", List.of("已有店A"), 200))
|
||||
.thenReturn(List.of("旗舷店"));
|
||||
|
||||
assertEquals(List.of("旗舷店"),
|
||||
service.availableShops(" 旗 ", CATEGORY_ID, null), "keyword trim 后下推;limit 缺省钳到 200");
|
||||
|
||||
// limit 钳制:500 请求 → 200(服务侧钳,mapper 不做第二次换算)。
|
||||
service.availableShops(null, CATEGORY_ID, 500);
|
||||
verify(productMapper).selectDistinctShopNames(null, List.of("已有店A"), 200);
|
||||
|
||||
// 不带排除分类:excludeNames 空清单(不生成 NOT IN);前两轮各查过一次名单表,共两次。
|
||||
when(productMapper.selectDistinctShopNames(null, List.of(), 50)).thenReturn(List.of("直接店"));
|
||||
assertEquals(List.of("直接店"), service.availableShops(null, null, 50));
|
||||
verify(shopMapper, times(2)).selectList(any());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 助手
|
||||
|
||||
private static JdGroupShopEntity shopRow(long id, long categoryId, String shopName) {
|
||||
JdGroupShopEntity entity = new JdGroupShopEntity();
|
||||
entity.setId(id);
|
||||
entity.setCategoryId(categoryId);
|
||||
entity.setShopName(shopName);
|
||||
entity.setCreatedBy(OPERATOR_ID);
|
||||
entity.setCreatedAt(LocalDateTime.of(2026, 11, 21, 10, 0));
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
+32
-1
@@ -48,7 +48,10 @@ class JdProductGroupServiceTest {
|
||||
|
||||
private final JdProductGroupMapper groupMapper = mock(JdProductGroupMapper.class);
|
||||
private final JdProductGroupAssignMapper assignMapper = mock(JdProductGroupAssignMapper.class);
|
||||
private final JdProductGroupService service = new JdProductGroupService(groupMapper, assignMapper);
|
||||
private final com.nanri.aiimage.modules.jd.service.JdGroupShopService groupShopService =
|
||||
mock(com.nanri.aiimage.modules.jd.service.JdGroupShopService.class);
|
||||
private final JdProductGroupService service =
|
||||
new JdProductGroupService(groupMapper, assignMapper, groupShopService);
|
||||
|
||||
@BeforeAll
|
||||
static void initTableInfo() {
|
||||
@@ -60,6 +63,11 @@ class JdProductGroupServiceTest {
|
||||
TableInfoHelper.initTableInfo(assistant, JdProductGroupAssignEntity.class);
|
||||
}
|
||||
|
||||
/** 名单服务默认空桩(V214 列表带 shops[]:未指定时全空名单,不 NPE)。 */
|
||||
private void stubNoShops() {
|
||||
when(groupShopService.listShopsByCategoryIds(any())).thenReturn(java.util.Map.of());
|
||||
}
|
||||
|
||||
// ===================== 新建 =====================
|
||||
|
||||
@Test
|
||||
@@ -222,6 +230,7 @@ class JdProductGroupServiceTest {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void 列表按当前分组取且productCount按归属表现算() {
|
||||
stubNoShops();
|
||||
JdProductGroupEntity first = categoryEntity(2L, "晚出厂");
|
||||
first.setSortOrder(0);
|
||||
JdProductGroupEntity second = categoryEntity(1L, "早出厂");
|
||||
@@ -257,10 +266,32 @@ class JdProductGroupServiceTest {
|
||||
assertTrue(service.list(0L).isEmpty());
|
||||
verifyNoInteractions(groupMapper);
|
||||
verifyNoInteractions(assignMapper);
|
||||
verifyNoInteractions(groupShopService);
|
||||
}
|
||||
|
||||
/** V214(v1.7 店铺名单):列表行带 shops[],按行集 id 一次批量取;无名单分类落空数组。 */
|
||||
@Test
|
||||
void 列表行带店铺名单_一次批量取_无名单落空数组() {
|
||||
JdProductGroupEntity withShops = categoryEntity(2L, "晚出厂");
|
||||
withShops.setSortOrder(0);
|
||||
JdProductGroupEntity withoutShops = categoryEntity(1L, "早出厂");
|
||||
withoutShops.setSortOrder(1);
|
||||
when(groupMapper.selectList(any())).thenReturn(List.of(withShops, withoutShops));
|
||||
when(assignMapper.selectCount(any())).thenReturn(0L, 0L);
|
||||
when(groupShopService.listShopsByCategoryIds(any()))
|
||||
.thenReturn(java.util.Map.of(2L, java.util.List.of("B店", "A店")));
|
||||
|
||||
List<JdProductGroupVo> items = service.list(GROUP_ID);
|
||||
|
||||
assertEquals(java.util.List.of("B店", "A店"), items.get(0).getShops(),
|
||||
"名单按行集 id 批量补(JdGroupShopService 一次查询,无逐行)");
|
||||
assertNotNull(items.get(1).getShops(), "无名单分类的 shops 必须是空数组不是 null(前端展开箭头判据)");
|
||||
assertTrue(items.get(1).getShops().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 多组并集取分类_空清单不生成IN查询() {
|
||||
stubNoShops();
|
||||
JdProductGroupEntity row = categoryEntity(2L, "晚出厂");
|
||||
when(groupMapper.selectList(any())).thenReturn(List.of(row));
|
||||
when(assignMapper.selectCount(any())).thenReturn(0L);
|
||||
|
||||
+2
-1
@@ -141,7 +141,8 @@ class JdProductQueryServiceUserPageTest {
|
||||
|
||||
JdProductPageVo zero = service.pageForUser(7L, null, 0L, 5000L);
|
||||
assertThat(zero.getPage()).as("页码下限钳到 1").isEqualTo(1L);
|
||||
assertThat(zero.getPageSize()).as("页宽上限钳到 100").isEqualTo(100L);
|
||||
assertThat(zero.getPageSize()).as("页宽上限钳到 1000(v1.7 对齐原型档位放宽,与后管只读页同出口)")
|
||||
.isEqualTo(1000L);
|
||||
|
||||
JdProductPageVo huge = service.pageForUser(7L, null, Long.MAX_VALUE, 20L);
|
||||
assertThat(huge.getPage()).as("页码上限钳到 10000(乘积不溢出,铁律 15 同形)").isEqualTo(10000L);
|
||||
|
||||
Reference in New Issue
Block a user