feat(jd): 京东数据库分组改店铺分类+左右分栏弹窗人工归类

This commit is contained in:
2026-11-15 21:46:26 +08:00
parent 91cf5dfaf7
commit e175e85973
18 changed files with 1022 additions and 144 deletions
@@ -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,
@@ -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;
}
@@ -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);
}
}
}
@@ -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);
}
// ---------------------------------------------------------------- 列表/详情搬运
@@ -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 &gt; 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 &gt;= #{syncedFrom}</if>
<if test="syncedTo != null">AND p.synced_at &lt;= #{syncedTo}</if>
</sql>