ec_order: 去掉 product_id,改为冗余 spec_code 字段

- 迁移SQL: V20260603__ec_order_drop_product_id_add_spec_code.sql
- Order 实体/DTO/VO: product_id → spec_code,字段联路调整
- OrderImportProcessor / OrderServiceImpl: 适配新字段
- 各 Overview Service: 更新订单关联逻辑
- 前端: OrderFormModal / index / types 同步适配
This commit is contained in:
小蟑螂
2026-05-03 23:06:20 +08:00
parent 8adffcc7e9
commit 63e54279c0
16 changed files with 230 additions and 105 deletions
@@ -939,7 +939,7 @@ public class OrderImportProcessor {
String specCode = dto.getPlatformOrderNo() != null ? null : null; // DTO中无specCode,需从record重新获取
// 重新从 CSVRecord 获取 specCode(Worker 不能直接访问 CSVRecordParser)
// 方案:从 DTO 中取,或者在 parseOrderFromCSV 时预存
// 由于 parseOrderFromCSV 已填充了 productId/specId,此处只处理找不到的情况
// 由于 parseOrderFromCSV 已填充了 productSpecId 等,此处只处理找不到的情况
if (dto.getProductSpecId() == null) {
// 重新获取关键字段
@@ -1008,8 +1008,9 @@ public class OrderImportProcessor {
throw new BusinessException("无法创建商品规格");
}
dto.setProductId(productSpec.getProductId());
dto.setShopRelationProductId(productSpec.getProductId());
dto.setProductSpecId(productSpec.getId());
dto.setSpecCode(productSpec.getSpecCode());
dto.setConfirm(isNewSpecCreated ? 0 : 1);
// linkProductName / linkSpecName fallback
@@ -1227,8 +1228,8 @@ public class OrderImportProcessor {
|| fieldMapping.containsKey("link_product_name")
|| fieldMapping.containsKey("link_spec_name");
if (compareProductBlock) {
if (!Objects.equals(db.getProductId(), incoming.getProductId())) return false;
if (!Objects.equals(db.getProductSpecId(), incoming.getProductSpecId())) return false;
if (!normStrEq(db.getSpecCode(), incoming.getSpecCode())) return false;
if (!normStrEq(db.getLinkProductName(), incoming.getLinkProductName())) return false;
if (!normStrEq(db.getLinkSpecName(), incoming.getLinkSpecName())) return false;
}
@@ -1296,8 +1297,8 @@ public class OrderImportProcessor {
c.setShopId(o.getShopId());
c.setPlatformOrderNo(o.getPlatformOrderNo());
c.setStatusId(o.getStatusId());
c.setProductId(o.getProductId());
c.setProductSpecId(o.getProductSpecId());
c.setSpecCode(o.getSpecCode());
c.setLinkProductName(o.getLinkProductName());
c.setLinkSpecName(o.getLinkSpecName());
c.setQuantity(o.getQuantity());
@@ -1590,8 +1591,8 @@ public class OrderImportProcessor {
order.setShopId(dto.getShopId());
order.setPlatformOrderNo(dto.getPlatformOrderNo());
order.setStatusId(dto.getStatusId());
order.setProductId(dto.getProductId());
order.setProductSpecId(dto.getProductSpecId());
order.setSpecCode(dto.getSpecCode());
order.setLinkProductName(dto.getLinkProductName());
order.setLinkSpecName(dto.getLinkSpecName());
order.setQuantity(dto.getQuantity());
@@ -1653,7 +1654,11 @@ public class OrderImportProcessor {
* 入队店铺-商品关联(主线程调用)
*/
private void queueShopProductRelation(CreateOrderDTO orderDTO, ImportRuntimeContext context) {
String relationKey = context.buildRelationKey(orderDTO.getShopId(), orderDTO.getProductId());
Long relationPid = orderDTO.getShopRelationProductId();
if (relationPid == null) {
return;
}
String relationKey = context.buildRelationKey(orderDTO.getShopId(), relationPid);
if (context.existingRelationKeys.contains(relationKey)
|| context.pendingRelationKeys.contains(relationKey)) {
return;
@@ -1661,7 +1666,7 @@ public class OrderImportProcessor {
ShopProductRelationPO relation = new ShopProductRelationPO();
relation.setCompanyId(context.getCompanyId());
relation.setShopId(orderDTO.getShopId());
relation.setProductId(orderDTO.getProductId());
relation.setProductId(relationPid);
relation.setSortOrder(0);
relation.setCreateBy(context.getCreateBy());
context.pendingRelations.add(relation);
@@ -1,5 +1,6 @@
package top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
@@ -59,17 +60,24 @@ public class CreateOrderDTO {
private Boolean orderTimeEmptyInInput;
/**
* 商品ID
* 商品规格ID(下单必选,商品由规格关联)
*/
@Schema(description = "商品ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "商品ID不能为空")
private Long productId;
@Schema(description = "商品规格ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "商品规格ID不能为空")
private Long productSpecId;
/**
* 商品规格ID
* 规格编码(可选;未传时服务端按规格ID补全写入订单)
*/
@Schema(description = "商品规格ID", example = "1")
private Long productSpecId;
@Schema(description = "规格编码", example = "SKU001")
private String specCode;
/**
* 导入流程用:店铺-商品关联所需商品ID,不入库,不对外序列化。
*/
@JsonIgnore
@Schema(hidden = true)
private Long shopRelationProductId;
/**
* 链接商品名称(冗余存储)
@@ -47,18 +47,18 @@ public class UpdateOrderDTO {
@NotNull(message = "下单时间不能为空")
private LocalDateTime orderTime;
/**
* 商品ID
*/
@Schema(description = "商品ID", example = "1")
private Long productId;
/**
* 商品规格ID
*/
@Schema(description = "商品规格ID", example = "1")
private Long productSpecId;
/**
* 规格编码
*/
@Schema(description = "规格编码", example = "SKU001")
private String specCode;
/**
* 链接商品名称(冗余存储)
*/
@@ -45,18 +45,18 @@ public class OrderPO {
@TableField("status_id")
private Long statusId;
/**
* 商品ID
*/
@TableField("product_id")
private Long productId;
/**
* 商品规格ID
*/
@TableField("product_spec_id")
private Long productSpecId;
/**
* 规格编码(冗余,与 ec_product_spec.spec_code 一致)
*/
@TableField("spec_code")
private String specCode;
/**
* 链接商品名称(冗余存储)
*/
@@ -95,18 +95,18 @@ public class OrderVO {
@Schema(description = "更新人ID", example = "1")
private Long updateBy;
/**
* 商品ID
*/
@Schema(description = "商品ID", example = "1")
private Long productId;
/**
* 商品规格ID
*/
@Schema(description = "商品规格ID", example = "1")
private Long productSpecId;
/**
* 规格编码
*/
@Schema(description = "规格编码", example = "SKU001")
private String specCode;
/**
* 链接商品名称(冗余存储)
*/
@@ -78,22 +78,15 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderPO> implemen
throw new BusinessException("店铺不存在");
}
// 验证商品是否存在且属于当前公司
ProductPO product = productService.getById(createOrderDTO.getProductId());
ProductSpecPO productSpec = productSpecService.getById(createOrderDTO.getProductSpecId());
if (productSpec == null || !productSpec.getCompanyId().equals(companyId)) {
throw new BusinessException("商品规格不存在");
}
ProductPO product = productService.getById(productSpec.getProductId());
if (product == null || !product.getCompanyId().equals(companyId)) {
throw new BusinessException("商品不存在");
}
// 验证商品规格是否存在且属于当前公司(如果提供了规格ID)
ProductSpecPO productSpec = null;
if (createOrderDTO.getProductSpecId() != null) {
productSpec = productSpecService.getById(createOrderDTO.getProductSpecId());
if (productSpec == null || !productSpec.getCompanyId().equals(companyId) ||
!productSpec.getProductId().equals(createOrderDTO.getProductId())) {
throw new BusinessException("商品规格不存在或不属于该商品");
}
}
// 验证订单状态是否存在且属于当前公司
OrderStatusPO orderStatus = orderStatusService.getById(createOrderDTO.getStatusId());
if (orderStatus == null || !orderStatus.getCompanyId().equals(companyId)) {
@@ -116,8 +109,10 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderPO> implemen
order.setShopId(createOrderDTO.getShopId());
order.setPlatformOrderNo(createOrderDTO.getPlatformOrderNo());
order.setStatusId(createOrderDTO.getStatusId());
order.setProductId(createOrderDTO.getProductId());
order.setProductSpecId(createOrderDTO.getProductSpecId());
String specCodeIn = createOrderDTO.getSpecCode();
order.setSpecCode(specCodeIn != null && !specCodeIn.trim().isEmpty()
? specCodeIn.trim() : productSpec.getSpecCode());
order.setLinkProductName(createOrderDTO.getLinkProductName());
order.setLinkSpecName(createOrderDTO.getLinkSpecName());
order.setQuantity(createOrderDTO.getQuantity());
@@ -147,17 +142,14 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderPO> implemen
if (shop == null || !shop.getCompanyId().equals(companyId)) {
throw new BusinessException("店铺不存在");
}
ProductPO product = productService.getById(createOrderDTO.getProductId());
ProductSpecPO productSpec = productSpecService.getById(createOrderDTO.getProductSpecId());
if (productSpec == null || !productSpec.getCompanyId().equals(companyId)) {
throw new BusinessException("商品规格不存在");
}
ProductPO product = productService.getById(productSpec.getProductId());
if (product == null || !product.getCompanyId().equals(companyId)) {
throw new BusinessException("商品不存在");
}
if (createOrderDTO.getProductSpecId() != null) {
ProductSpecPO productSpec = productSpecService.getById(createOrderDTO.getProductSpecId());
if (productSpec == null || !productSpec.getCompanyId().equals(companyId)
|| !productSpec.getProductId().equals(createOrderDTO.getProductId())) {
throw new BusinessException("商品规格不存在或不属于该商品");
}
}
OrderStatusPO orderStatus = orderStatusService.getById(createOrderDTO.getStatusId());
if (orderStatus == null || !orderStatus.getCompanyId().equals(companyId)) {
throw new BusinessException("订单状态不存在");
@@ -175,8 +167,10 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderPO> implemen
order.setShopId(createOrderDTO.getShopId());
order.setPlatformOrderNo(createOrderDTO.getPlatformOrderNo());
order.setStatusId(createOrderDTO.getStatusId());
order.setProductId(createOrderDTO.getProductId());
order.setProductSpecId(createOrderDTO.getProductSpecId());
String specCodeImp = createOrderDTO.getSpecCode();
order.setSpecCode(specCodeImp != null && !specCodeImp.trim().isEmpty()
? specCodeImp.trim() : productSpec.getSpecCode());
order.setLinkProductName(createOrderDTO.getLinkProductName());
order.setLinkSpecName(createOrderDTO.getLinkSpecName());
order.setQuantity(createOrderDTO.getQuantity());
@@ -313,7 +307,9 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderPO> implemen
wrapper.eq(OrderPO::getConfirm, filter.getConfirm());
}
if (filter.getProductId() != null) {
wrapper.eq(OrderPO::getProductId, filter.getProductId());
wrapper.apply(
"EXISTS (SELECT 1 FROM ec_product_spec s WHERE s.id = ec_order.product_spec_id AND s.product_id = {0})",
filter.getProductId());
}
}
@@ -433,23 +429,21 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderPO> implemen
orderVO.setShop(shopVO);
}
// 设置关联商品信息
if (order.getProductId() != null) {
ProductPO product = productService.getById(order.getProductId());
if (product != null) {
ProductVO productVO = new ProductVO();
BeanUtils.copyProperties(product, productVO);
orderVO.setProduct(productVO);
}
}
// 设置关联商品规格信息
// 设置关联商品规格与商品信息(商品经规格关联)
if (order.getProductSpecId() != null) {
ProductSpecPO productSpec = productSpecService.getById(order.getProductSpecId());
if (productSpec != null) {
ProductSpecVO productSpecVO = new ProductSpecVO();
BeanUtils.copyProperties(productSpec, productSpecVO);
orderVO.setProductSpec(productSpecVO);
if (productSpec.getProductId() != null) {
ProductPO product = productService.getById(productSpec.getProductId());
if (product != null) {
ProductVO productVO = new ProductVO();
BeanUtils.copyProperties(product, productVO);
orderVO.setProduct(productVO);
}
}
}
}
@@ -15,7 +15,9 @@ import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.dto.CategoryExportDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.overview.model.vo.CompanyOverviewVO;
import top.xyquan.ecomdmbbackend.modules.ecom.overview.service.CategoryExportExcelService;
import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductSpecMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductService;
import top.xyquan.ecomdmbbackend.modules.ecom.promotion.mapper.PromotionFeeMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.promotion.model.po.PromotionFeePO;
@@ -41,6 +43,7 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic
private final PromotionFeeMapper promotionFeeMapper;
private final BrushOrderMapper brushOrderMapper;
private final ProductService productService;
private final ProductSpecMapper productSpecMapper;
private final CategoryService categoryService;
private final ExportStatusRangeHelper exportStatusRangeHelper;
@@ -50,6 +53,7 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic
PromotionFeeMapper promotionFeeMapper,
BrushOrderMapper brushOrderMapper,
ProductService productService,
ProductSpecMapper productSpecMapper,
CategoryService categoryService,
ExportStatusRangeHelper exportStatusRangeHelper) {
this.currentUserContext = currentUserContext;
@@ -57,6 +61,7 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic
this.promotionFeeMapper = promotionFeeMapper;
this.brushOrderMapper = brushOrderMapper;
this.productService = productService;
this.productSpecMapper = productSpecMapper;
this.categoryService = categoryService;
this.exportStatusRangeHelper = exportStatusRangeHelper;
}
@@ -97,11 +102,22 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic
.le(BrushOrderPO::getBrushTime, endDate.atTime(23, 59, 59));
List<BrushOrderPO> brushOrders = brushOrderMapper.selectList(brushWrapper);
List<Long> productIds = orders.stream()
.map(OrderPO::getProductId)
List<Long> specIds = orders.stream()
.map(OrderPO::getProductSpecId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
Map<Long, Long> specIdToProductId = new HashMap<>();
if (!specIds.isEmpty()) {
List<ProductSpecPO> specs = productSpecMapper.selectList(new LambdaQueryWrapper<ProductSpecPO>()
.in(ProductSpecPO::getId, specIds)
.select(ProductSpecPO::getId, ProductSpecPO::getProductId));
for (ProductSpecPO s : specs) {
specIdToProductId.put(s.getId(), s.getProductId());
}
}
List<Long> productIds = specIdToProductId.values().stream().filter(Objects::nonNull).distinct()
.collect(Collectors.toList());
Map<Long, Long> productCategoryMap = new HashMap<>();
if (!productIds.isEmpty()) {
LambdaQueryWrapper<ProductPO> productWrapper = new LambdaQueryWrapper<>();
@@ -114,7 +130,9 @@ public class CategoryExportExcelServiceImpl implements CategoryExportExcelServic
Map<Long, List<OrderPO>> ordersByCategory = orders.stream()
.collect(Collectors.groupingBy(order -> {
Long categoryId = productCategoryMap.get(order.getProductId());
Long sid = order.getProductSpecId();
Long pid = sid != null ? specIdToProductId.get(sid) : null;
Long categoryId = pid != null ? productCategoryMap.get(pid) : null;
return categoryId != null ? categoryId : -1L;
}));
@@ -23,7 +23,9 @@ import top.xyquan.ecomdmbbackend.modules.ecom.shop.service.ShopService;
import top.xyquan.ecomdmbbackend.modules.ecom.category.model.po.CategoryPO;
import top.xyquan.ecomdmbbackend.modules.ecom.category.service.CategoryService;
import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductSpecMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO;
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
import java.math.BigDecimal;
@@ -31,9 +33,11 @@ import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
@@ -53,6 +57,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
private final ShopService shopService;
private final OrderStatusService orderStatusService;
private final ProductMapper productMapper;
private final ProductSpecMapper productSpecMapper;
private final CategoryService categoryService;
private final PlatformMapper platformMapper;
@@ -64,6 +69,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
ShopService shopService,
OrderStatusService orderStatusService,
ProductMapper productMapper,
ProductSpecMapper productSpecMapper,
CategoryService categoryService,
PlatformMapper platformMapper) {
this.currentUserContext = currentUserContext;
@@ -74,6 +80,7 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
this.shopService = shopService;
this.orderStatusService = orderStatusService;
this.productMapper = productMapper;
this.productSpecMapper = productSpecMapper;
this.categoryService = categoryService;
this.platformMapper = platformMapper;
}
@@ -813,6 +820,25 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
return statusList.stream().map(OrderStatusPO::getId).collect(Collectors.toList());
}
/** 订单行上的 product_spec_id → 商品 id */
private Map<Long, Long> orderSpecIdToProductIdMap(List<OrderPO> orders) {
List<Long> specIds = orders.stream()
.map(OrderPO::getProductSpecId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
if (specIds.isEmpty()) {
return Collections.emptyMap();
}
List<ProductSpecPO> specs = productSpecMapper.selectList(new LambdaQueryWrapper<ProductSpecPO>()
.in(ProductSpecPO::getId, specIds)
.select(ProductSpecPO::getId, ProductSpecPO::getProductId));
Map<Long, Long> map = new java.util.HashMap<>();
for (ProductSpecPO s : specs) {
map.put(s.getId(), s.getProductId());
}
return map;
}
/**
* Calculate category comparison data
@@ -829,11 +855,8 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
Map<Long, String> categoryNameMap = allCategories.stream()
.collect(Collectors.toMap(CategoryPO::getId, CategoryPO::getName));
// Collect all product IDs involved and batch query their category info
List<Long> productIds = orders.stream()
.map(OrderPO::getProductId)
.filter(id -> id != null)
.distinct()
Map<Long, Long> specIdToProductId = orderSpecIdToProductIdMap(orders);
List<Long> productIds = specIdToProductId.values().stream().filter(Objects::nonNull).distinct()
.collect(Collectors.toList());
Map<Long, Long> productCategoryMap = new java.util.HashMap<>();
@@ -849,8 +872,11 @@ public class CompanyOverviewServiceImpl implements CompanyOverviewService {
// Group orders by category
Map<Long, List<OrderPO>> ordersByCategory = orders.stream()
.collect(Collectors.groupingBy(order -> {
Long productId = order.getProductId();
if (productId == null) return -1L;
Long specId = order.getProductSpecId();
Long productId = specId != null ? specIdToProductId.get(specId) : null;
if (productId == null) {
return -1L;
}
Long categoryId = productCategoryMap.get(productId);
return categoryId != null ? categoryId : -1L;
}));
@@ -13,7 +13,9 @@ import top.xyquan.ecomdmbbackend.modules.ecom.brushorder.model.po.BrushOrderPO;
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.model.po.OrderStatusPO;
import top.xyquan.ecomdmbbackend.modules.ecom.order.status.service.OrderStatusService;
import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductSpecMapper;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO;
import top.xyquan.ecomdmbbackend.modules.ecom.promotion.model.po.PromotionFeePO;
import top.xyquan.ecomdmbbackend.modules.ecom.shop.model.po.ShopPO;
import top.xyquan.ecomdmbbackend.modules.ecom.shop.monthlytarget.mapper.ShopMonthlyTargetMapper;
@@ -29,6 +31,7 @@ import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
@@ -52,6 +55,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
private final ShopMonthlyTargetMapper shopMonthlyTargetMapper;
private final ShopService shopService;
private final ProductService productService;
private final ProductSpecMapper productSpecMapper;
private final OrderStatusService orderStatusService;
private final CategoryService categoryService;
@@ -62,6 +66,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
ShopMonthlyTargetMapper shopMonthlyTargetMapper,
ShopService shopService,
ProductService productService,
ProductSpecMapper productSpecMapper,
OrderStatusService orderStatusService,
CategoryService categoryService) {
this.currentUserContext = currentUserContext;
@@ -71,6 +76,7 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
this.shopMonthlyTargetMapper = shopMonthlyTargetMapper;
this.shopService = shopService;
this.productService = productService;
this.productSpecMapper = productSpecMapper;
this.orderStatusService = orderStatusService;
this.categoryService = categoryService;
}
@@ -363,6 +369,25 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
return profit.divide(totalCost, 4, RoundingMode.HALF_UP);
}
private Map<Long, Long> orderSpecIdToProductIdMap(List<OrderPO> orders) {
List<Long> specIds = orders.stream()
.map(OrderPO::getProductSpecId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
if (specIds.isEmpty()) {
return Collections.emptyMap();
}
List<ProductSpecPO> specs = productSpecMapper.selectList(new LambdaQueryWrapper<ProductSpecPO>()
.in(ProductSpecPO::getId, specIds)
.select(ProductSpecPO::getId, ProductSpecPO::getProductId));
Map<Long, Long> map = new HashMap<>();
for (ProductSpecPO s : specs) {
map.put(s.getId(), s.getProductId());
}
return map;
}
/**
* 计算多商品数据横向汇总
*/
@@ -371,9 +396,12 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
List<BrushOrderPO> brushOrders, List<Long> refundStatusIds) {
List<ShopOverviewStatisticsVO.ProductComparison> comparisons = new ArrayList<>();
// 按商品分组订单数据
Map<Long, Long> specToProduct = orderSpecIdToProductIdMap(orders);
Map<Long, List<OrderPO>> ordersByProduct = orders.stream()
.collect(Collectors.groupingBy(OrderPO::getProductId));
.collect(Collectors.groupingBy(o -> {
Long sid = o.getProductSpecId();
return sid != null ? specToProduct.getOrDefault(sid, -1L) : -1L;
}));
// 按商品分组刷单费数据(如果有商品ID)
Map<Long, List<BrushOrderPO>> brushOrdersByProduct = brushOrders.stream()
@@ -391,6 +419,9 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
for (Map.Entry<Long, List<OrderPO>> entry : ordersByProduct.entrySet()) {
Long productId = entry.getKey();
List<OrderPO> productOrders = entry.getValue();
if (productId == null || productId < 0) {
continue;
}
// 获取商品信息
ProductPO product = productService.getById(productId);
@@ -709,10 +740,13 @@ public class ShopOverviewStatisticsServiceImpl implements ShopOverviewStatistics
categoryMap.put(c.getId(), c);
}
Map<Long, Long> specToProduct = orderSpecIdToProductIdMap(orders);
// 按品类分组订单
Map<Long, List<OrderPO>> ordersByCategory = new HashMap<>();
for (OrderPO order : orders) {
Long categoryId = productCategoryMap.get(order.getProductId());
Long sid = order.getProductSpecId();
Long pid = sid != null ? specToProduct.get(sid) : null;
Long categoryId = pid != null ? productCategoryMap.get(pid) : null;
if (categoryId == null) {
categoryId = 0L; // 未分类
}
@@ -86,7 +86,8 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
// 查询订单数据
LambdaQueryWrapper<OrderPO> orderWrapper = new LambdaQueryWrapper<>();
orderWrapper.eq(OrderPO::getCompanyId, companyId)
.eq(OrderPO::getProductId, queryDTO.getProductId())
.apply("EXISTS (SELECT 1 FROM ec_product_spec s WHERE s.id = ec_order.product_spec_id AND s.product_id = {0})",
queryDTO.getProductId())
.ge(OrderPO::getOrderTime, startDateTime)
.le(OrderPO::getOrderTime, endDateTime);
List<OrderPO> orders = orderMapper.selectList(orderWrapper);
@@ -121,7 +122,8 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
LambdaQueryWrapper<OrderPO> lastPeriodWrapper = new LambdaQueryWrapper<>();
lastPeriodWrapper.eq(OrderPO::getCompanyId, companyId)
.eq(OrderPO::getProductId, queryDTO.getProductId())
.apply("EXISTS (SELECT 1 FROM ec_product_spec s WHERE s.id = ec_order.product_spec_id AND s.product_id = {0})",
queryDTO.getProductId())
.ge(OrderPO::getOrderTime, lastStartTime)
.le(OrderPO::getOrderTime, lastEndTime);
List<OrderPO> lastPeriodOrders = orderMapper.selectList(lastPeriodWrapper);
@@ -369,8 +371,18 @@ public class ProductOverviewServiceImpl implements ProductOverviewService {
return buildEmptySpecSummary();
}
Long productId = orders.get(0).getProductId();
Long companyId = orders.get(0).getCompanyId();
OrderPO firstOrder = orders.get(0);
Long companyId = firstOrder.getCompanyId();
Long productId = null;
if (firstOrder.getProductSpecId() != null) {
ProductSpecPO firstSpec = productSpecMapper.selectById(firstOrder.getProductSpecId());
if (firstSpec != null) {
productId = firstSpec.getProductId();
}
}
if (productId == null) {
return buildEmptySpecSummary();
}
LambdaQueryWrapper<ProductSpecPO> specWrapper = new LambdaQueryWrapper<>();
specWrapper.eq(ProductSpecPO::getCompanyId, companyId)