feat(phase-5): 后管京东数据库接口与页面+用户端采集入口——v1.4 Phase 5

This commit is contained in:
2026-11-14 12:47:08 +08:00
parent 6ce995f19e
commit ff859d25bc
42 changed files with 4839 additions and 37 deletions
@@ -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";
@@ -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);
}
}
@@ -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);
}
}
@@ -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);
}
@@ -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;
}
@@ -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;
}
}
@@ -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;
}
@@ -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 &gt; B &gt; 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;
}
@@ -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);
}
}
}
@@ -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 &gt; 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 &gt; 0">AND p.group_id = #{groupId}</if>
<if test="syncedFrom != null">AND p.synced_at &gt;= #{syncedFrom}</if>
<if test="syncedTo != null">AND p.synced_at &lt;= #{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>