feat(jd): 京东数据库筛选行加「分类状态」下拉(全部/已分类/未分类)
This commit is contained in:
+3
-1
@@ -103,6 +103,8 @@ public class AdminJdProductController {
|
||||
@Parameter(description = "分组 id") @RequestParam(name = "group_id", required = false) Long groupId,
|
||||
@Parameter(description = "只看未分类(group_id IS NULL,店铺分类弹窗「未分类」范围)")
|
||||
@RequestParam(name = "ungrouped", required = false) Boolean ungrouped,
|
||||
@Parameter(description = "只看已分类(group_id IS NOT NULL,页面筛选行「分类状态」档)")
|
||||
@RequestParam(name = "grouped", required = false) Boolean grouped,
|
||||
@Parameter(description = "同步开始时间(包含,ISO datetime)")
|
||||
@RequestParam(name = "synced_from", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime syncedFrom,
|
||||
@Parameter(description = "同步结束时间(包含,ISO datetime)")
|
||||
@@ -115,7 +117,7 @@ public class AdminJdProductController {
|
||||
// 非超管传什么都不收——公共库对每一家管理员都是同一份全量。
|
||||
Long scopedCompanyId = dataReadScope.superAdminScopedCompanyId(user, companyId);
|
||||
return ApiResponse.success(jdProductQueryService.page(page, pageSize, scopedCompanyId, shopName,
|
||||
productCode, titleKeyword, category, groupId, ungrouped, syncedFrom, syncedTo));
|
||||
productCode, titleKeyword, category, groupId, ungrouped, grouped, syncedFrom, syncedTo));
|
||||
}
|
||||
|
||||
@GetMapping("/categories")
|
||||
|
||||
@@ -42,9 +42,10 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
|
||||
|
||||
/**
|
||||
* 后管只读页总数(Phase 5,v1.4):与 {@link #selectProductPage} 同一过滤面
|
||||
* (公司/店铺名/商品编码/标题关键词/类目/分组/未分类/同步起止,全部 SQL 侧下推,
|
||||
* (公司/店铺名/商品编码/标题关键词/类目/分组/未分类/已分类/同步起止,全部 SQL 侧下推,
|
||||
* 铁律 20)。{@code companyId}/{@code groupId} 为空或 ≤0=不过滤该面;
|
||||
* {@code ungrouped=true}=只看未分类行(group_id IS NULL,店铺分类弹窗「未分类」范围)。
|
||||
* {@code ungrouped=true}=只看未分类行(group_id IS NULL,店铺分类弹窗「未分类」范围);
|
||||
* {@code grouped=true}=只看已分类行(group_id IS NOT NULL,页面筛选行「分类状态」档)。
|
||||
*/
|
||||
long countProducts(@Param("companyId") Long companyId,
|
||||
@Param("shopName") String shopName,
|
||||
@@ -53,6 +54,7 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
|
||||
@Param("category") String category,
|
||||
@Param("groupId") Long groupId,
|
||||
@Param("ungrouped") Boolean ungrouped,
|
||||
@Param("grouped") Boolean grouped,
|
||||
@Param("syncedFrom") LocalDateTime syncedFrom,
|
||||
@Param("syncedTo") LocalDateTime syncedTo);
|
||||
|
||||
@@ -95,6 +97,7 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
|
||||
@Param("category") String category,
|
||||
@Param("groupId") Long groupId,
|
||||
@Param("ungrouped") Boolean ungrouped,
|
||||
@Param("grouped") Boolean grouped,
|
||||
@Param("syncedFrom") LocalDateTime syncedFrom,
|
||||
@Param("syncedTo") LocalDateTime syncedTo,
|
||||
@Param("offset") long offset,
|
||||
|
||||
+15
-8
@@ -76,11 +76,12 @@ public class JdProductQueryService {
|
||||
*
|
||||
* <p>过滤面:公司(物化列 eq)、店铺名 contains、商品编码 contains、标题关键词 contains、
|
||||
* 类目 eq、分组 eq、未分类(group_id IS NULL,店铺分类弹窗「未分类」范围)、
|
||||
* 同步起止(datetime 闭区间)。</p>
|
||||
* 已分类(group_id IS NOT NULL,页面筛选行「分类状态」档,2026-11-15 客户口径
|
||||
* 「查询已分类的跟未分类,分类好的可以不显示」)、同步起止(datetime 闭区间)。</p>
|
||||
*/
|
||||
public JdProductPageVo page(Long page, Long pageSize, Long scopedCompanyId, String shopName,
|
||||
String productCode, String titleKeyword, String category, Long groupId, Boolean ungrouped,
|
||||
LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
Boolean grouped, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
if (syncedFrom != null && syncedTo != null && syncedFrom.isAfter(syncedTo)) {
|
||||
throw new BusinessException("开始时间不能晚于结束时间");
|
||||
}
|
||||
@@ -88,14 +89,15 @@ public class JdProductQueryService {
|
||||
long safeSize = pageSize == null ? PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT
|
||||
: Math.min(Math.max(pageSize, 1), MAX_PAGE_SIZE);
|
||||
Filter filter = filter(scopedCompanyId, shopName, productCode, titleKeyword, category,
|
||||
groupId, ungrouped, syncedFrom, syncedTo);
|
||||
groupId, ungrouped, grouped, syncedFrom, syncedTo);
|
||||
long total = jdProductMapper.countProducts(filter.companyId(), filter.shopName(), filter.productCode(),
|
||||
filter.titleKeyword(), filter.category(), filter.groupId(), filter.ungrouped(),
|
||||
filter.syncedFrom(), filter.syncedTo());
|
||||
filter.grouped(), filter.syncedFrom(), filter.syncedTo());
|
||||
List<JdProductEntity> rows = total <= 0 ? List.of()
|
||||
: jdProductMapper.selectProductPage(filter.companyId(), filter.shopName(), filter.productCode(),
|
||||
filter.titleKeyword(), filter.category(), filter.groupId(), filter.ungrouped(),
|
||||
filter.syncedFrom(), filter.syncedTo(), (safePage - 1) * safeSize, (int) safeSize);
|
||||
filter.grouped(), filter.syncedFrom(), filter.syncedTo(),
|
||||
(safePage - 1) * safeSize, (int) safeSize);
|
||||
List<JdProductListItemVo> items = rows.stream().map(this::toListItem).toList();
|
||||
fillGroupNames(items);
|
||||
JdProductPageVo vo = new JdProductPageVo();
|
||||
@@ -172,17 +174,22 @@ public class JdProductQueryService {
|
||||
|
||||
/** 服务侧过滤面归一:companyId/groupId ≤0 按没选处理,文本 trim 后空串归 null。 */
|
||||
private record Filter(Long companyId, String shopName, String productCode, String titleKeyword,
|
||||
String category, Long groupId, Boolean ungrouped, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
String category, Long groupId, Boolean ungrouped, Boolean grouped,
|
||||
LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
}
|
||||
|
||||
private static Filter filter(Long scopedCompanyId, String shopName, String productCode, String titleKeyword,
|
||||
String category, Long groupId, Boolean ungrouped, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
String category, Long groupId, Boolean ungrouped, Boolean grouped,
|
||||
LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
Long companyId = scopedCompanyId != null && scopedCompanyId > 0 ? scopedCompanyId : null;
|
||||
Long safeGroupId = groupId != null && groupId > 0 ? groupId : null;
|
||||
// 未分类与其它过滤面同口径:没显式要(true)=null=不进 IS NULL 分支。
|
||||
Boolean safeUngrouped = Boolean.TRUE.equals(ungrouped) ? Boolean.TRUE : null;
|
||||
// 已分类同口径:true=只看 group_id IS NOT NULL 的行;与未分类同选时 SQL 侧两谓词相与=空集
|
||||
// (前端下拉是单选,正常到不了这个组合)。
|
||||
Boolean safeGrouped = Boolean.TRUE.equals(grouped) ? Boolean.TRUE : null;
|
||||
return new Filter(companyId, trimToNull(shopName), trimToNull(productCode), trimToNull(titleKeyword),
|
||||
trimToNull(category), safeGroupId, safeUngrouped, syncedFrom, syncedTo);
|
||||
trimToNull(category), safeGroupId, safeUngrouped, safeGrouped, syncedFrom, syncedTo);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 列表/详情搬运
|
||||
|
||||
@@ -102,7 +102,9 @@
|
||||
|
||||
<!-- 后管只读页(Phase 5,v1.4)过滤面:公司(物化列 eq)、店铺名/商品编码/标题关键词
|
||||
(contains LIKE)、类目 eq、分组 eq、未分类(group_id IS NULL,店铺分类弹窗
|
||||
「未分类」范围)、同步起止(datetime 闭区间)。
|
||||
「未分类」范围)、已分类(group_id IS NOT NULL,页面筛选行「分类状态」档,
|
||||
2026-11-15 客户口径「查询已分类的跟未分类,分类好的可以不显示」)、
|
||||
同步起止(datetime 闭区间)。
|
||||
sale_status 筛选随 2026-11-15 客户口径撤掉(前端筛选框下线;列本身与详情/回传链不动)。
|
||||
全部走 #{} 占位符;offset/limit 必须是服务侧钳制后的值(铁律 15),mapper 不做第二次换算。 -->
|
||||
<sql id="productConds">
|
||||
@@ -113,6 +115,7 @@
|
||||
<if test="category != null and category != ''">AND p.category_path = #{category}</if>
|
||||
<if test="groupId != null and groupId > 0">AND p.group_id = #{groupId}</if>
|
||||
<if test="ungrouped != null and ungrouped">AND p.group_id IS NULL</if>
|
||||
<if test="grouped != null and grouped">AND p.group_id IS NOT NULL</if>
|
||||
<if test="syncedFrom != null">AND p.synced_at >= #{syncedFrom}</if>
|
||||
<if test="syncedTo != null">AND p.synced_at <= #{syncedTo}</if>
|
||||
</sql>
|
||||
|
||||
+33
-23
@@ -128,7 +128,7 @@ class AdminJdProductControllerTest {
|
||||
void 分页参数snake且带默认值() throws IOException {
|
||||
String source = stripComments(Files.readString(CONTROLLER));
|
||||
for (String param : List.of("page_size", "shop_name", "product_code", "title_keyword",
|
||||
"group_id", "ungrouped", "synced_from", "synced_to", "company_id")) {
|
||||
"group_id", "ungrouped", "grouped", "synced_from", "synced_to", "company_id")) {
|
||||
assertTrue(source.contains(param), "查询参数必须 snake_case:" + param);
|
||||
}
|
||||
// 商品状态筛选框已随 2026-11-15 客户口径从前端下线 ⇒ 服务端同名参数一起撤(反向钉住防回潜)。
|
||||
@@ -196,13 +196,13 @@ class AdminJdProductControllerTest {
|
||||
pageVo.setTotal(0L);
|
||||
pageVo.setPage(1L);
|
||||
pageVo.setPageSize((long) PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT);
|
||||
when(queryService.page(1L, 20L, 5L, null, null, null, null, null, null, null, null)).thenReturn(pageVo);
|
||||
when(queryService.page(1L, 20L, 5L, null, null, null, null, null, null, null, null, null)).thenReturn(pageVo);
|
||||
|
||||
ApiResponse<JdProductPageVo> response = controller.page(1L, 20L, null, null, null, null, null,
|
||||
null, null, null, 5L, request);
|
||||
null, null, null, null, 5L, request);
|
||||
|
||||
verify(dataReadScope).superAdminScopedCompanyId(operator, 5L);
|
||||
verify(queryService).page(1L, 20L, 5L, null, null, null, null, null, null, null, null);
|
||||
verify(queryService).page(1L, 20L, 5L, null, null, null, null, null, null, null, null, null);
|
||||
assertEquals(Boolean.TRUE, response.isSuccess(), "拒绝走 403 业务码,放行必须是 success 信封(B80-03)");
|
||||
assertEquals(pageVo, response.getData());
|
||||
}
|
||||
@@ -324,23 +324,23 @@ class AdminJdProductControllerTest {
|
||||
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper.class),
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(1L);
|
||||
when(mapper.countProducts(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, null);
|
||||
service.page(0L, 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(), isNull(), offset.capture(), limit.capture());
|
||||
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, null);
|
||||
service.page(Long.MAX_VALUE, 500L, 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(), any(), offsetMax.capture(), limitMax.capture());
|
||||
any(), any(), any(), any(), offsetMax.capture(), limitMax.capture());
|
||||
assertEquals(999_900L, offsetMax.getValue(), "(page-1)*page_size 最大 999_900,钳制后不得溢出变负");
|
||||
assertEquals(100, limitMax.getValue());
|
||||
}
|
||||
@@ -354,9 +354,9 @@ 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, null, null, null, from, to));
|
||||
() -> service.page(1L, 20L, null, null, null, null, null, null, null, null, from, to));
|
||||
assertTrue(ex.getMessage().contains("开始时间不能晚于结束时间"), "倒挂时间按业务异常拒绝");
|
||||
verify(mapper, never()).countProducts(any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
verify(mapper, never()).countProducts(any(), any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -365,27 +365,37 @@ class AdminJdProductControllerTest {
|
||||
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper.class),
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(0L);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(0L);
|
||||
|
||||
// 超管显式传 5 ⇒ 按公司 5 筛(收窄);groupId ≤0 按没选分组处理;文本两侧空白剥掉。
|
||||
// 类目(京麦末级类目名)走同一过滤面:传值原样直传、空白归 null。
|
||||
service.page(1L, 20L, 5L, " 店铺甲 ", " SKU-1 ", " 连衣裙 ", " 艺考形体服 ", 0L, null, null, null);
|
||||
service.page(1L, 20L, 5L, " 店铺甲 ", " SKU-1 ", " 连衣裙 ", " 艺考形体服 ", 0L, null, null, null, null);
|
||||
verify(mapper).countProducts(eq(5L), eq("店铺甲"), eq("SKU-1"), eq("连衣裙"),
|
||||
eq("艺考形体服"), isNull(), isNull(), isNull(), isNull());
|
||||
eq("艺考形体服"), isNull(), isNull(), isNull(), isNull(), isNull());
|
||||
// 全量(scopedCompanyId=null)⇒ companyId 不过滤。
|
||||
service.page(1L, 20L, null, null, null, null, null, null, null, null, null);
|
||||
service.page(1L, 20L, null, null, null, null, null, null, null, null, null, null);
|
||||
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), isNull(), isNull(), isNull());
|
||||
isNull(), isNull(), isNull(), isNull(), isNull());
|
||||
|
||||
// 未分类范围(店铺分类弹窗「未分类」页签):ungrouped=true 原样下推到 SQL 侧。
|
||||
service.page(1L, 20L, null, null, null, null, null, null, Boolean.TRUE, null, null);
|
||||
service.page(1L, 20L, null, null, null, null, null, null, Boolean.TRUE, null, null, null);
|
||||
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), eq(Boolean.TRUE), isNull(), isNull());
|
||||
isNull(), eq(Boolean.TRUE), isNull(), isNull(), isNull());
|
||||
// ungrouped=false / 缺省按不过滤处理(Filter 归一成 null,不进 IS NULL 分支);
|
||||
// 与②「全量」那发同形状,累计 2 次。
|
||||
service.page(1L, 20L, null, null, null, null, null, null, Boolean.FALSE, null, null);
|
||||
service.page(1L, 20L, null, null, null, null, null, null, Boolean.FALSE, null, null, null);
|
||||
verify(mapper, times(2)).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), isNull(), isNull(), isNull());
|
||||
isNull(), isNull(), isNull(), isNull(), isNull());
|
||||
|
||||
// 已分类档(页面筛选行「分类状态」=已分类,2026-11-15 客户口径「分类好的可以不显示」反选):
|
||||
// grouped=true 原样下推到 SQL 侧(group_id IS NOT NULL),ungrouped 不动。
|
||||
service.page(1L, 20L, null, null, null, null, null, null, null, Boolean.TRUE, null, null);
|
||||
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), isNull(), eq(Boolean.TRUE), isNull(), isNull());
|
||||
// grouped=false / 缺省同口径归一成 null(不进 IS NOT NULL 分支);与②「全量」累计 3 次。
|
||||
service.page(1L, 20L, null, null, null, null, null, null, null, Boolean.FALSE, null, null);
|
||||
verify(mapper, times(3)).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), isNull(), isNull(), isNull(), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -586,8 +596,8 @@ class AdminJdProductControllerTest {
|
||||
grouped.setGroupId(3L);
|
||||
JdProductEntity missing = productEntity(2L);
|
||||
missing.setGroupId(99L);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(2L);
|
||||
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(2L);
|
||||
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
anyLong(), anyInt())).thenReturn(List.of(grouped, missing));
|
||||
com.nanri.aiimage.modules.jd.model.entity.JdProductGroupEntity group =
|
||||
new com.nanri.aiimage.modules.jd.model.entity.JdProductGroupEntity();
|
||||
@@ -595,7 +605,7 @@ class AdminJdProductControllerTest {
|
||||
group.setName("秋冬款");
|
||||
when(groupMapper.selectBatchIds(any())).thenReturn(List.of(group));
|
||||
|
||||
JdProductPageVo vo = service.page(1L, 20L, 5L, null, null, null, null, null, null, null, null);
|
||||
JdProductPageVo vo = service.page(1L, 20L, 5L, null, null, null, null, null, null, null, null, null);
|
||||
|
||||
assertEquals("秋冬款", vo.getItems().get(0).getGroupName(), "行集 group_id 一次取齐分组名(无逐行查询)");
|
||||
assertNull(vo.getItems().get(1).getGroupName(), "分组已删=不猜名字(铁律 30),前端展示 -");
|
||||
|
||||
Reference in New Issue
Block a user