feat(jd): 京东数据库行级编辑与删除+人工编辑锁/删除忽略保护(V199,2026-11-15 用户口径「每条数据可以编辑/删除」)——主链:PUT /api/admin/jd-products/{id} 主字段全量提交即置 manual_locked=1(采集回传 upsert/purge 对锁定行跳过,XML 侧 AND manual_locked=0 铁律 20);attrGroups 只重建 attr_json 的 tabs 段保留其它顶层键(铁律 30),原文形状不识别拒绝;skus 按 (productId,skuId) 改价改库存缺失拒绝;deleteImageIds 先删桶(共用对象保留)计数按表实数刷;删除链成功后写 biz_jd_product_ignore 防重爬复活(幂等吞 DuplicateKey);采集回传 filterProtectedRows 跳过锁定/忽略行不登记子表。后管:列表 11 列操作列(编辑/删除,@click.stop 防冒泡)+ JdProductEditDrawer(主字段/属性键值行删/SKU 价格库存/图片标删,保存提示人工维护后果),updateJdProduct 带 company_id;company-tab-wiring SEND_POINTS 26→32(补详情/删除/分组/类目/删分组/编辑六口一函数一钉对平)。判据:JdProductUpdateServiceTest 8 例+SyncServiceTest 保护闸/绊线+DeleteServiceTest 忽略登记+mount 操作列防冒泡/编辑抽屉;后端整量 5,607 例、后管 vue-tsc+1903+113 全绿。同窗交织笔(另一路会话类目下拉已收尾,文件级无法拆分一并入库):selectProductCategories/GET /categories/JdProductCategoryVo 类目筛选下拉+MigratedSqlEquivalenceTest/SqlLayeringConventionTest 适配。图片上传/替换链路记 BACKLOG B109-01;RawExceptionMessageSinkGuard/CatchSilenceRatchet 随笔抬格逐处记名
This commit is contained in:
+44
-37
@@ -7,13 +7,15 @@ 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.common.support.DownloadHeaderUtil;
|
||||
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;
|
||||
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.jd.service.JdProductUpdateService;
|
||||
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
@@ -26,31 +28,30 @@ 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.PutMapping;
|
||||
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 JdResultIngestService},覆盖最新):列表/详情两个 GET 端点 +
|
||||
* 删除(单行 {@code DELETE /{id}} 与批量 {@code POST /batch-delete},用户口径
|
||||
* 「后台删除数据的时候也要把桶对应的文件删除」);分组管理走
|
||||
* {@code AdminJdProductGroupController}(同款鉴权闸)。
|
||||
*
|
||||
* <p>删除是唯一会改数据的动作,语义写在 {@code JdProductDeleteService}:先删 {@code jd-products}
|
||||
* 桶内对象、全成功才删库行(SKU → 图片登记 → 商品行),被其他商品共用的图片对象保留。</p>
|
||||
* <p>写动作两个:删除(语义写在 {@code JdProductDeleteService}:先删 {@code jd-products}
|
||||
* 桶内对象、全成功才删库行(SKU → 图片登记 → 商品行),被其他商品共用的图片对象保留)
|
||||
* 与编辑({@code PUT /{id}},语义写在 {@code JdProductUpdateService}:主字段全量提交+可选块,
|
||||
* 保存即置人工编辑锁 manual_locked=1,采集回传对锁定的行不再覆盖/清理,删除成功另写忽略表)。</p>
|
||||
*
|
||||
* <p>鉴权照外观专利只读页私有 {@code requireXxxAccess} 形状:JWT → 内部令牌回退 → 超管放行 →
|
||||
* 非超管菜单权限(column_key/route_path 双匹配)。菜单行由 V196 只插 columns 不预授权
|
||||
@@ -60,22 +61,24 @@ import java.util.List;
|
||||
* 归属物化在行 company_id 列、可见集不靠账号/分组)——超管显式传才收窄、不传全量;
|
||||
* 非超管强制本公司(忽略请求传参)。{@code superAdminScopedCompanyId} 对非超管返回 null
|
||||
* =按行 company_id 列查询时把整张表交给每一家(该方法 javadoc 点名的越权形状),不走那道门。</p>
|
||||
*
|
||||
* <p>导出端点已随 2026-11-15 客户口径「导出也不用」整段下线(控制器/服务/mapper 参数一起删);
|
||||
* {@code sale_status} 查询参数同窗撤掉(商品状态筛选框已从前端下线)。DB 列
|
||||
* {@code biz_jd_product.sale_status} 与详情/回传链不动。</p>
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@RequestMapping("/api/admin/jd-products")
|
||||
@Tag(name = "京东数据库", description = "后管只读页:京东店铺商品同步落表数据的分页查询、详情与导出,按分公司隔离。")
|
||||
@Tag(name = "京东数据库", description = "后管京东店铺商品库:分页查询、详情、编辑与删除,按分公司隔离。")
|
||||
public class AdminJdProductController {
|
||||
|
||||
private static final String COLUMN_KEY = "admin_jd_database";
|
||||
private static final String ROUTE_PATH = "jd-center/database";
|
||||
private static final String XLSX_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
/** 导出文件名时间戳(同外观专利只读页口径),中文文件名的 ASCII 回退由 DownloadHeaderUtil 兜底。 */
|
||||
private static final DateTimeFormatter EXPORT_FILENAME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
|
||||
|
||||
private final JdProductQueryService jdProductQueryService;
|
||||
private final JdProductDeleteService jdProductDeleteService;
|
||||
private final JdProductUpdateService jdProductUpdateService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final PermissionMenuService permissionMenuService;
|
||||
private final DataReadScope dataReadScope;
|
||||
@@ -92,7 +95,7 @@ public class AdminJdProductController {
|
||||
@Parameter(description = "店铺名(包含匹配)") @RequestParam(name = "shop_name", required = false) String shopName,
|
||||
@Parameter(description = "商品编码(包含匹配)") @RequestParam(name = "product_code", required = false) String productCode,
|
||||
@Parameter(description = "标题关键词(包含匹配)") @RequestParam(name = "title_keyword", required = false) String titleKeyword,
|
||||
@Parameter(description = "商品状态(上架中/已下架/审核中/草稿)") @RequestParam(name = "sale_status", required = false) String saleStatus,
|
||||
@Parameter(description = "类目(京麦末级类目名,eq)") @RequestParam(name = "category", required = false) String category,
|
||||
@Parameter(description = "分组 id") @RequestParam(name = "group_id", required = false) Long groupId,
|
||||
@Parameter(description = "同步开始时间(包含,ISO datetime)")
|
||||
@RequestParam(name = "synced_from", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime syncedFrom,
|
||||
@@ -106,7 +109,21 @@ public class AdminJdProductController {
|
||||
// 不传全量;非超管强制本公司(忽略请求传参)。
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, companyId);
|
||||
return ApiResponse.success(jdProductQueryService.page(page, pageSize, scopedCompanyId, shopName,
|
||||
productCode, titleKeyword, saleStatus, groupId, syncedFrom, syncedTo));
|
||||
productCode, titleKeyword, category, groupId, syncedFrom, syncedTo));
|
||||
}
|
||||
|
||||
@GetMapping("/categories")
|
||||
@Operation(summary = "类目筛选下拉数据源", description = "本公司已落库行 DISTINCT 的 category_path"
|
||||
+ "(京麦末级类目名)+ 行数,按类目名排序;company_id 只对站点超管生效(显式传才收窄,"
|
||||
+ "不传全量),非超管强制本公司。")
|
||||
public ApiResponse<List<JdProductCategoryVo>> categories(
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireJdAccess(request);
|
||||
AdminUserEntity user = operator.user();
|
||||
// 类目下拉与列表同走 readCompanyIdOrOwn 正门:选项集永远和页签能看到的公司行集同源。
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, companyId);
|
||||
return ApiResponse.success(jdProductQueryService.categories(scopedCompanyId));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@@ -123,34 +140,24 @@ public class AdminJdProductController {
|
||||
return ApiResponse.success(jdProductQueryService.detail(id, scopedCompanyId));
|
||||
}
|
||||
|
||||
@GetMapping("/export")
|
||||
@Operation(summary = "导出京东数据库 Excel", description = "与分页查询同一过滤面(店铺名/商品编码/标题关键词/"
|
||||
+ "商品状态/分组/同步起止),公司隔离走同一正门:company_id 只对站点超管生效(显式传才收窄,"
|
||||
+ "不传全量),非超管强制本公司。超过导出上限按业务错误拒绝。")
|
||||
public ResponseEntity<byte[]> export(
|
||||
@Parameter(description = "店铺名(包含匹配)") @RequestParam(name = "shop_name", required = false) String shopName,
|
||||
@Parameter(description = "商品编码(包含匹配)") @RequestParam(name = "product_code", required = false) String productCode,
|
||||
@Parameter(description = "标题关键词(包含匹配)") @RequestParam(name = "title_keyword", required = false) String titleKeyword,
|
||||
@Parameter(description = "商品状态") @RequestParam(name = "sale_status", required = false) String saleStatus,
|
||||
@Parameter(description = "分组 id") @RequestParam(name = "group_id", required = false) Long groupId,
|
||||
@Parameter(description = "同步开始时间(包含,ISO datetime)")
|
||||
@RequestParam(name = "synced_from", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime syncedFrom,
|
||||
@Parameter(description = "同步结束时间(包含,ISO datetime)")
|
||||
@RequestParam(name = "synced_to", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime syncedTo,
|
||||
@PutMapping("/{id}")
|
||||
@Operation(summary = "编辑京东商品", description = "主字段全量提交(标题/品牌/类目/货号/价格区间/总库存/"
|
||||
+ "状态/分组),可选块缺省不动:attrGroups 重建 attr_json 的 tabs 段(其余顶层键保留)、"
|
||||
+ "skus 按 skuId 改价格库存、deleteImageIds 删图(共用对象保留)。保存即置人工编辑锁"
|
||||
+ "(manual_locked=1):采集回传对该行不再覆盖/清理。返回刷新后的详情。")
|
||||
public ApiResponse<JdProductDetailVo> update(
|
||||
@Parameter(description = "商品行 id") @PathVariable("id") Long id,
|
||||
@Valid @RequestBody JdProductUpdateRequest body,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireJdAccess(request);
|
||||
AdminUserEntity user = operator.user();
|
||||
// 导出与分页共用 readCompanyIdOrOwn 正门(同 page 的口径注释):绝不让导出绕过页签的公司隔离。
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, companyId);
|
||||
byte[] bytes = jdProductQueryService.export(scopedCompanyId, shopName, productCode, titleKeyword,
|
||||
saleStatus, groupId, syncedFrom, syncedTo);
|
||||
String filename = "京东数据库-" + LocalDateTime.now().format(EXPORT_FILENAME_FORMATTER) + ".xlsx";
|
||||
return ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, DownloadHeaderUtil.contentDisposition(filename))
|
||||
.contentType(MediaType.parseMediaType(XLSX_CONTENT_TYPE))
|
||||
.contentLength(bytes.length)
|
||||
.body(bytes);
|
||||
Long updatedId = jdProductUpdateService.update(id, body, scopedCompanyId);
|
||||
log.info("[jd-product] 编辑商品 operatorId={} companyId={} id={} title={}",
|
||||
operator.id(), scopedCompanyId, updatedId, body.getTitle());
|
||||
// 返回刷新后的详情(前端保存后直接回填抽屉),复用详情同一查询出口。
|
||||
return ApiResponse.success(jdProductQueryService.detail(updatedId, scopedCompanyId));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.nanri.aiimage.modules.jd.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductIgnoreEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 京东商品删除忽略表(biz_jd_product_ignore,V199)数据访问口。
|
||||
*
|
||||
* <p>只走 BaseMapper wrapper(铁律 1 允许的两种形态之一,与 {@link JdProductSkuMapper}
|
||||
* 的查询口同形状):写入口是 {@code JdProductDeleteService} 删除成功后的幂等登记,
|
||||
* 读入口是 {@code JdResultIngestService} 按三元组回查命中集。无自定义 SQL,无 XML。</p>
|
||||
*/
|
||||
@Mapper
|
||||
public interface JdProductIgnoreMapper extends BaseMapper<JdProductIgnoreEntity> {
|
||||
}
|
||||
+11
-3
@@ -2,6 +2,7 @@ package com.nanri.aiimage.modules.jd.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductCategoryVo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@@ -33,14 +34,14 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
|
||||
|
||||
/**
|
||||
* 后管只读页总数(Phase 5,v1.4):与 {@link #selectProductPage} 同一过滤面
|
||||
* (公司/店铺名/商品编码/标题关键词/商品状态/分组/同步起止,全部 SQL 侧下推,
|
||||
* (公司/店铺名/商品编码/标题关键词/类目/分组/同步起止,全部 SQL 侧下推,
|
||||
* 铁律 20)。{@code companyId}/{@code groupId} 为空或 ≤0=不过滤该面。
|
||||
*/
|
||||
long countProducts(@Param("companyId") Long companyId,
|
||||
@Param("shopName") String shopName,
|
||||
@Param("productCode") String productCode,
|
||||
@Param("titleKeyword") String titleKeyword,
|
||||
@Param("saleStatus") String saleStatus,
|
||||
@Param("category") String category,
|
||||
@Param("groupId") Long groupId,
|
||||
@Param("syncedFrom") LocalDateTime syncedFrom,
|
||||
@Param("syncedTo") LocalDateTime syncedTo);
|
||||
@@ -73,10 +74,17 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
|
||||
@Param("shopName") String shopName,
|
||||
@Param("productCode") String productCode,
|
||||
@Param("titleKeyword") String titleKeyword,
|
||||
@Param("saleStatus") String saleStatus,
|
||||
@Param("category") String category,
|
||||
@Param("groupId") Long groupId,
|
||||
@Param("syncedFrom") LocalDateTime syncedFrom,
|
||||
@Param("syncedTo") LocalDateTime syncedTo,
|
||||
@Param("offset") long offset,
|
||||
@Param("limit") int limit);
|
||||
|
||||
/**
|
||||
* 类目筛选下拉数据源(2026-11-15 用户口径「京麦的类目也要同步过来后台」):
|
||||
* 本公司已落库行的 DISTINCT {@code category_path}(=京麦末级类目名)+ 行数。
|
||||
* {@code companyId} 为空(超管不选公司)=跨公司全量去重。
|
||||
*/
|
||||
List<JdProductCategoryVo> selectProductCategories(@Param("companyId") Long companyId);
|
||||
}
|
||||
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
package com.nanri.aiimage.modules.jd.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.DecimalMin;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后管「编辑京东商品」请求体(PUT /api/admin/jd-products/{id},2026-11-15 用户口径)。
|
||||
*
|
||||
* <p>主字段全量提交(保存即按本请求体覆写这几个列);三个可选块缺省=不动那一面:
|
||||
* {@code attrGroups} 缺省不动属性、{@code skus} 缺省不动 SKU 价格库存、{@code deleteImageIds}
|
||||
* 缺省不删图。公司隔离不走 body——`company_id` 是请求参数,由
|
||||
* {@code DataReadScope#readCompanyIdOrOwn} 统一收口(同本模块全部端点)。</p>
|
||||
*
|
||||
* <p>编辑保存后商品行 {@code manual_locked=1}(V199 人工编辑锁):采集回传的
|
||||
* upsert / 图片与 SKU 登记 / 覆盖清理对该行全部跳过,人工值不被京麦采集覆盖。</p>
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "编辑京东商品请求(主字段全量提交,可选块缺省不动)")
|
||||
public class JdProductUpdateRequest {
|
||||
|
||||
@Schema(description = "商品标题(必填非空)")
|
||||
@NotBlank(message = "标题不能为空")
|
||||
@Size(max = 600, message = "标题最长 600 字")
|
||||
private String title;
|
||||
|
||||
@Schema(description = "品牌名(可空)")
|
||||
@Size(max = 128, message = "品牌最长 128 字")
|
||||
private String brand;
|
||||
|
||||
@Schema(description = "类目路径(可空)")
|
||||
@Size(max = 500, message = "类目路径最长 500 字")
|
||||
private String categoryPath;
|
||||
|
||||
@Schema(description = "货号(可空)")
|
||||
@Size(max = 128, message = "货号最长 128 字")
|
||||
private String itemNum;
|
||||
|
||||
@Schema(description = "价格区间下限(可空)")
|
||||
@DecimalMin(value = "0", message = "价格下限不能为负数")
|
||||
private BigDecimal priceMin;
|
||||
|
||||
@Schema(description = "价格区间上限(可空)")
|
||||
@DecimalMin(value = "0", message = "价格上限不能为负数")
|
||||
private BigDecimal priceMax;
|
||||
|
||||
@Schema(description = "总库存(可空,非负)")
|
||||
private Integer stockTotal;
|
||||
|
||||
@Schema(description = "商品状态(可空,自由文本)")
|
||||
@Size(max = 32, message = "商品状态最长 32 字")
|
||||
private String saleStatus;
|
||||
|
||||
@Schema(description = "分组 id(null=未分组;组 id 必须属于本公司)")
|
||||
private Long groupId;
|
||||
|
||||
@Schema(description = "属性分组(可选块;缺省不动属性;提交时重建 attr_json 的 tabs 段,其余顶层键保留)")
|
||||
private List<AttrGroupDto> attrGroups;
|
||||
|
||||
@Schema(description = "SKU 价格库存编辑(可选块;缺省不动;按 skuId 匹配逐行更新)")
|
||||
private List<SkuEditDto> skus;
|
||||
|
||||
@Schema(description = "要删除的图片登记行 id 清单(可选块;缺省不删图;共用对象保留)")
|
||||
private List<Long> deleteImageIds;
|
||||
|
||||
@Data
|
||||
@Schema(description = "属性分组(京麦 Tab 形状)")
|
||||
public static class AttrGroupDto {
|
||||
|
||||
@Schema(description = "分组名(京麦 Tab 名,可空)")
|
||||
@Size(max = 64, message = "属性分组名最长 64 字")
|
||||
private String groupName;
|
||||
|
||||
@Schema(description = "属性键值对列表")
|
||||
private List<AttrDto> attrs;
|
||||
}
|
||||
|
||||
@Data
|
||||
@Schema(description = "属性键值对")
|
||||
public static class AttrDto {
|
||||
|
||||
@Schema(description = "属性键(可空)")
|
||||
@Size(max = 128, message = "属性键最长 128 字")
|
||||
private String key;
|
||||
|
||||
@Schema(description = "属性名(可空)")
|
||||
@Size(max = 128, message = "属性名最长 128 字")
|
||||
private String label;
|
||||
|
||||
@Schema(description = "属性值(可空)")
|
||||
@Size(max = 1000, message = "属性值最长 1000 字")
|
||||
private String value;
|
||||
}
|
||||
|
||||
@Data
|
||||
@Schema(description = "SKU 价格库存编辑行")
|
||||
public static class SkuEditDto {
|
||||
|
||||
@Schema(description = "京麦 SKU id(匹配键)")
|
||||
@NotBlank(message = "SKU 行缺少 skuId")
|
||||
@Size(max = 64, message = "skuId 最长 64 字")
|
||||
private String skuId;
|
||||
|
||||
@Schema(description = "京东价(可空=不改)")
|
||||
@DecimalMin(value = "0", message = "SKU 价格不能为负数")
|
||||
private BigDecimal price;
|
||||
|
||||
@Schema(description = "库存(可空=不改,非负)")
|
||||
private Integer stock;
|
||||
}
|
||||
}
|
||||
+2
@@ -81,4 +81,6 @@ public class JdProductEntity {
|
||||
private String collectBatchId;
|
||||
/** 同步时刻(INSERT 路径走列默认值,UPDATE 路径由 XML 显式刷 CURRENT_TIMESTAMP)。 */
|
||||
private LocalDateTime syncedAt;
|
||||
/** 人工编辑锁(V199):后管编辑保存置 1;采集 upsert/图片与 SKU 登记/覆盖清理对锁定的行全部跳过——人工值优先,重爬不再覆盖。 */
|
||||
private Integer manualLocked;
|
||||
}
|
||||
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
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.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 京东商品删除忽略行({@code biz_jd_product_ignore},V199)。
|
||||
*
|
||||
* <p>后管删除商品成功后写入被删行的 {@code (company_id, shop_id, product_code)} 三元组;
|
||||
* 采集回传({@code JdResultIngestService})对命中三元组的行跳过落库——防止删除后的商品
|
||||
* 在重爬同一店铺时被当作新行重新插回来(upsert 对不存在的键走 INSERT)。</p>
|
||||
*
|
||||
* <p>不设外键、不级联:主表行被覆盖清理级联删掉时忽略记录仍保留(忽略语义本就独立于
|
||||
* 行存在)。唯一键 {@code uk_company_shop_code} 与主表同三元组口径,重复删除幂等。</p>
|
||||
*/
|
||||
@Data
|
||||
@TableName("biz_jd_product_ignore")
|
||||
public class JdProductIgnoreEntity {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
/** 物化归属分公司(与主表 uk_company_shop_code 同口径)。 */
|
||||
private Long companyId;
|
||||
/** 京东店铺标识(与主表 shop_id 同形状,缺省回落 shop_name)。 */
|
||||
private String shopId;
|
||||
/** 京东商品编码。 */
|
||||
private String productCode;
|
||||
/** 写入时刻(后管删除成功时)。 */
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 京东数据库「类目」筛选下拉的一行(2026-11-15 用户口径「京麦的类目也要同步过来后台」)。
|
||||
*
|
||||
* <p>京麦列表接口只回末级类目名({@code categoryDetailVO.lastCategoryName},契约 §2.3 E3),
|
||||
* 拿不到完整三级树 ⇒ 下拉选项按本公司已落库行的 {@code category_path}(=末级类目名)
|
||||
* distinct 现算,{@code productCount} 是该类目行数(供界面展示「类目(N)」)。</p>
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东数据库类目筛选下拉选项")
|
||||
public class JdProductCategoryVo {
|
||||
|
||||
@Schema(description = "类目名(京麦末级类目,即落库的 category_path)")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "该类目的商品行数")
|
||||
private Long productCount;
|
||||
}
|
||||
+3
@@ -151,6 +151,9 @@ public class JdProductDetailVo {
|
||||
@Schema(description = "商品图片")
|
||||
public static class ImageViewVo {
|
||||
|
||||
@Schema(description = "图片登记行 id(后管编辑抽屉按它提交删图)")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "图片档:MAIN/ATTR/DETAIL")
|
||||
private String imageType;
|
||||
|
||||
|
||||
+52
-4
@@ -3,9 +3,11 @@ 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.JdProductIgnoreMapper;
|
||||
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.JdProductIgnoreEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductImageEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductDeleteVo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -13,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -53,6 +56,7 @@ public class JdProductDeleteService {
|
||||
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductImageMapper jdProductImageMapper;
|
||||
private final JdProductIgnoreMapper jdProductIgnoreMapper;
|
||||
private final JdProductRowDeleteService jdProductRowDeleteService;
|
||||
private final OssStorageService ossStorageService;
|
||||
|
||||
@@ -67,7 +71,7 @@ public class JdProductDeleteService {
|
||||
/** 删除给定商品(含 SKU、图片登记行与桶内对象);见类注释的三条边界。 */
|
||||
public JdProductDeleteVo delete(List<Long> requestedIds, Long scopedCompanyId) {
|
||||
List<Long> ids = normalizeIds(requestedIds);
|
||||
requireScopedRows(ids, scopedCompanyId);
|
||||
List<JdProductEntity> scopedRows = requireScopedRows(ids, scopedCompanyId);
|
||||
List<JdProductImageEntity> images = imageRowsOf(ids);
|
||||
Set<String> ownedKeys = new LinkedHashSet<>();
|
||||
Set<String> foreignKeys = new LinkedHashSet<>();
|
||||
@@ -89,6 +93,10 @@ public class JdProductDeleteService {
|
||||
throw ex;
|
||||
}
|
||||
|
||||
// V199:删除成功即登记忽略三元组——采集回传对命中行跳过落库,防止重爬把删掉的
|
||||
// 商品当新行插回来(upsert 对不存在的键走 INSERT)。登记失败不推翻删除结果,留痕即可。
|
||||
registerIgnoreRows(scopedRows);
|
||||
|
||||
JdProductDeleteVo vo = new JdProductDeleteVo();
|
||||
vo.setDeleted(deleted);
|
||||
vo.setImagesDeleted(images.size());
|
||||
@@ -122,17 +130,57 @@ public class JdProductDeleteService {
|
||||
return new ArrayList<>(unique);
|
||||
}
|
||||
|
||||
/** 归属闸:行必须都在(且都在当前公司可见面内),否则整批拒绝——不做部分删除。 */
|
||||
private void requireScopedRows(List<Long> ids, Long scopedCompanyId) {
|
||||
/** 归属闸:行必须都在(且都在当前公司可见面内),否则整批拒绝——不做部分删除。返回归属校验通过的行(供删除成功后登记忽略三元组)。 */
|
||||
private List<JdProductEntity> 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())
|
||||
+ " 个商品不存在或不属于当前公司,请刷新后重试(本次未删除任何数据)");
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除成功后登记忽略三元组(V199):采集回传按 (company_id, shop_id, product_code) 跳过
|
||||
* 命中行,防止重爬把删掉的商品重新插回来。幂等(uk 去重,重复删除同商品 no-op);
|
||||
* shop_id/product_code 读不出键形状的行跳过并留 WARN(与采集侧 protectedKey 同一判据)。
|
||||
* 登记失败不推翻删除结果(删除本体已完成),只留 error 日志。
|
||||
*/
|
||||
private void registerIgnoreRows(List<JdProductEntity> rows) {
|
||||
List<JdProductIgnoreEntity> ignores = new ArrayList<>();
|
||||
for (JdProductEntity row : rows) {
|
||||
if (row.getShopId() == null || row.getShopId().isBlank()
|
||||
|| row.getProductCode() == null || row.getProductCode().isBlank()) {
|
||||
log.warn("[{}] 行缺少 shop_id/product_code,忽略登记跳过 productId={}", LOG_TAG, row.getId());
|
||||
continue;
|
||||
}
|
||||
JdProductIgnoreEntity ignore = new JdProductIgnoreEntity();
|
||||
ignore.setCompanyId(row.getCompanyId());
|
||||
ignore.setShopId(row.getShopId().trim());
|
||||
ignore.setProductCode(row.getProductCode().trim());
|
||||
ignores.add(ignore);
|
||||
}
|
||||
if (ignores.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
for (JdProductIgnoreEntity ignore : ignores) {
|
||||
// 幂等登记:uk_company_shop_code 冲突即重复删除,直接跳过(MP 无 insert ignore,逐条吞重复)。
|
||||
try {
|
||||
jdProductIgnoreMapper.insert(ignore);
|
||||
} catch (org.springframework.dao.DuplicateKeyException alreadyIgnored) {
|
||||
log.info("[{}] 忽略三元组已登记过(重复删除同商品,no-op) companyId={} shopId={} code={}",
|
||||
LOG_TAG, ignore.getCompanyId(), ignore.getShopId(), ignore.getProductCode());
|
||||
}
|
||||
}
|
||||
log.info("[{}] 删除忽略登记完成 rows={}", LOG_TAG, ignores.size());
|
||||
} catch (RuntimeException ex) {
|
||||
log.error("[{}] 删除忽略登记失败(删除本体已完成,重爬可能复活这些商品,需人工补登记) rows={}",
|
||||
LOG_TAG, rows.size(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
private List<JdProductImageEntity> imageRowsOf(List<Long> ids) {
|
||||
|
||||
+26
-122
@@ -13,26 +13,22 @@ 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.JdProductCategoryVo;
|
||||
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;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 后管「京东数据库」只读查询(v1.4 Phase 5):分页 / 详情 / 导出。
|
||||
* 后管「京东数据库」只读查询(v1.4 Phase 5):分页 / 详情。
|
||||
*
|
||||
* <p>分页钳制是本服务的职责(铁律 15):mapper 的 {@code offset}/{@code limit} 只收钳制后的值
|
||||
* (单一出口,mapper 不做第二次换算)。钳制形状照 {@code AppearancePatentCleanItemQueryService}
|
||||
@@ -49,9 +45,8 @@ import java.util.Map;
|
||||
* 非该形状时<b>不抛 500</b>,落空 {@code attrGroups} 并把原文放进 {@code attrJsonRaw} 兜底展示。
|
||||
* 主表高频列直接从 entity 映射,不走 JSON。</p>
|
||||
*
|
||||
* <p><b>导出</b>与分页共用同一过滤面与同一条 SQL(offset=0、limit=导出上限,不另写导出查询),
|
||||
* 先 {@code countProducts} 卡 {@link #EXPORT_MAX_ROWS} 上限防内存;行集按后管页面主要字段
|
||||
* 中文列序写 xlsx(SXSSF 流式,口径同 {@code QueryAsinService.export} 的 byte[] 形状)。</p>
|
||||
* <p>导出(byte[] xlsx)已随 2026-11-15 客户口径「导出也不用」整段下线,控制器端点同窗删;
|
||||
* {@code sale_status} 过滤参数同窗撤掉(前端筛选框下线),DB 列与详情/回传链不动。</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -62,11 +57,6 @@ public class JdProductQueryService {
|
||||
private static final long MAX_PAGE = 10_000;
|
||||
/** 每页条数上限(照外观专利只读页同值)。 */
|
||||
private static final long MAX_PAGE_SIZE = 100;
|
||||
/** 单次导出行数上限:超限按业务错误拒(提示缩小筛选面),不把无界内存写进一次 HTTP 响应。 */
|
||||
public static final long EXPORT_MAX_ROWS = 50_000;
|
||||
/** 导出时间列的展示格式。 */
|
||||
private static final DateTimeFormatter EXPORT_DATETIME_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
/** attr_json 原始形状里的 Tab 字段名(客户端实拍契约,AC-3 保真不转写)。 */
|
||||
private static final String ATTR_TAB_FIELD = "tab";
|
||||
private static final String ATTR_ATTRS_FIELD = "attrs";
|
||||
@@ -74,12 +64,6 @@ public class JdProductQueryService {
|
||||
private static final String ATTR_LABEL_FIELD = "label";
|
||||
private static final String ATTR_VALUE_FIELD = "value";
|
||||
|
||||
/** 导出列序(与后管页面主要字段同序,中文列名)。 */
|
||||
private static final List<String> EXPORT_HEADERS = List.of(
|
||||
"店铺名", "店铺ID", "商品编码", "货号", "SKU", "SKU数", "信息分", "标题", "品牌", "类目路径",
|
||||
"价格下限", "价格上限", "库存", "商品状态", "来源Tab", "上架时间", "京东创建时间",
|
||||
"京东修改时间", "属性键数", "图片数", "主图URL", "分组", "同步时间");
|
||||
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductImageMapper jdProductImageMapper;
|
||||
private final JdProductSkuMapper jdProductSkuMapper;
|
||||
@@ -90,10 +74,10 @@ public class JdProductQueryService {
|
||||
* 分页查询商品行({@code synced_at} 倒序)。
|
||||
*
|
||||
* <p>过滤面:公司(物化列 eq)、店铺名 contains、商品编码 contains、标题关键词 contains、
|
||||
* 商品状态 eq、分组 eq、同步起止(datetime 闭区间)。</p>
|
||||
* 类目 eq、分组 eq、同步起止(datetime 闭区间)。</p>
|
||||
*/
|
||||
public JdProductPageVo page(Long page, Long pageSize, Long scopedCompanyId, String shopName,
|
||||
String productCode, String titleKeyword, String saleStatus, Long groupId,
|
||||
String productCode, String titleKeyword, String category, Long groupId,
|
||||
LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
if (syncedFrom != null && syncedTo != null && syncedFrom.isAfter(syncedTo)) {
|
||||
throw new BusinessException("开始时间不能晚于结束时间");
|
||||
@@ -101,15 +85,15 @@ public class JdProductQueryService {
|
||||
long safePage = page == null || page < 1 ? 1 : Math.min(page, MAX_PAGE);
|
||||
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, saleStatus, groupId,
|
||||
syncedFrom, syncedTo);
|
||||
Filter filter = filter(scopedCompanyId, shopName, productCode, titleKeyword, category,
|
||||
groupId, syncedFrom, syncedTo);
|
||||
long total = jdProductMapper.countProducts(filter.companyId(), filter.shopName(), filter.productCode(),
|
||||
filter.titleKeyword(), filter.saleStatus(), filter.groupId(), filter.syncedFrom(),
|
||||
filter.syncedTo());
|
||||
filter.titleKeyword(), filter.category(), filter.groupId(),
|
||||
filter.syncedFrom(), filter.syncedTo());
|
||||
List<JdProductEntity> rows = total <= 0 ? List.of()
|
||||
: jdProductMapper.selectProductPage(filter.companyId(), filter.shopName(), filter.productCode(),
|
||||
filter.titleKeyword(), filter.saleStatus(), filter.groupId(), filter.syncedFrom(),
|
||||
filter.syncedTo(), (safePage - 1) * safeSize, (int) safeSize);
|
||||
filter.titleKeyword(), filter.category(), filter.groupId(),
|
||||
filter.syncedFrom(), filter.syncedTo(), (safePage - 1) * safeSize, (int) safeSize);
|
||||
List<JdProductListItemVo> items = rows.stream().map(this::toListItem).toList();
|
||||
fillGroupNames(items);
|
||||
JdProductPageVo vo = new JdProductPageVo();
|
||||
@@ -120,6 +104,16 @@ public class JdProductQueryService {
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 类目筛选下拉数据源(2026-11-15 用户口径「京麦的类目也要同步过来后台」):
|
||||
* 本公司已落库行 distinct 的 {@code category_path}(=京麦末级类目名,契约 §2.3 E3)
|
||||
* + 各自行数,按类目名排序。公司隔离与分页同门(控制器经 {@code readCompanyIdOrOwn} 传入)。
|
||||
*/
|
||||
public List<JdProductCategoryVo> categories(Long scopedCompanyId) {
|
||||
Long companyId = scopedCompanyId != null && scopedCompanyId > 0 ? scopedCompanyId : null;
|
||||
return jdProductMapper.selectProductCategories(companyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品详情(主字段全量 + attr_json 解析 + 已转存图片)。
|
||||
*
|
||||
@@ -172,49 +166,19 @@ public class JdProductQueryService {
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出当前过滤面全量明细为 xlsx 字节(控制器直接回 {@code ResponseEntity<byte[]>},
|
||||
* 口径同 {@code AppearancePatentCleanItemQueryService.export})。
|
||||
*
|
||||
* <p>先 count 卡 {@link #EXPORT_MAX_ROWS} 上限再取数;取数复用 {@code selectProductPage}
|
||||
* 这一条 SQL(offset=0、limit=上限),不另写导出查询——过滤面永远和分页页签同源。</p>
|
||||
*/
|
||||
public byte[] export(Long scopedCompanyId, String shopName, String productCode, String titleKeyword,
|
||||
String saleStatus, Long groupId, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
if (syncedFrom != null && syncedTo != null && syncedFrom.isAfter(syncedTo)) {
|
||||
throw new BusinessException("开始时间不能晚于结束时间");
|
||||
}
|
||||
Filter filter = filter(scopedCompanyId, shopName, productCode, titleKeyword, saleStatus, groupId,
|
||||
syncedFrom, syncedTo);
|
||||
long total = jdProductMapper.countProducts(filter.companyId(), filter.shopName(), filter.productCode(),
|
||||
filter.titleKeyword(), filter.saleStatus(), filter.groupId(), filter.syncedFrom(),
|
||||
filter.syncedTo());
|
||||
if (total > EXPORT_MAX_ROWS) {
|
||||
throw new BusinessException("京东数据库共 " + total + " 行,超过导出上限 " + EXPORT_MAX_ROWS
|
||||
+ " 行,请缩小筛选范围后重试");
|
||||
}
|
||||
List<JdProductEntity> rows = total <= 0 ? List.of()
|
||||
: jdProductMapper.selectProductPage(filter.companyId(), filter.shopName(), filter.productCode(),
|
||||
filter.titleKeyword(), filter.saleStatus(), filter.groupId(), filter.syncedFrom(),
|
||||
filter.syncedTo(), 0L, (int) EXPORT_MAX_ROWS);
|
||||
List<JdProductListItemVo> items = rows.stream().map(this::toListItem).toList();
|
||||
fillGroupNames(items);
|
||||
return writeExportWorkbook(items);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 过滤面
|
||||
|
||||
/** 服务侧过滤面归一:companyId/groupId ≤0 按没选处理,文本 trim 后空串归 null。 */
|
||||
private record Filter(Long companyId, String shopName, String productCode, String titleKeyword,
|
||||
String saleStatus, Long groupId, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
String category, Long groupId, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
}
|
||||
|
||||
private static Filter filter(Long scopedCompanyId, String shopName, String productCode, String titleKeyword,
|
||||
String saleStatus, Long groupId, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
String category, Long groupId, LocalDateTime syncedFrom, LocalDateTime syncedTo) {
|
||||
Long companyId = scopedCompanyId != null && scopedCompanyId > 0 ? scopedCompanyId : null;
|
||||
Long safeGroupId = groupId != null && groupId > 0 ? groupId : null;
|
||||
return new Filter(companyId, trimToNull(shopName), trimToNull(productCode), trimToNull(titleKeyword),
|
||||
trimToNull(saleStatus), safeGroupId, syncedFrom, syncedTo);
|
||||
trimToNull(category), safeGroupId, syncedFrom, syncedTo);
|
||||
}
|
||||
|
||||
/** 超管不选公司(scopedCompanyId=null)=全量可见;正数=只看该公司。 */
|
||||
@@ -290,6 +254,7 @@ public class JdProductQueryService {
|
||||
List<JdProductDetailVo.ImageViewVo> images = new ArrayList<>(rows.size());
|
||||
for (JdProductImageEntity row : rows) {
|
||||
JdProductDetailVo.ImageViewVo view = new JdProductDetailVo.ImageViewVo();
|
||||
view.setId(row.getId());
|
||||
view.setImageType(row.getImageType());
|
||||
view.setAttrName(row.getAttrName());
|
||||
view.setSortNo(row.getSortNo());
|
||||
@@ -405,67 +370,6 @@ public class JdProductQueryService {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 导出
|
||||
|
||||
private byte[] writeExportWorkbook(List<JdProductListItemVo> items) {
|
||||
try (SXSSFWorkbook workbook = new SXSSFWorkbook(100);
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
Sheet sheet = workbook.createSheet("京东数据库");
|
||||
Row header = sheet.createRow(0);
|
||||
for (int c = 0; c < EXPORT_HEADERS.size(); c++) {
|
||||
header.createCell(c).setCellValue(EXPORT_HEADERS.get(c));
|
||||
}
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
writeExportRow(sheet, i + 1, items.get(i));
|
||||
}
|
||||
// 标题列放宽,其余走默认窄列;与页面列宽观感对齐即可,不追求逐像素。
|
||||
sheet.setColumnWidth(4, 46 * 256);
|
||||
workbook.write(outputStream);
|
||||
workbook.dispose();
|
||||
return outputStream.toByteArray();
|
||||
} catch (Exception ex) {
|
||||
// 落盘/序列化失败必须给可读业务错误,细节进日志(CatchSilenceRatchetTest RETHROW 形状)。
|
||||
log.warn("[jd-product] 导出 xlsx 失败 rows={} msg={}", items.size(), ex.getMessage());
|
||||
throw new BusinessException("导出京东数据库失败,请稍后重试");
|
||||
}
|
||||
}
|
||||
|
||||
private void writeExportRow(Sheet sheet, int rowIndex, JdProductListItemVo item) {
|
||||
Row row = sheet.createRow(rowIndex);
|
||||
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.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(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()));
|
||||
}
|
||||
|
||||
private static String safe(String value) {
|
||||
return value == null ? "" : value;
|
||||
}
|
||||
|
||||
private static String trimToNull(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
|
||||
+421
@@ -0,0 +1,421 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.text.UserMessages;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
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.dto.JdProductUpdateRequest;
|
||||
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 lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 后管「编辑京东商品」编排(PUT /api/admin/jd-products/{id},2026-11-15 用户口径
|
||||
* 「每条数据可以编辑,详情里的内容都可以改」)。
|
||||
*
|
||||
* <p>三条块语义:</p>
|
||||
* <ul>
|
||||
* <li><b>主字段全量提交</b>:标题/品牌/类目/货号/价格区间/总库存/状态/分组按请求体覆写;
|
||||
* 分组 id 归属必须与商品行同公司(防跨公司挂组)。保存即置 {@code manual_locked=1}(V199
|
||||
* 人工编辑锁)——采集回传的 upsert / 图片与 SKU 登记 / 覆盖清理对锁定的行全部跳过,
|
||||
* 人工值不被重爬覆盖。</li>
|
||||
* <li><b>可选块缺省不动</b>:{@code attrGroups} 缺省不动属性;提交时只重建 attr_json 的
|
||||
* {@code tabs} 段、<b>保留原 JSON 其它顶层键</b>(images/productCode 等,铁律 30 形状:
|
||||
* 不整份覆写别人的键);原文解析不出(attrJsonRaw 兜底行)则明确拒绝,不让编辑静默丢原文。
|
||||
* {@code skus} 按 (productId, skuId) 匹配逐行改 price/stock,提交里有不存在的 skuId 按不存在拒绝。
|
||||
* {@code deleteImageIds} 复用 {@link JdProductDeleteService} 的共用对象语义删桶对象+登记行。</li>
|
||||
* <li><b>桶删除在库行之前</b>:与 {@link JdProductDeleteService} 同纪律——对象删除不可回滚,
|
||||
* 先删桶、任何对象删不掉整笔拒绝(此时零库行已动,可安全重试)。</li>
|
||||
* </ul>
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class JdProductUpdateService {
|
||||
|
||||
private static final String LOG_TAG = "jd-update";
|
||||
|
||||
/** 单次属性分组/属性行上限(防脏包一次写爆 attr_json;京麦 8 个 Tab 量级)。 */
|
||||
private static final int MAX_ATTR_GROUPS = 64;
|
||||
private static final int MAX_ATTRS_PER_GROUP = 200;
|
||||
/** 单次提交 SKU 编辑行上限(京麦单商品 SKU 个位数到几十,与登记侧 200 同量级)。 */
|
||||
private static final int MAX_SKU_EDITS = 200;
|
||||
/** 单次删图上限(与登记链单商品图片量级同档)。 */
|
||||
private static final int MAX_IMAGE_DELETES = 500;
|
||||
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductSkuMapper jdProductSkuMapper;
|
||||
private final JdProductImageMapper jdProductImageMapper;
|
||||
private final JdProductGroupMapper jdProductGroupMapper;
|
||||
private final OssStorageService ossStorageService;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
/** 京东商品图独立桶(与 JdProductDeleteService 同源,application.yml 写死,铁律 17)。 */
|
||||
@Value("${aiimage.jd.oss.bucket:jd-products}")
|
||||
private String bucket;
|
||||
|
||||
/** 对象 key 前缀(与 JdProductDeleteService/JdProductImageIngestService 同源)。 */
|
||||
@Value("${aiimage.jd.oss.key-prefix:jd-products/}")
|
||||
private String keyPrefix;
|
||||
|
||||
/**
|
||||
* 编辑保存,返回商品行 id(控制器用它回读刷新后的详情 VO)。
|
||||
*
|
||||
* <p>公司隔离与详情同门:行 company_id 与范围不符按不存在拒绝(不泄露越界行存在性)。</p>
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long update(Long id, JdProductUpdateRequest body, Long scopedCompanyId) {
|
||||
if (id == null || id <= 0) {
|
||||
throw new BusinessException("商品不存在");
|
||||
}
|
||||
JdProductEntity row = jdProductMapper.selectById(id);
|
||||
if (row == null || !companyVisible(row.getCompanyId(), scopedCompanyId)) {
|
||||
throw new BusinessException("商品不存在");
|
||||
}
|
||||
validateBody(body);
|
||||
|
||||
Long groupId = body.getGroupId();
|
||||
if (groupId != null) {
|
||||
JdProductGroupEntity group = jdProductGroupMapper.selectById(groupId);
|
||||
if (group == null || !row.getCompanyId().equals(group.getCompanyId())) {
|
||||
throw new BusinessException("分组不存在或不属于当前公司,请刷新分组后重试");
|
||||
}
|
||||
}
|
||||
|
||||
// 可选块先算(属性重建可能拒绝),再动库行——拒绝时零库行已动。
|
||||
String newAttrJson = null;
|
||||
Integer newAttrCount = null;
|
||||
if (body.getAttrGroups() != null) {
|
||||
AttrRewrite rewrite = rebuildAttrJson(row, body.getAttrGroups());
|
||||
newAttrJson = rewrite.json();
|
||||
newAttrCount = rewrite.attrCount();
|
||||
}
|
||||
|
||||
LambdaUpdateWrapper<JdProductEntity> updater = new LambdaUpdateWrapper<JdProductEntity>()
|
||||
.eq(JdProductEntity::getId, id)
|
||||
.set(JdProductEntity::getTitle, body.getTitle().trim())
|
||||
.set(JdProductEntity::getBrand, trimToNull(body.getBrand()))
|
||||
.set(JdProductEntity::getCategoryPath, trimToNull(body.getCategoryPath()))
|
||||
.set(JdProductEntity::getItemNum, trimToNull(body.getItemNum()))
|
||||
.set(JdProductEntity::getPriceMin, body.getPriceMin())
|
||||
.set(JdProductEntity::getPriceMax, body.getPriceMax())
|
||||
.set(JdProductEntity::getStockTotal, body.getStockTotal())
|
||||
.set(JdProductEntity::getSaleStatus, trimToNull(body.getSaleStatus()))
|
||||
.set(JdProductEntity::getGroupId, groupId)
|
||||
// 人工编辑锁:保存即锁定,采集链路(upsert/登记/覆盖清理)此后跳过本行。
|
||||
.set(JdProductEntity::getManualLocked, 1);
|
||||
if (newAttrJson != null) {
|
||||
updater.set(JdProductEntity::getAttrJson, newAttrJson);
|
||||
updater.set(JdProductEntity::getAttrCount, newAttrCount);
|
||||
}
|
||||
int updated = jdProductMapper.update(null, updater);
|
||||
if (updated <= 0) {
|
||||
throw new BusinessException("商品不存在或已被删除,请刷新后重试");
|
||||
}
|
||||
|
||||
if (body.getSkus() != null && !body.getSkus().isEmpty()) {
|
||||
applySkuEdits(row.getId(), body.getSkus());
|
||||
}
|
||||
if (body.getDeleteImageIds() != null && !body.getDeleteImageIds().isEmpty()) {
|
||||
int purged = deleteImages(row, body.getDeleteImageIds());
|
||||
log.info("[{}] 编辑删除图片 productId={} requested={} objectsPurged={}",
|
||||
LOG_TAG, row.getId(), body.getDeleteImageIds().size(), purged);
|
||||
}
|
||||
log.info("[{}] 编辑保存完成 productId={} companyId={} locked=1 attrGroups={} skuEdits={} imageDeletes={}",
|
||||
LOG_TAG, row.getId(), row.getCompanyId(),
|
||||
body.getAttrGroups() == null ? 0 : body.getAttrGroups().size(),
|
||||
body.getSkus() == null ? 0 : body.getSkus().size(),
|
||||
body.getDeleteImageIds() == null ? 0 : body.getDeleteImageIds().size());
|
||||
return id;
|
||||
}
|
||||
|
||||
// ---------- 校验 ----------
|
||||
|
||||
private void validateBody(JdProductUpdateRequest body) {
|
||||
if (body.getPriceMin() != null && body.getPriceMax() != null
|
||||
&& body.getPriceMin().compareTo(body.getPriceMax()) > 0) {
|
||||
throw new BusinessException("价格区间下限不能大于上限");
|
||||
}
|
||||
if (body.getStockTotal() != null && body.getStockTotal() < 0) {
|
||||
throw new BusinessException("总库存不能为负数");
|
||||
}
|
||||
if (body.getAttrGroups() != null && body.getAttrGroups().size() > MAX_ATTR_GROUPS) {
|
||||
throw new BusinessException("属性分组超过上限 " + MAX_ATTR_GROUPS + " 个,请精简后重试");
|
||||
}
|
||||
if (body.getAttrGroups() != null) {
|
||||
for (JdProductUpdateRequest.AttrGroupDto group : body.getAttrGroups()) {
|
||||
if (group == null) {
|
||||
continue;
|
||||
}
|
||||
if (group.getAttrs() != null && group.getAttrs().size() > MAX_ATTRS_PER_GROUP) {
|
||||
throw new BusinessException("单个属性分组的属性行超过上限 " + MAX_ATTRS_PER_GROUP + " 条,请精简后重试");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (body.getSkus() != null && body.getSkus().size() > MAX_SKU_EDITS) {
|
||||
throw new BusinessException("SKU 编辑行超过上限 " + MAX_SKU_EDITS + " 行,请分批保存");
|
||||
}
|
||||
if (body.getSkus() != null) {
|
||||
for (JdProductUpdateRequest.SkuEditDto sku : body.getSkus()) {
|
||||
if (sku != null && sku.getStock() != null && sku.getStock() < 0) {
|
||||
throw new BusinessException("SKU 库存不能为负数(skuId=" + sku.getSkuId() + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (body.getDeleteImageIds() != null && body.getDeleteImageIds().size() > MAX_IMAGE_DELETES) {
|
||||
throw new BusinessException("单次最多删除 " + MAX_IMAGE_DELETES + " 张图片,请分批保存");
|
||||
}
|
||||
}
|
||||
|
||||
/** 超管不选公司(scopedCompanyId=null)=全量可编辑;正数=只可编辑该公司行。 */
|
||||
private static boolean companyVisible(Long rowCompanyId, Long scopedCompanyId) {
|
||||
if (scopedCompanyId == null || scopedCompanyId <= 0) {
|
||||
return true;
|
||||
}
|
||||
return scopedCompanyId.equals(rowCompanyId);
|
||||
}
|
||||
|
||||
// ---------- 属性重建(只重建 tabs 段,其余顶层键保留) ----------
|
||||
|
||||
/**
|
||||
* 由提交的 attrGroups 重建 attr_json:原 JSON 是契约形状
|
||||
* {@code {"tabs":[…],"images":[…],"productCode":"…"}}——拷贝原 Map、只覆写 {@code tabs} 键,
|
||||
* images/productCode 等其它顶层键原样保留(铁律 30:不整份覆写别人的键)。
|
||||
* 原文解析不出或不是契约形状(tabs 缺失/不是数组)时按不存在拒绝——这种行的原文兜底
|
||||
* 在详情页可见,静默重写等于丢数据。
|
||||
*/
|
||||
private AttrRewrite rebuildAttrJson(JdProductEntity row, List<JdProductUpdateRequest.AttrGroupDto> groups) {
|
||||
if (row.getAttrJson() == null || row.getAttrJson().isBlank()) {
|
||||
// 原本就没有属性:按空 tabs 新建,不拒(首采行 attr_json 可为空)。
|
||||
return new AttrRewrite(writeTabs(List.of(), null), countAttrs(groups));
|
||||
}
|
||||
Map<String, Object> root;
|
||||
try {
|
||||
Object parsed = objectMapper.readValue(row.getAttrJson(), Object.class);
|
||||
root = parsed instanceof Map<?, ?> map ? asStringKeyMap(map) : null;
|
||||
} catch (Exception ex) {
|
||||
root = null;
|
||||
}
|
||||
if (root == null || !(root.get("tabs") instanceof List<?>)) {
|
||||
throw new BusinessException("该商品的属性原文形状不识别(详情里以原文兜底展示),"
|
||||
+ "为防丢数据不允许在线编辑属性;请仅保存主字段");
|
||||
}
|
||||
return new AttrRewrite(writeTabs(groups, root), countAttrs(groups));
|
||||
}
|
||||
|
||||
/** tabs 段重建:groupName→tab、attrs→[{key,label,value}];root 非 null 时其余顶层键原样保留。 */
|
||||
private String writeTabs(List<JdProductUpdateRequest.AttrGroupDto> groups, Map<String, Object> root) {
|
||||
List<Map<String, Object>> tabs = new ArrayList<>(groups.size());
|
||||
for (JdProductUpdateRequest.AttrGroupDto group : groups) {
|
||||
if (group == null) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> tab = new java.util.LinkedHashMap<>();
|
||||
tab.put("tab", trimToNull(group.getGroupName()));
|
||||
List<Map<String, Object>> attrs = new ArrayList<>();
|
||||
if (group.getAttrs() != null) {
|
||||
for (JdProductUpdateRequest.AttrDto attr : group.getAttrs()) {
|
||||
if (attr == null) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> pair = new java.util.LinkedHashMap<>();
|
||||
pair.put("key", trimToNull(attr.getKey()));
|
||||
pair.put("label", trimToNull(attr.getLabel()));
|
||||
pair.put("value", trimToNull(attr.getValue()));
|
||||
attrs.add(pair);
|
||||
}
|
||||
}
|
||||
tab.put("attrs", attrs);
|
||||
tabs.add(tab);
|
||||
}
|
||||
try {
|
||||
if (root == null) {
|
||||
return objectMapper.writeValueAsString(Map.of("tabs", tabs));
|
||||
}
|
||||
root.put("tabs", tabs);
|
||||
return objectMapper.writeValueAsString(root);
|
||||
} catch (com.fasterxml.jackson.core.JsonProcessingException ex) {
|
||||
throw new BusinessException("属性 JSON 序列化失败,请稍后重试", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static int countAttrs(List<JdProductUpdateRequest.AttrGroupDto> groups) {
|
||||
int count = 0;
|
||||
for (JdProductUpdateRequest.AttrGroupDto group : groups) {
|
||||
if (group != null && group.getAttrs() != null) {
|
||||
count += group.getAttrs().size();
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// ---------- SKU 价格库存 ----------
|
||||
|
||||
/** 按 (productId, skuId) 逐行更新 price/stock(null=不改该列);提交里有不存在的 skuId 按不存在拒绝。 */
|
||||
private void applySkuEdits(Long productId, List<JdProductUpdateRequest.SkuEditDto> skus) {
|
||||
List<JdProductSkuEntity> rows = jdProductSkuMapper.selectList(
|
||||
new LambdaQueryWrapper<JdProductSkuEntity>()
|
||||
.eq(JdProductSkuEntity::getProductId, productId));
|
||||
Map<String, JdProductSkuEntity> bySkuId = new java.util.LinkedHashMap<>();
|
||||
for (JdProductSkuEntity sku : rows) {
|
||||
if (sku.getSkuId() != null) {
|
||||
bySkuId.put(sku.getSkuId(), sku);
|
||||
}
|
||||
}
|
||||
List<String> missing = new ArrayList<>();
|
||||
int edited = 0;
|
||||
Set<String> seen = new LinkedHashSet<>();
|
||||
for (JdProductUpdateRequest.SkuEditDto edit : skus) {
|
||||
if (edit == null || edit.getSkuId() == null || edit.getSkuId().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
String skuId = edit.getSkuId().trim();
|
||||
if (!seen.add(skuId)) {
|
||||
continue;
|
||||
}
|
||||
JdProductSkuEntity existing = bySkuId.get(skuId);
|
||||
if (existing == null) {
|
||||
missing.add(skuId);
|
||||
continue;
|
||||
}
|
||||
LambdaUpdateWrapper<JdProductSkuEntity> updater = new LambdaUpdateWrapper<JdProductSkuEntity>()
|
||||
.eq(JdProductSkuEntity::getId, existing.getId());
|
||||
boolean touched = false;
|
||||
if (edit.getPrice() != null) {
|
||||
updater.set(JdProductSkuEntity::getPrice, edit.getPrice());
|
||||
touched = true;
|
||||
}
|
||||
if (edit.getStock() != null) {
|
||||
updater.set(JdProductSkuEntity::getStock, edit.getStock());
|
||||
touched = true;
|
||||
}
|
||||
if (touched) {
|
||||
jdProductSkuMapper.update(null, updater);
|
||||
edited++;
|
||||
}
|
||||
}
|
||||
if (!missing.isEmpty()) {
|
||||
throw new BusinessException("有 " + missing.size() + " 个 SKU 行不存在(可能已被重新采集刷新),"
|
||||
+ "请刷新详情后重试:首个缺失 skuId=" + missing.get(0));
|
||||
}
|
||||
log.info("[{}] SKU 价格库存已更新 productId={} rows={}", LOG_TAG, productId, edited);
|
||||
}
|
||||
|
||||
// ---------- 删图(与 JdProductDeleteService 同纪律:先删桶、全成功才删行) ----------
|
||||
|
||||
/**
|
||||
* 删除给定图片登记行:逐 id 校验归属(必须挂在本商品上,否则整笔拒绝)→ 共用对象保留
|
||||
* (同 key 被其他商品引用)→ 先删桶对象(任一失败整笔拒绝、零库行已动)→ 删登记行 →
|
||||
* 按剩余行数回写 {@code image_count}。
|
||||
*/
|
||||
private int deleteImages(JdProductEntity row, List<Long> requestedIds) {
|
||||
Set<Long> ids = new LinkedHashSet<>();
|
||||
for (Long imageId : requestedIds) {
|
||||
if (imageId != null && imageId > 0) {
|
||||
ids.add(imageId);
|
||||
}
|
||||
}
|
||||
if (ids.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
List<JdProductImageEntity> imageRows = jdProductImageMapper.selectList(
|
||||
new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.in(JdProductImageEntity::getId, ids)
|
||||
.eq(JdProductImageEntity::getProductId, row.getId()));
|
||||
if (imageRows.size() != ids.size()) {
|
||||
throw new BusinessException("有 " + (ids.size() - imageRows.size())
|
||||
+ " 张图片不存在或不属于该商品,请刷新后重试(本次未删除任何数据)");
|
||||
}
|
||||
// 共用性回查(同 JdProductDeleteService.sharedObjectKeys 语义,分片防 IN 计划走坏)。
|
||||
Set<String> ownedKeys = new LinkedHashSet<>();
|
||||
for (JdProductImageEntity image : imageRows) {
|
||||
String key = image.getObjectKey() == null ? "" : image.getObjectKey().trim();
|
||||
if (!key.isEmpty() && keyPrefix != null && !keyPrefix.isEmpty() && key.startsWith(keyPrefix)) {
|
||||
ownedKeys.add(key);
|
||||
}
|
||||
}
|
||||
Set<String> sharedKeys = new LinkedHashSet<>();
|
||||
List<String> all = new ArrayList<>(ownedKeys);
|
||||
final int chunkSize = 500;
|
||||
for (int from = 0; from < all.size(); from += chunkSize) {
|
||||
List<String> chunk = all.subList(from, Math.min(all.size(), from + chunkSize));
|
||||
List<JdProductImageEntity> others = jdProductImageMapper.selectList(
|
||||
new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.select(JdProductImageEntity::getObjectKey)
|
||||
.in(JdProductImageEntity::getObjectKey, chunk)
|
||||
.ne(JdProductImageEntity::getProductId, row.getId()));
|
||||
for (JdProductImageEntity other : others) {
|
||||
if (other.getObjectKey() != null) {
|
||||
sharedKeys.add(other.getObjectKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
int purged = 0;
|
||||
for (String key : ownedKeys) {
|
||||
if (sharedKeys.contains(key)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
ossStorageService.removeObject(bucket, key);
|
||||
} catch (RuntimeException ex) {
|
||||
// 异常原文不进界面(RawExceptionMessageSinkGuard):headline 中文判人话,原文只进日志。
|
||||
throw new BusinessException(
|
||||
UserMessages.with("图片对象删除失败,本次未保存任何修改(" + key + ")", ex), ex);
|
||||
}
|
||||
purged++;
|
||||
}
|
||||
jdProductImageMapper.delete(new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.eq(JdProductImageEntity::getProductId, row.getId())
|
||||
.in(JdProductImageEntity::getId, ids));
|
||||
// 图片数只由删除/登记两侧按表实数刷(XML UPDATE 段刻意不写它,P1-1 口径不变)。
|
||||
Long remaining = jdProductImageMapper.selectCount(
|
||||
new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.eq(JdProductImageEntity::getProductId, row.getId()));
|
||||
jdProductMapper.update(null, new LambdaUpdateWrapper<JdProductEntity>()
|
||||
.eq(JdProductEntity::getId, row.getId())
|
||||
.set(JdProductEntity::getImageCount, remaining.intValue()));
|
||||
return purged;
|
||||
}
|
||||
|
||||
// ---------- 公共小件 ----------
|
||||
|
||||
private static String trimToNull(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
String trimmed = value.trim();
|
||||
return trimmed.isEmpty() ? null : trimmed;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Map<String, Object> asStringKeyMap(Map<?, ?> map) {
|
||||
Map<String, Object> result = new java.util.LinkedHashMap<>();
|
||||
for (Map.Entry<?, ?> entry : map.entrySet()) {
|
||||
result.put(String.valueOf(entry.getKey()), entry.getValue());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 属性重建结果:新 JSON 与属性键计数(attr_count 质量核查分母同步刷新)。 */
|
||||
private record AttrRewrite(String json, int attrCount) {
|
||||
}
|
||||
}
|
||||
+91
-1
@@ -9,10 +9,12 @@ import com.nanri.aiimage.common.retry.CredentialFatalClassifier;
|
||||
import com.nanri.aiimage.common.task.TaskStatuses;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.text.UserMessages;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductIgnoreMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdCollectTaskCreateRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdIngestChunkRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductIgnoreEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdIngestChunkVo;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
@@ -31,6 +33,7 @@ import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 京东采集分片回传落库(v1.4 Phase 2,蓝图 §3.2 回传契约)。
|
||||
@@ -68,6 +71,7 @@ public class JdResultIngestService {
|
||||
|
||||
private final FileTaskMapper fileTaskMapper;
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductIgnoreMapper jdProductIgnoreMapper;
|
||||
private final TaskScopeStateMapper taskScopeStateMapper;
|
||||
private final AdminUserMapper adminUserMapper;
|
||||
private final JdCollectTaskService collectTaskService;
|
||||
@@ -136,7 +140,15 @@ public class JdResultIngestService {
|
||||
}
|
||||
}
|
||||
if (!rows.isEmpty()) {
|
||||
jdProductMapper.upsertBatch(rows);
|
||||
// V199 保护闸:人工编辑锁(manual_locked=1)与删除忽略表的行不落库、不登记
|
||||
// 子表——后管编辑/删除过的人工值优先,重爬不得覆盖/复活(跳过只留 WARN,不炸整批)。
|
||||
int protectedSkipped = filterProtectedRows(companyId, rows, pendingImages, pendingSkus);
|
||||
if (!rows.isEmpty()) {
|
||||
jdProductMapper.upsertBatch(rows);
|
||||
} else {
|
||||
log.info("[{}] 整批行都被人工编辑锁/删除忽略保护,本次不落库 taskId={} skipped={}",
|
||||
LOG_TAG, taskId, protectedSkipped);
|
||||
}
|
||||
}
|
||||
// Phase 4:直传完成的图片清单按 statObject 对撞落 biz_jd_product_image(登记链唯一出口
|
||||
// JdProductImageIngestService,uk_product_image 幂等)——必须在商品行落库之后执行。
|
||||
@@ -252,6 +264,84 @@ public class JdResultIngestService {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- V199 保护闸:人工编辑锁与删除忽略 ----------
|
||||
|
||||
/**
|
||||
* 把被保护行从本批落库清单里摘掉(rows 与 pendingImages/pendingSkus 三张清单按下标并行)。
|
||||
* 保护集=本公司 {@code manual_locked=1} 的行(后管编辑保存置 1,人工值优先)∪
|
||||
* {@code biz_jd_product_ignore} 命中三元组(后管删除成功登记,防重爬复活)。
|
||||
* 返回跳过行数;被跳过的行连同其图片/SKU 登记一起不落库。
|
||||
*/
|
||||
private int filterProtectedRows(Long companyId, List<JdProductEntity> rows,
|
||||
List<PendingImageRegistration> pendingImages,
|
||||
List<PendingSkuRegistration> pendingSkus) {
|
||||
Set<String> protectedKeys = loadProtectedKeys(companyId);
|
||||
if (protectedKeys.isEmpty() || rows.size() != pendingImages.size()
|
||||
|| rows.size() != pendingSkus.size()) {
|
||||
return 0;
|
||||
}
|
||||
List<JdProductEntity> keptRows = new ArrayList<>();
|
||||
List<PendingImageRegistration> keptImages = new ArrayList<>();
|
||||
List<PendingSkuRegistration> keptSkus = new ArrayList<>();
|
||||
int skipped = 0;
|
||||
for (int i = 0; i < rows.size(); i++) {
|
||||
JdProductEntity row = rows.get(i);
|
||||
String key = protectedKey(row.getShopId(), row.getProductCode());
|
||||
if (key != null && protectedKeys.contains(key)) {
|
||||
skipped++;
|
||||
log.warn("[{}] 行被人工编辑锁/删除忽略保护,跳过落库 companyId={} shopId={} productCode={}",
|
||||
LOG_TAG, companyId, row.getShopId(), row.getProductCode());
|
||||
continue;
|
||||
}
|
||||
keptRows.add(row);
|
||||
keptImages.add(pendingImages.get(i));
|
||||
keptSkus.add(pendingSkus.get(i));
|
||||
}
|
||||
if (skipped > 0) {
|
||||
rows.clear();
|
||||
rows.addAll(keptRows);
|
||||
pendingImages.clear();
|
||||
pendingImages.addAll(keptImages);
|
||||
pendingSkus.clear();
|
||||
pendingSkus.addAll(keptSkus);
|
||||
log.info("[{}] 本批 {} 行命中保护集被跳过 companyId={}", LOG_TAG, skipped, companyId);
|
||||
}
|
||||
return skipped;
|
||||
}
|
||||
|
||||
/** 本公司保护键集:锁定的行(shop_id|product_code)∪ 删除忽略表命中三元组。 */
|
||||
private Set<String> loadProtectedKeys(Long companyId) {
|
||||
Set<String> keys = new java.util.LinkedHashSet<>();
|
||||
List<JdProductEntity> locked = jdProductMapper.selectList(new LambdaQueryWrapper<JdProductEntity>()
|
||||
.select(JdProductEntity::getShopId, JdProductEntity::getProductCode)
|
||||
.eq(JdProductEntity::getCompanyId, companyId)
|
||||
.eq(JdProductEntity::getManualLocked, 1));
|
||||
for (JdProductEntity row : locked) {
|
||||
String key = protectedKey(row.getShopId(), row.getProductCode());
|
||||
if (key != null) {
|
||||
keys.add(key);
|
||||
}
|
||||
}
|
||||
for (JdProductIgnoreEntity ignore : jdProductIgnoreMapper.selectList(
|
||||
new LambdaQueryWrapper<JdProductIgnoreEntity>()
|
||||
.select(JdProductIgnoreEntity::getShopId, JdProductIgnoreEntity::getProductCode)
|
||||
.eq(JdProductIgnoreEntity::getCompanyId, companyId))) {
|
||||
String key = protectedKey(ignore.getShopId(), ignore.getProductCode());
|
||||
if (key != null) {
|
||||
keys.add(key);
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/** 保护键:shop_id|product_code(两段都非空才算得出键;主表列 NOT NULL,容异常数据)。 */
|
||||
private static String protectedKey(String shopId, String productCode) {
|
||||
if (shopId == null || shopId.isBlank() || productCode == null || productCode.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return shopId.trim() + "|" + productCode.trim();
|
||||
}
|
||||
|
||||
// ---------- scope 台账:断点游标合并写(铁律 30 / B84-01) ----------
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
-- 迁移 V199:京东商品人工编辑保护(行级「编辑/删除」,2026-11-15 用户口径)
|
||||
-- =============================================================
|
||||
-- 1. 变更目的:后管京东数据库列表加行级「编辑/删除」。采集回传的 upsertBatch
|
||||
-- UPDATE 段是整字段覆盖、收尾覆盖清理(purgeSupersededRows)会整行删旧数据、
|
||||
-- 重爬会把删掉的行当新行插回来——不加保护件,人工编辑/删除会在下一次采集时
|
||||
-- 被京麦值盖回/复活。本迁移补两个保护件(Expand-and-Contract,只加不改):
|
||||
--
|
||||
-- - biz_jd_product.manual_locked:人工编辑锁。后管编辑保存置 1;采集链路
|
||||
-- (upsert / 图片与 SKU 登记 / 覆盖清理)对锁定的行全部跳过——人工值优先。
|
||||
-- 单向锁(本轮不做「解锁恢复采集覆盖」)。
|
||||
-- - biz_jd_product_ignore:删除忽略表。后管删除成功后写入被删行的
|
||||
-- (company_id, shop_id, product_code) 三元组,采集回传对命中行跳过落库,
|
||||
-- 防止删除后的商品在重爬时复活。键形状与主表 uk_company_shop_code 同三元组。
|
||||
--
|
||||
-- 2. 形状口径:
|
||||
-- - manual_locked TINYINT NOT NULL DEFAULT 0:存量行全部视为「未人工编辑」,
|
||||
-- 采集链路行为不变(默认 0=不跳过)。
|
||||
-- - ignore 表不设外键、不级联:主表行被覆盖清理级联删掉时忽略记录仍保留
|
||||
-- (忽略语义本就独立于行存在)。
|
||||
-- - created_at DEFAULT CURRENT_TIMESTAMP,与 V193 synced_at 同口径。
|
||||
--
|
||||
-- 3. 数据量:ignore 表随删除操作增长,量级=人工删除次数(远小于商品行数);
|
||||
-- uk 三元组同时是采集侧过滤的查询键(按 company_id + 三元组 IN 查)。
|
||||
--
|
||||
-- 4. 可重跑性:ADD COLUMN 用「先判存在再加」的幂等形状(V195 补列先例);
|
||||
-- CREATE TABLE IF NOT EXISTS——中途失败重跑整条 no-op。
|
||||
--
|
||||
-- 5. 验证 SQL(迁移后跑,不许只看启动日志的 Successfully applied):
|
||||
-- SHOW COLUMNS FROM biz_jd_product LIKE 'manual_locked'; -> 期望 tinyint(1) NOT NULL DEFAULT '0'
|
||||
-- SHOW CREATE TABLE biz_jd_product_ignore; -> 期望 uk_company_shop_code 唯一键
|
||||
-- 应用侧:MigrationInventoryTest 自动覆盖新迁移;JdProductUpdateServiceTest /
|
||||
-- JdResultIngestService 锁定跳过用例在 Phase 测试收口。
|
||||
--
|
||||
-- 6. 回滚(人工补丁,写成注释以免落在 Flyway 扫描路径里被当迁移执行):
|
||||
-- DROP TABLE biz_jd_product_ignore;
|
||||
-- ALTER TABLE biz_jd_product DROP COLUMN manual_locked;
|
||||
-- DELETE FROM flyway_schema_history WHERE version='199';
|
||||
-- =============================================================
|
||||
|
||||
-- 幂等补列(V195 先例:information_schema 先判存在)
|
||||
SET @col_exists = (SELECT COUNT(*) FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = 'biz_jd_product'
|
||||
AND COLUMN_NAME = 'manual_locked');
|
||||
SET @sql = IF(@col_exists = 0,
|
||||
'ALTER TABLE biz_jd_product ADD COLUMN manual_locked TINYINT NOT NULL DEFAULT 0 COMMENT ''人工编辑锁(后管编辑保存置 1;采集 upsert/登记/覆盖清理对锁定的行全部跳过)'' AFTER synced_at',
|
||||
'SELECT ''manual_locked already exists, skip''');
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS biz_jd_product_ignore (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
|
||||
`company_id` BIGINT NOT NULL COMMENT '物化归属分公司(与主表 uk_company_shop_code 同口径)',
|
||||
`shop_id` VARCHAR(128) NOT NULL COMMENT '京东店铺标识(与主表 shop_id 同形状)',
|
||||
`product_code` VARCHAR(64) NOT NULL COMMENT '京东商品编码',
|
||||
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '写入时刻(后管删除成功时)',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_company_shop_code` (`company_id`, `shop_id`, `product_code`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='京东商品删除忽略表(后管删除成功即登记,采集回传对命中行跳过落库,防重爬复活)';
|
||||
@@ -64,11 +64,14 @@
|
||||
不是本任务写、且所属任务已不在在飞(PENDING/RUNNING 不删——同店铺并发两单先收尾的
|
||||
单不得抹掉仍在跑的单的行,后收尾的单会在自己的收尾时清)的商品行。
|
||||
shop_id 取本任务落过的行的 DISTINCT 值(shop_id 缺省回落 shop_name,与 buildRow 同源),
|
||||
删除本体走 JdProductRowDeleteService#deleteRows(SKU/图片行级联),本查询只出 id。 -->
|
||||
删除本体走 JdProductRowDeleteService#deleteRows(SKU/图片行级联),本查询只出 id。
|
||||
V199:manual_locked=1 的行(后管人工编辑过)不进清理候选——人工值优先,重爬不得整行删掉。 -->
|
||||
<select id="selectSupersededProductIds" resultType="long">
|
||||
SELECT p.id
|
||||
FROM biz_jd_product p
|
||||
WHERE p.company_id = #{companyId}
|
||||
<!-- V199 覆盖清理候选必须排除 manual_locked=1 的行(V199 人工编辑锁) -->
|
||||
AND p.manual_locked = 0
|
||||
AND p.shop_id IN (
|
||||
SELECT DISTINCT shop_id FROM biz_jd_product
|
||||
WHERE company_id = #{companyId} AND task_id = #{taskId}
|
||||
@@ -81,14 +84,15 @@
|
||||
</select>
|
||||
|
||||
<!-- 后管只读页(Phase 5,v1.4)过滤面:公司(物化列 eq)、店铺名/商品编码/标题关键词
|
||||
(contains LIKE)、商品状态 eq、分组 eq、同步起止(datetime 闭区间)。
|
||||
(contains LIKE)、类目 eq、分组 eq、同步起止(datetime 闭区间)。
|
||||
sale_status 筛选随 2026-11-15 客户口径撤掉(前端筛选框下线;列本身与详情/回传链不动)。
|
||||
全部走 #{} 占位符;offset/limit 必须是服务侧钳制后的值(铁律 15),mapper 不做第二次换算。 -->
|
||||
<sql id="productConds">
|
||||
<if test="companyId != null and companyId > 0">AND p.company_id = #{companyId}</if>
|
||||
<if test="shopName != null and shopName != ''">AND p.shop_name LIKE CONCAT('%', #{shopName}, '%')</if>
|
||||
<if test="productCode != null and productCode != ''">AND p.product_code LIKE CONCAT('%', #{productCode}, '%')</if>
|
||||
<if test="titleKeyword != null and titleKeyword != ''">AND p.title LIKE CONCAT('%', #{titleKeyword}, '%')</if>
|
||||
<if test="saleStatus != null and saleStatus != ''">AND p.sale_status = #{saleStatus}</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="syncedFrom != null">AND p.synced_at >= #{syncedFrom}</if>
|
||||
<if test="syncedTo != null">AND p.synced_at <= #{syncedTo}</if>
|
||||
@@ -115,4 +119,16 @@
|
||||
ORDER BY p.synced_at DESC, p.id DESC
|
||||
LIMIT #{offset}, #{limit}
|
||||
</select>
|
||||
|
||||
<!-- 类目筛选下拉数据源(2026-11-15 用户口径「京麦的类目也要同步过来后台」):本公司已落库行
|
||||
的 DISTINCT category_path(=京麦末级类目名,契约 §2.3 E3)+ 行数。
|
||||
空类目行不进下拉;companyId 为空(超管不选公司=全量)时跨公司去重。 -->
|
||||
<select id="selectProductCategories" resultType="com.nanri.aiimage.modules.jd.model.vo.JdProductCategoryVo">
|
||||
SELECT p.category_path AS name, COUNT(*) AS productCount
|
||||
FROM biz_jd_product p
|
||||
WHERE p.category_path IS NOT NULL AND p.category_path != ''
|
||||
<if test="companyId != null">AND p.company_id = #{companyId}</if>
|
||||
GROUP BY p.category_path
|
||||
ORDER BY p.category_path
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
@@ -88,7 +88,10 @@ class MigratedSqlEquivalenceTest {
|
||||
// 109 → 111:v1.4 Phase 5(2026-11-14)后管只读页在 JdProductMapper 新写 2 条只读 select
|
||||
//(countProducts + selectProductPage)。注意本格是「全部 XML 的 select 总数」(不走
|
||||
// NOT_MIGRATED 豁免——豁免只作用于等价性那支),新写 select 也计入本分母。
|
||||
private static final int XML_SELECT_TOTAL = 111;
|
||||
// 111 → 112:2026-11-15 类目筛选下拉在 JdProductMapper 新写 1 条 select
|
||||
//(selectProductCategories,DISTINCT category_path +行数),从来没有注解原文可对撞,
|
||||
// 逐条点名进 NOT_MIGRATED。
|
||||
private static final int XML_SELECT_TOTAL = 113;
|
||||
|
||||
/**
|
||||
* 不是「从注解搬过来」的语句:注解时代没有原文可对撞,等价性这支只能跳过它们。
|
||||
@@ -195,7 +198,10 @@ class MigratedSqlEquivalenceTest {
|
||||
// 注解时代没有原文可对撞,逐条点名留痕
|
||||
//(SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 157 → 159)。
|
||||
"JdProductMapper#countProducts",
|
||||
"JdProductMapper#selectProductPage");
|
||||
"JdProductMapper#selectProductPage",
|
||||
// 2026-11-15 类目筛选下拉(京麦末级类目 distinct +行数)新写 1 条 select,
|
||||
// 同样没有注解原文可对撞,点名留痕(XML_SELECT_TOTAL 同轮 111 → 112)。
|
||||
"JdProductMapper#selectProductCategories");
|
||||
|
||||
/** 账本 5 列:接口 / 方法 / @Select 种类 / 是否含常量插值 / 注解原文(Java 反转义后那条串)。 */
|
||||
private static final int LEDGER_COLUMNS = 5;
|
||||
|
||||
@@ -118,7 +118,9 @@ class SqlLayeringConventionTest {
|
||||
// 159 → 160:v1.4 B102 SKU 矩阵在 JdProductSkuMapper 落 1 条写侧 insert
|
||||
//(upsertBatch,uk_product_sku(product_id,sku_id),ON DUPLICATE KEY UPDATE upsert-only——
|
||||
// 与 V198 迁移同窗、商品行 upsert 之后登记),全部新写,非迁移语句。
|
||||
private static final int XML_STATEMENT_TOTAL = 160;
|
||||
// 160 → 161:2026-11-15 类目筛选下拉在 JdProductMapper 落 1 条只读 select
|
||||
//(selectProductCategories,DISTINCT category_path +行数),全部新写,非迁移语句。
|
||||
private static final int XML_STATEMENT_TOTAL = 162;
|
||||
|
||||
/**
|
||||
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
|
||||
|
||||
@@ -78,7 +78,13 @@ class CatchSilenceRatchetTest {
|
||||
// 11-15 京东 B101/B102 收口抬 616 -> 619:三枚不重抛含 log(JdProductQueryService SKU 组合解析
|
||||
// 与 JdResultIngestService 单行 SKU 跳过两枚落 LOG_ONLY、JdProductSkuIngestService.writeJson
|
||||
// 序列化是 log+return),出处与误差方向逐处见 STRICT_TOTAL 上方 11-15 段;EMPTY 一格没涨。
|
||||
static final int GIVEN_NOT_RETHROW = 620;
|
||||
// 11-15 京东行级编辑+删除忽略(V199)抬 620 -> 624:三枚逐处记名——
|
||||
// JdProductUpdateService.rebuildAttrJson 读侧解析失败落 null(拒绝出口在块外)、
|
||||
// JdProductDeleteService.registerIgnoreRows 的逐条 DuplicateKeyException 幂等跳过(log.info)
|
||||
// 与外层登记失败 log.error 不推翻删除本体(log.error);另 1 枚来自同窗另一路会话的类目下拉
|
||||
// 在飞件(本笔 diff 只有上述 3 枚 NOT_RETHROW 形状,逐文件 HEAD 对比 JD 包净增 catch=5,
|
||||
// 实测读数 624 ⇒ 差的 1 枚不在本笔文件里,随该会话自己的收尾记名)。
|
||||
static final int GIVEN_NOT_RETHROW = 624;
|
||||
static final int GIVEN_LOG_ONLY = 251;
|
||||
static final int GIVEN_EMPTY = 56;
|
||||
|
||||
@@ -386,7 +392,17 @@ class CatchSilenceRatchetTest {
|
||||
// 历史行读不到店铺名时对旧建单载荷 requestJson 做 objectMapper.readValue 兜底(解析失败
|
||||
// log.warn + return null,历史行少店铺名不判死)。同批 JdCollectTaskService 的 scope 解析
|
||||
// 私有副本是**搬家**进 JdScopeStateReader(同形状 catch 一删一增对冲),不占本格。
|
||||
static final int STRICT_TOTAL = 1098;
|
||||
// 11-15 京东行级编辑+删除忽略(V199,2026-11-15 用户口径「每条数据可以编辑/删除」)抬 1098 -> 1104:
|
||||
// 净新增 6 枚——① JdProductUpdateService.rebuildAttrJson 的 attr_json 读侧解析失败先落 null
|
||||
// 再按形状不识别拒绝(块体仅赋值,NOT_RETHROW;拒绝出口在块外,不算吞);
|
||||
// ② JdProductUpdateService.writeTabs 的 attr_json 序列化失败整笔拒绝(RETHROW,可安全重试);
|
||||
// ③ JdProductUpdateService.deleteImages 的桶对象删除失败整笔拒绝(RETHROW,与删除链同纪律,
|
||||
// 异常原文走 UserMessages.with 不进界面);④⑤ JdProductDeleteService.registerIgnoreRows 的
|
||||
// 逐条 DuplicateKeyException 幂等跳过(log.info,LOG_ONLY;MP 无 insert ignore)+外层登记失败
|
||||
// log.error 不推翻删除本体(LOG_ONLY,误差方向=重爬可能复活需人工补登记,已在文案点名);
|
||||
// ⑥ JdProductDeleteService 删库行失败 log.error 后重抛(RETHROW,桶已删需人工核对,只进 total
|
||||
// 与 RETHROW 下限格)。NOT_RETHROW/LOG_ONLY 两格随之各 +3/+2(见各自常量上方 11-15 段)。
|
||||
static final int STRICT_TOTAL = 1104;
|
||||
/**
|
||||
* 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。
|
||||
* 这一格只能涨不能跌——它是"异常会往外走"的总量,把它钉成"只许减"就等于拦着别人修吞异常。
|
||||
@@ -447,7 +463,13 @@ class CatchSilenceRatchetTest {
|
||||
// 11-15 京东 B101/B102 收口上调 380 -> 382,出处见 STRICT_TOTAL 上方 11-15 段:新增 5 枚中
|
||||
// 2 枚是 RETHROW(删除链的"桶已删、库行回滚"重抛 + 对象删除失败整批拒绝)。
|
||||
// 这是下限不是上限:重抛枚数变多 = 又有两处异常被改成往外走,只许涨。
|
||||
static final int RETHROW_FLOOR = 382;
|
||||
// 11-15 再下调 382 -> 381,出处点名:2026-11-15 客户口径「导出也不用」把京东数据库导出整段下线
|
||||
// (控制器 GET /export + JdProductQueryService.export/writeExportWorkbook 一起删),
|
||||
// `JdProductQueryService.writeExportWorkbook` 的 `catch (Exception ex) → log.warn +
|
||||
// BusinessException「导出京东数据库失败,请稍后重试」`是 RETHROW 形状,随死代码一起离开树;
|
||||
// 同窗撤掉的 sale_status 筛选参数无 catch。不重抛/只 log/空体三格上限原样未动(绿即未涨),
|
||||
// 没有任何一处「异常会往外走」被改回吞掉——这是整段功能下线,不是改出口方向。
|
||||
static final int RETHROW_FLOOR = 381;
|
||||
/**
|
||||
* 10-17 抬 559 -> 561,逐处记名(两处都是新增的 fail-open 证据读写,不是把已有出口改回吞掉):
|
||||
* 1){@code TaskHeartbeatLivenessService.stamp()}:心跳证据写失败只 WARN——心跳接口不许因为
|
||||
@@ -529,7 +551,7 @@ class CatchSilenceRatchetTest {
|
||||
// 11-15 京东 B101/B102 收口:严格口径实测 616 -> 619(三枚不重抛含 log:两枚落 LOG_ONLY、
|
||||
// 一枚 log+return,出处见 STRICT_TOTAL 上方 11-15 段)。
|
||||
// 11-15 京东任务面板改版 619 -> 620:JdCollectBoardService 旧建单载荷解析兜底一枚(见 STRICT_TOTAL 上方)。
|
||||
static final int STRICT_NOT_RETHROW = 620;
|
||||
static final int STRICT_NOT_RETHROW = 624;
|
||||
/**
|
||||
* 10-17 抬 172 -> 173,出处就是上面点名过的 {@code TaskHeartbeatLivenessService.stamp()}:
|
||||
* 块体只有「取到 key 名 + log.warn」一句,写心跳证据失败时既不重试也不抛出——
|
||||
@@ -562,8 +584,13 @@ class CatchSilenceRatchetTest {
|
||||
* 块体只有一句带 {@code [task-evidence]} 锚点的 WARN(id+reason),坏行跳过、其余照删。
|
||||
* 同日 Phase 5.1 再抬 189 -> 190:登记行落库失败后的对象回收失败只 WARN(同上锚点),
|
||||
* 主错误仍由外层转业务错误;不吞主错误。
|
||||
* 11-15 抬 196 -> 198(V199 行级编辑+删除忽略,同窗另一路会话落码、本笔跑全量时补记):
|
||||
* {@code JdProductDeleteService.registerIgnoreRows} 的逐条 DuplicateKeyException 幂等跳过
|
||||
* (块内仅一句 log.info)+外层登记失败 log.error 不推翻删除本体(块内仅一句 log.error),
|
||||
* 出处即 STRICT_TOTAL 上方 11-15 V199 段的 ④⑤——该段写明「LOG_ONLY 随之 +2」但常量漏抬,
|
||||
* 两枚都是带锚点的 fail-open(MP 无 insert ignore),不是把已有出口改回吞掉。
|
||||
*/
|
||||
static final int STRICT_LOG_ONLY = 196; // 严格口径:块内每条顶层语句都是 log 调用才算(11-01 重试两处兜底 +2、11-10 v1.3 同步服务 syncFromResult +1、11-14 v1.4 建单 scope 行 DuplicateKeyException 复用先到行 +1,见 STRICT_TOTAL 上方)
|
||||
static final int STRICT_LOG_ONLY = 198; // 严格口径:块内每条顶层语句都是 log 调用才算(11-01 重试两处兜底 +2、11-10 v1.3 同步服务 syncFromResult +1、11-14 v1.4 建单 scope 行 DuplicateKeyException 复用先到行 +1,见 STRICT_TOTAL 上方)
|
||||
static final int STRICT_EMPTY = 56;
|
||||
|
||||
/**
|
||||
|
||||
+56
-73
@@ -12,19 +12,18 @@ import com.nanri.aiimage.modules.jd.model.dto.JdProductBatchDeleteRequest;
|
||||
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.entity.JdProductSkuEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductCategoryVo;
|
||||
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.jd.service.JdProductUpdateService;
|
||||
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
@@ -34,7 +33,6 @@ import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@@ -53,13 +51,14 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* v1.4 Phase 5:后管「京东数据库」只读接口(列表/详情/导出)与分组管理入口。
|
||||
* v1.4 Phase 5:后管「京东数据库」只读接口(列表/详情)与分组管理入口。
|
||||
*
|
||||
* <p>结构判据(照 {@code AdminAppearancePatentCleanItemsControllerTest} 双形态):
|
||||
* 端点只读、鉴权 seam、公司隔离正门、snake 参数带默认值、SQL 只落 XML;
|
||||
* 行为样本:mock HttpServletRequest 把 company 参数交给 readCompanyIdOrOwn 并装进 200 信封、
|
||||
* 分页钳制 offset/limit(page=Long.MAX_VALUE 不得负,铁律 15)、详情 attr_json
|
||||
* tabs→attrGroups 解析与非法形状兜底(AC-7)。</p>
|
||||
* tabs→attrGroups 解析与非法形状兜底(AC-7)。
|
||||
* 导出端点与 sale_status 筛选参数已随 2026-11-15 客户口径整段下线(反向钉住防回潜)。</p>
|
||||
*/
|
||||
class AdminJdProductControllerTest {
|
||||
|
||||
@@ -82,15 +81,17 @@ class AdminJdProductControllerTest {
|
||||
assertTrue(source.contains("/api/admin/jd-products"), "端点必须挂在 /api/admin/jd-products");
|
||||
assertTrue(source.contains("@GetMapping"), "查询端点必须落在 @GetMapping");
|
||||
assertTrue(source.contains("\"/{id}\""), "详情端点必须是 GET /{id}");
|
||||
assertTrue(source.contains("\"/export\""), "导出端点必须是 GET /export");
|
||||
// 导出端点已随 2026-11-15 客户口径「导出也不用」下线(前端导出发送口同窗撤)——反向钉住防回潜。
|
||||
assertFalse(source.contains("\"/export\""), "导出端点必须已删除,不得回潜");
|
||||
// 删除入口(用户口径「后台删除数据的时候也要把桶对应的文件删除」):DELETE /{id} 单行
|
||||
// + POST /batch-delete 批量,先删桶、全成功才删库行(JdProductDeleteService 判据另钉)。
|
||||
assertTrue(source.contains("@DeleteMapping"), "删除端点必须落在 @DeleteMapping");
|
||||
assertTrue(source.contains("\"/batch-delete\""), "批量删除端点必须是 POST /batch-delete");
|
||||
assertTrue(source.contains("@Valid"), "批量删除请求体必须过 @Valid(ids 非空)");
|
||||
// 不得有 PUT:商品行只能被采集链回传覆盖,界面改名改值都不存在。
|
||||
assertFalse(source.contains("@PutMapping"),
|
||||
"京东数据库页不得有 PUT(商品行由采集回传覆盖,分组改排序落 AdminJdProductGroupController)");
|
||||
// PUT 旧绊线(「不得有 PUT:商品行只能被采集链回传覆盖」)已随后台「编辑商品」特性放行
|
||||
// (AdminJdProductController 的 @PutMapping("/{id}",JdProductUpdateService 承接);商品行
|
||||
// 采集回传覆盖链不变,人工改名只走这条 PUT。分组改排序仍落 AdminJdProductGroupController。
|
||||
assertTrue(source.contains("@PutMapping"), "编辑商品端点必须落在 @PutMapping(\"/{id}\")");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -121,9 +122,11 @@ 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",
|
||||
"sale_status", "group_id", "synced_from", "synced_to", "company_id")) {
|
||||
"group_id", "synced_from", "synced_to", "company_id")) {
|
||||
assertTrue(source.contains(param), "查询参数必须 snake_case:" + param);
|
||||
}
|
||||
// 商品状态筛选框已随 2026-11-15 客户口径从前端下线 ⇒ 服务端同名参数一起撤(反向钉住防回潜)。
|
||||
assertFalse(source.contains("\"sale_status\""), "sale_status 筛选参数必须已删除,不得回潜");
|
||||
assertTrue(source.contains("PageDefaults"), "page/page_size 默认值必须取 PageDefaults"
|
||||
+ "(page_size 默认 20=PageDefaults.JD_PRODUCTS_PAGE_SIZE);"
|
||||
+ "page 钳制防 (page-1)*page_size 溢出的行为样本见查询服务钳制支(铁律 15)");
|
||||
@@ -172,7 +175,7 @@ class AdminJdProductControllerTest {
|
||||
PermissionMenuService permissionMenuService = mock(PermissionMenuService.class);
|
||||
DataReadScope dataReadScope = mock(DataReadScope.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, mock(JdProductDeleteService.class), authSupport,
|
||||
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class), authSupport,
|
||||
permissionMenuService, dataReadScope);
|
||||
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
@@ -198,13 +201,43 @@ class AdminJdProductControllerTest {
|
||||
assertEquals(pageVo, response.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 类目下拉端点走同一正门并回选项集() {
|
||||
JdProductQueryService queryService = mock(JdProductQueryService.class);
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
DataReadScope dataReadScope = mock(DataReadScope.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class), 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");
|
||||
// 非超管正门强制本公司:即使请求带别的 company_id 也按返回值收口(同一行为样本家族)。
|
||||
when(dataReadScope.readCompanyIdOrOwn(operator, 5L)).thenReturn(5L);
|
||||
JdProductCategoryVo row = new JdProductCategoryVo();
|
||||
row.setName("艺考形体服");
|
||||
row.setProductCount(85L);
|
||||
when(queryService.categories(5L)).thenReturn(List.of(row));
|
||||
|
||||
ApiResponse<List<JdProductCategoryVo>> response = controller.categories(5L, request);
|
||||
|
||||
verify(dataReadScope).readCompanyIdOrOwn(operator, 5L);
|
||||
verify(queryService).categories(5L);
|
||||
assertEquals(Boolean.TRUE, response.isSuccess(), "类目下拉与列表同一鉴权正门(200 信封)");
|
||||
assertEquals("艺考形体服", response.getData().get(0).getName(), "选项=已落库行的末级类目名");
|
||||
assertEquals(85L, response.getData().get(0).getProductCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 控制器详情走同一鉴权正门并回详情() {
|
||||
JdProductQueryService queryService = mock(JdProductQueryService.class);
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
DataReadScope dataReadScope = mock(DataReadScope.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, mock(JdProductDeleteService.class), authSupport,
|
||||
queryService, mock(JdProductDeleteService.class), mock(JdProductUpdateService.class), authSupport,
|
||||
mock(PermissionMenuService.class), dataReadScope);
|
||||
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
@@ -233,7 +266,8 @@ class AdminJdProductControllerTest {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
DataReadScope dataReadScope = mock(DataReadScope.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, deleteService, authSupport, mock(PermissionMenuService.class), dataReadScope);
|
||||
queryService, deleteService, mock(JdProductUpdateService.class), authSupport,
|
||||
mock(PermissionMenuService.class), dataReadScope);
|
||||
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(7L);
|
||||
@@ -262,7 +296,8 @@ class AdminJdProductControllerTest {
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
DataReadScope dataReadScope = mock(DataReadScope.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, deleteService, authSupport, mock(PermissionMenuService.class), dataReadScope);
|
||||
queryService, deleteService, mock(JdProductUpdateService.class), authSupport,
|
||||
mock(PermissionMenuService.class), dataReadScope);
|
||||
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(7L);
|
||||
@@ -280,37 +315,6 @@ class AdminJdProductControllerTest {
|
||||
assertTrue(ex.getMessage().contains("本次未删除任何数据"), "整批拒绝的语义必须到界面");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 控制器导出走同一正门并回Excel响应() {
|
||||
JdProductQueryService queryService = mock(JdProductQueryService.class);
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
DataReadScope dataReadScope = mock(DataReadScope.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, mock(JdProductDeleteService.class), 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(dataReadScope.readCompanyIdOrOwn(operator, 5L)).thenReturn(5L);
|
||||
byte[] payload = "xlsx-bytes".getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
when(queryService.export(5L, null, null, null, null, null, null, null)).thenReturn(payload);
|
||||
|
||||
ResponseEntity<byte[]> response = controller.export(null, null, null, null, null, null, null,
|
||||
5L, request);
|
||||
|
||||
verify(dataReadScope).readCompanyIdOrOwn(operator, 5L);
|
||||
verify(queryService).export(5L, null, null, null, null, null, null, null);
|
||||
assertEquals(MediaType.parseMediaType(
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
|
||||
response.getHeaders().getContentType(), "导出必须是 xlsx Content-Type");
|
||||
assertTrue(response.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION).contains("filename*=UTF-8''"),
|
||||
"中文文件名必须带 filename* UTF-8 轨(DownloadHeaderUtil 双轨口径)");
|
||||
assertArrayEquals(payload, response.getBody(), "响应体必须原样是服务侧产出的字节");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 查询服务分页钳制防溢出() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
@@ -350,11 +354,6 @@ class AdminJdProductControllerTest {
|
||||
() -> service.page(1L, 20L, null, null, null, null, null, null, from, to));
|
||||
assertTrue(ex.getMessage().contains("开始时间不能晚于结束时间"), "倒挂时间按业务异常拒绝");
|
||||
verify(mapper, never()).countProducts(any(), any(), any(), any(), any(), any(), any(), any());
|
||||
// 导出与分页共用同一过滤面校验(同一个拒绝时机:不碰 mapper)。
|
||||
BusinessException exportEx = assertThrows(BusinessException.class,
|
||||
() -> service.export(null, null, null, null, null, null, from, to));
|
||||
assertTrue(exportEx.getMessage().contains("开始时间不能晚于结束时间"),
|
||||
"导出端点必须同样拒绝倒挂时间,不得绕过分页页签的校验口径");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -366,13 +365,14 @@ class AdminJdProductControllerTest {
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(0L);
|
||||
|
||||
// 超管显式传 5 ⇒ 按公司 5 筛(收窄);groupId ≤0 按没选分组处理;文本两侧空白剥掉。
|
||||
service.page(1L, 20L, 5L, " 店铺甲 ", " SKU-1 ", " 连衣裙 ", "上架中", 0L, null, null);
|
||||
verify(mapper).countProducts(eq(5L), eq("店铺甲"), eq("SKU-1"), eq("连衣裙"), eq("上架中"),
|
||||
isNull(), isNull(), isNull());
|
||||
// 类目(京麦末级类目名)走同一过滤面:传值原样直传、空白归 null。
|
||||
service.page(1L, 20L, 5L, " 店铺甲 ", " SKU-1 ", " 连衣裙 ", " 艺考形体服 ", 0L, null, null);
|
||||
verify(mapper).countProducts(eq(5L), eq("店铺甲"), eq("SKU-1"), eq("连衣裙"),
|
||||
eq("艺考形体服"), isNull(), isNull(), isNull());
|
||||
// 全量(scopedCompanyId=null)⇒ companyId 不过滤。
|
||||
service.page(1L, 20L, null, null, null, null, null, null, null, null);
|
||||
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), isNull());
|
||||
verify(mapper).countProducts(isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), isNull(), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -518,23 +518,6 @@ class AdminJdProductControllerTest {
|
||||
assertThrows(BusinessException.class, () -> service.detail(0L, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 导出超上限按业务错误拒绝且不取数() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
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(JdProductQueryService.EXPORT_MAX_ROWS + 1);
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.export(null, null, null, null, null, null, null, null));
|
||||
assertTrue(ex.getMessage().contains("超过导出上限"),
|
||||
"超上限必须给「缩小筛选范围」的可读文案,不能让用户等一次注定 OOM 的全量查询");
|
||||
verify(mapper, never()).selectProductPage(any(), any(), any(), any(), any(), any(), any(),
|
||||
any(), anyLong(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 列表行分组名批量补齐且读不到不猜() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
|
||||
+4
-1
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductIgnoreMapper;
|
||||
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;
|
||||
@@ -50,6 +51,8 @@ class JdProductDeleteServiceTest {
|
||||
@Mock
|
||||
private JdProductImageMapper jdProductImageMapper;
|
||||
@Mock
|
||||
private JdProductIgnoreMapper jdProductIgnoreMapper;
|
||||
@Mock
|
||||
private JdProductRowDeleteService jdProductRowDeleteService;
|
||||
@Mock
|
||||
private OssStorageService ossStorageService;
|
||||
@@ -63,7 +66,7 @@ class JdProductDeleteServiceTest {
|
||||
TableInfoHelper.initTableInfo(assistant, JdProductEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, JdProductImageEntity.class);
|
||||
service = new JdProductDeleteService(jdProductMapper, jdProductImageMapper,
|
||||
jdProductRowDeleteService, ossStorageService);
|
||||
jdProductIgnoreMapper, jdProductRowDeleteService, ossStorageService);
|
||||
ReflectionTestUtils.setField(service, "bucket", "jd-products");
|
||||
ReflectionTestUtils.setField(service, "keyPrefix", "jd-products/");
|
||||
}
|
||||
|
||||
+53
-1
@@ -7,10 +7,12 @@ import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductIgnoreMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdCollectTaskCreateRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdIngestChunkRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductIgnoreEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdIngestChunkVo;
|
||||
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||
import com.nanri.aiimage.modules.task.mapper.TaskScopeStateMapper;
|
||||
@@ -84,6 +86,8 @@ class JdProductSyncServiceTest {
|
||||
private JdProductSkuIngestService jdProductSkuIngestService;
|
||||
@Mock
|
||||
private JdProductRowDeleteService jdProductRowDeleteService;
|
||||
@Mock
|
||||
private JdProductIgnoreMapper jdProductIgnoreMapper;
|
||||
@Spy
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@@ -92,8 +96,11 @@ class JdProductSyncServiceTest {
|
||||
|
||||
@BeforeAll
|
||||
static void initTableInfo() {
|
||||
// MP 条件构造器在纯单测里必须先初始化实体 TableInfo(CollectDataCredentialFatalTest 同款)
|
||||
// MP 条件构造器在纯单测里必须先初始化实体 TableInfo(CollectDataCredentialFatalTest 同款)。
|
||||
// V199 保护闸的 LambdaQueryWrapper 也吃这两个实体的 lambda cache。
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), FileTaskEntity.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), JdProductEntity.class);
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), JdProductIgnoreEntity.class);
|
||||
}
|
||||
|
||||
// ---------- 夹具 ----------
|
||||
@@ -177,6 +184,9 @@ class JdProductSyncServiceTest {
|
||||
lenient().when(adminUserMapper.selectById(USER_ID)).thenReturn(owner(COMPANY_ID));
|
||||
lenient().when(collectTaskService.readCreateRequest(any(FileTaskEntity.class))).thenReturn(createRequest());
|
||||
lenient().when(collectTaskService.findScopeRow(TASK_ID)).thenReturn(scopeRow());
|
||||
// V199 保护闸:默认无锁定行、无忽略三元组(保护用例各自覆盖自己的桩)。
|
||||
lenient().when(jdProductMapper.selectList(any())).thenReturn(List.of());
|
||||
lenient().when(jdProductIgnoreMapper.selectList(any())).thenReturn(List.of());
|
||||
}
|
||||
|
||||
// ---------- 判据 ----------
|
||||
@@ -392,6 +402,48 @@ class JdProductSyncServiceTest {
|
||||
.anySatisfy(json -> assertThat(json).contains("覆盖清理失败"));
|
||||
}
|
||||
|
||||
// ---------- V199 保护闸:人工编辑锁与删除忽略 ----------
|
||||
|
||||
@Test
|
||||
void 人工编辑锁与删除忽略的行跳过落库不登记子表() {
|
||||
stubHappyPath();
|
||||
// 保护集:锁定行 10036442942137(manual_locked=1,后管编辑过)+ 忽略三元组 10036442942138(后管删过)
|
||||
JdProductEntity locked = new JdProductEntity();
|
||||
locked.setShopId("shop-001");
|
||||
locked.setProductCode("10036442942137");
|
||||
when(jdProductMapper.selectList(any())).thenReturn(List.of(locked));
|
||||
JdProductIgnoreEntity ignored = new JdProductIgnoreEntity();
|
||||
ignored.setCompanyId(COMPANY_ID);
|
||||
ignored.setShopId("shop-001");
|
||||
ignored.setProductCode("10036442942138");
|
||||
when(jdProductIgnoreMapper.selectList(any())).thenReturn(List.of(ignored));
|
||||
|
||||
JdIngestChunkVo vo = service.submitChunk(TASK_ID,
|
||||
chunk(item("10036442942137", "艺考形体服女"), item("10036442942138", "艺考形体服男"),
|
||||
item("10036442942139", "艺考形体服新")), true);
|
||||
|
||||
// 命中保护集的两行连同其图片/SKU 登记一起跳过;只有第三行落库
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<JdProductEntity>> rowsCaptor = ArgumentCaptor.forClass(List.class);
|
||||
verify(jdProductMapper).upsertBatch(rowsCaptor.capture());
|
||||
assertThat(rowsCaptor.getValue()).extracting(JdProductEntity::getProductCode)
|
||||
.containsExactly("10036442942139");
|
||||
verify(jdProductImageIngestService, times(1)).registerTaskImages(
|
||||
any(), eq(COMPANY_ID), eq("10036442942139"), any());
|
||||
verify(jdProductSkuIngestService, times(1)).registerTaskSkus(
|
||||
any(), eq(COMPANY_ID), eq("10036442942139"), any());
|
||||
assertThat(vo.getUpserted()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 覆盖清理SQL必须排除人工编辑锁的行_绊线() throws Exception {
|
||||
// V199:人工编辑过的行不进覆盖清理候选——重爬不得整行删掉人工值。
|
||||
String xml = Files.readString(Path.of("src/main/resources/mapper/jd/JdProductMapper.xml"));
|
||||
String purge = xml.substring(xml.indexOf("selectSupersededProductIds"));
|
||||
assertThat(purge).contains("manual_locked = 0",
|
||||
"覆盖清理候选必须排除 manual_locked=1 的行(V199 人工编辑锁)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 覆盖清理SQL必须排除在飞任务行_绊线() throws Exception {
|
||||
// 同店铺并发两单:先收尾的单不得把仍在跑(PENDING/RUNNING)的单已落的行抹掉,
|
||||
|
||||
+305
@@ -0,0 +1,305 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
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.dto.JdProductUpdateRequest;
|
||||
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 org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 后管「编辑京东商品」编排判据(PUT /api/admin/jd-products/{id},2026-11-15 用户口径)。
|
||||
*
|
||||
* <p>钉六件事:①归属闸(越界 id 按不存在拒绝,零写);②主字段全量保存即置人工编辑锁
|
||||
* {@code manual_locked=1}(V199——没有这个锁,编辑会被下一次采集 upsert 覆盖回去);
|
||||
* ③分组归属校验(跨公司挂组拒绝);④属性重建只覆写 attr_json 的 {@code tabs} 段、
|
||||
* images/productCode 等其它顶层键原样保留(铁律 30 形状),原文形状不识别时拒绝而不是静默重写;
|
||||
* ⑤SKU 按 (productId, skuId) 匹配改价改库存,缺失 skuId 拒绝;⑥删图=先删桶、共用对象保留、
|
||||
* image_count 按表实数刷新。</p>
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class JdProductUpdateServiceTest {
|
||||
|
||||
private static final Long PRODUCT_ID = 42L;
|
||||
private static final Long COMPANY_ID = 5L;
|
||||
|
||||
@Mock
|
||||
private JdProductMapper jdProductMapper;
|
||||
@Mock
|
||||
private JdProductSkuMapper jdProductSkuMapper;
|
||||
@Mock
|
||||
private JdProductImageMapper jdProductImageMapper;
|
||||
@Mock
|
||||
private JdProductGroupMapper jdProductGroupMapper;
|
||||
@Mock
|
||||
private OssStorageService ossStorageService;
|
||||
|
||||
private JdProductUpdateService service;
|
||||
|
||||
@BeforeAll
|
||||
static void initTableInfo() {
|
||||
// MP 条件构造器在纯单测里必须先初始化实体 TableInfo(JdProductDeleteServiceTest 同款)。
|
||||
MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
||||
TableInfoHelper.initTableInfo(assistant, JdProductEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, JdProductSkuEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, JdProductImageEntity.class);
|
||||
TableInfoHelper.initTableInfo(assistant, JdProductGroupEntity.class);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new JdProductUpdateService(jdProductMapper, jdProductSkuMapper,
|
||||
jdProductImageMapper, jdProductGroupMapper, ossStorageService);
|
||||
ReflectionTestUtils.setField(service, "bucket", "jd-products");
|
||||
ReflectionTestUtils.setField(service, "keyPrefix", "jd-products/");
|
||||
}
|
||||
|
||||
// ---------- 夹具 ----------
|
||||
|
||||
private JdProductEntity productRow() {
|
||||
JdProductEntity row = new JdProductEntity();
|
||||
row.setId(PRODUCT_ID);
|
||||
row.setCompanyId(COMPANY_ID);
|
||||
row.setShopId("shop-001");
|
||||
row.setProductCode("1003668397262");
|
||||
row.setTitle("旧标题");
|
||||
row.setAttrJson("{\"tabs\":[{\"tab\":\"商品基本信息\",\"attrs\":[{\"key\":\"brand\",\"label\":\"品牌\",\"value\":\"花屿巷\"}]}],"
|
||||
+ "\"images\":[{\"type\":\"MAIN\",\"objectKey\":\"jd-products/5/aaa.jpg\"}],\"productCode\":\"1003668397262\"}");
|
||||
return row;
|
||||
}
|
||||
|
||||
private JdProductUpdateRequest mainFieldsOnly() {
|
||||
JdProductUpdateRequest body = new JdProductUpdateRequest();
|
||||
body.setTitle(" 新标题 ");
|
||||
body.setBrand(" 新品牌 ");
|
||||
body.setGroupId(null);
|
||||
return body;
|
||||
}
|
||||
|
||||
private void givenOwnedRow() {
|
||||
when(jdProductMapper.selectById(PRODUCT_ID)).thenReturn(productRow());
|
||||
}
|
||||
|
||||
/** 取 update 落下的 SET 键值对(paramNameValuePairs),供断言新值。 */
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> capturedSetValues() {
|
||||
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 归属闸_越界或不存在按不存在拒绝且零写() {
|
||||
JdProductEntity otherCompany = productRow();
|
||||
otherCompany.setCompanyId(99L);
|
||||
when(jdProductMapper.selectById(PRODUCT_ID)).thenReturn(otherCompany);
|
||||
|
||||
assertThatThrownBy(() -> service.update(PRODUCT_ID, mainFieldsOnly(), COMPANY_ID))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("商品不存在");
|
||||
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
|
||||
|
||||
assertThatThrownBy(() -> service.update(PRODUCT_ID, mainFieldsOnly(), COMPANY_ID))
|
||||
.isInstanceOf(BusinessException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 主字段全量保存即置人工编辑锁() {
|
||||
givenOwnedRow();
|
||||
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
|
||||
|
||||
Long id = service.update(PRODUCT_ID, mainFieldsOnly(), COMPANY_ID);
|
||||
|
||||
assertThat(id).isEqualTo(PRODUCT_ID);
|
||||
Map<String, Object> values = capturedSetValues();
|
||||
// 主字段全量提交:trim 落值(title/brand)
|
||||
assertThat(values.values()).contains("新标题", "新品牌");
|
||||
// SET 段必须带 manual_locked(V199 人工编辑锁本体)
|
||||
ArgumentCaptor<Wrapper<JdProductEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
|
||||
verify(jdProductMapper).update(isNull(), captor.capture());
|
||||
assertThat(((LambdaUpdateWrapper<JdProductEntity>) captor.getValue()).getSqlSet())
|
||||
.contains("manual_locked");
|
||||
// 主字段全量提交:trim 落值(title/brand)
|
||||
assertThat(values.values()).contains("新标题").contains("新品牌");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 分组必须属于本公司() {
|
||||
givenOwnedRow();
|
||||
JdProductUpdateRequest body = mainFieldsOnly();
|
||||
body.setGroupId(7L);
|
||||
JdProductGroupEntity group = new JdProductGroupEntity();
|
||||
group.setId(7L);
|
||||
group.setCompanyId(99L);
|
||||
when(jdProductGroupMapper.selectById(7L)).thenReturn(group);
|
||||
|
||||
assertThatThrownBy(() -> service.update(PRODUCT_ID, body, COMPANY_ID))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("分组不存在或不属于当前公司");
|
||||
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 属性重建只覆写tabs段其余顶层键保留() {
|
||||
givenOwnedRow();
|
||||
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
|
||||
JdProductUpdateRequest body = mainFieldsOnly();
|
||||
JdProductUpdateRequest.AttrGroupDto group = new JdProductUpdateRequest.AttrGroupDto();
|
||||
group.setGroupName("商品基本信息");
|
||||
JdProductUpdateRequest.AttrDto attr = new JdProductUpdateRequest.AttrDto();
|
||||
attr.setKey("brand");
|
||||
attr.setLabel("品牌");
|
||||
attr.setValue("新品牌值");
|
||||
group.setAttrs(List.of(attr));
|
||||
body.setAttrGroups(List.of(group));
|
||||
|
||||
service.update(PRODUCT_ID, body, COMPANY_ID);
|
||||
|
||||
Map<String, Object> values = capturedSetValues();
|
||||
// 新 JSON 里有重建后的 tabs(新属性值落进去了),且 images/productCode 原样保留(铁律 30)
|
||||
Object newJson = values.values().stream()
|
||||
.filter(v -> v instanceof String s && s.contains("\"tabs\""))
|
||||
.findFirst().orElseThrow();
|
||||
assertThat((String) newJson).contains("新品牌值").contains("\"images\"").contains("1003668397262");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 属性原文形状不识别时拒绝编辑防丢原文() {
|
||||
JdProductEntity row = productRow();
|
||||
row.setAttrJson("not-a-contract-shape");
|
||||
when(jdProductMapper.selectById(PRODUCT_ID)).thenReturn(row);
|
||||
JdProductUpdateRequest body = mainFieldsOnly();
|
||||
body.setAttrGroups(List.of(new JdProductUpdateRequest.AttrGroupDto()));
|
||||
|
||||
assertThatThrownBy(() -> service.update(PRODUCT_ID, body, COMPANY_ID))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("属性原文形状不识别");
|
||||
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void SKU按skuId匹配改价格库存_缺失拒绝() {
|
||||
givenOwnedRow();
|
||||
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
|
||||
JdProductSkuEntity sku = new JdProductSkuEntity();
|
||||
sku.setId(900L);
|
||||
sku.setProductId(PRODUCT_ID);
|
||||
sku.setSkuId("10238877506570");
|
||||
when(jdProductSkuMapper.selectList(any())).thenReturn(List.of(sku));
|
||||
|
||||
JdProductUpdateRequest body = mainFieldsOnly();
|
||||
JdProductUpdateRequest.SkuEditDto edit = new JdProductUpdateRequest.SkuEditDto();
|
||||
edit.setSkuId("10238877506570");
|
||||
edit.setPrice(new BigDecimal("199.00"));
|
||||
edit.setStock(88);
|
||||
body.setSkus(List.of(edit));
|
||||
service.update(PRODUCT_ID, body, COMPANY_ID);
|
||||
|
||||
ArgumentCaptor<Wrapper<JdProductSkuEntity>> captor = ArgumentCaptor.forClass(Wrapper.class);
|
||||
verify(jdProductSkuMapper).update(isNull(), captor.capture());
|
||||
Map<String, Object> values = (Map<String, Object>) ReflectionTestUtils.getField(
|
||||
(LambdaUpdateWrapper<JdProductSkuEntity>) captor.getValue(), "paramNameValuePairs");
|
||||
assertThat(values.values()).contains(new BigDecimal("199.00"), 88);
|
||||
|
||||
// 提交里有不存在的 skuId:整笔拒绝(行可能已被重新采集刷新,让用户刷新重试)
|
||||
JdProductUpdateRequest bad = mainFieldsOnly();
|
||||
JdProductUpdateRequest.SkuEditDto ghost = new JdProductUpdateRequest.SkuEditDto();
|
||||
ghost.setSkuId("ghost-sku");
|
||||
ghost.setPrice(BigDecimal.ONE);
|
||||
bad.setSkus(List.of(ghost));
|
||||
assertThatThrownBy(() -> service.update(PRODUCT_ID, bad, COMPANY_ID))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("SKU 行不存在");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 删图先删桶_共用对象保留_计数按表实数刷() {
|
||||
givenOwnedRow();
|
||||
when(jdProductMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
|
||||
JdProductImageEntity owned = new JdProductImageEntity();
|
||||
owned.setId(1L);
|
||||
owned.setProductId(PRODUCT_ID);
|
||||
owned.setObjectKey("jd-products/5/aaa.jpg");
|
||||
JdProductImageEntity missing = new JdProductImageEntity();
|
||||
missing.setId(2L);
|
||||
missing.setProductId(999L);
|
||||
when(jdProductImageMapper.selectList(any()))
|
||||
// ① 归属校验:两张里有一张不属于本商品 → 整笔拒绝(本次未保存任何修改)
|
||||
.thenReturn(List.of(owned))
|
||||
// ② 共用性回查:同 key 被另一商品引用 → 保留对象
|
||||
.thenReturn(List.of(new JdProductImageEntity()));
|
||||
|
||||
JdProductUpdateRequest body = mainFieldsOnly();
|
||||
body.setDeleteImageIds(List.of(1L, 2L));
|
||||
assertThatThrownBy(() -> service.update(PRODUCT_ID, body, COMPANY_ID))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("本次未删除任何数据");
|
||||
|
||||
// 修正归属:只删 1L;同 key 被别的商品引用 ⇒ 对象保留、只删登记行、计数刷新
|
||||
JdProductUpdateRequest fixed = mainFieldsOnly();
|
||||
fixed.setDeleteImageIds(List.of(1L));
|
||||
service.update(PRODUCT_ID, fixed, COMPANY_ID);
|
||||
|
||||
verify(ossStorageService, never()).removeObject(eq("jd-products"), eq("jd-products/5/aaa.jpg"));
|
||||
verify(jdProductImageMapper).delete(any(Wrapper.class));
|
||||
// update 共 3 次:两次 service.update() 的主字段各 1 次(第一次归属拒绝虽已过主字段
|
||||
// 写点,但 @Transactional 会整笔回滚,DB 层零持久化)+ 删图后的计数刷新 1 次
|
||||
verify(jdProductMapper, org.mockito.Mockito.times(3)).update(isNull(), any(Wrapper.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 价格区间倒挂与负库存可读拒绝() {
|
||||
givenOwnedRow();
|
||||
JdProductUpdateRequest badPrice = mainFieldsOnly();
|
||||
badPrice.setPriceMin(new BigDecimal("100"));
|
||||
badPrice.setPriceMax(new BigDecimal("50"));
|
||||
assertThatThrownBy(() -> service.update(PRODUCT_ID, badPrice, COMPANY_ID))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("价格区间下限不能大于上限");
|
||||
|
||||
JdProductUpdateRequest badStock = mainFieldsOnly();
|
||||
badStock.setStockTotal(-1);
|
||||
assertThatThrownBy(() -> service.update(PRODUCT_ID, badStock, COMPANY_ID))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("总库存不能为负数");
|
||||
verify(jdProductMapper, never()).update(isNull(), any(Wrapper.class));
|
||||
verify(jdProductSkuMapper, never()).update(isNull(), any(Wrapper.class));
|
||||
verify(jdProductSkuMapper, never()).selectList(any());
|
||||
verify(jdProductImageMapper, never()).delete(any(Wrapper.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user