feat(phase-5): 后管京东数据库接口与页面+用户端采集入口——v1.4 Phase 5
This commit is contained in:
@@ -56,6 +56,15 @@ public final class PageDefaults {
|
||||
/** {@link #ADMIN_LIST_PAGE_SIZE} 的数字镜像。 */
|
||||
public static final int ADMIN_LIST_PAGE_SIZE_INT = 20;
|
||||
|
||||
/**
|
||||
* 京东数据库后管只读页默认容量 = 20(v1.4 Phase 5,蓝图 §3.2「page_size 默认 20、上限 100」;
|
||||
* 上限钳制在 {@code JdProductQueryService},不在注解上)。
|
||||
*/
|
||||
public static final String JD_PRODUCTS_PAGE_SIZE = "20";
|
||||
|
||||
/** {@link #JD_PRODUCTS_PAGE_SIZE} 的数字镜像。 */
|
||||
public static final int JD_PRODUCTS_PAGE_SIZE_INT = 20;
|
||||
|
||||
/** 上架/采集类任务历史列表默认容量 = 50。现存 2 处 {@code page_size}(collect-data、publish)。 */
|
||||
public static final String TASK_LIST_PAGE_SIZE = "50";
|
||||
|
||||
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
package com.nanri.aiimage.modules.jd.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.api.PageDefaults;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
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.vo.JdProductDetailVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductPageVo;
|
||||
import com.nanri.aiimage.modules.jd.service.JdProductQueryService;
|
||||
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import 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.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 后管「京东数据库」只读页(v1.4 Phase 5)。数据由采集回传同步落表
|
||||
* ({@code JdResultIngestService},覆盖最新),本页只读——列表/详情/导出三个 GET 端点,
|
||||
* 不出表、不改商品行、无导入;分组管理走 {@code AdminJdProductGroupController}(同款鉴权闸)。
|
||||
*
|
||||
* <p>鉴权照外观专利只读页私有 {@code requireXxxAccess} 形状:JWT → 内部令牌回退 → 超管放行 →
|
||||
* 非超管菜单权限(column_key/route_path 双匹配)。菜单行由 V196 只插 columns 不预授权
|
||||
* ⇒ 默认仅 super_admin 可见可访问,其余角色由超管在权限页手工勾授后才放行。</p>
|
||||
*
|
||||
* <p>公司隔离正门是 {@code readCompanyIdOrOwn}(同外观专利只读页/店铺密钥的表形状:
|
||||
* 归属物化在行 company_id 列、可见集不靠账号/分组)——超管显式传才收窄、不传全量;
|
||||
* 非超管强制本公司(忽略请求传参)。{@code superAdminScopedCompanyId} 对非超管返回 null
|
||||
* =按行 company_id 列查询时把整张表交给每一家(该方法 javadoc 点名的越权形状),不走那道门。</p>
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@RequestMapping("/api/admin/jd-products")
|
||||
@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 AdminAuthSupport adminAuthSupport;
|
||||
private final PermissionMenuService permissionMenuService;
|
||||
private final DataReadScope dataReadScope;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "分页查询京东数据库商品", description = "按分公司隔离分页查询同步落表的京东商品;"
|
||||
+ "company_id 只对站点超管生效(显式传才收窄,不传全量),非超管强制本公司。")
|
||||
@ApiResponses({
|
||||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功", content = @Content(schema = @Schema(implementation = JdProductPageVo.class)))
|
||||
})
|
||||
public ApiResponse<JdProductPageVo> page(
|
||||
@Parameter(description = "页码") @RequestParam(name = "page", defaultValue = PageDefaults.FIRST_PAGE) Long page,
|
||||
@Parameter(description = "每页数量") @RequestParam(name = "page_size", defaultValue = PageDefaults.JD_PRODUCTS_PAGE_SIZE) Long pageSize,
|
||||
@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,
|
||||
@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.page(page, pageSize, scopedCompanyId, shopName,
|
||||
productCode, titleKeyword, saleStatus, groupId, syncedFrom, syncedTo));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@Operation(summary = "商品详情", description = "主字段全量 + attr_json 属性分组(契约形状不识别时落空分组"
|
||||
+ "并把原文放 attrJsonRaw 兜底,AC-7 未识别属性不丢弃)+ 已转存图片(100% 自有域名,AC-4)。"
|
||||
+ "公司隔离与列表同门:越界 id 按不存在拒绝。")
|
||||
public ApiResponse<JdProductDetailVo> detail(
|
||||
@Parameter(description = "商品行 id") @PathVariable("id") Long id,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireJdAccess(request);
|
||||
AdminUserEntity user = operator.user();
|
||||
// 详情同样过公司隔离正门:超管不传 company_id = 全量可见;非超管恒为本公司(服务按它收口)。
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, null);
|
||||
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,
|
||||
@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);
|
||||
}
|
||||
|
||||
private RequestOperator requireJdAccess(HttpServletRequest request) {
|
||||
AdminUserEntity operator = resolveOperator(request);
|
||||
if (operator == null || operator.getId() == null || operator.getId() <= 0) {
|
||||
throw new BusinessException(403, "无权访问京东数据库");
|
||||
}
|
||||
boolean superAdmin = DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(operator));
|
||||
if (!superAdmin && !hasJdPermission(operator)) {
|
||||
throw new BusinessException(403, "无权访问京东数据库");
|
||||
}
|
||||
return new RequestOperator(operator.getId(), superAdmin, operator);
|
||||
}
|
||||
|
||||
private AdminUserEntity resolveOperator(HttpServletRequest request) {
|
||||
try {
|
||||
return adminAuthSupport.requireUser(request);
|
||||
} catch (BusinessException authFailure) {
|
||||
AdminUserEntity internalOperator = resolveInternalOperator(request);
|
||||
if (internalOperator != null) {
|
||||
return internalOperator;
|
||||
}
|
||||
throw authFailure;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasJdPermission(AdminUserEntity operator) {
|
||||
return permissionMenuService.getUserColumnPermissions(operator.getId(), "admin")
|
||||
.stream()
|
||||
.anyMatch(item -> COLUMN_KEY.equals(item.getColumnKey())
|
||||
|| ROUTE_PATH.equals(item.getRoutePath()));
|
||||
}
|
||||
|
||||
private AdminUserEntity resolveInternalOperator(HttpServletRequest request) {
|
||||
if (!isTrustedInternalRequest(request.getHeader("X-Internal-Token"))) {
|
||||
return null;
|
||||
}
|
||||
String rawOperatorId = request.getParameter("operatorId");
|
||||
if (rawOperatorId == null || rawOperatorId.isBlank()) {
|
||||
rawOperatorId = request.getParameter("operator_id");
|
||||
}
|
||||
if (rawOperatorId == null || rawOperatorId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return permissionMenuService.requireUserOperator(Long.parseLong(rawOperatorId.trim()));
|
||||
} catch (NumberFormatException | BusinessException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private record RequestOperator(Long id, boolean superAdmin, AdminUserEntity user) {
|
||||
}
|
||||
|
||||
private boolean isTrustedInternalRequest(String suppliedToken) {
|
||||
return adminAuthSupport.isTrustedSuppliedToken(suppliedToken);
|
||||
}
|
||||
}
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
package com.nanri.aiimage.modules.jd.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
import com.nanri.aiimage.common.service.DataReadScope;
|
||||
import com.nanri.aiimage.modules.jd.model.dto.JdProductGroupSaveRequest;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductGroupVo;
|
||||
import com.nanri.aiimage.modules.jd.service.JdProductGroupService;
|
||||
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.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.util.List;
|
||||
|
||||
/**
|
||||
* 后管「京东数据库」分组管理(v1.4 Phase 5,R4):新建 / 改名 / 删除 / 下拉列表。
|
||||
*
|
||||
* <p>京东数据库专属分组(不是店铺管理的分组集):按公司隔离(唯一键
|
||||
* {@code uk_company_group_name});删除分组不删商品——商品行 {@code group_id} 置 NULL
|
||||
* (AC-8,事务两步落 {@code JdProductGroupService})。</p>
|
||||
*
|
||||
* <p>鉴权与 {@code AdminJdProductController} 同款私有闸(同一 column_key/route_path):
|
||||
* 普通授权用户可读写分组(AC-9),默认仅 super_admin;公司隔离正门同样是
|
||||
* {@code readCompanyIdOrOwn}——超管显式传才收窄,非超管强制本公司。</p>
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@RequestMapping("/api/admin/jd-product-groups")
|
||||
@Tag(name = "京东数据库分组", description = "京东商品自定义分组管理:按公司隔离的列表/新建/改名/删除,删除不删商品(脱组)。")
|
||||
public class AdminJdProductGroupController {
|
||||
|
||||
private static final String COLUMN_KEY = "admin_jd_database";
|
||||
private static final String ROUTE_PATH = "jd-center/database";
|
||||
|
||||
private final JdProductGroupService jdProductGroupService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final PermissionMenuService permissionMenuService;
|
||||
private final DataReadScope dataReadScope;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "分组列表(含组内商品数)", description = "按公司隔离返回全部分组,按 sort_order, id 升序;"
|
||||
+ "company_id 只对站点超管生效(显式传才收窄,不传全量),非超管强制本公司。"
|
||||
+ "productCount 为读侧现算列(分组下拉展示用)。")
|
||||
public ApiResponse<List<JdProductGroupVo>> list(
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireJdAccess(request);
|
||||
AdminUserEntity user = operator.user();
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, companyId);
|
||||
return ApiResponse.success(jdProductGroupService.list(scopedCompanyId));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@Operation(summary = "新建分组", description = "同公司重名按业务错误拒绝(409),不落库;"
|
||||
+ "归属公司走公司隔离正门(站点超管必须显式传 company_id)。")
|
||||
public ApiResponse<Long> create(
|
||||
@Valid @RequestBody JdProductGroupSaveRequest body,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireJdAccess(request);
|
||||
AdminUserEntity user = operator.user();
|
||||
// 超管不传 company_id 时 scopedCompanyId 为空 ⇒ 服务侧拒绝(分组必须归属一家公司)。
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, companyId);
|
||||
Long id = jdProductGroupService.create(scopedCompanyId, body.getName(), body.getSortOrder());
|
||||
log.info("[jd-product-group] 新建分组 operatorId={} companyId={} groupId={}",
|
||||
operator.id(), scopedCompanyId, id);
|
||||
return ApiResponse.success(id);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@Operation(summary = "分组改名/改排序", description = "越界 id(不属于本公司)按不存在拒绝;"
|
||||
+ "改成本公司已有名字按业务错误拒绝(409)。")
|
||||
public ApiResponse<Boolean> rename(
|
||||
@Parameter(description = "分组 id") @PathVariable("id") Long id,
|
||||
@Valid @RequestBody JdProductGroupSaveRequest body,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireJdAccess(request);
|
||||
AdminUserEntity user = operator.user();
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, null);
|
||||
jdProductGroupService.rename(id, scopedCompanyId, body.getName(), body.getSortOrder());
|
||||
log.info("[jd-product-group] 分组改名 operatorId={} groupId={}", operator.id(), id);
|
||||
return ApiResponse.success(Boolean.TRUE);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@Operation(summary = "删除分组", description = "删除分组不删商品(AC-8):先同事务把该组商品 "
|
||||
+ "group_id 置 NULL 再删分组行;返回实际脱组的商品行数。越界 id 按不存在拒绝。")
|
||||
public ApiResponse<Integer> delete(
|
||||
@Parameter(description = "分组 id") @PathVariable("id") Long id,
|
||||
HttpServletRequest request) {
|
||||
RequestOperator operator = requireJdAccess(request);
|
||||
AdminUserEntity user = operator.user();
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(user, null);
|
||||
int detached = jdProductGroupService.delete(id, scopedCompanyId);
|
||||
// 管理端破坏性动作必须留痕:谁、删了哪个分组、脱组了多少商品。
|
||||
log.info("[jd-product-group] 删除分组 operatorId={} groupId={} detached={}",
|
||||
operator.id(), id, detached);
|
||||
return ApiResponse.success(detached);
|
||||
}
|
||||
|
||||
private RequestOperator requireJdAccess(HttpServletRequest request) {
|
||||
AdminUserEntity operator = resolveOperator(request);
|
||||
if (operator == null || operator.getId() == null || operator.getId() <= 0) {
|
||||
throw new BusinessException(403, "无权访问京东数据库");
|
||||
}
|
||||
boolean superAdmin = DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(operator));
|
||||
if (!superAdmin && !hasJdPermission(operator)) {
|
||||
throw new BusinessException(403, "无权访问京东数据库");
|
||||
}
|
||||
return new RequestOperator(operator.getId(), superAdmin, operator);
|
||||
}
|
||||
|
||||
private AdminUserEntity resolveOperator(HttpServletRequest request) {
|
||||
try {
|
||||
return adminAuthSupport.requireUser(request);
|
||||
} catch (BusinessException authFailure) {
|
||||
AdminUserEntity internalOperator = resolveInternalOperator(request);
|
||||
if (internalOperator != null) {
|
||||
return internalOperator;
|
||||
}
|
||||
throw authFailure;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasJdPermission(AdminUserEntity operator) {
|
||||
return permissionMenuService.getUserColumnPermissions(operator.getId(), "admin")
|
||||
.stream()
|
||||
.anyMatch(item -> COLUMN_KEY.equals(item.getColumnKey())
|
||||
|| ROUTE_PATH.equals(item.getRoutePath()));
|
||||
}
|
||||
|
||||
private AdminUserEntity resolveInternalOperator(HttpServletRequest request) {
|
||||
if (!isTrustedInternalRequest(request.getHeader("X-Internal-Token"))) {
|
||||
return null;
|
||||
}
|
||||
String rawOperatorId = request.getParameter("operatorId");
|
||||
if (rawOperatorId == null || rawOperatorId.isBlank()) {
|
||||
rawOperatorId = request.getParameter("operator_id");
|
||||
}
|
||||
if (rawOperatorId == null || rawOperatorId.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return permissionMenuService.requireUserOperator(Long.parseLong(rawOperatorId.trim()));
|
||||
} catch (NumberFormatException | BusinessException ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private record RequestOperator(Long id, boolean superAdmin, AdminUserEntity user) {
|
||||
}
|
||||
|
||||
private boolean isTrustedInternalRequest(String suppliedToken) {
|
||||
return adminAuthSupport.isTrustedSuppliedToken(suppliedToken);
|
||||
}
|
||||
}
|
||||
+33
-1
@@ -5,6 +5,7 @@ import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -28,5 +29,36 @@ public interface JdProductMapper extends BaseMapper<JdProductEntity> {
|
||||
* AppearancePatentCleanItemMapper#upsertBatch 同形)。唯一键四元组自身
|
||||
* (company_id/shop_id/product_code)不出现在 UPDATE 段——不随更新漂移。</p>
|
||||
*/
|
||||
int upsertBatch(@Param("rows") List<JdProductEntity> rows);
|
||||
int upsertBatch(@Param("rows") List<JdProductEntity> rows);
|
||||
|
||||
/**
|
||||
* 后管只读页总数(Phase 5,v1.4):与 {@link #selectProductPage} 同一过滤面
|
||||
* (公司/店铺名/商品编码/标题关键词/商品状态/分组/同步起止,全部 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("groupId") Long groupId,
|
||||
@Param("syncedFrom") LocalDateTime syncedFrom,
|
||||
@Param("syncedTo") LocalDateTime syncedTo);
|
||||
|
||||
/**
|
||||
* 后管只读页分页查询(synced_at 倒序)。
|
||||
*
|
||||
* <p>{@code offset}/{@code limit} 必须是服务侧钳制后的值——page 钳制防
|
||||
* {@code (page-1)*page_size} 溢出(铁律 15),mapper 不做第二次换算,避免两处各算各的。</p>
|
||||
*/
|
||||
List<JdProductEntity> selectProductPage(@Param("companyId") Long companyId,
|
||||
@Param("shopName") String shopName,
|
||||
@Param("productCode") String productCode,
|
||||
@Param("titleKeyword") String titleKeyword,
|
||||
@Param("saleStatus") String saleStatus,
|
||||
@Param("groupId") Long groupId,
|
||||
@Param("syncedFrom") LocalDateTime syncedFrom,
|
||||
@Param("syncedTo") LocalDateTime syncedTo,
|
||||
@Param("offset") long offset,
|
||||
@Param("limit") int limit);
|
||||
}
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.nanri.aiimage.modules.jd.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 京东商品分组新建/改名请求体(v1.4 Phase 5)。归属公司不走 body:由控制器经
|
||||
* {@code DataReadScope#readCompanyIdOrOwn} 的公司隔离正门算出,客户端伪造无效。
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东商品分组保存请求")
|
||||
public class JdProductGroupSaveRequest {
|
||||
|
||||
@Schema(description = "分组名(同公司内唯一)")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "下拉展示顺序(新建缺省 0;改名不传保持原值)")
|
||||
private Integer sortOrder;
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东数据库商品详情(后管只读页抽屉,v1.4 Phase 5)。
|
||||
*
|
||||
* <p>主字段从 {@code biz_jd_product} 全量搬运;属性区来自 {@code attr_json} 的
|
||||
* {@code tabs} 数组(客户端实拍契约:{@code {"tabs":[{"tab":"...","attrs":[{key,label,value}]}],
|
||||
* "images":[...],"productCode":"..."}})——解析<b>必须健壮</b>:attr_json 为空/非法/非该形状
|
||||
* 时不抛 500,落空 {@code attrGroups} 并把原文放进 {@link #attrJsonRaw} 兜底展示
|
||||
* (AC-7 未识别属性不丢弃)。图片区来自 {@code biz_jd_product_image}(100% 自有域名,AC-4)。</p>
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东数据库商品详情")
|
||||
public class JdProductDetailVo {
|
||||
|
||||
@Schema(description = "商品行 id")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "物化归属分公司")
|
||||
private Long companyId;
|
||||
|
||||
@Schema(description = "来源任务 id(biz_file_task.id)")
|
||||
private Long taskId;
|
||||
|
||||
@Schema(description = "京东店铺标识")
|
||||
private String shopId;
|
||||
|
||||
@Schema(description = "京麦店铺名")
|
||||
private String shopName;
|
||||
|
||||
@Schema(description = "京东商品编码")
|
||||
private String productCode;
|
||||
|
||||
@Schema(description = "京东 SKU/货号")
|
||||
private String skuId;
|
||||
|
||||
@Schema(description = "商品标题")
|
||||
private String title;
|
||||
|
||||
@Schema(description = "品牌")
|
||||
private String brand;
|
||||
|
||||
@Schema(description = "品牌 id")
|
||||
private String brandId;
|
||||
|
||||
@Schema(description = "类目路径(京麦原始形状)")
|
||||
private String categoryPath;
|
||||
|
||||
@Schema(description = "价格区间下限")
|
||||
private BigDecimal priceMin;
|
||||
|
||||
@Schema(description = "价格区间上限")
|
||||
private BigDecimal priceMax;
|
||||
|
||||
@Schema(description = "库存合计")
|
||||
private Integer stockTotal;
|
||||
|
||||
@Schema(description = "商品状态原值")
|
||||
private String saleStatus;
|
||||
|
||||
@Schema(description = "采集来源 Tab(ONSALE/OFFSALE)")
|
||||
private String listTab;
|
||||
|
||||
@Schema(description = "京东侧创建时间")
|
||||
private LocalDateTime jdCreatedAt;
|
||||
|
||||
@Schema(description = "京东侧修改时间")
|
||||
private LocalDateTime jdUpdatedAt;
|
||||
|
||||
@Schema(description = "自有桶主图 URL")
|
||||
private String mainImageUrl;
|
||||
|
||||
@Schema(description = "京东详情页 URL(仅作溯源)")
|
||||
private String detailUrl;
|
||||
|
||||
@Schema(description = "属性键数量(质量核查分母)")
|
||||
private Integer attrCount;
|
||||
|
||||
@Schema(description = "已转存图片数")
|
||||
private Integer imageCount;
|
||||
|
||||
@Schema(description = "自定义分组 id(未挂组为空)")
|
||||
private Long groupId;
|
||||
|
||||
@Schema(description = "本次采集批次号")
|
||||
private String collectBatchId;
|
||||
|
||||
@Schema(description = "同步时刻")
|
||||
private LocalDateTime syncedAt;
|
||||
|
||||
@Schema(description = "属性分组(attr_json 的 tabs 数组映射;形状不识别时为空列表)")
|
||||
private List<AttrGroupVo> attrGroups;
|
||||
|
||||
@Schema(description = "attr_json 原文兜底(仅当解析失败/形状不识别时下发,AC-7 未识别属性不丢弃)")
|
||||
private String attrJsonRaw;
|
||||
|
||||
@Schema(description = "已转存图片(自有域名 URL)")
|
||||
private List<ImageViewVo> images;
|
||||
|
||||
/** attr_json 的一个 Tab:{@code {"tab":"商品基本信息","attrs":[...]}}。 */
|
||||
@Data
|
||||
@Schema(description = "属性分组(一个 Tab 一组)")
|
||||
public static class AttrGroupVo {
|
||||
|
||||
@Schema(description = "分组名(京东侧 Tab 名)")
|
||||
private String groupName;
|
||||
|
||||
@Schema(description = "属性键值对")
|
||||
private List<AttrVo> attrs;
|
||||
}
|
||||
|
||||
/** attr_json 的一个属性三元组:{@code {"key":"...","label":"...","value":...}}。 */
|
||||
@Data
|
||||
@Schema(description = "属性键值对")
|
||||
public static class AttrVo {
|
||||
|
||||
@Schema(description = "属性键(京东原始 key)")
|
||||
private String key;
|
||||
|
||||
@Schema(description = "属性展示名(京东原始 label)")
|
||||
private String label;
|
||||
|
||||
@Schema(description = "属性值(任意 JSON 值,读侧转字符串)")
|
||||
private String value;
|
||||
}
|
||||
|
||||
/** 一张已转存图片(biz_jd_product_image 行的读侧投影)。 */
|
||||
@Data
|
||||
@Schema(description = "商品图片")
|
||||
public static class ImageViewVo {
|
||||
|
||||
@Schema(description = "图片档:MAIN/ATTR/DETAIL")
|
||||
private String imageType;
|
||||
|
||||
@Schema(description = "销售属性名(ATTR 档专用)")
|
||||
private String attrName;
|
||||
|
||||
@Schema(description = "同档内排序")
|
||||
private Integer sortNo;
|
||||
|
||||
@Schema(description = "自有域名回读 URL")
|
||||
private String url;
|
||||
|
||||
@Schema(description = "对象字节数")
|
||||
private Long byteSize;
|
||||
|
||||
@Schema(description = "图片宽(像素,可空)")
|
||||
private Integer width;
|
||||
|
||||
@Schema(description = "图片高(像素,可空)")
|
||||
private Integer height;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 京东商品自定义分组(分组下拉/管理面,v1.4 Phase 5)。
|
||||
*
|
||||
* <p>{@code productCount} 是读侧现算列({@code JdProductMapper} BaseMapper selectCount
|
||||
* wrapper 按 {@code group_id} 统计,分组下拉展示用)。</p>
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东商品自定义分组")
|
||||
public class JdProductGroupVo {
|
||||
|
||||
@Schema(description = "分组 id")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "分组名(同公司内唯一)")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "下拉展示顺序")
|
||||
private Integer sortOrder;
|
||||
|
||||
@Schema(description = "组内商品数(读侧现算)")
|
||||
private Long productCount;
|
||||
|
||||
@Schema(description = "创建时刻")
|
||||
private LocalDateTime createdAt;
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 京东数据库列表行(后管只读页,v1.4 Phase 5)。
|
||||
*
|
||||
* <p>行值从 {@code biz_jd_product} 原样搬运(读侧不加工);{@code groupName} 是唯一的
|
||||
* 读侧加工列(按 {@code group_id} 批量取分组名补齐,分组已删/未挂组为 null)。</p>
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东数据库列表行")
|
||||
public class JdProductListItemVo {
|
||||
|
||||
@Schema(description = "商品行 id")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "京麦店铺名")
|
||||
private String shopName;
|
||||
|
||||
@Schema(description = "京东店铺标识")
|
||||
private String shopId;
|
||||
|
||||
@Schema(description = "京东商品编码")
|
||||
private String productCode;
|
||||
|
||||
@Schema(description = "京东 SKU/货号")
|
||||
private String skuId;
|
||||
|
||||
@Schema(description = "商品标题")
|
||||
private String title;
|
||||
|
||||
@Schema(description = "品牌")
|
||||
private String brand;
|
||||
|
||||
@Schema(description = "类目路径(京麦原始形状,A > B > C)")
|
||||
private String categoryPath;
|
||||
|
||||
@Schema(description = "价格区间下限")
|
||||
private BigDecimal priceMin;
|
||||
|
||||
@Schema(description = "价格区间上限")
|
||||
private BigDecimal priceMax;
|
||||
|
||||
@Schema(description = "库存合计")
|
||||
private Integer stockTotal;
|
||||
|
||||
@Schema(description = "商品状态原值(上架中/已下架/审核中/草稿)")
|
||||
private String saleStatus;
|
||||
|
||||
@Schema(description = "采集来源 Tab(ONSALE/OFFSALE)")
|
||||
private String listTab;
|
||||
|
||||
@Schema(description = "京东侧修改时间")
|
||||
private LocalDateTime jdUpdatedAt;
|
||||
|
||||
@Schema(description = "自有桶主图 URL(京东原图域名不落库,AC-4)")
|
||||
private String mainImageUrl;
|
||||
|
||||
@Schema(description = "属性键数量(质量核查分母)")
|
||||
private Integer attrCount;
|
||||
|
||||
@Schema(description = "已转存图片数")
|
||||
private Integer imageCount;
|
||||
|
||||
@Schema(description = "自定义分组 id(未挂组为空)")
|
||||
private Long groupId;
|
||||
|
||||
@Schema(description = "分组名(读侧批量补齐;未挂组/分组已删为空)")
|
||||
private String groupName;
|
||||
|
||||
@Schema(description = "同步时刻")
|
||||
private LocalDateTime syncedAt;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.nanri.aiimage.modules.jd.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东数据库分页结果(后管只读页,v1.4 Phase 5;照 {@code AppearancePatentCleanItemPageVo}
|
||||
* 手拼形状——本表量级先走 OFFSET 分页,不做 keyset 游标)。
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "京东数据库分页结果")
|
||||
public class JdProductPageVo {
|
||||
|
||||
@Schema(description = "列表")
|
||||
private List<JdProductListItemVo> items;
|
||||
|
||||
@Schema(description = "总数")
|
||||
private Long total;
|
||||
|
||||
@Schema(description = "页码")
|
||||
private Long page;
|
||||
|
||||
@Schema(description = "每页数量")
|
||||
private Long pageSize;
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
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.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper;
|
||||
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.JdProductGroupEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductGroupVo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 京东商品自定义分组业务(v1.4 Phase 5,R4):下拉列表 / 新建 / 改名 / 删除。
|
||||
*
|
||||
* <p>分组表是简单单表,CRUD 全走 BaseMapper + wrapper(SqlLayering 惯例,无 XML)。
|
||||
* 业务规则:</p>
|
||||
* <ul>
|
||||
* <li><b>按公司隔离</b>:所有读写都带 {@code companyId} 面;越界 id(行不存在或
|
||||
* {@code company_id} 与传入公司不符)一律按「分组不存在」拒绝,不泄露存在性。</li>
|
||||
* <li><b>同公司重名拒绝</b>(唯一键 {@code uk_company_group_name}):先查后插给可读的
|
||||
* 409 业务错误,不拿 DB 异常撞 500;改名时排除自身再查。</li>
|
||||
* <li><b>删除两步一事务</b>(AC-8):先把该组商品 {@code biz_jd_product.group_id} 置 NULL
|
||||
* (商品不删,脱组即成「未分组」),再删分组行——同一事务,失败一起回滚。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>{@code companyId} 由控制器经 {@code DataReadScope#readCompanyIdOrOwn} 算好传入:
|
||||
* {@code null} = 超管没选公司(rename/delete 放行、create 没有「没选公司」语义——
|
||||
* 分组必须归属一家公司,直接拒绝),正数=该公司(非超管恒为自己的公司)。</p>
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class JdProductGroupService {
|
||||
|
||||
private final JdProductGroupMapper jdProductGroupMapper;
|
||||
private final JdProductMapper jdProductMapper;
|
||||
|
||||
/**
|
||||
* 分组全量(按 {@code sort_order, id} 升序,下拉展示顺序);
|
||||
* {@code productCount} 用 {@code JdProductMapper} BaseMapper selectCount wrapper
|
||||
* 按 {@code group_id} 统计(分组下拉展示用)。
|
||||
*/
|
||||
public List<JdProductGroupVo> list(Long scopedCompanyId) {
|
||||
LambdaQueryWrapper<JdProductGroupEntity> query = new LambdaQueryWrapper<JdProductGroupEntity>()
|
||||
.eq(scopedCompanyId != null && scopedCompanyId > 0,
|
||||
JdProductGroupEntity::getCompanyId, scopedCompanyId)
|
||||
.orderByAsc(JdProductGroupEntity::getSortOrder)
|
||||
.orderByAsc(JdProductGroupEntity::getId);
|
||||
List<JdProductGroupVo> items = new ArrayList<>();
|
||||
for (JdProductGroupEntity group : jdProductGroupMapper.selectList(query)) {
|
||||
JdProductGroupVo vo = new JdProductGroupVo();
|
||||
vo.setId(group.getId());
|
||||
vo.setName(group.getName());
|
||||
vo.setSortOrder(group.getSortOrder());
|
||||
vo.setCreatedAt(group.getCreatedAt());
|
||||
vo.setProductCount(jdProductMapper.selectCount(
|
||||
new LambdaQueryWrapper<JdProductEntity>().eq(JdProductEntity::getGroupId, group.getId())));
|
||||
items.add(vo);
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/** 新建分组:同公司重名(trim 后)按 409 业务错误拒绝,不落库。 */
|
||||
public Long create(Long scopedCompanyId, String name, Integer sortOrder) {
|
||||
if (scopedCompanyId == null || scopedCompanyId <= 0) {
|
||||
throw new BusinessException(400, "创建分组必须指定公司");
|
||||
}
|
||||
String safeName = requireName(name);
|
||||
requireNameFree(scopedCompanyId, safeName, null);
|
||||
JdProductGroupEntity group = new JdProductGroupEntity();
|
||||
group.setCompanyId(scopedCompanyId);
|
||||
group.setName(safeName);
|
||||
group.setSortOrder(sortOrder == null ? 0 : sortOrder);
|
||||
jdProductGroupMapper.insert(group);
|
||||
return group.getId();
|
||||
}
|
||||
|
||||
/** 改名/改排序:越界 id 拒绝,重名(排除自身)拒绝。 */
|
||||
public void rename(Long id, Long scopedCompanyId, String name, Integer sortOrder) {
|
||||
String safeName = requireName(name);
|
||||
JdProductGroupEntity group = requireOwnedGroup(id, scopedCompanyId);
|
||||
requireNameFree(group.getCompanyId(), safeName, group.getId());
|
||||
group.setName(safeName);
|
||||
if (sortOrder != null) {
|
||||
group.setSortOrder(sortOrder);
|
||||
}
|
||||
jdProductGroupMapper.updateById(group);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除分组(事务两步,AC-8):先把该组商品 {@code group_id} 置 NULL(商品保留,
|
||||
* 脱组即「未分组」),再删分组行。越界 id 拒绝。
|
||||
*
|
||||
* @return 实际脱组的商品行数(前端拿它做「已解挂 N 个商品」的反馈,不猜)
|
||||
*/
|
||||
@Transactional
|
||||
public int delete(Long id, Long scopedCompanyId) {
|
||||
JdProductGroupEntity group = requireOwnedGroup(id, scopedCompanyId);
|
||||
int detached = jdProductMapper.update(null, new LambdaUpdateWrapper<JdProductEntity>()
|
||||
.set(JdProductEntity::getGroupId, null)
|
||||
.eq(JdProductEntity::getCompanyId, group.getCompanyId())
|
||||
.eq(JdProductEntity::getGroupId, group.getId()));
|
||||
jdProductGroupMapper.deleteById(group.getId());
|
||||
return detached;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 业务规则
|
||||
|
||||
/** 名字归一:trim 后必须非空。 */
|
||||
private static String requireName(String name) {
|
||||
if (name == null || name.trim().isEmpty()) {
|
||||
throw new BusinessException(400, "分组名不能为空");
|
||||
}
|
||||
return name.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分组行归属校验:不存在 / 公司不符(越界 id)一律按「分组不存在」拒绝——
|
||||
* 越界探测拿到的与 id 不存在是同一种响应,不泄露跨公司行的存在性。
|
||||
* {@code scopedCompanyId} 为空=超管没选公司、跨公司放行。
|
||||
*/
|
||||
private JdProductGroupEntity requireOwnedGroup(Long id, Long scopedCompanyId) {
|
||||
if (id == null || id <= 0) {
|
||||
throw new BusinessException("分组不存在");
|
||||
}
|
||||
JdProductGroupEntity group = jdProductGroupMapper.selectById(id);
|
||||
if (group == null || (scopedCompanyId != null && scopedCompanyId > 0
|
||||
&& !scopedCompanyId.equals(group.getCompanyId()))) {
|
||||
throw new BusinessException("分组不存在");
|
||||
}
|
||||
return group;
|
||||
}
|
||||
|
||||
/** 同公司重名拒绝(409):{@code excludeId} 非空=改名时排除自身。 */
|
||||
private void requireNameFree(Long companyId, String name, Long excludeId) {
|
||||
Long count = jdProductGroupMapper.selectCount(new LambdaQueryWrapper<JdProductGroupEntity>()
|
||||
.eq(JdProductGroupEntity::getCompanyId, companyId)
|
||||
.eq(JdProductGroupEntity::getName, name)
|
||||
.ne(excludeId != null, JdProductGroupEntity::getId, excludeId));
|
||||
if (count != null && count > 0) {
|
||||
throw new BusinessException(409, "同公司下已存在同名分组:" + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
+403
@@ -0,0 +1,403 @@
|
||||
package com.nanri.aiimage.modules.jd.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.nanri.aiimage.common.api.PageDefaults;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductImageMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.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.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):分页 / 详情 / 导出。
|
||||
*
|
||||
* <p>分页钳制是本服务的职责(铁律 15):mapper 的 {@code offset}/{@code limit} 只收钳制后的值
|
||||
* (单一出口,mapper 不做第二次换算)。钳制形状照 {@code AppearancePatentCleanItemQueryService}
|
||||
* 先例:{@code page} 上限 {@link #MAX_PAGE}、{@code page_size} 上限 {@link #MAX_PAGE_SIZE},
|
||||
* {@code page=Long.MAX_VALUE} 不会把乘积顶成负数生成 {@code LIMIT -N} 的 500 错。</p>
|
||||
*
|
||||
* <p><b>公司隔离</b>:{@code scopedCompanyId} 由控制器经 {@code DataReadScope#readCompanyIdOrOwn}
|
||||
* 算好传入——{@code null} = 超管没选公司、查全量;正数 = 只查该公司(非超管恒为自己的公司)。
|
||||
* 详情同样按它收口:行 {@code company_id} 与公司隔离范围不符按不存在拒绝(不泄露跨公司行的存在性)。</p>
|
||||
*
|
||||
* <p><b>详情 attr_json 解析(AC-7 未识别属性不丢弃)</b>:客户端实拍契约
|
||||
* {@code {"tabs":[{"tab":"...","attrs":[{key,label,value}]}],"images":[...],"productCode":"..."}}——
|
||||
* {@code tabs} 数组映射成 {@code attrGroups}(groupName=tab 字段);attr_json 为空/非法 JSON/
|
||||
* 非该形状时<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>
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class JdProductQueryService {
|
||||
|
||||
/** 页码上限(CR-18 关键词分页同值):10000 页 x 100 条 = 100 万行封顶,乘积不溢出。 */
|
||||
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";
|
||||
private static final String ATTR_KEY_FIELD = "key";
|
||||
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", "标题", "品牌", "类目路径",
|
||||
"价格下限", "价格上限", "库存", "商品状态", "来源Tab",
|
||||
"京东更新时间", "属性键数", "图片数", "分组", "同步时间");
|
||||
|
||||
private final JdProductMapper jdProductMapper;
|
||||
private final JdProductImageMapper jdProductImageMapper;
|
||||
private final JdProductGroupMapper jdProductGroupMapper;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* 分页查询商品行({@code synced_at} 倒序)。
|
||||
*
|
||||
* <p>过滤面:公司(物化列 eq)、店铺名 contains、商品编码 contains、标题关键词 contains、
|
||||
* 商品状态 eq、分组 eq、同步起止(datetime 闭区间)。</p>
|
||||
*/
|
||||
public JdProductPageVo page(Long page, Long pageSize, 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("开始时间不能晚于结束时间");
|
||||
}
|
||||
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);
|
||||
long total = jdProductMapper.countProducts(filter.companyId(), filter.shopName(), filter.productCode(),
|
||||
filter.titleKeyword(), filter.saleStatus(), 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);
|
||||
List<JdProductListItemVo> items = rows.stream().map(this::toListItem).toList();
|
||||
fillGroupNames(items);
|
||||
JdProductPageVo vo = new JdProductPageVo();
|
||||
vo.setItems(items);
|
||||
vo.setTotal(total);
|
||||
vo.setPage(safePage);
|
||||
vo.setPageSize(safeSize);
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 商品详情(主字段全量 + attr_json 解析 + 已转存图片)。
|
||||
*
|
||||
* <p>公司隔离与分页同门:{@code scopedCompanyId} 非空且与行 {@code company_id} 不符时按
|
||||
* 不存在拒绝——不泄露跨公司行的存在性(越界 id 探测拿到的与 id 不存在是同一种响应)。
|
||||
* attr_json 非契约形状/解析异常时不抛 500:落空 {@code attrGroups}、原文进
|
||||
* {@code attrJsonRaw} 兜底(AC-7)。</p>
|
||||
*/
|
||||
public JdProductDetailVo detail(Long id, 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("商品不存在");
|
||||
}
|
||||
JdProductDetailVo vo = new JdProductDetailVo();
|
||||
vo.setId(row.getId());
|
||||
vo.setCompanyId(row.getCompanyId());
|
||||
vo.setTaskId(row.getTaskId());
|
||||
vo.setShopId(row.getShopId());
|
||||
vo.setShopName(row.getShopName());
|
||||
vo.setProductCode(row.getProductCode());
|
||||
vo.setSkuId(row.getSkuId());
|
||||
vo.setTitle(row.getTitle());
|
||||
vo.setBrand(row.getBrand());
|
||||
vo.setBrandId(row.getBrandId());
|
||||
vo.setCategoryPath(row.getCategoryPath());
|
||||
vo.setPriceMin(row.getPriceMin());
|
||||
vo.setPriceMax(row.getPriceMax());
|
||||
vo.setStockTotal(row.getStockTotal());
|
||||
vo.setSaleStatus(row.getSaleStatus());
|
||||
vo.setListTab(row.getListTab());
|
||||
vo.setJdCreatedAt(row.getJdCreatedAt());
|
||||
vo.setJdUpdatedAt(row.getJdUpdatedAt());
|
||||
vo.setMainImageUrl(row.getMainImageUrl());
|
||||
vo.setDetailUrl(row.getDetailUrl());
|
||||
vo.setAttrCount(row.getAttrCount());
|
||||
vo.setImageCount(row.getImageCount());
|
||||
vo.setGroupId(row.getGroupId());
|
||||
vo.setCollectBatchId(row.getCollectBatchId());
|
||||
vo.setSyncedAt(row.getSyncedAt());
|
||||
parseAttrGroups(vo, row.getAttrJson());
|
||||
vo.setImages(loadImages(row.getId()));
|
||||
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) {
|
||||
}
|
||||
|
||||
private static Filter filter(Long scopedCompanyId, String shopName, String productCode, String titleKeyword,
|
||||
String saleStatus, 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);
|
||||
}
|
||||
|
||||
/** 超管不选公司(scopedCompanyId=null)=全量可见;正数=只看该公司。 */
|
||||
private static boolean companyVisible(Long rowCompanyId, Long scopedCompanyId) {
|
||||
if (scopedCompanyId == null || scopedCompanyId <= 0) {
|
||||
return true;
|
||||
}
|
||||
return scopedCompanyId.equals(rowCompanyId);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 列表/详情搬运
|
||||
|
||||
/** 行值原样搬运(读侧不加工),groupName 由 {@link #fillGroupNames} 批量补齐。 */
|
||||
private JdProductListItemVo toListItem(JdProductEntity row) {
|
||||
JdProductListItemVo vo = new JdProductListItemVo();
|
||||
vo.setId(row.getId());
|
||||
vo.setShopName(row.getShopName());
|
||||
vo.setShopId(row.getShopId());
|
||||
vo.setProductCode(row.getProductCode());
|
||||
vo.setSkuId(row.getSkuId());
|
||||
vo.setTitle(row.getTitle());
|
||||
vo.setBrand(row.getBrand());
|
||||
vo.setCategoryPath(row.getCategoryPath());
|
||||
vo.setPriceMin(row.getPriceMin());
|
||||
vo.setPriceMax(row.getPriceMax());
|
||||
vo.setStockTotal(row.getStockTotal());
|
||||
vo.setSaleStatus(row.getSaleStatus());
|
||||
vo.setListTab(row.getListTab());
|
||||
vo.setJdUpdatedAt(row.getJdUpdatedAt());
|
||||
vo.setMainImageUrl(row.getMainImageUrl());
|
||||
vo.setAttrCount(row.getAttrCount());
|
||||
vo.setImageCount(row.getImageCount());
|
||||
vo.setGroupId(row.getGroupId());
|
||||
vo.setSyncedAt(row.getSyncedAt());
|
||||
return vo;
|
||||
}
|
||||
|
||||
/** 批量补分组名:行集的 group_id 一次取齐(一次查询,无逐行查询);读不到的分组不猜名字。 */
|
||||
private void fillGroupNames(List<JdProductListItemVo> items) {
|
||||
List<Long> groupIds = items.stream()
|
||||
.map(JdProductListItemVo::getGroupId)
|
||||
.filter(id -> id != null && id > 0)
|
||||
.distinct()
|
||||
.toList();
|
||||
if (groupIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Map<Long, String> namesById = new LinkedHashMap<>();
|
||||
for (JdProductGroupEntity group : jdProductGroupMapper.selectBatchIds(groupIds)) {
|
||||
if (group.getId() != null && group.getName() != null && !group.getName().isBlank()) {
|
||||
namesById.put(group.getId(), group.getName());
|
||||
}
|
||||
}
|
||||
for (JdProductListItemVo item : items) {
|
||||
if (item.getGroupId() != null) {
|
||||
item.setGroupName(namesById.get(item.getGroupId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 已转存图片:按档内排序号、id 稳定升序(详情抽屉分区展示顺序)。 */
|
||||
private List<JdProductDetailVo.ImageViewVo> loadImages(Long productId) {
|
||||
List<JdProductImageEntity> rows = jdProductImageMapper.selectList(
|
||||
new LambdaQueryWrapper<JdProductImageEntity>()
|
||||
.eq(JdProductImageEntity::getProductId, productId)
|
||||
.orderByAsc(JdProductImageEntity::getSortNo)
|
||||
.orderByAsc(JdProductImageEntity::getId));
|
||||
List<JdProductDetailVo.ImageViewVo> images = new ArrayList<>(rows.size());
|
||||
for (JdProductImageEntity row : rows) {
|
||||
JdProductDetailVo.ImageViewVo view = new JdProductDetailVo.ImageViewVo();
|
||||
view.setImageType(row.getImageType());
|
||||
view.setAttrName(row.getAttrName());
|
||||
view.setSortNo(row.getSortNo());
|
||||
view.setUrl(row.getUrl());
|
||||
view.setByteSize(row.getByteSize());
|
||||
view.setWidth(row.getWidth());
|
||||
view.setHeight(row.getHeight());
|
||||
images.add(view);
|
||||
}
|
||||
return images;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- attr_json 解析
|
||||
|
||||
/**
|
||||
* attr_json → attrGroups。契约形状 {@code {"tabs":[{"tab":..,"attrs":[..]}]}};
|
||||
* 为空/非法 JSON/形状不识别一律落空列表并把原文放进 {@code attrJsonRaw}(不抛 500,AC-7)。
|
||||
*/
|
||||
private void parseAttrGroups(JdProductDetailVo vo, String attrJson) {
|
||||
vo.setAttrGroups(List.of());
|
||||
if (attrJson == null || attrJson.isBlank()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
JsonNode root = objectMapper.readTree(attrJson);
|
||||
JsonNode tabs = root == null ? null : root.get("tabs");
|
||||
if (tabs == null || !tabs.isArray() || tabs.isEmpty()) {
|
||||
vo.setAttrJsonRaw(attrJson);
|
||||
return;
|
||||
}
|
||||
List<JdProductDetailVo.AttrGroupVo> groups = new ArrayList<>();
|
||||
for (JsonNode tabNode : tabs) {
|
||||
JsonNode tab = tabNode.get(ATTR_TAB_FIELD);
|
||||
JsonNode attrs = tabNode.get(ATTR_ATTRS_FIELD);
|
||||
if (attrs == null || !attrs.isArray()) {
|
||||
// 该 Tab 没有 attrs 数组=形状不识别的那部分:不丢原文,整段落 attrJsonRaw 兜底
|
||||
vo.setAttrJsonRaw(attrJson);
|
||||
continue;
|
||||
}
|
||||
JdProductDetailVo.AttrGroupVo group = new JdProductDetailVo.AttrGroupVo();
|
||||
group.setGroupName(tab == null || tab.isNull() ? null : tab.asText());
|
||||
List<JdProductDetailVo.AttrVo> attrList = new ArrayList<>();
|
||||
for (JsonNode attrNode : attrs) {
|
||||
JdProductDetailVo.AttrVo attr = new JdProductDetailVo.AttrVo();
|
||||
JsonNode key = attrNode.get(ATTR_KEY_FIELD);
|
||||
JsonNode label = attrNode.get(ATTR_LABEL_FIELD);
|
||||
JsonNode value = attrNode.get(ATTR_VALUE_FIELD);
|
||||
attr.setKey(key == null || key.isNull() ? null : key.asText());
|
||||
attr.setLabel(label == null || label.isNull() ? null : label.asText());
|
||||
attr.setValue(value == null || value.isNull() ? null : value.asText());
|
||||
attrList.add(attr);
|
||||
}
|
||||
group.setAttrs(attrList);
|
||||
groups.add(group);
|
||||
}
|
||||
vo.setAttrGroups(groups);
|
||||
} catch (Exception ex) {
|
||||
// 解析异常按未识别形状兜底:原文透出,绝不把详情页打成 500(AC-7)
|
||||
log.warn("[jd-product] attr_json 解析失败 productId={} msg={}", vo.getId(), ex.getMessage());
|
||||
vo.setAttrGroups(List.of());
|
||||
vo.setAttrJsonRaw(attrJson);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 导出
|
||||
|
||||
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.getSkuId()));
|
||||
row.createCell(4).setCellValue(safe(item.getTitle()));
|
||||
row.createCell(5).setCellValue(safe(item.getBrand()));
|
||||
row.createCell(6).setCellValue(safe(item.getCategoryPath()));
|
||||
row.createCell(7).setCellValue(item.getPriceMin() == null ? "" : item.getPriceMin().toPlainString());
|
||||
row.createCell(8).setCellValue(item.getPriceMax() == null ? "" : item.getPriceMax().toPlainString());
|
||||
row.createCell(9).setCellValue(item.getStockTotal() == null ? "" : String.valueOf(item.getStockTotal()));
|
||||
row.createCell(10).setCellValue(safe(item.getSaleStatus()));
|
||||
row.createCell(11).setCellValue(safe(item.getListTab()));
|
||||
row.createCell(12).setCellValue(item.getJdUpdatedAt() == null
|
||||
? "" : EXPORT_DATETIME_FORMATTER.format(item.getJdUpdatedAt()));
|
||||
row.createCell(13).setCellValue(item.getAttrCount() == null ? "" : String.valueOf(item.getAttrCount()));
|
||||
row.createCell(14).setCellValue(item.getImageCount() == null ? "" : String.valueOf(item.getImageCount()));
|
||||
row.createCell(15).setCellValue(safe(item.getGroupName()));
|
||||
row.createCell(16).setCellValue(item.getSyncedAt() == null
|
||||
? "" : 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;
|
||||
}
|
||||
String trimmed = value.trim();
|
||||
return trimmed.isEmpty() ? null : trimmed;
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,40 @@
|
||||
attr_count = VALUES(attr_count),
|
||||
image_count = VALUES(image_count),
|
||||
collect_batch_id = VALUES(collect_batch_id),
|
||||
synced_at = CURRENT_TIMESTAMP
|
||||
synced_at = CURRENT_TIMESTAMP
|
||||
</insert>
|
||||
|
||||
<!-- 后管只读页(Phase 5,v1.4)过滤面:公司(物化列 eq)、店铺名/商品编码/标题关键词
|
||||
(contains LIKE)、商品状态 eq、分组 eq、同步起止(datetime 闭区间)。
|
||||
全部走 #{} 占位符;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="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>
|
||||
</sql>
|
||||
|
||||
<!-- 只读页总数(与 selectProductPage 同一过滤面)。 -->
|
||||
<select id="countProducts" resultType="long">
|
||||
SELECT COUNT(*)
|
||||
FROM biz_jd_product p
|
||||
WHERE 1=1 <include refid="productConds"/>
|
||||
</select>
|
||||
|
||||
<!-- 只读页分页查询(synced_at 倒序)。
|
||||
group_name 不在此联表:读侧按行集 group_id 批量取分组名补齐(一次查询,无逐行查询)。 -->
|
||||
<select id="selectProductPage" resultType="com.nanri.aiimage.modules.jd.model.entity.JdProductEntity">
|
||||
SELECT p.id, p.company_id, p.user_id, p.task_id, p.shop_id, p.shop_name, p.product_code, p.sku_id,
|
||||
p.title, p.brand, p.brand_id, p.category_path, p.price_min, p.price_max, p.stock_total,
|
||||
p.sale_status, p.list_tab, p.jd_created_at, p.jd_updated_at, p.main_image_url, p.detail_url,
|
||||
p.attr_json, p.attr_count, p.image_count, p.group_id, p.collect_batch_id, p.synced_at
|
||||
FROM biz_jd_product p
|
||||
WHERE 1=1 <include refid="productConds"/>
|
||||
ORDER BY p.synced_at DESC, p.id DESC
|
||||
LIMIT #{offset}, #{limit}
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
@@ -85,7 +85,10 @@ class MigratedSqlEquivalenceTest {
|
||||
// 105 → 109:2026-11-11「按主管划分」同一 mapper 再落 4 条 select(主管聚合 count/page
|
||||
// +按主管导出 count/page),同样从来没有注解原文可对撞,逐条点名
|
||||
//(SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 151 → 155)。
|
||||
private static final int XML_SELECT_TOTAL = 109;
|
||||
// 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;
|
||||
|
||||
/**
|
||||
* 不是「从注解搬过来」的语句:注解时代没有原文可对撞,等价性这支只能跳过它们。
|
||||
@@ -186,7 +189,13 @@ class MigratedSqlEquivalenceTest {
|
||||
// uk_product_image),注解时代没有原文可对撞,逐条点名留痕
|
||||
//(SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 155 → 157)。
|
||||
"JdProductMapper#upsertBatch",
|
||||
"JdProductImageMapper#upsertBatch");
|
||||
"JdProductImageMapper#upsertBatch",
|
||||
// v1.4 Phase 5(2026-11-14):后管只读页在 JdProductMapper 新写 2 条只读 select
|
||||
//(countProducts + selectProductPage,同一过滤面、offset/limit 由服务钳制后传参),
|
||||
// 注解时代没有原文可对撞,逐条点名留痕
|
||||
//(SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 157 → 159)。
|
||||
"JdProductMapper#countProducts",
|
||||
"JdProductMapper#selectProductPage");
|
||||
|
||||
/** 账本 5 列:接口 / 方法 / @Select 种类 / 是否含常量插值 / 注解原文(Java 反转义后那条串)。 */
|
||||
private static final int LEDGER_COLUMNS = 5;
|
||||
|
||||
@@ -112,7 +112,10 @@ class SqlLayeringConventionTest {
|
||||
// JdProductMapper#upsertBatch(覆盖最新,uk_company_shop_code)+
|
||||
// JdProductImageMapper#upsertBatch(图片登记,uk_product_image),全部新写,
|
||||
// 同轮登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(该文件 XML_SELECT_TOTAL 不动:无 select)。
|
||||
private static final int XML_STATEMENT_TOTAL = 157;
|
||||
// 157 → 159:v1.4 Phase 5 后管只读页在 JdProductMapper 落 2 条只读 select
|
||||
//(countProducts + selectProductPage,同过滤面、offset/limit 由服务钳制后传参),
|
||||
// 同轮登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(该文件 XML_SELECT_TOTAL 不动)。
|
||||
private static final int XML_STATEMENT_TOTAL = 159;
|
||||
|
||||
/**
|
||||
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
|
||||
|
||||
+17
-11
@@ -65,6 +65,9 @@ class PageDefaultsLedgerTest {
|
||||
// 与明细列表端点同参数名同默认值(按出站点计数、不去重)
|
||||
"AdminAppearancePatentCleanItemsController.java#page=FIRST_PAGE",
|
||||
"AdminAppearancePatentCleanItemsController.java#page_size=CLEAN_ITEMS_PAGE_SIZE",
|
||||
// v1.4 Phase 5(2026-11-14):京东数据库后管只读页分页出站点(page/page_size 默认 20)
|
||||
"AdminJdProductController.java#page=FIRST_PAGE",
|
||||
"AdminJdProductController.java#page_size=JD_PRODUCTS_PAGE_SIZE",
|
||||
"AppearancePatentController.java#limit=DEFAULT_RESULT_LIMIT",
|
||||
"CollectDataController.java#page=FIRST_PAGE",
|
||||
"CollectDataController.java#page_size=TASK_LIST_PAGE_SIZE",
|
||||
@@ -115,11 +118,12 @@ class PageDefaultsLedgerTest {
|
||||
List<String> constants = actual.stream().filter(s -> !s.contains("LITERAL")).sorted().toList();
|
||||
|
||||
assertThat(constants)
|
||||
.as("58 处分页/条数出站点的具名默认值必须与台账一一对应"
|
||||
+ "(56→58:2026-11-11 /supervisors 主管聚合卡片页的 page/page_size 两处出站点)")
|
||||
.as("60 处分页/条数出站点的具名默认值必须与台账一一对应"
|
||||
+ "(58→60:v1.4 Phase 5 京东数据库后管只读页 AdminJdProductController 的"
|
||||
+ " page/page_size 两处出站点)")
|
||||
.isEqualTo(LEDGER.stream().sorted().toList());
|
||||
// 同口径非零对照:台账不是空的,扫描器也确实数得出 58 处
|
||||
assertThat(constants).hasSize(58);
|
||||
// 同口径非零对照:台账不是空的,扫描器也确实数得出 60 处
|
||||
assertThat(constants).hasSize(60);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -134,13 +138,14 @@ class PageDefaultsLedgerTest {
|
||||
void oneSemanticCannotGrowASecondDefault() {
|
||||
List<String> actual = sites();
|
||||
assertThat(kindsUsing(actual, "page", "FIRST_PAGE"))
|
||||
.as("页码口径只有一套:全部从 1 起(25→26:2026-11-11 /supervisors 主管聚合卡片页也是分页出站点)")
|
||||
.isEqualTo(26);
|
||||
.as("页码口径只有一套:全部从 1 起(26→27:v1.4 Phase 5 京东数据库只读页也是分页出站点)")
|
||||
.isEqualTo(27);
|
||||
assertThat(distinctConstants(actual, "page_size"))
|
||||
.as("page_size 的默认容量集合:10/15/20/50/1000 五种各自登记,新来第六种要红"
|
||||
+ "(10 是 v1.3 无侵权只读页,蓝图 §3.2 拍板、与前端 OldPagination 首屏同值)")
|
||||
.as("page_size 的默认容量集合:10/15/20/50/1000 各自登记(20 有两把名字:超管台 "
|
||||
+ "ADMIN_LIST_PAGE_SIZE 与 v1.4 京东数据库 JD_PRODUCTS_PAGE_SIZE),新语义要红")
|
||||
.containsExactlyInAnyOrder("USER_LIST_PAGE_SIZE", "ADMIN_LIST_PAGE_SIZE",
|
||||
"TASK_LIST_PAGE_SIZE", "FULL_EXPORT_PAGE_SIZE", "CLEAN_ITEMS_PAGE_SIZE");
|
||||
"TASK_LIST_PAGE_SIZE", "FULL_EXPORT_PAGE_SIZE", "CLEAN_ITEMS_PAGE_SIZE",
|
||||
"JD_PRODUCTS_PAGE_SIZE");
|
||||
// 同一个语义有两种拼写(@RequestParam(name = "page_size") 与 camelCase 的 pageSize),
|
||||
// 而两种拼写实际用到的常量本来就不同。原来把同一个四项清单套在两种拼写上,
|
||||
// 红的是台账写法而不是有人新增了默认值——2026-10-10 出包前对平这一条。
|
||||
@@ -151,10 +156,10 @@ class PageDefaultsLedgerTest {
|
||||
bothSpellings.addAll(distinctConstants(actual, "page_size"));
|
||||
bothSpellings.addAll(distinctConstants(actual, "pageSize"));
|
||||
assertThat(bothSpellings)
|
||||
.as("两种拼写合起来仍只许那五种容量:谁长出第六种,这里就红")
|
||||
.as("两种拼写合起来仍只许那几把容量:谁长出第七种,这里就红")
|
||||
.containsExactlyInAnyOrderElementsOf(java.util.List.of("USER_LIST_PAGE_SIZE",
|
||||
"ADMIN_LIST_PAGE_SIZE", "TASK_LIST_PAGE_SIZE", "FULL_EXPORT_PAGE_SIZE",
|
||||
"CLEAN_ITEMS_PAGE_SIZE"));
|
||||
"CLEAN_ITEMS_PAGE_SIZE", "JD_PRODUCTS_PAGE_SIZE"));
|
||||
assertThat(distinctConstants(actual, "limit"))
|
||||
.as("limit 是「本次最多返回几条」,不许和 page_size 混用同一批常量")
|
||||
.containsExactlyInAnyOrder("DEFAULT_RESULT_LIMIT", "ADMIN_SCAN_LIMIT");
|
||||
@@ -186,6 +191,7 @@ class PageDefaultsLedgerTest {
|
||||
assertPair(PageDefaults.FULL_EXPORT_PAGE_SIZE, PageDefaults.FULL_EXPORT_PAGE_SIZE_INT);
|
||||
assertPair(PageDefaults.DEFAULT_RESULT_LIMIT, PageDefaults.DEFAULT_RESULT_LIMIT_INT);
|
||||
assertPair(PageDefaults.ADMIN_SCAN_LIMIT, PageDefaults.ADMIN_SCAN_LIMIT_INT);
|
||||
assertPair(PageDefaults.JD_PRODUCTS_PAGE_SIZE, PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -69,7 +69,11 @@ class CatchSilenceRatchetTest {
|
||||
// LOG_ONLY 与 EMPTY 一格没涨(逐处记名见 STRICT_TOTAL 上方注释)——改的是"查不到"怎么表达,不是把它藏起来。
|
||||
// 11-10 v1.3 外观专利无侵权同步链抬 608 -> 611:三枚不重抛(同步服务两枚+控制器内部令牌回退一枚),
|
||||
// 逐处记名见 STRICT_TOTAL 上方注释;LOG_ONLY 宽口径格 251 没动(宽口径实测已落到 193,离上限很远)。
|
||||
static final int GIVEN_NOT_RETHROW = 611;
|
||||
// 11-14 v1.4 京东数据库抬 611 -> 615:三枚不重抛(Phase 5 后管两处 resolveInternalOperator 返回 null
|
||||
// + parseAttrGroups 的 attr_json 兜底;Phase 2/4 的 7 枚全是 RETHROW、Phase 2 的 scope 行
|
||||
// DuplicateKeyException 是 LOG_ONLY,都不占本格),逐处记名见 STRICT_TOTAL 上方注释;
|
||||
// LOG_ONLY 宽口径格 251 没动(宽口径实测 194,离上限很远)。
|
||||
static final int GIVEN_NOT_RETHROW = 615;
|
||||
static final int GIVEN_LOG_ONLY = 251;
|
||||
static final int GIVEN_EMPTY = 56;
|
||||
|
||||
@@ -324,7 +328,31 @@ class CatchSilenceRatchetTest {
|
||||
// 本格与 NOT_RETHROW/LOG_ONLY/EMPTY 都不动)。误差方向:导出请求的 IO 失败必须给用户
|
||||
// 可读业务错误而不是 500 裸堆栈;反向对照——改成往外抛 IOException=前端拿到 Spring 兜底文案,
|
||||
// 改成吞掉返回空字节=用户拿到 0 字节 xlsx 还以为导出成功(比报错更贵)。
|
||||
static final int STRICT_TOTAL = 1074;
|
||||
// 11-14 v1.4 京东数据库抬 1074 -> 1090,十六枚逐处记名(Phase 2 采集域 7 枚与 Phase 4 通用化
|
||||
// 3 枚随各自 commit 落码、当时未进点名册,Phase 5 收尾跑全量时由本棘轮一次性抓出,此处并册):
|
||||
// 【Phase 4 通用化预签名,3 枚全 RETHROW】`OssStorageService.presignObjectUpload` /
|
||||
// `objectSize`(ErrorResponseException 非 404 分支 + Exception 兜底,共 2 枚):全部转
|
||||
// `storageFailure`——预签名/对撞失败必须给可读业务错误,静默 return null 会让客户端拿
|
||||
// null uploadUrl 直传炸得更深。
|
||||
// 【Phase 2 采集域,7 枚】`JdCollectTaskService`:① 建单载荷序列化 `catch Exception` →
|
||||
// BusinessException(RETHROW);② scope 行 `catch DuplicateKeyException` → log.info 复用
|
||||
// 先到行(LOG_ONLY +1,并发建单幂等语义,不吞成败);③④ `readCreateRequest` 的
|
||||
// `catch BusinessException → throw ex` + `catch Exception → BusinessException`(RETHROW,
|
||||
// 载荷损坏按异常拒绝回传不静默兜)。`JdResultIngestService`:⑤⑥ `parseState`/`writeJson`
|
||||
// → BusinessException(RETHROW,台账损坏显形);⑦ `toSummary` `catch IllegalArgumentException`
|
||||
// → BusinessException 带字段消息(RETHROW)。
|
||||
// 【Phase 5 后管,6 枚】① `AdminJdProductController.resolveOperator` 与 ②
|
||||
// `AdminJdProductGroupController.resolveOperator`:`catch BusinessException` ⇒ 先试内部令牌
|
||||
// 回退,回退失败把原鉴权异常原样重抛(RETHROW +2)。③④ 同两文件的
|
||||
// `resolveInternalOperator`:`catch NumberFormatException | BusinessException` ⇒ 返回 null ⇒
|
||||
// 上层把原 401/403 原样抛回(NOT_RETHROW +2)。⑤ `JdProductQueryService.parseAttrGroups`:
|
||||
// attr_json 解析 `catch Exception` ⇒ log.warn 带 productId + 空分组 + 原文透 attrJsonRaw
|
||||
// 兜底(AC-7 未识别属性不丢弃;NOT_RETHROW +1)。反向对照:改成往外抛=一条脏 attr_json
|
||||
// 让整行详情 500;改成静默空分组不带原文=用户看到的属性被悄悄吞掉。⑥
|
||||
// `JdProductQueryService.writeExportWorkbook`:SXSSF 落盘 `catch Exception` →
|
||||
// BusinessException「导出京东数据库失败,请稍后重试」(RETHROW +1,与 11-11 外观专利导出同款)。
|
||||
// 本批复核办法:单跑实测 total 1090、notRethrow 614、logOnly 194、empty 56(EMPTY 一格没涨)。
|
||||
static final int STRICT_TOTAL = 1090;
|
||||
/**
|
||||
* 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。
|
||||
* 这一格只能涨不能跌——它是"异常会往外走"的总量,把它钉成"只许减"就等于拦着别人修吞异常。
|
||||
@@ -453,7 +481,12 @@ class CatchSilenceRatchetTest {
|
||||
// 11-05 密钥余额三面:五枚 NOT_RETHROW(1065->1069 那批),严格口径实测 603 -> 608;
|
||||
// 上一格 604 是**上限**而实测量一直低它一枚,这次新增把那一枚额度用掉又超上去——逐处见 STRICT_TOTAL 上方。
|
||||
// 11-10 v1.3 同步链:严格口径实测 608 -> 611(三枚不重抛),出处见 STRICT_TOTAL 上方注释。
|
||||
static final int STRICT_NOT_RETHROW = 611;
|
||||
// 11-14 v1.4 京东数据库后管:严格口径实测 611 -> 614(三枚不重抛:两处 resolveInternalOperator
|
||||
// 返回 null + parseAttrGroups 的 attr_json 兜底),出处见 STRICT_TOTAL 上方注释。
|
||||
// 本格含 LOG_ONLY(incl 口径):同批 Phase 2 建单 scope 行那枚 DuplicateKeyException 复用
|
||||
// 先到行把 LOG_ONLY 抬 193 -> 194 时连带占本格一枚额度 ⇒ 614 -> 615(出处同 STRICT_TOTAL
|
||||
// 上方注释;这是本格**含 LOG_ONLY** 口径的连带,不是又一枚新的不重抛)。
|
||||
static final int STRICT_NOT_RETHROW = 615;
|
||||
/**
|
||||
* 10-17 抬 172 -> 173,出处就是上面点名过的 {@code TaskHeartbeatLivenessService.stamp()}:
|
||||
* 块体只有「取到 key 名 + log.warn」一句,写心跳证据失败时既不重试也不抛出——
|
||||
@@ -487,7 +520,7 @@ class CatchSilenceRatchetTest {
|
||||
* 同日 Phase 5.1 再抬 189 -> 190:登记行落库失败后的对象回收失败只 WARN(同上锚点),
|
||||
* 主错误仍由外层转业务错误;不吞主错误。
|
||||
*/
|
||||
static final int STRICT_LOG_ONLY = 193; // 严格口径:块内每条顶层语句都是 log 调用才算(11-01 重试两处兜底 +2、11-10 v1.3 同步服务 syncFromResult +1,见 STRICT_TOTAL 上方)
|
||||
static final int STRICT_LOG_ONLY = 194; // 严格口径:块内每条顶层语句都是 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;
|
||||
|
||||
/**
|
||||
|
||||
+496
@@ -0,0 +1,496 @@
|
||||
package com.nanri.aiimage.modules.jd.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.api.PageDefaults;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.service.DataReadScope;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductImageMapper;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductMapper;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.entity.JdProductImageEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductDetailVo;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductPageVo;
|
||||
import com.nanri.aiimage.modules.jd.service.JdProductQueryService;
|
||||
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;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
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;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 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>
|
||||
*/
|
||||
class AdminJdProductControllerTest {
|
||||
|
||||
private static final Path MODULE = Path.of("").toAbsolutePath();
|
||||
private static final Path MAIN = MODULE.resolve("src/main/java/com/nanri/aiimage");
|
||||
private static final Path CONTROLLER =
|
||||
MAIN.resolve("modules/jd/controller/AdminJdProductController.java");
|
||||
private static final Path GROUP_CONTROLLER =
|
||||
MAIN.resolve("modules/jd/controller/AdminJdProductGroupController.java");
|
||||
private static final Path MAPPER = MAIN.resolve("modules/jd/mapper/JdProductMapper.java");
|
||||
private static final String COLUMN_KEY = "admin_jd_database";
|
||||
private static final String ROUTE_PATH = "jd-center/database";
|
||||
|
||||
// ===================== 结构判据 =====================
|
||||
|
||||
@Test
|
||||
void 控制器与只读端点就位() throws IOException {
|
||||
assertTrue(Files.exists(CONTROLLER), "控制器必须落在 " + CONTROLLER);
|
||||
String source = stripComments(Files.readString(CONTROLLER));
|
||||
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");
|
||||
// 本页只读(出表走导出端点):禁 POST/PUT/DELETE,分组管理另有同款鉴权闸的分组控制器。
|
||||
assertFalse(source.contains("@PostMapping") || source.contains("@PutMapping")
|
||||
|| source.contains("@DeleteMapping"),
|
||||
"京东数据库页只读:不得有 POST/PUT/DELETE(分组 CRUD 落 AdminJdProductGroupController)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 鉴权闸照外观专利只读页私有require形状() throws IOException {
|
||||
String source = stripComments(Files.readString(CONTROLLER));
|
||||
for (String seam : List.of("requireUser", "X-Internal-Token", "isSuper",
|
||||
"getUserColumnPermissions", COLUMN_KEY, ROUTE_PATH, "requireJdAccess")) {
|
||||
assertTrue(source.contains(seam), "鉴权闸缺接缝 " + seam
|
||||
+ "——照 AdminAppearancePatentCleanItemsController 私有 requireXxxAccess 形状"
|
||||
+ "(JWT→内部令牌回退→超管放行→菜单双匹配)");
|
||||
}
|
||||
assertTrue(source.contains("getColumnKey") && source.contains("getRoutePath"),
|
||||
"菜单权限必须 column_key/route_path 双匹配(默认不命中即拒)");
|
||||
assertTrue(source.contains("无权访问京东数据库"), "拒绝文案必须点名京东数据库");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 公司隔离走readCompanyIdOrOwn() throws IOException {
|
||||
String source = stripComments(Files.readString(CONTROLLER));
|
||||
assertTrue(source.contains("DataReadScope"), "公司隔离唯一出口是 DataReadScope");
|
||||
assertTrue(source.contains("readCompanyIdOrOwn"),
|
||||
"读侧必须走 readCompanyIdOrOwn:超管显式传才收窄,非超管强制本公司");
|
||||
assertFalse(source.contains("superAdminScopedCompanyId"),
|
||||
"不得走 superAdminScopedCompanyId:它对非超管返回 null=按行 company_id 查询时把整张表交给每一家");
|
||||
}
|
||||
|
||||
@Test
|
||||
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")) {
|
||||
assertTrue(source.contains(param), "查询参数必须 snake_case:" + param);
|
||||
}
|
||||
assertTrue(source.contains("PageDefaults"), "page/page_size 默认值必须取 PageDefaults"
|
||||
+ "(page_size 默认 20=PageDefaults.JD_PRODUCTS_PAGE_SIZE);"
|
||||
+ "page 钳制防 (page-1)*page_size 溢出的行为样本见查询服务钳制支(铁律 15)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 查询SQL只落XML不写注解SQL() throws IOException {
|
||||
assertTrue(Files.exists(MAPPER), "查询必须走 JdProductMapper(铁律 1)");
|
||||
String mapperSource = stripComments(Files.readString(MAPPER));
|
||||
assertFalse(mapperSource.matches("(?s).*@(Select|Insert|Update|Delete)\\s*\\(.*"),
|
||||
"不得新写注解 SQL(铁律 1:查询 SQL 全部落 XML Mapper)");
|
||||
assertTrue(mapperSource.contains("@Mapper"), "mapper 接口必须带 @Mapper");
|
||||
Path xml = findJdProductMapperXml();
|
||||
assertNotNull(xml, "mapper XML 必须落在 src/main/resources/mapper/**(铁律 1)");
|
||||
String xmlText = stripComments(Files.readString(xml));
|
||||
assertTrue(xmlText.contains("countProducts"), "XML 必须有 countProducts(只读页总数)");
|
||||
assertTrue(xmlText.contains("selectProductPage"), "XML 必须有 selectProductPage(只读页分页)");
|
||||
assertFalse(xmlText.contains("LIMIT #{page}"),
|
||||
"分页只收服务钳制后的 offset/limit,绝不 LIMIT #{page} 直拼(铁律 15)");
|
||||
assertTrue(xmlText.contains("ORDER BY p.synced_at DESC, p.id DESC"),
|
||||
"排序必须是 synced_at 倒序(同外观专利只读页口径)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 分组控制器与同款鉴权闸就位() throws IOException {
|
||||
assertTrue(Files.exists(GROUP_CONTROLLER),
|
||||
"分组控制器必须落在 " + GROUP_CONTROLLER + "(AC-9:普通授权用户可读写分组)");
|
||||
String source = stripComments(Files.readString(GROUP_CONTROLLER));
|
||||
assertTrue(source.contains("/api/admin/jd-product-groups"), "分组端点必须挂在 /api/admin/jd-product-groups");
|
||||
for (String seam : List.of("requireUser", "X-Internal-Token", "isSuper",
|
||||
"getUserColumnPermissions", COLUMN_KEY, ROUTE_PATH, "requireJdAccess")) {
|
||||
assertTrue(source.contains(seam), "分组鉴权闸缺接缝 " + seam + "(与商品页同款)");
|
||||
}
|
||||
// 普通(非超管)授权用户可读写分组:菜单双匹配那道闸在两个控制器上逐字同源。
|
||||
for (String verb : List.of("@PostMapping", "@PutMapping", "@DeleteMapping")) {
|
||||
assertTrue(source.contains(verb), "分组 CRUD 必须有 " + verb + "(AC-9)");
|
||||
}
|
||||
}
|
||||
|
||||
// ===================== 行为样本 =====================
|
||||
|
||||
@Test
|
||||
void 控制器把公司参数交给readCompanyIdOrOwn并把结果装进200信封() {
|
||||
JdProductQueryService queryService = mock(JdProductQueryService.class);
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
PermissionMenuService permissionMenuService = mock(PermissionMenuService.class);
|
||||
DataReadScope dataReadScope = mock(DataReadScope.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, authSupport, permissionMenuService, 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=5 ⇒ 正门把 5 交出来(不传时正门给 null=全量,同一行为样本家族)。
|
||||
when(dataReadScope.readCompanyIdOrOwn(operator, 5L)).thenReturn(5L);
|
||||
JdProductPageVo pageVo = new JdProductPageVo();
|
||||
pageVo.setItems(List.of());
|
||||
pageVo.setTotal(0L);
|
||||
pageVo.setPage(1L);
|
||||
pageVo.setPageSize((long) PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT);
|
||||
when(queryService.page(1L, 20L, 5L, null, null, null, null, null, null, null)).thenReturn(pageVo);
|
||||
|
||||
ApiResponse<JdProductPageVo> response = controller.page(1L, 20L, null, null, null, null, null,
|
||||
null, null, 5L, request);
|
||||
|
||||
verify(dataReadScope).readCompanyIdOrOwn(operator, 5L);
|
||||
verify(queryService).page(1L, 20L, 5L, null, null, null, null, null, null, null);
|
||||
assertEquals(Boolean.TRUE, response.isSuccess(), "拒绝走 403 业务码,放行必须是 success 信封(B80-03)");
|
||||
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, 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 参数:超管不传=全量可见;非超管由正门强制本公司(服务按返回值收口)。
|
||||
when(dataReadScope.readCompanyIdOrOwn(operator, null)).thenReturn(null);
|
||||
JdProductDetailVo detailVo = new JdProductDetailVo();
|
||||
detailVo.setId(11L);
|
||||
when(queryService.detail(11L, null)).thenReturn(detailVo);
|
||||
|
||||
ApiResponse<JdProductDetailVo> response = controller.detail(11L, request);
|
||||
|
||||
verify(dataReadScope).readCompanyIdOrOwn(operator, null);
|
||||
verify(queryService).detail(11L, null);
|
||||
assertEquals(Boolean.TRUE, response.isSuccess());
|
||||
assertEquals(detailVo, response.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 控制器导出走同一正门并回Excel响应() {
|
||||
JdProductQueryService queryService = mock(JdProductQueryService.class);
|
||||
AdminAuthSupport authSupport = mock(AdminAuthSupport.class);
|
||||
DataReadScope dataReadScope = mock(DataReadScope.class);
|
||||
AdminJdProductController controller = new AdminJdProductController(
|
||||
queryService, 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);
|
||||
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(1L);
|
||||
|
||||
// page=0 / size=null ⇒ 归一为第 1 页、默认 20 条;offset = 0。
|
||||
service.page(0L, null, null, null, null, null, null, null, null, null);
|
||||
ArgumentCaptor<Long> offset = ArgumentCaptor.forClass(Long.class);
|
||||
ArgumentCaptor<Integer> limit = ArgumentCaptor.forClass(Integer.class);
|
||||
verify(mapper).selectProductPage(isNull(), isNull(), isNull(), isNull(), isNull(), isNull(),
|
||||
isNull(), isNull(), offset.capture(), limit.capture());
|
||||
assertEquals(0L, offset.getValue());
|
||||
assertEquals(PageDefaults.JD_PRODUCTS_PAGE_SIZE_INT, limit.getValue());
|
||||
|
||||
// page=Long.MAX_VALUE / size=500 ⇒ 钳到 10_000 页、100 条;乘积 999_900 不得变负(铁律 15)。
|
||||
service.page(Long.MAX_VALUE, 500L, null, null, null, null, null, null, null, null);
|
||||
ArgumentCaptor<Long> offsetMax = ArgumentCaptor.forClass(Long.class);
|
||||
ArgumentCaptor<Integer> limitMax = ArgumentCaptor.forClass(Integer.class);
|
||||
verify(mapper, times(2)).selectProductPage(any(), any(), any(), any(), any(), any(),
|
||||
any(), any(), offsetMax.capture(), limitMax.capture());
|
||||
assertEquals(999_900L, offsetMax.getValue(), "(page-1)*page_size 最大 999_900,钳制后不得溢出变负");
|
||||
assertEquals(100, limitMax.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 查询服务同步起止倒挂拒绝() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
|
||||
LocalDateTime from = LocalDateTime.of(2026, 11, 14, 12, 0);
|
||||
LocalDateTime to = LocalDateTime.of(2026, 11, 14, 8, 0);
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.page(1L, 20L, null, null, null, null, null, null, from, to));
|
||||
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
|
||||
void 查询服务过滤面原样直传且空串归null() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
|
||||
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());
|
||||
// 全量(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());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 详情解析attr_json的tabs映射成attrGroups() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductImageMapper imageMapper = mock(JdProductImageMapper.class);
|
||||
JdProductQueryService service = new JdProductQueryService(mapper, imageMapper,
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
|
||||
|
||||
JdProductEntity row = productEntity(11L);
|
||||
// 客户端实拍契约:tabs[{tab, attrs[{key,label,value}]}];value 允许数字型 JSON 值(转字符串)。
|
||||
row.setAttrJson("{\"tabs\":["
|
||||
+ "{\"tab\":\"商品基本信息\",\"attrs\":["
|
||||
+ "{\"key\":\"款号\",\"label\":\"货号\",\"value\":\"A123\"},"
|
||||
+ "{\"key\":\"颜色\",\"label\":\"颜色分类\",\"value\":3}]},"
|
||||
+ "{\"tab\":\"规格\",\"attrs\":[{\"key\":\"size\",\"label\":\"尺码\",\"value\":\"M\"}]}],"
|
||||
+ "\"images\":[],\"productCode\":\"A123\"}");
|
||||
when(mapper.selectById(11L)).thenReturn(row);
|
||||
JdProductImageEntity image = new JdProductImageEntity();
|
||||
image.setProductId(11L);
|
||||
image.setImageType("MAIN");
|
||||
image.setSortNo(0);
|
||||
image.setUrl("https://oss.aishufu.top/jd-products/a.jpg");
|
||||
image.setByteSize(1024L);
|
||||
image.setWidth(800);
|
||||
image.setHeight(800);
|
||||
when(imageMapper.selectList(any())).thenReturn(List.of(image));
|
||||
|
||||
JdProductDetailVo vo = service.detail(11L, 5L);
|
||||
|
||||
assertEquals(2, vo.getAttrGroups().size(), "tabs 数组逐个映射成 attrGroups(groupName=tab 字段)");
|
||||
assertEquals("商品基本信息", vo.getAttrGroups().get(0).getGroupName());
|
||||
assertEquals(2, vo.getAttrGroups().get(0).getAttrs().size());
|
||||
assertEquals("货号", vo.getAttrGroups().get(0).getAttrs().get(0).getLabel());
|
||||
assertEquals("A123", vo.getAttrGroups().get(0).getAttrs().get(0).getValue());
|
||||
assertEquals("3", vo.getAttrGroups().get(0).getAttrs().get(1).getValue(),
|
||||
"数字型 value 转字符串下发(读侧不丢键)");
|
||||
assertEquals("规格", vo.getAttrGroups().get(1).getGroupName());
|
||||
assertNull(vo.getAttrJsonRaw(), "契约形状解析成功时不带原文兜底(原文只在不识别时透出)");
|
||||
assertEquals(1, vo.getImages().size());
|
||||
assertEquals("MAIN", vo.getImages().get(0).getImageType());
|
||||
assertEquals("https://oss.aishufu.top/jd-products/a.jpg", vo.getImages().get(0).getUrl(),
|
||||
"图片 URL 100% 自有域名(AC-4)");
|
||||
// 主表高频列直接从 entity 映射,不走 JSON。
|
||||
assertEquals("A123", vo.getProductCode());
|
||||
assertEquals(new BigDecimal("129.00"), vo.getPriceMin());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 详情非法attr_json落空分组且原文兜底不抛500() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
|
||||
|
||||
// 形状一:非法 JSON(解析异常)。
|
||||
JdProductEntity broken = productEntity(11L);
|
||||
broken.setAttrJson("{\"tabs\": [broken");
|
||||
when(mapper.selectById(11L)).thenReturn(broken);
|
||||
JdProductDetailVo brokenVo = service.detail(11L, 5L);
|
||||
assertTrue(brokenVo.getAttrGroups().isEmpty(), "解析异常落空 attrGroups,不抛 500(AC-7)");
|
||||
assertEquals("{\"tabs\": [broken", brokenVo.getAttrJsonRaw(), "原文进 attrJsonRaw 兜底展示");
|
||||
|
||||
// 形状二:合法 JSON 但没有 tabs(形状不识别)。
|
||||
JdProductEntity foreign = productEntity(11L);
|
||||
foreign.setAttrJson("{\"attrs\":[{\"key\":\"x\",\"value\":\"y\"}]}");
|
||||
when(mapper.selectById(11L)).thenReturn(foreign);
|
||||
JdProductDetailVo foreignVo = service.detail(11L, 5L);
|
||||
assertTrue(foreignVo.getAttrGroups().isEmpty(), "非契约形状落空 attrGroups");
|
||||
assertEquals("{\"attrs\":[{\"key\":\"x\",\"value\":\"y\"}]}", foreignVo.getAttrJsonRaw(),
|
||||
"未识别属性不丢弃:原文兜底透出(AC-7)");
|
||||
|
||||
// 形状三:attr_json 为空 ⇒ 空分组、无原文兜底。
|
||||
JdProductEntity blank = productEntity(11L);
|
||||
blank.setAttrJson(null);
|
||||
when(mapper.selectById(11L)).thenReturn(blank);
|
||||
JdProductDetailVo blankVo = service.detail(11L, 5L);
|
||||
assertTrue(blankVo.getAttrGroups().isEmpty());
|
||||
assertNull(blankVo.getAttrJsonRaw());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 详情公司越界按不存在拒绝() {
|
||||
JdProductMapper mapper = mock(JdProductMapper.class);
|
||||
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class));
|
||||
|
||||
JdProductEntity row = productEntity(11L);
|
||||
row.setCompanyId(6L);
|
||||
when(mapper.selectById(11L)).thenReturn(row);
|
||||
|
||||
// 非超管(scopedCompanyId=5):行属公司 6 ⇒ 按「商品不存在」拒绝,不泄露跨公司行的存在性。
|
||||
BusinessException ex = assertThrows(BusinessException.class, () -> service.detail(11L, 5L));
|
||||
assertTrue(ex.getMessage().contains("商品不存在"));
|
||||
// 超管不选公司(null)=全量可见。
|
||||
assertNotNull(service.detail(11L, null));
|
||||
// id 非法同样拒绝。
|
||||
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.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);
|
||||
com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper groupMapper =
|
||||
mock(com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper.class);
|
||||
JdProductQueryService service = new JdProductQueryService(mapper, mock(JdProductImageMapper.class),
|
||||
groupMapper);
|
||||
|
||||
JdProductEntity grouped = productEntity(1L);
|
||||
grouped.setGroupId(3L);
|
||||
JdProductEntity missing = productEntity(2L);
|
||||
missing.setGroupId(99L);
|
||||
when(mapper.countProducts(any(), any(), any(), any(), any(), any(), any(), any())).thenReturn(2L);
|
||||
when(mapper.selectProductPage(any(), any(), any(), any(), any(), any(), any(), any(),
|
||||
anyLong(), anyInt())).thenReturn(List.of(grouped, missing));
|
||||
com.nanri.aiimage.modules.jd.model.entity.JdProductGroupEntity group =
|
||||
new com.nanri.aiimage.modules.jd.model.entity.JdProductGroupEntity();
|
||||
group.setId(3L);
|
||||
group.setName("秋冬款");
|
||||
when(groupMapper.selectBatchIds(any())).thenReturn(List.of(group));
|
||||
|
||||
JdProductPageVo vo = service.page(1L, 20L, 5L, null, null, null, null, null, null, null);
|
||||
|
||||
assertEquals("秋冬款", vo.getItems().get(0).getGroupName(), "行集 group_id 一次取齐分组名(无逐行查询)");
|
||||
assertNull(vo.getItems().get(1).getGroupName(), "分组已删=不猜名字(铁律 30),前端展示 -");
|
||||
verify(groupMapper, times(1)).selectBatchIds(any());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 助手
|
||||
|
||||
/** 列表/详情用例共用的商品行夹具:归属公司 5,主字段齐备便于按字段对撞。 */
|
||||
private static JdProductEntity productEntity(long id) {
|
||||
JdProductEntity entity = new JdProductEntity();
|
||||
entity.setId(id);
|
||||
entity.setCompanyId(5L);
|
||||
entity.setShopId("shop-1");
|
||||
entity.setShopName("测试店铺");
|
||||
entity.setProductCode("A123");
|
||||
entity.setSkuId("SKU-1");
|
||||
entity.setTitle("测试连衣裙");
|
||||
entity.setBrand("Anker");
|
||||
entity.setCategoryPath("女装 > 连衣裙 > 碎花裙");
|
||||
entity.setPriceMin(new BigDecimal("129.00"));
|
||||
entity.setPriceMax(new BigDecimal("159.00"));
|
||||
entity.setStockTotal(300);
|
||||
entity.setSaleStatus("上架中");
|
||||
entity.setListTab("ONSALE");
|
||||
entity.setAttrCount(12);
|
||||
entity.setImageCount(1);
|
||||
entity.setSyncedAt(LocalDateTime.of(2026, 11, 14, 10, 30));
|
||||
return entity;
|
||||
}
|
||||
|
||||
private static Path findJdProductMapperXml() throws IOException {
|
||||
Path resources = MODULE.resolve("src/main/resources/mapper");
|
||||
if (!Files.exists(resources)) {
|
||||
return null;
|
||||
}
|
||||
try (Stream<Path> files = Files.walk(resources)) {
|
||||
return files.filter(path -> path.getFileName().toString()
|
||||
.equals("JdProductMapper.xml")).findFirst().orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
private static String stripComments(String source) {
|
||||
String noBlock = source.replaceAll("(?s)/\\*.*?\\*/", "");
|
||||
StringBuilder out = new StringBuilder();
|
||||
for (String line : noBlock.split("\n", -1)) {
|
||||
int idx = line.indexOf("//");
|
||||
out.append(idx >= 0 ? line.substring(0, idx) : line).append('\n');
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
+244
@@ -0,0 +1,244 @@
|
||||
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.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.jd.mapper.JdProductGroupMapper;
|
||||
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.JdProductGroupEntity;
|
||||
import com.nanri.aiimage.modules.jd.model.vo.JdProductGroupVo;
|
||||
import com.nanri.aiimage.testutil.UpdateWrapperProbe;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* v1.4 Phase 5(R4/AC-8/AC-9):京东商品自定义分组业务规则。
|
||||
*
|
||||
* <p>判据:同公司重名拒绝不落库(409)、删除先把商品 group_id 置 NULL 再删分组行
|
||||
* (事务两步,AC-8)、越公司 id 操作按不存在拒绝(不泄露存在性)、productCount
|
||||
* 读侧现算统计(分组下拉展示用)。</p>
|
||||
*/
|
||||
class JdProductGroupServiceTest {
|
||||
|
||||
private final JdProductGroupMapper groupMapper = mock(JdProductGroupMapper.class);
|
||||
private final JdProductMapper productMapper = mock(JdProductMapper.class);
|
||||
private final JdProductGroupService service = new JdProductGroupService(groupMapper, productMapper);
|
||||
|
||||
@BeforeEach
|
||||
void initTableInfo() {
|
||||
// 探针要读 LambdaUpdateWrapper 的 SET/WHERE 段:裸单测 JVM 里 lambda 列缓存不初始化就解析不了,
|
||||
// AccountExpiryServiceTest 同款(无 GlobalConfig 下 underCamel=false,列名归一由探针兜)。
|
||||
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||
JdProductEntity.class);
|
||||
}
|
||||
|
||||
// ===================== 新建 =====================
|
||||
|
||||
@Test
|
||||
void 新建同公司重名拒绝且不落库() {
|
||||
when(groupMapper.selectCount(any())).thenReturn(1L);
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.create(5L, " 秋冬款 ", 2));
|
||||
assertEquals(409, ex.getCode(), "重名冲突=409(语义正确的资源冲突码,仓内 DeviceLogService 有先例)");
|
||||
assertTrue(ex.getMessage().contains("同名分组"), "重名必须给可读文案,不拿 DB 唯一键异常撞 500");
|
||||
verify(groupMapper, never()).insert(any(JdProductGroupEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 新建成功落公司归属与归一名() {
|
||||
when(groupMapper.selectCount(any())).thenReturn(0L);
|
||||
when(groupMapper.insert(any(JdProductGroupEntity.class))).thenAnswer(invocation -> {
|
||||
JdProductGroupEntity group = invocation.getArgument(0);
|
||||
group.setId(9L);
|
||||
return 1;
|
||||
});
|
||||
|
||||
Long id = service.create(5L, " 秋冬款 ", 2);
|
||||
|
||||
assertEquals(9L, id);
|
||||
ArgumentCaptor<JdProductGroupEntity> captor = ArgumentCaptor.forClass(JdProductGroupEntity.class);
|
||||
verify(groupMapper).insert(captor.capture());
|
||||
JdProductGroupEntity saved = captor.getValue();
|
||||
assertEquals(5L, saved.getCompanyId(), "归属公司来自隔离正门的 scopedCompanyId,不是请求体");
|
||||
assertEquals("秋冬款", saved.getName(), "名字 trim 后落库");
|
||||
assertEquals(2, saved.getSortOrder());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 新建缺公司或空名直接拒绝() {
|
||||
// 超管没选公司(scopedCompanyId=null)=分组没有归属公司,直接拒绝。
|
||||
BusinessException noCompany = assertThrows(BusinessException.class,
|
||||
() -> service.create(null, "秋冬款", null));
|
||||
assertEquals(400, noCompany.getCode());
|
||||
BusinessException blankName = assertThrows(BusinessException.class,
|
||||
() -> service.create(5L, " ", null));
|
||||
assertEquals(400, blankName.getCode());
|
||||
verifyNoInteractions(groupMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 新建排序缺省归零() {
|
||||
when(groupMapper.selectCount(any())).thenReturn(0L);
|
||||
|
||||
service.create(5L, "默认排序", null);
|
||||
|
||||
ArgumentCaptor<JdProductGroupEntity> captor = ArgumentCaptor.forClass(JdProductGroupEntity.class);
|
||||
verify(groupMapper).insert(captor.capture());
|
||||
assertEquals(0, captor.getValue().getSortOrder(), "sortOrder 缺省 0(下拉按 sort_order, id 升序)");
|
||||
}
|
||||
|
||||
// ===================== 改名 =====================
|
||||
|
||||
@Test
|
||||
void 改名越公司id按不存在拒绝() {
|
||||
JdProductGroupEntity group = groupEntity(1L, 6L, "别家的组");
|
||||
when(groupMapper.selectById(1L)).thenReturn(group);
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.rename(1L, 5L, "新名字", null));
|
||||
assertTrue(ex.getMessage().contains("分组不存在"),
|
||||
"越界 id 与 id 不存在是同一种响应,不泄露跨公司行的存在性");
|
||||
verify(groupMapper, never()).updateById(any(JdProductGroupEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 改名重名拒绝且不落库() {
|
||||
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, 5L, "旧名"));
|
||||
when(groupMapper.selectCount(any())).thenReturn(1L);
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class,
|
||||
() -> service.rename(1L, 5L, "已有名", 3));
|
||||
assertEquals(409, ex.getCode());
|
||||
verify(groupMapper, never()).updateById(any(JdProductGroupEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 改名成功带归一名与排序() {
|
||||
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, 5L, "旧名"));
|
||||
when(groupMapper.selectCount(any())).thenReturn(0L);
|
||||
|
||||
service.rename(1L, 5L, " 新名 ", 7);
|
||||
|
||||
ArgumentCaptor<JdProductGroupEntity> captor = ArgumentCaptor.forClass(JdProductGroupEntity.class);
|
||||
verify(groupMapper).updateById(captor.capture());
|
||||
assertEquals("新名", captor.getValue().getName());
|
||||
assertEquals(7, captor.getValue().getSortOrder());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 改名排序不传保持原值() {
|
||||
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, 5L, "旧名"));
|
||||
when(groupMapper.selectCount(any())).thenReturn(0L);
|
||||
|
||||
service.rename(1L, 5L, "只改名", null);
|
||||
|
||||
ArgumentCaptor<JdProductGroupEntity> captor = ArgumentCaptor.forClass(JdProductGroupEntity.class);
|
||||
verify(groupMapper).updateById(captor.capture());
|
||||
assertEquals(3, captor.getValue().getSortOrder(), "sortOrder 不传=保持原值(改名不改排序)");
|
||||
}
|
||||
|
||||
// ===================== 删除(AC-8 事务两步) =====================
|
||||
|
||||
@Test
|
||||
void 删除先把商品组置NULL再删分组行() {
|
||||
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, 5L, "秋冬款"));
|
||||
when(productMapper.update(any(), any(Wrapper.class))).thenReturn(3);
|
||||
|
||||
int detached = service.delete(1L, 5L);
|
||||
|
||||
assertEquals(3, detached, "返回实际脱组的商品行数(前端拿它做反馈,不猜)");
|
||||
ArgumentCaptor<Wrapper<JdProductEntity>> wrapperCaptor = ArgumentCaptor.forClass(Wrapper.class);
|
||||
verify(productMapper).update(any(), wrapperCaptor.capture());
|
||||
// 置 NULL 语义必须落在 SET group_id=NULL(商品保留,脱组即「未分组」),且限定本公司该组。
|
||||
var setValues = UpdateWrapperProbe.setValues(wrapperCaptor.getValue());
|
||||
assertTrue(setValues.containsKey("group_id") && setValues.get("group_id") == null,
|
||||
"删除分组必须把商品 group_id 置 NULL(SET group_id=NULL),不是删商品行:实得 " + setValues);
|
||||
var whereEq = UpdateWrapperProbe.whereEq(wrapperCaptor.getValue());
|
||||
assertEquals(5L, ((Number) whereEq.get("company_id")).longValue(), "脱组必须限定本公司");
|
||||
assertEquals(1L, ((Number) whereEq.get("group_id")).longValue(), "脱组必须限定该组");
|
||||
verify(groupMapper).deleteById(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 删除越公司id拒绝且一个字节都不写() {
|
||||
when(groupMapper.selectById(1L)).thenReturn(groupEntity(1L, 6L, "别家的组"));
|
||||
|
||||
assertThrows(BusinessException.class, () -> service.delete(1L, 5L));
|
||||
verifyNoInteractions(productMapper);
|
||||
verify(groupMapper, never()).deleteById(anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 删除分组不存在直接拒绝() {
|
||||
when(groupMapper.selectById(404L)).thenReturn(null);
|
||||
|
||||
assertThrows(BusinessException.class, () -> service.delete(404L, null));
|
||||
verifyNoInteractions(productMapper);
|
||||
}
|
||||
|
||||
// ===================== 列表(productCount 统计) =====================
|
||||
|
||||
@Test
|
||||
void 列表按排序返回且productCount现算() {
|
||||
JdProductGroupEntity first = groupEntity(2L, 5L, "晚出厂");
|
||||
first.setSortOrder(0);
|
||||
JdProductGroupEntity second = groupEntity(1L, 5L, "早出厂");
|
||||
second.setSortOrder(1);
|
||||
when(groupMapper.selectList(any())).thenReturn(List.of(first, second));
|
||||
when(productMapper.selectCount(any())).thenReturn(12L, 0L);
|
||||
|
||||
List<JdProductGroupVo> items = service.list(5L);
|
||||
|
||||
assertEquals(2, items.size());
|
||||
assertEquals("晚出厂", items.get(0).getName(), "selectList 的排序由 wrapper 决定,服务不重排");
|
||||
assertEquals(12L, items.get(0).getProductCount(), "productCount 用 JdProductMapper selectCount 现算");
|
||||
assertEquals(0L, items.get(1).getProductCount(), "空组也是 0,不是 null(下拉展示口径)");
|
||||
assertEquals(1, items.get(1).getSortOrder());
|
||||
verify(productMapper, times(2)).selectCount(any());
|
||||
assertNotNull(items.get(0).getCreatedAt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 列表超管不选公司查全量() {
|
||||
when(groupMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
List<JdProductGroupVo> items = service.list(null);
|
||||
|
||||
assertTrue(items.isEmpty());
|
||||
verify(groupMapper).selectList(any());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 助手
|
||||
|
||||
private static JdProductGroupEntity groupEntity(long id, long companyId, String name) {
|
||||
JdProductGroupEntity group = new JdProductGroupEntity();
|
||||
group.setId(id);
|
||||
group.setCompanyId(companyId);
|
||||
group.setName(name);
|
||||
group.setSortOrder(3);
|
||||
group.setCreatedAt(LocalDateTime.of(2026, 11, 14, 10, 0));
|
||||
return group;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user