feat(jd): 列表列对齐+主图投影+SKU 矩阵+删除链收口(B101/B102 服务端与后管)
This commit is contained in:
+52
-3
@@ -8,8 +8,11 @@ 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.common.support.DownloadHeaderUtil;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdProductBatchDeleteRequest;
|
||||
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.JdProductDeleteService;
|
||||
import com.nanri.aiimage.modules.jd.service.JdProductQueryService;
|
||||
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
@@ -19,25 +22,35 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后管「京东数据库」只读页(v1.4 Phase 5)。数据由采集回传同步落表
|
||||
* ({@code JdResultIngestService},覆盖最新),本页只读——列表/详情/导出三个 GET 端点,
|
||||
* 不出表、不改商品行、无导入;分组管理走 {@code AdminJdProductGroupController}(同款鉴权闸)。
|
||||
* 后管「京东数据库」(v1.4 Phase 5)。数据由采集回传同步落表
|
||||
* ({@code JdResultIngestService},覆盖最新):列表/详情/导出三个 GET 端点 +
|
||||
* 删除(单行 {@code DELETE /{id}} 与批量 {@code POST /batch-delete},用户口径
|
||||
* 「后台删除数据的时候也要把桶对应的文件删除」);分组管理走
|
||||
* {@code AdminJdProductGroupController}(同款鉴权闸)。
|
||||
*
|
||||
* <p>删除是唯一会改数据的动作,语义写在 {@code JdProductDeleteService}:先删 {@code jd-products}
|
||||
* 桶内对象、全成功才删库行(SKU → 图片登记 → 商品行),被其他商品共用的图片对象保留。</p>
|
||||
*
|
||||
* <p>鉴权照外观专利只读页私有 {@code requireXxxAccess} 形状:JWT → 内部令牌回退 → 超管放行 →
|
||||
* 非超管菜单权限(column_key/route_path 双匹配)。菜单行由 V196 只插 columns 不预授权
|
||||
@@ -62,6 +75,7 @@ public class AdminJdProductController {
|
||||
private static final DateTimeFormatter EXPORT_FILENAME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
|
||||
|
||||
private final JdProductQueryService jdProductQueryService;
|
||||
private final JdProductDeleteService jdProductDeleteService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final PermissionMenuService permissionMenuService;
|
||||
private final DataReadScope dataReadScope;
|
||||
@@ -139,6 +153,41 @@ public class AdminJdProductController {
|
||||
.body(bytes);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@Operation(summary = "删除单个京东商品", description = "先删 jd-products 桶内该商品的图片对象、"
|
||||
+ "全部成功后才删库行(SKU → 图片登记 → 商品行);被其他商品共用的图片对象保留。"
|
||||
+ "任何对象删不掉就整批拒绝、一行库行都不动。越界 id 按不存在拒绝。")
|
||||
public ApiResponse<JdProductDeleteVo> delete(
|
||||
@Parameter(description = "商品行 id") @PathVariable("id") Long id,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireJdAccess(request);
|
||||
AdminUserEntity user = operator.user();
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, companyId);
|
||||
JdProductDeleteVo vo = jdProductDeleteService.delete(List.of(id), scopedCompanyId);
|
||||
// 管理端破坏性动作必须留痕:谁、删了哪些行、桶里清了几个对象(同分组删除口径)。
|
||||
log.info("[jd-product] 删除商品 operatorId={} companyId={} id={} deleted={} objectsDeleted={}",
|
||||
operator.id(), scopedCompanyId, id, vo.getDeleted(), vo.getObjectsDeleted());
|
||||
return ApiResponse.success(vo);
|
||||
}
|
||||
|
||||
@PostMapping("/batch-delete")
|
||||
@Operation(summary = "批量删除京东商品", description = "与单行删除同一出口(先删桶对象、全成功才删库行);"
|
||||
+ "单次最多 200 个,id 里有一行不存在或不属于当前公司则整批拒绝(不做部分删除)。")
|
||||
public ApiResponse<JdProductDeleteVo> batchDelete(
|
||||
@Valid @RequestBody JdProductBatchDeleteRequest body,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireJdAccess(request);
|
||||
AdminUserEntity user = operator.user();
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, companyId);
|
||||
JdProductDeleteVo vo = jdProductDeleteService.delete(body.getIds(), scopedCompanyId);
|
||||
log.info("[jd-product] 批量删除商品 operatorId={} companyId={} requested={} deleted={} objectsDeleted={} kept={}",
|
||||
operator.id(), scopedCompanyId, body.getIds().size(), vo.getDeleted(),
|
||||
vo.getObjectsDeleted(), vo.getObjectsKept());
|
||||
return ApiResponse.success(vo);
|
||||
}
|
||||
|
||||
private RequestOperator requireJdAccess(HttpServletRequest request) {
|
||||
AdminUserEntity operator = resolveOperator(request);
|
||||
if (operator == null || operator.getId() == null || operator.getId() <= 0) {
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.nanri.aiimage.modules.jd.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductSkuEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东商品 SKU 矩阵(biz_jd_product_sku,V198)数据访问口。
|
||||
*
|
||||
* <p>写入口唯一是 {@link #upsertBatch}:按唯一键 {@code uk_product_sku(product_id, sku_id)}
|
||||
* upsert——同商品同 SKU 重复采集刷新价格/库存/名称,行数不增;SQL 全落
|
||||
* {@code mapper/jd/JdProductSkuMapper.xml}(铁律 1)。</p>
|
||||
*
|
||||
* <p>不做 delete:京麦侧删掉某 SKU 后旧行会留着(与商品主表同口径「只覆盖不删行」),
|
||||
* 但 SKU 数真源仍是商品行的 {@code sku_count}(列表列),抽屉里的矩阵按本表逐行展示。</p>
|
||||
*/
|
||||
@Mapper
|
||||
public interface JdProductSkuMapper extends BaseMapper<JdProductSkuEntity> {
|
||||
|
||||
/** 多值批量 upsert(冲突键 uk_product_sku);单商品 SKU 数上限由调用方钳制。 */
|
||||
int upsertBatch(@Param("rows") List<JdProductSkuEntity> rows);
|
||||
}
|
||||
+13
@@ -93,6 +93,15 @@ public class JdIngestChunkRequest {
|
||||
|
||||
private String skuId;
|
||||
|
||||
/** 货号(京麦列表行 itemNum,V197;与 skuId 不同)。 */
|
||||
private String itemNum;
|
||||
|
||||
/** SKU 数(京麦列表行 productSkuInfoVO.skuCount,V197)。 */
|
||||
private Integer skuCount;
|
||||
|
||||
/** 信息分(京麦列表行 healthScoreVO.score,V197)。 */
|
||||
private Integer healthScore;
|
||||
|
||||
private String brand;
|
||||
|
||||
private String brandId;
|
||||
@@ -115,6 +124,10 @@ public class JdIngestChunkRequest {
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private String jdCreatedAt;
|
||||
|
||||
/** 京东侧上架时间(yyyy-MM-dd HH:mm:ss;京麦列表「上架时间」列,V197)。 */
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private String jdOnlineAt;
|
||||
|
||||
/** 京东侧修改时间(yyyy-MM-dd HH:mm:ss)。 */
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private String jdUpdatedAt;
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.nanri.aiimage.modules.jd.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后管批量删除京东商品的请求体(v1.4 补口)。公司隔离不走 body——`company_id` 是请求参数,
|
||||
* 由 `DataReadScope#readCompanyIdOrOwn` 统一收口(同本模块全部分页/导出/分组端点),
|
||||
* 免得 body 里再出现一次"公司不传就当全公司"的假 tab 形状。
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "批量删除京东商品请求")
|
||||
public class JdProductBatchDeleteRequest {
|
||||
|
||||
@Schema(description = "商品行 id 清单(单次最多 200 个)")
|
||||
@NotEmpty(message = "请先选择要删除的商品")
|
||||
private List<Long> ids;
|
||||
}
|
||||
+8
@@ -39,6 +39,12 @@ public class JdProductEntity {
|
||||
private String productCode;
|
||||
/** 京东 SKU/货号。 */
|
||||
private String skuId;
|
||||
/** 货号(京麦列表行 {@code itemNum},V197;与 {@code skuId} 不是同一个值)。 */
|
||||
private String itemNum;
|
||||
/** SKU 数(京麦列表行 {@code productSkuInfoVO.skuCount},V197)。 */
|
||||
private Integer skuCount;
|
||||
/** 信息分(京麦列表行 {@code healthScoreVO.score},V197)。 */
|
||||
private Integer healthScore;
|
||||
private String title;
|
||||
private String brand;
|
||||
private String brandId;
|
||||
@@ -55,6 +61,8 @@ public class JdProductEntity {
|
||||
private String listTab;
|
||||
/** 京东侧创建时间。 */
|
||||
private LocalDateTime jdCreatedAt;
|
||||
/** 京东侧上架时间(V197,京麦列表行的上架时间列,与创建时间不是同一个值)。 */
|
||||
private LocalDateTime jdOnlineAt;
|
||||
/** 京东侧修改时间。 */
|
||||
private LocalDateTime jdUpdatedAt;
|
||||
/** 自有桶主图 URL(京东原图域名不落库,AC-4)。 */
|
||||
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.nanri.aiimage.modules.jd.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 京东商品 SKU 行({@code biz_jd_product_sku},V198,v1.4 详情面补齐)。
|
||||
*
|
||||
* <p>京麦详情页「销售属性」区是一个 SKU 矩阵(真机样本 12 行 = 颜色 3 × 尺码 4),
|
||||
* 每行有自己的 {@code skuId} / 价格 / 库存 / 销售属性组合。幂等键
|
||||
* {@code uk_product_sku(product_id, sku_id)}:二次采集 upsert,行数不增。</p>
|
||||
*
|
||||
* <p>{@code saleAttrsJson} 存组合维度({@code [{"name":"颜色","value":"上衣"},…]})——
|
||||
* 京麦把它当展示维度而不是「商品属性」,所以不并进 {@code attr_json}(那列是属性三元组正本)。</p>
|
||||
*/
|
||||
@Data
|
||||
@TableName("biz_jd_product_sku")
|
||||
public class JdProductSkuEntity {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
/** 商品行 id(biz_jd_product.id,不是京麦 productId)。 */
|
||||
private Long productId;
|
||||
/** 物化归属分公司(与商品行一致)。 */
|
||||
private Long companyId;
|
||||
/** 京麦 SKU id(真机样本 10238877506570)。 */
|
||||
private String skuId;
|
||||
/** SKU 名(京麦 skuName,含销售属性后缀)。 */
|
||||
private String skuName;
|
||||
/** 京麦 skuSeq(商家自定义序号)。 */
|
||||
private String skuSeq;
|
||||
/** SKU 京东价(京麦 jdPrice)。 */
|
||||
private BigDecimal price;
|
||||
/** SKU 库存(京麦 stockNum)。 */
|
||||
private Integer stock;
|
||||
/** 销售属性组合 JSON([{"name":"颜色","value":"上衣"},…])。 */
|
||||
private String saleAttrsJson;
|
||||
/** 同商品内排序(详情抽屉 SKU 矩阵顺序)。 */
|
||||
private Integer sortNo;
|
||||
/** 同步时刻(upsert 刷新)。 */
|
||||
private LocalDateTime syncedAt;
|
||||
}
|
||||
@@ -21,6 +21,9 @@ public class JdIngestChunkVo {
|
||||
|
||||
private int imagesRegistered;
|
||||
|
||||
/** SKU 矩阵落库行数(V198 详情面补齐;客户端未带 detail.skus 时恒 0)。 */
|
||||
private int skusRegistered;
|
||||
|
||||
/** 点名原因(accept=false 时给采集端与日志看)。 */
|
||||
private String message;
|
||||
}
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 京东数据库「删除商品」回执(v1.4 补口,用户口径「后台删除数据的时候也要把桶对应的文件删除」)。
|
||||
*
|
||||
* <p>删除是先删桶、全成功才删库行(见 {@code JdProductDeleteService}):任何一个对象删不掉
|
||||
* 就整批拒绝、回滚,不留"库里没有、桶里还在"的孤儿。因此本 VO 只在**真的删成功**时返回。</p>
|
||||
*
|
||||
* <p>三个对象计数分开报,是因为它们对用户意义不同:{@code objectsDeleted} 是这次真清掉的图,
|
||||
* {@code objectsKept} 是**仍被别的商品引用**的内容寻址对象(同公司同内容同 key,删了会让别的
|
||||
* 商品裂图),{@code objectsSkipped} 是 key 不在配置前缀内、不敢动的历史脏行(只留 WARN,不动手)。</p>
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "删除京东商品回执")
|
||||
public class JdProductDeleteVo {
|
||||
|
||||
@Schema(description = "实际删除的商品行数")
|
||||
private int deleted;
|
||||
|
||||
@Schema(description = "随之删除的图片登记行数")
|
||||
private int imagesDeleted;
|
||||
|
||||
@Schema(description = "jd-products 桶内实际删除的对象数")
|
||||
private int objectsDeleted;
|
||||
|
||||
@Schema(description = "仍被其他商品引用而保留的对象数(共用图不删)")
|
||||
private int objectsKept;
|
||||
|
||||
@Schema(description = "key 不在配置前缀内、未删除的对象数(脏行留痕)")
|
||||
private int objectsSkipped;
|
||||
}
|
||||
+54
@@ -41,6 +41,15 @@ public class JdProductDetailVo {
|
||||
@Schema(description = "京东 SKU/货号")
|
||||
private String skuId;
|
||||
|
||||
@Schema(description = "货号(京麦列表列,与 SKU 不同)")
|
||||
private String itemNum;
|
||||
|
||||
@Schema(description = "SKU 数(京麦列表列)")
|
||||
private Integer skuCount;
|
||||
|
||||
@Schema(description = "信息分(京麦列表列)")
|
||||
private Integer healthScore;
|
||||
|
||||
@Schema(description = "商品标题")
|
||||
private String title;
|
||||
|
||||
@@ -71,6 +80,9 @@ public class JdProductDetailVo {
|
||||
@Schema(description = "京东侧创建时间")
|
||||
private LocalDateTime jdCreatedAt;
|
||||
|
||||
@Schema(description = "京东侧上架时间")
|
||||
private LocalDateTime jdOnlineAt;
|
||||
|
||||
@Schema(description = "京东侧修改时间")
|
||||
private LocalDateTime jdUpdatedAt;
|
||||
|
||||
@@ -104,6 +116,9 @@ public class JdProductDetailVo {
|
||||
@Schema(description = "已转存图片(自有域名 URL)")
|
||||
private List<ImageViewVo> images;
|
||||
|
||||
@Schema(description = "SKU 矩阵(京麦详情页「销售属性」区,V198;旧数据为空列表)")
|
||||
private List<SkuVo> skus;
|
||||
|
||||
/** attr_json 的一个 Tab:{@code {"tab":"商品基本信息","attrs":[...]}}。 */
|
||||
@Data
|
||||
@Schema(description = "属性分组(一个 Tab 一组)")
|
||||
@@ -157,4 +172,43 @@ public class JdProductDetailVo {
|
||||
@Schema(description = "图片高(像素,可空)")
|
||||
private Integer height;
|
||||
}
|
||||
|
||||
/** 一个 SKU(biz_jd_product_sku 行的读侧投影,京麦详情页「销售属性」区)。 */
|
||||
@Data
|
||||
@Schema(description = "SKU 行")
|
||||
public static class SkuVo {
|
||||
|
||||
@Schema(description = "京麦 SKU id(真机样本 10238877506570)")
|
||||
private String skuId;
|
||||
|
||||
@Schema(description = "SKU 名(京麦 skuName)")
|
||||
private String skuName;
|
||||
|
||||
@Schema(description = "商家自定义序号(京麦 skuSeq)")
|
||||
private String skuSeq;
|
||||
|
||||
@Schema(description = "SKU 京东价")
|
||||
private BigDecimal price;
|
||||
|
||||
@Schema(description = "SKU 库存")
|
||||
private Integer stock;
|
||||
|
||||
@Schema(description = "销售属性组合([{\"name\":\"颜色\",\"value\":\"上衣\"}])")
|
||||
private List<SkuAttrVo> saleAttrs;
|
||||
|
||||
@Schema(description = "同商品内排序")
|
||||
private Integer sortNo;
|
||||
}
|
||||
|
||||
/** 销售属性组合的一项(颜色=上衣 / 尺码=S)。 */
|
||||
@Data
|
||||
@Schema(description = "销售属性组合项")
|
||||
public static class SkuAttrVo {
|
||||
|
||||
@Schema(description = "属性名(京麦 attrName)")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "属性值(京麦 attrValueAlias)")
|
||||
private String value;
|
||||
}
|
||||
}
|
||||
|
||||
+15
@@ -31,6 +31,15 @@ public class JdProductListItemVo {
|
||||
@Schema(description = "京东 SKU/货号")
|
||||
private String skuId;
|
||||
|
||||
@Schema(description = "货号(京麦列表列,与 SKU 不同)")
|
||||
private String itemNum;
|
||||
|
||||
@Schema(description = "SKU 数(京麦列表列,如「12个SKU」)")
|
||||
private Integer skuCount;
|
||||
|
||||
@Schema(description = "信息分(京麦列表列,0~100)")
|
||||
private Integer healthScore;
|
||||
|
||||
@Schema(description = "商品标题")
|
||||
private String title;
|
||||
|
||||
@@ -55,6 +64,12 @@ public class JdProductListItemVo {
|
||||
@Schema(description = "采集来源 Tab(ONSALE/OFFSALE)")
|
||||
private String listTab;
|
||||
|
||||
@Schema(description = "京东侧创建时间")
|
||||
private LocalDateTime jdCreatedAt;
|
||||
|
||||
@Schema(description = "京东侧上架时间")
|
||||
private LocalDateTime jdOnlineAt;
|
||||
|
||||
@Schema(description = "京东侧修改时间")
|
||||
private LocalDateTime jdUpdatedAt;
|
||||
|
||||
|
||||
+207
@@ -0,0 +1,207 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductImageMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductImageEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductDeleteVo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 京东数据库「删除商品」编排(v1.4 补口,用户口径「后台删除数据的时候也要把桶对应的文件删除」)。
|
||||
*
|
||||
* <p>执行顺序是本类的全部要点:<b>先删桶、全成功才删库行</b>。理由不是洁癖——图片对象是
|
||||
* 内容寻址的({@code <key-prefix><companyId>/<sha256>.<ext>}),一旦库行先消失,
|
||||
* 之后就再没人知道该删哪些 key:删不掉的会变成永久孤儿(该桶 {@code retention-days=0}=永久保留)。</p>
|
||||
*
|
||||
* <p>三条边界:</p>
|
||||
* <ul>
|
||||
* <li><b>共用对象不删</b>:同一张图可能挂在多个商品上(同 key),删一个商品时还要看
|
||||
* {@code biz_jd_product_image} 里有没有别的商品引用它;有就保留,计入 {@code objectsKept}。</li>
|
||||
* <li><b>只删自己前缀内的 key</b>:key 不以 {@code aiimage.jd.oss.key-prefix} 开头的行只留 WARN、
|
||||
* 不动手(防早期脏数据把删除扩到别的桶前缀上),计入 {@code objectsSkipped}。</li>
|
||||
* <li><b>归属先判、整批拒绝</b>:id 里有一行不存在或不属于当前公司 ⇒ 整批不删(不做部分删除,
|
||||
* 也不泄露越界行的存在性),文案点明"本次未删除任何数据"。</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class JdProductDeleteService {
|
||||
|
||||
private static final String LOG_TAG = "jd-delete";
|
||||
|
||||
/** 单次批量删除上限(与列表页一次最多可选行数同量级;超限让用户分批,防一次拖死事务与桶)。 */
|
||||
public static final int MAX_BATCH = 200;
|
||||
|
||||
/** 单次可能触碰的对象数上限(200 行 × 200 图/行理论值 4 万;超过说明这批数据异常,拒绝)。 */
|
||||
private static final int MAX_OBJECTS = 20000;
|
||||
|
||||
/** 共用性回查的分片大小(IN 清单过大在 MySQL 侧会走坏执行计划)。 */
|
||||
private static final int KEY_LOOKUP_CHUNK = 500;
|
||||
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductImageMapper jdProductImageMapper;
|
||||
private final JdProductRowDeleteService jdProductRowDeleteService;
|
||||
private final OssStorageService ossStorageService;
|
||||
|
||||
/** 京东商品图独立桶(application.yml 写死 jd-products,不设 env 防 ILM 误配,铁律 17)。 */
|
||||
@Value("${aiimage.jd.oss.bucket:jd-products}")
|
||||
private String bucket;
|
||||
|
||||
/** 对象 key 前缀(与 {@code JdProductImageIngestService} 同源)。 */
|
||||
@Value("${aiimage.jd.oss.key-prefix:jd-products/}")
|
||||
private String keyPrefix;
|
||||
|
||||
/** 删除给定商品(含 SKU、图片登记行与桶内对象);见类注释的三条边界。 */
|
||||
public JdProductDeleteVo delete(List<Long> requestedIds, Long scopedCompanyId) {
|
||||
List<Long> ids = normalizeIds(requestedIds);
|
||||
requireScopedRows(ids, scopedCompanyId);
|
||||
List<JdProductImageEntity> images = imageRowsOf(ids);
|
||||
Set<String> ownedKeys = new LinkedHashSet<>();
|
||||
Set<String> foreignKeys = new LinkedHashSet<>();
|
||||
splitKeys(images, ownedKeys, foreignKeys);
|
||||
if (!foreignKeys.isEmpty()) {
|
||||
log.warn("[{}] 有 {} 个对象 key 不在配置前缀 {} 内,本次不动手 productIds={} sample={}",
|
||||
LOG_TAG, foreignKeys.size(), keyPrefix, ids, firstOf(foreignKeys));
|
||||
}
|
||||
Set<String> sharedKeys = sharedObjectKeys(ownedKeys, ids);
|
||||
int purged = purgeObjects(ownedKeys, sharedKeys);
|
||||
|
||||
int deleted;
|
||||
try {
|
||||
deleted = jdProductRowDeleteService.deleteRows(ids);
|
||||
} catch (RuntimeException ex) {
|
||||
// 桶已删空、库行回滚:数据面会裂图。把可复算的信息留在日志里(key 清单可从图片登记行重建)。
|
||||
log.error("[{}] 桶内已删 {} 个对象但库行删除失败(需人工核对 productIds={})",
|
||||
LOG_TAG, purged, ids, ex);
|
||||
throw ex;
|
||||
}
|
||||
|
||||
JdProductDeleteVo vo = new JdProductDeleteVo();
|
||||
vo.setDeleted(deleted);
|
||||
vo.setImagesDeleted(images.size());
|
||||
vo.setObjectsDeleted(purged);
|
||||
vo.setObjectsKept(ownedKeys.size() - purged);
|
||||
vo.setObjectsSkipped(foreignKeys.size());
|
||||
log.info("[{}] 删除完成 companyId={} productIds={} deleted={} images={} objectsDeleted={} kept={} skipped={}",
|
||||
LOG_TAG, scopedCompanyId, ids, deleted, images.size(), purged, vo.getObjectsKept(),
|
||||
foreignKeys.size());
|
||||
return vo;
|
||||
}
|
||||
|
||||
// ---------- 校验与取数 ----------
|
||||
|
||||
private List<Long> normalizeIds(List<Long> requestedIds) {
|
||||
if (requestedIds == null || requestedIds.isEmpty()) {
|
||||
throw new BusinessException("请先选择要删除的商品");
|
||||
}
|
||||
Set<Long> unique = new LinkedHashSet<>();
|
||||
for (Long id : requestedIds) {
|
||||
if (id != null && id > 0) {
|
||||
unique.add(id);
|
||||
}
|
||||
}
|
||||
if (unique.isEmpty()) {
|
||||
throw new BusinessException("请先选择要删除的商品");
|
||||
}
|
||||
if (unique.size() > MAX_BATCH) {
|
||||
throw new BusinessException("单次最多删除 " + MAX_BATCH + " 个商品,请分批操作");
|
||||
}
|
||||
return new ArrayList<>(unique);
|
||||
}
|
||||
|
||||
/** 归属闸:行必须都在(且都在当前公司可见面内),否则整批拒绝——不做部分删除。 */
|
||||
private void requireScopedRows(List<Long> ids, Long scopedCompanyId) {
|
||||
boolean scoped = scopedCompanyId != null && scopedCompanyId > 0;
|
||||
List<JdProductEntity> rows = jdProductMapper.selectList(new LambdaQueryWrapper<JdProductEntity>()
|
||||
.select(JdProductEntity::getId)
|
||||
.in(JdProductEntity::getId, ids)
|
||||
.eq(scoped, JdProductEntity::getCompanyId, scopedCompanyId));
|
||||
if (rows.size() != ids.size()) {
|
||||
throw new BusinessException("有 " + (ids.size() - rows.size())
|
||||
+ " 个商品不存在或不属于当前公司,请刷新后重试(本次未删除任何数据)");
|
||||
}
|
||||
}
|
||||
|
||||
private List<JdProductImageEntity> imageRowsOf(List<Long> ids) {
|
||||
return jdProductImageMapper.selectList(new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.in(JdProductImageEntity::getProductId, ids));
|
||||
}
|
||||
|
||||
/** 图片登记行的 key 分两堆:前缀内的(可删)与前缀外的(只留痕)。 */
|
||||
private void splitKeys(List<JdProductImageEntity> images, Set<String> owned, Set<String> foreign) {
|
||||
for (JdProductImageEntity image : images) {
|
||||
String key = image.getObjectKey() == null ? "" : image.getObjectKey().trim();
|
||||
if (key.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (keyPrefix != null && !keyPrefix.isEmpty() && key.startsWith(keyPrefix)) {
|
||||
owned.add(key);
|
||||
} else {
|
||||
foreign.add(key);
|
||||
}
|
||||
}
|
||||
if (owned.size() > MAX_OBJECTS) {
|
||||
throw new BusinessException("这批商品的图片对象超过 " + MAX_OBJECTS + " 个,请分批删除");
|
||||
}
|
||||
}
|
||||
|
||||
/** 仍被「其他商品」引用的 key(内容寻址⇒同公司同内容同 key,删了会让别的商品裂图)。 */
|
||||
private Set<String> sharedObjectKeys(Set<String> keys, List<Long> deletingIds) {
|
||||
Set<String> shared = new LinkedHashSet<>();
|
||||
if (keys.isEmpty()) {
|
||||
return shared;
|
||||
}
|
||||
List<String> all = new ArrayList<>(keys);
|
||||
for (int from = 0; from < all.size(); from += KEY_LOOKUP_CHUNK) {
|
||||
List<String> chunk = all.subList(from, Math.min(all.size(), from + KEY_LOOKUP_CHUNK));
|
||||
List<JdProductImageEntity> others = jdProductImageMapper.selectList(
|
||||
new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.select(JdProductImageEntity::getObjectKey)
|
||||
.in(JdProductImageEntity::getObjectKey, chunk)
|
||||
.notIn(JdProductImageEntity::getProductId, deletingIds));
|
||||
for (JdProductImageEntity other : others) {
|
||||
if (other.getObjectKey() != null) {
|
||||
shared.add(other.getObjectKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
return shared;
|
||||
}
|
||||
|
||||
// ---------- 桶 ----------
|
||||
|
||||
/** 逐个删对象;任何一个失败立刻抛(此时一行库行都还没动,整批可安全重试)。 */
|
||||
private int purgeObjects(Set<String> keys, Set<String> shared) {
|
||||
int purged = 0;
|
||||
for (String key : keys) {
|
||||
if (shared.contains(key)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
ossStorageService.removeObject(bucket, key);
|
||||
} catch (RuntimeException ex) {
|
||||
throw new BusinessException("桶内对象删除失败,本次未删除任何数据(" + key + "):"
|
||||
+ ex.getMessage());
|
||||
}
|
||||
purged++;
|
||||
}
|
||||
return purged;
|
||||
}
|
||||
|
||||
private static String firstOf(Set<String> keys) {
|
||||
return keys.isEmpty() ? "" : keys.iterator().next();
|
||||
}
|
||||
}
|
||||
+21
-5
@@ -255,7 +255,7 @@ public class JdProductImageIngestService {
|
||||
}
|
||||
if (!rows.isEmpty()) {
|
||||
jdProductImageMapper.upsertBatch(rows);
|
||||
refreshProductImageCount(productId);
|
||||
refreshProductImageProjection(productId);
|
||||
}
|
||||
log.info("[{}] 图片登记完成 taskId={} productId={} productCode={} received={} registered={} "
|
||||
+ "skipped={} mismatched={}",
|
||||
@@ -264,15 +264,31 @@ public class JdProductImageIngestService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 登记后把商品行 image_count 刷成图片表实际行数(V193 列 DEFAULT 0,不刷会长期停在 0
|
||||
* 冒充没图);按表实数取值天然幂等,重复登记不累计。
|
||||
* 登记后把商品行的图片投影刷成图片表实数:{@code image_count}=已登记图片行数,
|
||||
* {@code main_image_url}=MAIN 档首图的自有桶 URL(B98-01:这一列此前全仓无写点,
|
||||
* 后管列表主图位恒「无图」,即使图片已经传进桶里)。
|
||||
*
|
||||
* <p>按表实数取、不按本次登记的清单累加:重复登记天然幂等,删图后也会被纠正。
|
||||
* 没有任何 MAIN 图时把主图 URL 置 NULL(宁可显示「无图」,也不留一张已经被替换的旧图)。</p>
|
||||
*/
|
||||
private void refreshProductImageCount(Long productId) {
|
||||
private void refreshProductImageProjection(Long productId) {
|
||||
Long total = jdProductImageMapper.selectCount(new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.eq(JdProductImageEntity::getProductId, productId));
|
||||
String mainImageUrl = null;
|
||||
List<JdProductImageEntity> mains = jdProductImageMapper.selectList(
|
||||
new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.eq(JdProductImageEntity::getProductId, productId)
|
||||
.eq(JdProductImageEntity::getImageType, "MAIN")
|
||||
.orderByAsc(JdProductImageEntity::getSortNo)
|
||||
.orderByAsc(JdProductImageEntity::getId)
|
||||
.last("limit 1"));
|
||||
if (mains != null && !mains.isEmpty()) {
|
||||
mainImageUrl = mains.get(0).getUrl();
|
||||
}
|
||||
jdProductMapper.update(null, new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<JdProductEntity>()
|
||||
.eq(JdProductEntity::getId, productId)
|
||||
.set(JdProductEntity::getImageCount, total == null ? 0 : total.intValue()));
|
||||
.set(JdProductEntity::getImageCount, total == null ? 0 : total.intValue())
|
||||
.set(JdProductEntity::getMainImageUrl, mainImageUrl));
|
||||
}
|
||||
|
||||
// ---------- 公共小件 ----------
|
||||
|
||||
+91
-18
@@ -8,9 +8,11 @@ import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductImageMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductGroupEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductImageEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductSkuEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductDetailVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductListItemVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductPageVo;
|
||||
@@ -74,12 +76,13 @@ public class JdProductQueryService {
|
||||
|
||||
/** 导出列序(与后管页面主要字段同序,中文列名)。 */
|
||||
private static final List<String> EXPORT_HEADERS = List.of(
|
||||
"店铺名", "店铺ID", "商品编码", "SKU", "标题", "品牌", "类目路径",
|
||||
"价格下限", "价格上限", "库存", "商品状态", "来源Tab",
|
||||
"京东更新时间", "属性键数", "图片数", "分组", "同步时间");
|
||||
"店铺名", "店铺ID", "商品编码", "货号", "SKU", "SKU数", "信息分", "标题", "品牌", "类目路径",
|
||||
"价格下限", "价格上限", "库存", "商品状态", "来源Tab", "上架时间", "京东创建时间",
|
||||
"京东修改时间", "属性键数", "图片数", "主图URL", "分组", "同步时间");
|
||||
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductImageMapper jdProductImageMapper;
|
||||
private final JdProductSkuMapper jdProductSkuMapper;
|
||||
private final JdProductGroupMapper jdProductGroupMapper;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@@ -141,6 +144,9 @@ public class JdProductQueryService {
|
||||
vo.setShopName(row.getShopName());
|
||||
vo.setProductCode(row.getProductCode());
|
||||
vo.setSkuId(row.getSkuId());
|
||||
vo.setItemNum(row.getItemNum());
|
||||
vo.setSkuCount(row.getSkuCount());
|
||||
vo.setHealthScore(row.getHealthScore());
|
||||
vo.setTitle(row.getTitle());
|
||||
vo.setBrand(row.getBrand());
|
||||
vo.setBrandId(row.getBrandId());
|
||||
@@ -151,6 +157,7 @@ public class JdProductQueryService {
|
||||
vo.setSaleStatus(row.getSaleStatus());
|
||||
vo.setListTab(row.getListTab());
|
||||
vo.setJdCreatedAt(row.getJdCreatedAt());
|
||||
vo.setJdOnlineAt(row.getJdOnlineAt());
|
||||
vo.setJdUpdatedAt(row.getJdUpdatedAt());
|
||||
vo.setMainImageUrl(row.getMainImageUrl());
|
||||
vo.setDetailUrl(row.getDetailUrl());
|
||||
@@ -161,6 +168,7 @@ public class JdProductQueryService {
|
||||
vo.setSyncedAt(row.getSyncedAt());
|
||||
parseAttrGroups(vo, row.getAttrJson());
|
||||
vo.setImages(loadImages(row.getId()));
|
||||
vo.setSkus(loadSkus(row.getId()));
|
||||
return vo;
|
||||
}
|
||||
|
||||
@@ -227,6 +235,9 @@ public class JdProductQueryService {
|
||||
vo.setShopId(row.getShopId());
|
||||
vo.setProductCode(row.getProductCode());
|
||||
vo.setSkuId(row.getSkuId());
|
||||
vo.setItemNum(row.getItemNum());
|
||||
vo.setSkuCount(row.getSkuCount());
|
||||
vo.setHealthScore(row.getHealthScore());
|
||||
vo.setTitle(row.getTitle());
|
||||
vo.setBrand(row.getBrand());
|
||||
vo.setCategoryPath(row.getCategoryPath());
|
||||
@@ -235,6 +246,8 @@ public class JdProductQueryService {
|
||||
vo.setStockTotal(row.getStockTotal());
|
||||
vo.setSaleStatus(row.getSaleStatus());
|
||||
vo.setListTab(row.getListTab());
|
||||
vo.setJdCreatedAt(row.getJdCreatedAt());
|
||||
vo.setJdOnlineAt(row.getJdOnlineAt());
|
||||
vo.setJdUpdatedAt(row.getJdUpdatedAt());
|
||||
vo.setMainImageUrl(row.getMainImageUrl());
|
||||
vo.setAttrCount(row.getAttrCount());
|
||||
@@ -289,8 +302,59 @@ public class JdProductQueryService {
|
||||
return images;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- attr_json 解析
|
||||
/**
|
||||
* SKU 矩阵(biz_jd_product_sku,V198):按商品内排序号、id 稳定升序。
|
||||
*
|
||||
* <p>{@code sale_attrs_json} 解不出时该行 {@code saleAttrs} 落空列表——
|
||||
* 价格/库存/名称照常给(一行组合维度脏了不该让整张矩阵消失)。</p>
|
||||
*/
|
||||
private List<JdProductDetailVo.SkuVo> loadSkus(Long productId) {
|
||||
List<JdProductSkuEntity> rows = jdProductSkuMapper.selectList(
|
||||
new LambdaQueryWrapper<JdProductSkuEntity>()
|
||||
.eq(JdProductSkuEntity::getProductId, productId)
|
||||
.orderByAsc(JdProductSkuEntity::getSortNo)
|
||||
.orderByAsc(JdProductSkuEntity::getId));
|
||||
List<JdProductDetailVo.SkuVo> skus = new ArrayList<>(rows.size());
|
||||
for (JdProductSkuEntity row : rows) {
|
||||
JdProductDetailVo.SkuVo view = new JdProductDetailVo.SkuVo();
|
||||
view.setSkuId(row.getSkuId());
|
||||
view.setSkuName(row.getSkuName());
|
||||
view.setSkuSeq(row.getSkuSeq());
|
||||
view.setPrice(row.getPrice());
|
||||
view.setStock(row.getStock());
|
||||
view.setSortNo(row.getSortNo());
|
||||
view.setSaleAttrs(parseSaleAttrs(row.getSaleAttrsJson(), row.getSkuId()));
|
||||
skus.add(view);
|
||||
}
|
||||
return skus;
|
||||
}
|
||||
|
||||
/** 销售属性组合 JSON → VO 列表(非法/空按空列表,原文只进日志)。 */
|
||||
private List<JdProductDetailVo.SkuAttrVo> parseSaleAttrs(String json, String skuId) {
|
||||
List<JdProductDetailVo.SkuAttrVo> attrs = new ArrayList<>();
|
||||
if (json == null || json.isBlank()) {
|
||||
return attrs;
|
||||
}
|
||||
try {
|
||||
JsonNode array = objectMapper.readTree(json);
|
||||
if (!array.isArray()) {
|
||||
return attrs;
|
||||
}
|
||||
for (JsonNode node : array) {
|
||||
JdProductDetailVo.SkuAttrVo attr = new JdProductDetailVo.SkuAttrVo();
|
||||
attr.setName(node.path("name").asText(null));
|
||||
attr.setValue(node.path("value").asText(null));
|
||||
if (attr.getName() != null || attr.getValue() != null) {
|
||||
attrs.add(attr);
|
||||
}
|
||||
}
|
||||
} catch (com.fasterxml.jackson.core.JsonProcessingException ex) {
|
||||
log.warn("[jd-db] SKU 销售属性组合解析失败 skuId={} err={}", skuId, ex.getMessage());
|
||||
}
|
||||
return attrs;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- attr_json 解析
|
||||
/**
|
||||
* attr_json → attrGroups。契约形状 {@code {"tabs":[{"tab":..,"attrs":[..]}]}};
|
||||
* 为空/非法 JSON/形状不识别一律落空列表并把原文放进 {@code attrJsonRaw}(不抛 500,AC-7)。
|
||||
@@ -371,21 +435,30 @@ public class JdProductQueryService {
|
||||
row.createCell(0).setCellValue(safe(item.getShopName()));
|
||||
row.createCell(1).setCellValue(safe(item.getShopId()));
|
||||
row.createCell(2).setCellValue(safe(item.getProductCode()));
|
||||
row.createCell(3).setCellValue(safe(item.getSkuId()));
|
||||
row.createCell(4).setCellValue(safe(item.getTitle()));
|
||||
row.createCell(5).setCellValue(safe(item.getBrand()));
|
||||
row.createCell(6).setCellValue(safe(item.getCategoryPath()));
|
||||
row.createCell(7).setCellValue(item.getPriceMin() == null ? "" : item.getPriceMin().toPlainString());
|
||||
row.createCell(8).setCellValue(item.getPriceMax() == null ? "" : item.getPriceMax().toPlainString());
|
||||
row.createCell(9).setCellValue(item.getStockTotal() == null ? "" : String.valueOf(item.getStockTotal()));
|
||||
row.createCell(10).setCellValue(safe(item.getSaleStatus()));
|
||||
row.createCell(11).setCellValue(safe(item.getListTab()));
|
||||
row.createCell(12).setCellValue(item.getJdUpdatedAt() == null
|
||||
row.createCell(3).setCellValue(safe(item.getItemNum()));
|
||||
row.createCell(4).setCellValue(safe(item.getSkuId()));
|
||||
row.createCell(5).setCellValue(item.getSkuCount() == null ? "" : String.valueOf(item.getSkuCount()));
|
||||
row.createCell(6).setCellValue(item.getHealthScore() == null
|
||||
? "" : String.valueOf(item.getHealthScore()));
|
||||
row.createCell(7).setCellValue(safe(item.getTitle()));
|
||||
row.createCell(8).setCellValue(safe(item.getBrand()));
|
||||
row.createCell(9).setCellValue(safe(item.getCategoryPath()));
|
||||
row.createCell(10).setCellValue(item.getPriceMin() == null ? "" : item.getPriceMin().toPlainString());
|
||||
row.createCell(11).setCellValue(item.getPriceMax() == null ? "" : item.getPriceMax().toPlainString());
|
||||
row.createCell(12).setCellValue(item.getStockTotal() == null ? "" : String.valueOf(item.getStockTotal()));
|
||||
row.createCell(13).setCellValue(safe(item.getSaleStatus()));
|
||||
row.createCell(14).setCellValue(safe(item.getListTab()));
|
||||
row.createCell(15).setCellValue(item.getJdOnlineAt() == null
|
||||
? "" : EXPORT_DATETIME_FORMATTER.format(item.getJdOnlineAt()));
|
||||
row.createCell(16).setCellValue(item.getJdCreatedAt() == null
|
||||
? "" : EXPORT_DATETIME_FORMATTER.format(item.getJdCreatedAt()));
|
||||
row.createCell(17).setCellValue(item.getJdUpdatedAt() == null
|
||||
? "" : EXPORT_DATETIME_FORMATTER.format(item.getJdUpdatedAt()));
|
||||
row.createCell(13).setCellValue(item.getAttrCount() == null ? "" : String.valueOf(item.getAttrCount()));
|
||||
row.createCell(14).setCellValue(item.getImageCount() == null ? "" : String.valueOf(item.getImageCount()));
|
||||
row.createCell(15).setCellValue(safe(item.getGroupName()));
|
||||
row.createCell(16).setCellValue(item.getSyncedAt() == null
|
||||
row.createCell(18).setCellValue(item.getAttrCount() == null ? "" : String.valueOf(item.getAttrCount()));
|
||||
row.createCell(19).setCellValue(item.getImageCount() == null ? "" : String.valueOf(item.getImageCount()));
|
||||
row.createCell(20).setCellValue(safe(item.getMainImageUrl()));
|
||||
row.createCell(21).setCellValue(safe(item.getGroupName()));
|
||||
row.createCell(22).setCellValue(item.getSyncedAt() == null
|
||||
? "" : EXPORT_DATETIME_FORMATTER.format(item.getSyncedAt()));
|
||||
}
|
||||
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductImageMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductImageEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductSkuEntity;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东商品「库行」删除的唯一出口(子表 → 主表,同一事务)。
|
||||
*
|
||||
* <p>为什么单独一支:对象存储的删除是**不可回滚的外部动作**,它必须发生在事务之外
|
||||
* ({@link JdProductDeleteService} 先删桶、全成功才调本类)。若把三次 delete 与 S3 调用塞进
|
||||
* 同一个事务,S3 通一次网就多占一次行锁,而且事务回滚也救不回已经删掉的对象。</p>
|
||||
*
|
||||
* <p>删除顺序固定 {@code SKU → 图片行 → 商品行}:三张表都按 {@code product_id} 关联、没有外键,
|
||||
* 先删子行不会因为"主行先没"而留下查不到的孤儿;反过来则会。SQL 全走 BaseMapper wrapper
|
||||
* (铁律 1 允许的两种形态之一,与 {@link JdProductGroupService} 同形状)。</p>
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class JdProductRowDeleteService {
|
||||
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductImageMapper jdProductImageMapper;
|
||||
private final JdProductSkuMapper jdProductSkuMapper;
|
||||
|
||||
/**
|
||||
* 删除给定商品行的 SKU / 图片登记 / 商品行,返回商品行删除条数。
|
||||
*
|
||||
* <p>调用方必须保证 {@code ids} 非空(MP 的 {@code delete(wrapper)} 在 id 清单为空时
|
||||
* 会退化成"删全表")——本类首行再挡一次,防后来者直接调它。</p>
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public int deleteRows(List<Long> ids) {
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
throw new IllegalArgumentException("删除商品行必须给出非空 id 清单");
|
||||
}
|
||||
jdProductSkuMapper.delete(new LambdaQueryWrapper<JdProductSkuEntity>()
|
||||
.in(JdProductSkuEntity::getProductId, ids));
|
||||
jdProductImageMapper.delete(new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.in(JdProductImageEntity::getProductId, ids));
|
||||
int deleted = jdProductMapper.deleteBatchIds(ids);
|
||||
log.info("[jd-delete] 库行已删 productIds={} rowCount={} deleted={}", ids.size(), ids.size(), deleted);
|
||||
return deleted;
|
||||
}
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductSkuMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductSkuEntity;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 京东商品 SKU 矩阵入库(v1.4 详情面补齐,V198)。
|
||||
*
|
||||
* <p>用户口径(2026-11-14):「后台的数据库要和京麦对上,包括详情里」——京麦详情页
|
||||
* 「销售属性」区是一张 SKU 矩阵(真机样本 12 行 = 颜色 3 × 尺码 4),每行有自己的
|
||||
* skuId / 价格 / 库存 / 属性组合。此前只有商品主表上的主 SKU 与 SKU 个数,抽屉里
|
||||
* 看不到这张矩阵。</p>
|
||||
*
|
||||
* <p>链路与图片登记同形:chunk 回传里带 detail.skus → 商品行 upsert **之后**才按
|
||||
* (companyId, taskId, productCode) 回查到 {@code biz_jd_product.id}(首采行 upsert 前
|
||||
* 必然查不到)→ 落 {@link JdProductSkuMapper#upsertBatch}(uk_product_sku 幂等)。</p>
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class JdProductSkuIngestService {
|
||||
|
||||
private static final String LOG_TAG = "jd-skus";
|
||||
|
||||
/** 单商品 SKU 数上限(防脏包一次写爆;京麦单商品 SKU 通常个位数到几十)。 */
|
||||
@Value("${aiimage.jd.sku-max-per-product:200}")
|
||||
private int skuMaxPerProduct;
|
||||
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductSkuMapper jdProductSkuMapper;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* 登记一个商品的 SKU 矩阵,返回实际落库行数(商品行不存在/清单为空返回 0)。
|
||||
*
|
||||
* @param specs 客户端 detail.skus 逐行(形状见 {@link JdIngestSkuSpec}),空元素跳过
|
||||
*/
|
||||
public int registerTaskSkus(Long taskId, Long companyId, String productCode,
|
||||
List<JdIngestSkuSpec> specs) {
|
||||
if (specs == null || specs.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
JdProductEntity product = jdProductMapper.selectOne(new LambdaQueryWrapper<JdProductEntity>()
|
||||
.eq(JdProductEntity::getCompanyId, companyId)
|
||||
.eq(JdProductEntity::getTaskId, taskId)
|
||||
.eq(JdProductEntity::getProductCode, productCode)
|
||||
.last("limit 1"));
|
||||
if (product == null) {
|
||||
log.warn("[{}] 商品行未落库,SKU 登记跳过 taskId={} productCode={} skuCount={}",
|
||||
LOG_TAG, taskId, productCode, specs.size());
|
||||
return 0;
|
||||
}
|
||||
int cap = Math.max(1, skuMaxPerProduct);
|
||||
List<JdIngestSkuSpec> effective = specs;
|
||||
if (specs.size() > cap) {
|
||||
effective = specs.subList(0, cap);
|
||||
log.warn("[{}] 单商品 SKU 超上限截断 taskId={} productCode={} received={} cap={}",
|
||||
LOG_TAG, taskId, productCode, specs.size(), cap);
|
||||
}
|
||||
List<JdProductSkuEntity> rows = new ArrayList<>();
|
||||
int skipped = 0;
|
||||
int sortNo = 0;
|
||||
for (JdIngestSkuSpec spec : effective) {
|
||||
if (spec == null || isBlank(spec.skuId())) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
JdProductSkuEntity row = new JdProductSkuEntity();
|
||||
row.setProductId(product.getId());
|
||||
row.setCompanyId(companyId);
|
||||
row.setSkuId(spec.skuId().trim());
|
||||
row.setSkuName(spec.skuName());
|
||||
row.setSkuSeq(spec.skuSeq());
|
||||
row.setPrice(spec.price());
|
||||
row.setStock(spec.stock());
|
||||
row.setSaleAttrsJson(writeJson(spec.saleAttrs()));
|
||||
row.setSortNo(spec.sortNo() == null ? sortNo : spec.sortNo());
|
||||
rows.add(row);
|
||||
sortNo++;
|
||||
}
|
||||
if (skipped > 0) {
|
||||
log.warn("[{}] SKU 登记跳过缺 skuId 的行 taskId={} productCode={} skipped={}",
|
||||
LOG_TAG, taskId, productCode, skipped);
|
||||
}
|
||||
if (rows.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
jdProductSkuMapper.upsertBatch(rows);
|
||||
return rows.size();
|
||||
}
|
||||
|
||||
private String writeJson(List<Map<String, String>> saleAttrs) {
|
||||
if (saleAttrs == null || saleAttrs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return objectMapper.writeValueAsString(saleAttrs);
|
||||
} catch (com.fasterxml.jackson.core.JsonProcessingException ex) {
|
||||
// 组合维度假格式化失败不该让整批回传 500:留日志、这行不写组合(价格库存照落)。
|
||||
log.warn("[{}] 销售属性组合序列化失败(该 SKU 不写组合) err={}", LOG_TAG, ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isBlank(String value) {
|
||||
return value == null || value.trim().isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户端 detail.skus 的一行(中性形状:回传 DTO 与本服务解耦,判据与实现各测各的)。
|
||||
*/
|
||||
public record JdIngestSkuSpec(String skuId, String skuName, String skuSeq,
|
||||
java.math.BigDecimal price, Integer stock,
|
||||
Integer sortNo, List<Map<String, String>> saleAttrs) {
|
||||
|
||||
/** 从任意 map 形状宽容取一行(缺键按 null,不认识的键忽略)。 */
|
||||
@SuppressWarnings("unchecked")
|
||||
public static JdIngestSkuSpec fromMap(Map<String, Object> raw, int sortNo) {
|
||||
if (raw == null || raw.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Object price = raw.get("price");
|
||||
Object stock = raw.get("stock");
|
||||
List<Map<String, String>> saleAttrs = null;
|
||||
Object attrs = raw.get("saleAttrs");
|
||||
if (attrs instanceof List<?> list) {
|
||||
saleAttrs = new ArrayList<>();
|
||||
for (Object item : list) {
|
||||
if (!(item instanceof Map<?, ?> map)) {
|
||||
continue;
|
||||
}
|
||||
Map<String, String> pair = new LinkedHashMap<>();
|
||||
Object name = map.get("name");
|
||||
Object value = map.get("value");
|
||||
if (name != null) {
|
||||
pair.put("name", String.valueOf(name));
|
||||
}
|
||||
if (value != null) {
|
||||
pair.put("value", String.valueOf(value));
|
||||
}
|
||||
if (!pair.isEmpty()) {
|
||||
saleAttrs.add(pair);
|
||||
}
|
||||
}
|
||||
}
|
||||
Object sort = raw.get("sortNo");
|
||||
return new JdIngestSkuSpec(
|
||||
raw.get("skuId") == null ? null : String.valueOf(raw.get("skuId")),
|
||||
raw.get("skuName") == null ? null : String.valueOf(raw.get("skuName")),
|
||||
raw.get("skuSeq") == null ? null : String.valueOf(raw.get("skuSeq")),
|
||||
price == null ? null : new java.math.BigDecimal(String.valueOf(price)),
|
||||
stock == null ? null : (int) Double.parseDouble(String.valueOf(stock)),
|
||||
sort instanceof Number number ? number.intValue() : sortNo,
|
||||
saleAttrs);
|
||||
}
|
||||
}
|
||||
}
|
||||
+54
@@ -70,6 +70,7 @@ public class JdResultIngestService {
|
||||
private final JdStaleTaskService jdStaleTaskService;
|
||||
private final TaskProgressSnapshotService taskProgressSnapshotService;
|
||||
private final JdProductImageIngestService jdProductImageIngestService;
|
||||
private final JdProductSkuIngestService jdProductSkuIngestService;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public JdIngestChunkVo submitChunk(Long taskId, JdIngestChunkRequest request, boolean trustedErrorReporter) {
|
||||
@@ -101,11 +102,14 @@ public class JdResultIngestService {
|
||||
|
||||
int deduped = 0;
|
||||
int imageCount = 0;
|
||||
int skuCount = 0;
|
||||
List<JdProductEntity> rows = new ArrayList<>();
|
||||
// 图片登记延后到 upsert 之后(P0-2):registerTaskImages 按 companyId+taskId+productCode
|
||||
// 查商品行,首采行在 upsert 前必然不存在 → 登记会被 100% 跳过、图片永远落不了库。
|
||||
// 这里先收集,行落库后再逐商品登记。
|
||||
List<PendingImageRegistration> pendingImages = new ArrayList<>();
|
||||
// SKU 矩阵同理(V198):要先拿到 biz_jd_product.id 才能落 biz_jd_product_sku。
|
||||
List<PendingSkuRegistration> pendingSkus = new ArrayList<>();
|
||||
if (request.getItems() != null) {
|
||||
for (JdIngestChunkRequest.JdIngestItemDto item : request.getItems()) {
|
||||
if (item == null || isBlank(item.getProductCode())) {
|
||||
@@ -122,6 +126,8 @@ public class JdResultIngestService {
|
||||
rows.add(buildRow(task, companyId, createRequest, item, summary, request.getBatchId(), scope));
|
||||
pendingImages.add(new PendingImageRegistration(item.getProductCode().trim(),
|
||||
toImageSpecs(item.getImages())));
|
||||
pendingSkus.add(new PendingSkuRegistration(item.getProductCode().trim(),
|
||||
toSkuSpecs(item.getDetail())));
|
||||
}
|
||||
}
|
||||
if (!rows.isEmpty()) {
|
||||
@@ -133,6 +139,11 @@ public class JdResultIngestService {
|
||||
imageCount += jdProductImageIngestService.registerTaskImages(taskId, companyId,
|
||||
pending.productCode(), pending.specs());
|
||||
}
|
||||
// V198 详情面补齐:SKU 矩阵同样必须在商品行落库之后(要回查 biz_jd_product.id)。
|
||||
for (PendingSkuRegistration pending : pendingSkus) {
|
||||
skuCount += jdProductSkuIngestService.registerTaskSkus(taskId, companyId,
|
||||
pending.productCode(), pending.specs());
|
||||
}
|
||||
int itemsReceived = mergeScopeState(scope, request, request.getItems() == null ? 0 : request.getItems().size());
|
||||
if (Boolean.TRUE.equals(request.getIsEnd())) {
|
||||
finalizeSuccess(task, itemsReceived);
|
||||
@@ -142,6 +153,7 @@ public class JdResultIngestService {
|
||||
vo.setDeduped(deduped);
|
||||
vo.setUpserted(rows.size());
|
||||
vo.setImagesRegistered(imageCount);
|
||||
vo.setSkusRegistered(skuCount);
|
||||
return vo;
|
||||
}
|
||||
|
||||
@@ -285,6 +297,9 @@ public class JdResultIngestService {
|
||||
row.setShopName(shopName);
|
||||
row.setProductCode(item.getProductCode().trim());
|
||||
row.setSkuId(summary.getSkuId());
|
||||
row.setItemNum(summary.getItemNum());
|
||||
row.setSkuCount(summary.getSkuCount());
|
||||
row.setHealthScore(summary.getHealthScore());
|
||||
row.setTitle(summary.getTitle().trim());
|
||||
row.setBrand(summary.getBrand());
|
||||
row.setBrandId(summary.getBrandId());
|
||||
@@ -295,6 +310,7 @@ public class JdResultIngestService {
|
||||
row.setSaleStatus(summary.getSaleStatus());
|
||||
row.setListTab(firstNonBlank(summary.getListTab(), cursorTab(scope)));
|
||||
row.setJdCreatedAt(parseJdTime(summary.getJdCreatedAt()));
|
||||
row.setJdOnlineAt(parseJdTime(summary.getJdOnlineAt()));
|
||||
row.setJdUpdatedAt(parseJdTime(summary.getJdUpdatedAt()));
|
||||
row.setDetailUrl(summary.getDetailUrl());
|
||||
// detail 原样透传入 attr_json(AC-3 保真防丢键),不裁剪不转写;键数进 attr_count 供质量核查。
|
||||
@@ -410,4 +426,42 @@ public class JdResultIngestService {
|
||||
private record PendingImageRegistration(String productCode,
|
||||
List<JdProductImageIngestService.JdIngestImageSpec> specs) {
|
||||
}
|
||||
|
||||
/** 延后登记的 SKU 矩阵(V198 同形:商品行 upsert 之后才有 product_id)。 */
|
||||
private record PendingSkuRegistration(String productCode,
|
||||
List<JdProductSkuIngestService.JdIngestSkuSpec> specs) {
|
||||
}
|
||||
|
||||
/**
|
||||
* chunk 行 detail.skus → SKU 登记规格(缺 skus/形状不合返回空,不炸整批)。
|
||||
*
|
||||
* <p>SKU 缺失只影响详情抽屉里的矩阵,不影响列表字段落库——所以解析失败按行级留痕
|
||||
* (WARN + productCode),绝不因为一个商品的 skus 脏了就让整批回传 500。</p>
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<JdProductSkuIngestService.JdIngestSkuSpec> toSkuSpecs(Map<String, Object> detail) {
|
||||
if (detail == null || detail.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Object raw = detail.get("skus");
|
||||
if (!(raw instanceof List<?> list) || list.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
List<JdProductSkuIngestService.JdIngestSkuSpec> specs = new ArrayList<>();
|
||||
int sortNo = 0;
|
||||
for (Object item : list) {
|
||||
if (!(item instanceof Map<?, ?>)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
specs.add(JdProductSkuIngestService.JdIngestSkuSpec.fromMap(
|
||||
(Map<String, Object>) item, sortNo));
|
||||
} catch (RuntimeException ex) {
|
||||
// 单行 SKU 形状不合法(price 非数字等)只跳过这一行,原文只进日志。
|
||||
log.warn("[{}] SKU 行形状不合法,跳过 sortNo={} err={}", LOG_TAG, sortNo, ex.getMessage());
|
||||
}
|
||||
sortNo++;
|
||||
}
|
||||
return specs;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user