feat(jd): 京东数据库分组改店铺分类+左右分栏弹窗人工归类
This commit is contained in:
+21
-1
@@ -7,6 +7,7 @@ import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
import com.nanri.aiimage.common.service.DataReadScope;
|
||||
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.JdProductUpdateRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductCategoryVo;
|
||||
@@ -100,6 +101,8 @@ public class AdminJdProductController {
|
||||
@Parameter(description = "标题关键词(包含匹配)") @RequestParam(name = "title_keyword", required = false) String titleKeyword,
|
||||
@Parameter(description = "类目(京麦末级类目名,eq)") @RequestParam(name = "category", required = false) String category,
|
||||
@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 = "同步开始时间(包含,ISO datetime)")
|
||||
@RequestParam(name = "synced_from", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime syncedFrom,
|
||||
@Parameter(description = "同步结束时间(包含,ISO datetime)")
|
||||
@@ -112,7 +115,7 @@ public class AdminJdProductController {
|
||||
// 非超管传什么都不收——公共库对每一家管理员都是同一份全量。
|
||||
Long scopedCompanyId = dataReadScope.superAdminScopedCompanyId(user, companyId);
|
||||
return ApiResponse.success(jdProductQueryService.page(page, pageSize, scopedCompanyId, shopName,
|
||||
productCode, titleKeyword, category, groupId, syncedFrom, syncedTo));
|
||||
productCode, titleKeyword, category, groupId, ungrouped, syncedFrom, syncedTo));
|
||||
}
|
||||
|
||||
@GetMapping("/categories")
|
||||
@@ -172,6 +175,23 @@ public class AdminJdProductController {
|
||||
return ApiResponse.success(vo);
|
||||
}
|
||||
|
||||
@PostMapping("/batch-assign-group")
|
||||
@Operation(summary = "批量归类京东商品(店铺分类弹窗)", description = "勾选商品行 → 归入指定分类"
|
||||
+ "(groupId 必须真实存在)或移出分类(groupId=null,group_id 置 NULL)。与单行编辑"
|
||||
+ "同口径:归类即人工干预,落 manual_locked=1(采集 upsert/登记/覆盖清理此后跳过本行,"
|
||||
+ "分类不被重爬清理冲掉,代价是该行价格/库存随锁停更)。单次最多 200 个;id 不存在的"
|
||||
+ "行不计入更新数、不整批拒绝(弹窗行集与库行有采集刷新时间差,掉队行跳过)。")
|
||||
public ApiResponse<Integer> batchAssignGroup(
|
||||
@Valid @RequestBody JdProductBatchAssignGroupRequest body,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireJdAccess(request);
|
||||
int updated = jdProductUpdateService.assignGroup(body.getIds(), body.getGroupId());
|
||||
// 管理端写动作必须留痕:谁、把多少行归到哪个分类(null=移出分类)。
|
||||
log.info("[jd-product] 批量归类 operatorId={} requested={} updated={} groupId={}",
|
||||
operator.id(), body.getIds().size(), updated, body.getGroupId());
|
||||
return ApiResponse.success(updated);
|
||||
}
|
||||
|
||||
@PostMapping("/batch-delete")
|
||||
@Operation(summary = "批量删除京东商品", description = "与单行删除同一出口(先删桶对象、全成功才删库行);"
|
||||
+ "单次最多 200 个,id 里有一行不存在则整批拒绝(不做部分删除)。")
|
||||
|
||||
@@ -42,8 +42,9 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
|
||||
|
||||
/**
|
||||
* 后管只读页总数(Phase 5,v1.4):与 {@link #selectProductPage} 同一过滤面
|
||||
* (公司/店铺名/商品编码/标题关键词/类目/分组/同步起止,全部 SQL 侧下推,
|
||||
* 铁律 20)。{@code companyId}/{@code groupId} 为空或 ≤0=不过滤该面。
|
||||
* (公司/店铺名/商品编码/标题关键词/类目/分组/未分类/同步起止,全部 SQL 侧下推,
|
||||
* 铁律 20)。{@code companyId}/{@code groupId} 为空或 ≤0=不过滤该面;
|
||||
* {@code ungrouped=true}=只看未分类行(group_id IS NULL,店铺分类弹窗「未分类」范围)。
|
||||
*/
|
||||
long countProducts(@Param("companyId") Long companyId,
|
||||
@Param("shopName") String shopName,
|
||||
@@ -51,6 +52,7 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
|
||||
@Param("titleKeyword") String titleKeyword,
|
||||
@Param("category") String category,
|
||||
@Param("groupId") Long groupId,
|
||||
@Param("ungrouped") Boolean ungrouped,
|
||||
@Param("syncedFrom") LocalDateTime syncedFrom,
|
||||
@Param("syncedTo") LocalDateTime syncedTo);
|
||||
|
||||
@@ -92,6 +94,7 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
|
||||
@Param("titleKeyword") String titleKeyword,
|
||||
@Param("category") String category,
|
||||
@Param("groupId") Long groupId,
|
||||
@Param("ungrouped") Boolean ungrouped,
|
||||
@Param("syncedFrom") LocalDateTime syncedFrom,
|
||||
@Param("syncedTo") LocalDateTime syncedTo,
|
||||
@Param("offset") long offset,
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.nanri.aiimage.modules.jd.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后管京东商品批量归类请求体(店铺分类弹窗,2026-11-15):勾选商品行 → 归入左侧选中分类
|
||||
* 或移出分类。公共库写侧无公司维度(2026-11-15 用户定案),body 不带 company_id——
|
||||
* 公司维度的假 tab 形状随 2026-11-15 公共化已从写侧端点整批摘除。
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东商品批量归类请求(归入分类或移出分类)")
|
||||
public class JdProductBatchAssignGroupRequest {
|
||||
|
||||
@Schema(description = "商品行 id 清单(单次最多 200 个,与批量删除同档)")
|
||||
@NotEmpty(message = "请先选择要归类的商品")
|
||||
@Size(max = 200, message = "单次最多归类 200 个商品,请分批操作")
|
||||
private List<Long> ids;
|
||||
|
||||
@Schema(description = "目标分类 id;null=移出分类(group_id 置 NULL,商品回到未分类)")
|
||||
private Long groupId;
|
||||
}
|
||||
+5
-5
@@ -104,19 +104,19 @@ public class JdProductGroupService {
|
||||
/** 名字归一:trim 后必须非空。 */
|
||||
private static String requireName(String name) {
|
||||
if (name == null || name.trim().isEmpty()) {
|
||||
throw new BusinessException(400, "分组名不能为空");
|
||||
throw new BusinessException(400, "分类名不能为空");
|
||||
}
|
||||
return name.trim();
|
||||
}
|
||||
|
||||
/** 分组行存在校验:不存在一律按「分组不存在」拒绝。 */
|
||||
/** 分类行存在校验:不存在一律按「分类不存在」拒绝。 */
|
||||
private JdProductGroupEntity requireExistingGroup(Long id) {
|
||||
if (id == null || id <= 0) {
|
||||
throw new BusinessException("分组不存在");
|
||||
throw new BusinessException("分类不存在");
|
||||
}
|
||||
JdProductGroupEntity group = jdProductGroupMapper.selectById(id);
|
||||
if (group == null) {
|
||||
throw new BusinessException("分组不存在");
|
||||
throw new BusinessException("分类不存在");
|
||||
}
|
||||
return group;
|
||||
}
|
||||
@@ -127,7 +127,7 @@ public class JdProductGroupService {
|
||||
.eq(JdProductGroupEntity::getName, name)
|
||||
.ne(excludeId != null, JdProductGroupEntity::getId, excludeId));
|
||||
if (count != null && count > 0) {
|
||||
throw new BusinessException(409, "已存在同名分组:" + name);
|
||||
throw new BusinessException(409, "已存在同名分类:" + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
-8
@@ -75,10 +75,11 @@ public class JdProductQueryService {
|
||||
* 分页查询商品行({@code synced_at} 倒序)。
|
||||
*
|
||||
* <p>过滤面:公司(物化列 eq)、店铺名 contains、商品编码 contains、标题关键词 contains、
|
||||
* 类目 eq、分组 eq、同步起止(datetime 闭区间)。</p>
|
||||
* 类目 eq、分组 eq、未分类(group_id IS NULL,店铺分类弹窗「未分类」范围)、
|
||||
* 同步起止(datetime 闭区间)。</p>
|
||||
*/
|
||||
public JdProductPageVo page(Long page, Long pageSize, Long scopedCompanyId, String shopName,
|
||||
String productCode, String titleKeyword, String category, Long groupId,
|
||||
String productCode, String titleKeyword, String category, Long groupId, Boolean ungrouped,
|
||||
LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
if (syncedFrom != null && syncedTo != null && syncedFrom.isAfter(syncedTo)) {
|
||||
throw new BusinessException("开始时间不能晚于结束时间");
|
||||
@@ -87,13 +88,13 @@ 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, syncedFrom, syncedTo);
|
||||
groupId, ungrouped, syncedFrom, syncedTo);
|
||||
long total = jdProductMapper.countProducts(filter.companyId(), filter.shopName(), filter.productCode(),
|
||||
filter.titleKeyword(), filter.category(), filter.groupId(),
|
||||
filter.titleKeyword(), filter.category(), filter.groupId(), filter.ungrouped(),
|
||||
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.titleKeyword(), filter.category(), filter.groupId(), filter.ungrouped(),
|
||||
filter.syncedFrom(), filter.syncedTo(), (safePage - 1) * safeSize, (int) safeSize);
|
||||
List<JdProductListItemVo> items = rows.stream().map(this::toListItem).toList();
|
||||
fillGroupNames(items);
|
||||
@@ -171,15 +172,17 @@ public class JdProductQueryService {
|
||||
|
||||
/** 服务侧过滤面归一:companyId/groupId ≤0 按没选处理,文本 trim 后空串归 null。 */
|
||||
private record Filter(Long companyId, String shopName, String productCode, String titleKeyword,
|
||||
String category, Long groupId, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
String category, Long groupId, Boolean ungrouped, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
}
|
||||
|
||||
private static Filter filter(Long scopedCompanyId, String shopName, String productCode, String titleKeyword,
|
||||
String category, Long groupId, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
String category, Long groupId, Boolean ungrouped, 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;
|
||||
return new Filter(companyId, trimToNull(shopName), trimToNull(productCode), trimToNull(titleKeyword),
|
||||
trimToNull(category), safeGroupId, syncedFrom, syncedTo);
|
||||
trimToNull(category), safeGroupId, safeUngrouped, syncedFrom, syncedTo);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 列表/详情搬运
|
||||
|
||||
+46
-1
@@ -54,6 +54,9 @@ public class JdProductUpdateService {
|
||||
|
||||
private static final String LOG_TAG = "jd-update";
|
||||
|
||||
/** 单次批量归类上限(与批量删除同档,防脏包一次写爆)。 */
|
||||
private static final int MAX_ASSIGN_IDS = 200;
|
||||
|
||||
/** 单次属性分组/属性行上限(防脏包一次写爆 attr_json;京麦 8 个 Tab 量级)。 */
|
||||
private static final int MAX_ATTR_GROUPS = 64;
|
||||
private static final int MAX_ATTRS_PER_GROUP = 200;
|
||||
@@ -97,7 +100,7 @@ public class JdProductUpdateService {
|
||||
if (groupId != null) {
|
||||
JdProductGroupEntity group = jdProductGroupMapper.selectById(groupId);
|
||||
if (group == null) {
|
||||
throw new BusinessException("分组不存在,请刷新分组后重试");
|
||||
throw new BusinessException("分类不存在,请刷新分类后重试");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,6 +153,48 @@ public class JdProductUpdateService {
|
||||
|
||||
// ---------- 校验 ----------
|
||||
|
||||
/**
|
||||
* 批量人工归类(店铺分类弹窗,2026-11-15):勾选商品行 → 归入指定分类或移出分类。
|
||||
*
|
||||
* <p>与单行编辑同口径(用户拍板「置锁」):归类即人工干预,落 {@code manual_locked=1}——
|
||||
* 采集 upsert 本就不写 group_id,置锁后覆盖清理也不再把这些行整行删掉,分类永不丢;
|
||||
* 代价是该行的价格/库存等字段随锁停更(与编辑保存同一语义)。</p>
|
||||
*
|
||||
* <p>{@code groupId} null=移出分类(group_id 置 NULL 回到未分类);非 null 时必须真实
|
||||
* 存在(分类全平台一份,无公司维度)。只写 group_id 与锁两个列,不动其它字段——
|
||||
* 与 {@link #update} 的全量覆写不同,这是行集级轻touch。</p>
|
||||
*
|
||||
* @return 实际更新的行数(id 不存在/已被删的行不计入,不按不存在整批拒绝——
|
||||
* 弹窗行集与库行之间天然有采集刷新的时间差,掉队的行跳过即可)
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int assignGroup(List<Long> ids, Long groupId) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
throw new BusinessException("请先选择要归类的商品");
|
||||
}
|
||||
List<Long> distinctIds = ids.stream().filter(id -> id != null && id > 0).distinct().toList();
|
||||
if (distinctIds.isEmpty()) {
|
||||
throw new BusinessException("请先选择要归类的商品");
|
||||
}
|
||||
if (distinctIds.size() > MAX_ASSIGN_IDS) {
|
||||
throw new BusinessException("单次最多归类 " + MAX_ASSIGN_IDS + " 个商品,请分批操作");
|
||||
}
|
||||
if (groupId != null) {
|
||||
JdProductGroupEntity group = jdProductGroupMapper.selectById(groupId);
|
||||
if (group == null) {
|
||||
throw new BusinessException("分类不存在,请刷新分类后重试");
|
||||
}
|
||||
}
|
||||
int updated = jdProductMapper.update(null, new LambdaUpdateWrapper<JdProductEntity>()
|
||||
.in(JdProductEntity::getId, distinctIds)
|
||||
.set(JdProductEntity::getGroupId, groupId)
|
||||
// 人工编辑锁:归类即人工干预,采集链路(upsert/登记/覆盖清理)此后跳过本行。
|
||||
.set(JdProductEntity::getManualLocked, 1));
|
||||
log.info("[{}] 批量归类 requested={} distinct={} groupId={} updated={}",
|
||||
LOG_TAG, ids.size(), distinctIds.size(), groupId, updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
private void validateBody(JdProductUpdateRequest body) {
|
||||
if (body.getPriceMin() != null && body.getPriceMax() != null
|
||||
&& body.getPriceMin().compareTo(body.getPriceMax()) > 0) {
|
||||
|
||||
@@ -101,7 +101,8 @@
|
||||
</select>
|
||||
|
||||
<!-- 后管只读页(Phase 5,v1.4)过滤面:公司(物化列 eq)、店铺名/商品编码/标题关键词
|
||||
(contains LIKE)、类目 eq、分组 eq、同步起止(datetime 闭区间)。
|
||||
(contains LIKE)、类目 eq、分组 eq、未分类(group_id IS NULL,店铺分类弹窗
|
||||
「未分类」范围)、同步起止(datetime 闭区间)。
|
||||
sale_status 筛选随 2026-11-15 客户口径撤掉(前端筛选框下线;列本身与详情/回传链不动)。
|
||||
全部走 #{} 占位符;offset/limit 必须是服务侧钳制后的值(铁律 15),mapper 不做第二次换算。 -->
|
||||
<sql id="productConds">
|
||||
@@ -111,6 +112,7 @@
|
||||
<if test="titleKeyword != null and titleKeyword != ''">AND p.title LIKE CONCAT('%', #{titleKeyword}, '%')</if>
|
||||
<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="syncedFrom != null">AND p.synced_at >= #{syncedFrom}</if>
|
||||
<if test="syncedTo != null">AND p.synced_at <= #{syncedTo}</if>
|
||||
</sql>
|
||||
|
||||
+73
-19
@@ -90,6 +90,9 @@ class AdminJdProductControllerTest {
|
||||
assertTrue(source.contains("@DeleteMapping"), "删除端点必须落在 @DeleteMapping");
|
||||
assertTrue(source.contains("\"/batch-delete\""), "批量删除端点必须是 POST /batch-delete");
|
||||
assertTrue(source.contains("@Valid"), "批量删除请求体必须过 @Valid(ids 非空)");
|
||||
// 批量归类端点(店铺分类弹窗,2026-11-15):POST /batch-assign-group,归入/移出分类。
|
||||
assertTrue(source.contains("\"/batch-assign-group\""),
|
||||
"批量归类端点必须是 POST /batch-assign-group(店铺分类弹窗人工归类)");
|
||||
// PUT 旧绊线(「不得有 PUT:商品行只能被采集链回传覆盖」)已随后台「编辑商品」特性放行
|
||||
// (AdminJdProductController 的 @PutMapping("/{id}",JdProductUpdateService 承接);商品行
|
||||
// 采集回传覆盖链不变,人工改名只走这条 PUT。分组改排序仍落 AdminJdProductGroupController。
|
||||
@@ -125,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", "synced_from", "synced_to", "company_id")) {
|
||||
"group_id", "ungrouped", "synced_from", "synced_to", "company_id")) {
|
||||
assertTrue(source.contains(param), "查询参数必须 snake_case:" + param);
|
||||
}
|
||||
// 商品状态筛选框已随 2026-11-15 客户口径从前端下线 ⇒ 服务端同名参数一起撤(反向钉住防回潜)。
|
||||
@@ -193,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)).thenReturn(pageVo);
|
||||
when(queryService.page(1L, 20L, 5L, null, null, null, null, null, null, null, null)).thenReturn(pageVo);
|
||||
|
||||
ApiResponse<JdProductPageVo> response = controller.page(1L, 20L, null, null, null, null, null,
|
||||
null, null, 5L, request);
|
||||
null, null, null, 5L, request);
|
||||
|
||||
verify(dataReadScope).superAdminScopedCompanyId(operator, 5L);
|
||||
verify(queryService).page(1L, 20L, 5L, null, null, null, null, null, null, null);
|
||||
verify(queryService).page(1L, 20L, 5L, null, null, null, null, null, null, null, null);
|
||||
assertEquals(Boolean.TRUE, response.isSuccess(), "拒绝走 403 业务码,放行必须是 success 信封(B80-03)");
|
||||
assertEquals(pageVo, response.getData());
|
||||
}
|
||||
@@ -321,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())).thenReturn(1L);
|
||||
when(mapper.countProducts(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);
|
||||
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(), 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);
|
||||
service.page(Long.MAX_VALUE, 500L, 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());
|
||||
any(), any(), any(), offsetMax.capture(), limitMax.capture());
|
||||
assertEquals(999_900L, offsetMax.getValue(), "(page-1)*page_size 最大 999_900,钳制后不得溢出变负");
|
||||
assertEquals(100, limitMax.getValue());
|
||||
}
|
||||
@@ -351,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, from, to));
|
||||
() -> service.page(1L, 20L, 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());
|
||||
verify(mapper, never()).countProducts(any(), any(), any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -362,17 +365,68 @@ 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())).thenReturn(0L);
|
||||
when(mapper.countProducts(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);
|
||||
service.page(1L, 20L, 5L, " 店铺甲 ", " SKU-1 ", " 连衣裙 ", " 艺考形体服 ", 0L, null, null, null);
|
||||
verify(mapper).countProducts(eq(5L), eq("店铺甲"), eq("SKU-1"), eq("连衣裙"),
|
||||
eq("艺考形体服"), isNull(), isNull(), isNull());
|
||||
eq("艺考形体服"), isNull(), isNull(), isNull(), isNull());
|
||||
// 全量(scopedCompanyId=null)⇒ companyId 不过滤。
|
||||
service.page(1L, 20L, null, null, null, null, null, null, null, null);
|
||||
service.page(1L, 20L, null, null, null, null, null, null, null, null, null);
|
||||
verify(mapper).countProducts(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);
|
||||
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), eq(Boolean.TRUE), isNull(), isNull());
|
||||
// ungrouped=false / 缺省按不过滤处理(Filter 归一成 null,不进 IS NULL 分支);
|
||||
// 与②「全量」那发同形状,累计 2 次。
|
||||
service.page(1L, 20L, null, null, null, null, null, null, Boolean.FALSE, null, null);
|
||||
verify(mapper, times(2)).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), isNull(), isNull(), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 批量归类端点走同一鉴权正门且参数透传() {
|
||||
JdProductQueryService queryService = mock(JdProductQueryService.class);
|
||||
JdProductUpdateService updateService = mock(JdProductUpdateService.class);
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
DataReadScope dataReadScope = mock(DataReadScope.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, mock(JdProductDeleteService.class), updateService, authSupport,
|
||||
mock(PermissionMenuService.class), dataReadScope);
|
||||
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(7L);
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
when(authSupport.requireUser(request)).thenReturn(operator);
|
||||
when(authSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
when(updateService.assignGroup(List.of(11L, 12L), 3L)).thenReturn(2);
|
||||
|
||||
com.nanri.aiimage.modules.jd.model.dto.JdProductBatchAssignGroupRequest body =
|
||||
new com.nanri.aiimage.modules.jd.model.dto.JdProductBatchAssignGroupRequest();
|
||||
body.setIds(List.of(11L, 12L));
|
||||
body.setGroupId(3L);
|
||||
ApiResponse<Integer> response = controller.batchAssignGroup(body, request);
|
||||
|
||||
// 公共库写侧没有公司维度:不收 company_id、DataReadScope 不参与。
|
||||
verifyNoInteractions(dataReadScope);
|
||||
verify(updateService).assignGroup(List.of(11L, 12L), 3L);
|
||||
assertEquals(Boolean.TRUE, response.isSuccess(), "放行必须是 success 信封(B80-03)");
|
||||
assertEquals(2, response.getData(), "回执=实际更新的行数");
|
||||
|
||||
// 业务失败(目标分类不存在)文案原样透出,不吞。
|
||||
when(updateService.assignGroup(List.of(11L), 404L))
|
||||
.thenThrow(new BusinessException("分类不存在,请刷新分类后重试"));
|
||||
com.nanri.aiimage.modules.jd.model.dto.JdProductBatchAssignGroupRequest ghostBody =
|
||||
new com.nanri.aiimage.modules.jd.model.dto.JdProductBatchAssignGroupRequest();
|
||||
ghostBody.setIds(List.of(11L));
|
||||
ghostBody.setGroupId(404L);
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> controller.batchAssignGroup(ghostBody, request));
|
||||
assertTrue(ex.getMessage().contains("分类不存在"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -532,8 +586,8 @@ class AdminJdProductControllerTest {
|
||||
grouped.setGroupId(3L);
|
||||
JdProductEntity missing = productEntity(2L);
|
||||
missing.setGroupId(99L);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(2L);
|
||||
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
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(),
|
||||
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();
|
||||
@@ -541,7 +595,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);
|
||||
JdProductPageVo vo = service.page(1L, 20L, 5L, null, null, null, null, null, null, null, null);
|
||||
|
||||
assertEquals("秋冬款", vo.getItems().get(0).getGroupName(), "行集 group_id 一次取齐分组名(无逐行查询)");
|
||||
assertNull(vo.getItems().get(1).getGroupName(), "分组已删=不猜名字(铁律 30),前端展示 -");
|
||||
|
||||
+2
-2
@@ -64,7 +64,7 @@ class JdProductGroupServiceTest {
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.create(" 秋冬款 ", 2));
|
||||
assertEquals(409, ex.getCode(), "重名冲突=409(语义正确的资源冲突码,仓内 DeviceLogService 有先例)");
|
||||
assertTrue(ex.getMessage().contains("同名分组"), "重名必须给可读文案,不拿 DB 唯一键异常撞 500");
|
||||
assertTrue(ex.getMessage().contains("同名分类"), "重名必须给可读文案,不拿 DB 唯一键异常撞 500(2026-11-15 起界面措辞「分类」)");
|
||||
verify(groupMapper, never()).insert(any(JdProductGroupEntity.class));
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ class JdProductGroupServiceTest {
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.rename(1L, "新名字", null));
|
||||
assertTrue(ex.getMessage().contains("分组不存在"));
|
||||
assertTrue(ex.getMessage().contains("分类不存在"));
|
||||
verify(groupMapper, never()).updateById(any(JdProductGroupEntity.class));
|
||||
}
|
||||
|
||||
|
||||
+80
-2
@@ -172,17 +172,95 @@ class JdProductUpdateServiceTest {
|
||||
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
|
||||
assertThat(service.update(PRODUCT_ID, body)).isEqualTo(PRODUCT_ID);
|
||||
|
||||
// 分组不存在 ⇒ 拒绝且零写。
|
||||
// 分组不存在 ⇒ 拒绝且零写(2026-11-15 起界面措辞「分类」)。
|
||||
JdProductUpdateRequest ghost = mainFieldsOnly();
|
||||
ghost.setGroupId(404L);
|
||||
when(jdProductGroupMapper.selectById(404L)).thenReturn(null);
|
||||
assertThatThrownBy(() -> service.update(PRODUCT_ID, ghost))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("分组不存在");
|
||||
.hasMessageContaining("分类不存在");
|
||||
// update 只剩成功挂组那 1 次:ghost 分支零写点(同用例前半段已成功写过一次,不能按 never 数)。
|
||||
verify(jdProductMapper, org.mockito.Mockito.times(1)).update(isNull(), any(Wrapper.class));
|
||||
}
|
||||
|
||||
// ---------- 批量归类(店铺分类弹窗,2026-11-15) ----------
|
||||
|
||||
/** 取批量归类落下的 SET 键值对(paramNameValuePairs),供断言 groupId/ids 落值。 */
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> capturedAssignValues() {
|
||||
ArgumentCaptor<Wrapper<JdProductEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
|
||||
verify(jdProductMapper).update(isNull(), captor.capture());
|
||||
LambdaUpdateWrapper<JdProductEntity> wrapper = (LambdaUpdateWrapper<JdProductEntity>) captor.getValue();
|
||||
return (Map<String, Object>) ReflectionTestUtils.getField(wrapper, "paramNameValuePairs");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 批量归类成功_置分类并落人工编辑锁() {
|
||||
JdProductGroupEntity group = new JdProductGroupEntity();
|
||||
group.setId(7L);
|
||||
when(jdProductGroupMapper.selectById(7L)).thenReturn(group);
|
||||
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(3);
|
||||
|
||||
// 入参含重复 id:去重后 1/2/3 三行
|
||||
int updated = service.assignGroup(List.of(1L, 1L, 2L, 3L), 7L);
|
||||
|
||||
assertThat(updated).isEqualTo(3);
|
||||
ArgumentCaptor<Wrapper<JdProductEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
|
||||
verify(jdProductMapper).update(isNull(), captor.capture());
|
||||
LambdaUpdateWrapper<JdProductEntity> wrapper = (LambdaUpdateWrapper<JdProductEntity>) captor.getValue();
|
||||
// SET 段必须同时带 group_id 与 manual_locked(归类即人工干预:置锁后覆盖清理不再删行)
|
||||
assertThat(wrapper.getSqlSet()).contains("group_id").contains("manual_locked");
|
||||
// WHERE 段(IN ids)惰性构参:先触发 getSqlSegment 再读参数表,IN 值才落进 paramNameValuePairs。
|
||||
wrapper.getSqlSegment();
|
||||
Map<String, Object> values = capturedAssignValues();
|
||||
assertThat(values.values()).contains(7L, 1L, 2L, 3L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 批量归类目标分类不存在拒绝且零写() {
|
||||
when(jdProductGroupMapper.selectById(404L)).thenReturn(null);
|
||||
|
||||
assertThatThrownBy(() -> service.assignGroup(List.of(1L, 2L), 404L))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("分类不存在");
|
||||
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 批量归类空清单与超上限可读拒绝() {
|
||||
assertThatThrownBy(() -> service.assignGroup(List.of(), null))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("请先选择要归类的商品");
|
||||
assertThatThrownBy(() -> service.assignGroup(List.of(0L, -1L), null))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("请先选择要归类的商品");
|
||||
|
||||
List<Long> tooMany = new java.util.ArrayList<>();
|
||||
for (long i = 1; i <= 201; i++) {
|
||||
tooMany.add(i);
|
||||
}
|
||||
assertThatThrownBy(() -> service.assignGroup(tooMany, null))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("单次最多归类 200");
|
||||
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 移出分类_groupId置空仍落人工编辑锁() {
|
||||
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(2);
|
||||
|
||||
int updated = service.assignGroup(List.of(5L, 6L), null);
|
||||
|
||||
assertThat(updated).isEqualTo(2);
|
||||
ArgumentCaptor<Wrapper<JdProductEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
|
||||
verify(jdProductMapper).update(isNull(), captor.capture());
|
||||
LambdaUpdateWrapper<JdProductEntity> wrapper = (LambdaUpdateWrapper<JdProductEntity>) captor.getValue();
|
||||
// groupId=null 也显式落 SET(group_id=NULL=回到未分类),锁照落。
|
||||
assertThat(wrapper.getSqlSet()).contains("group_id").contains("manual_locked");
|
||||
// 移出分类没有目标组:不查分组表。
|
||||
verify(jdProductGroupMapper, never()).selectById(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 属性重建只覆写tabs段其余顶层键保留() {
|
||||
givenOwnedRow();
|
||||
|
||||
Reference in New Issue
Block a user