From abcf1894db4cdcd7ffa34ddc943585fb611f7925 Mon Sep 17 00:00:00 2001 From: ah-max150 Date: Tue, 1 Sep 2026 19:50:12 +0800 Subject: [PATCH] =?UTF-8?q?remove:=20=E7=A7=BB=E9=99=A4=E6=97=A0=E7=94=A8m?= =?UTF-8?q?ongo=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docs/订单同步转换与BI日汇聚设计.md | 14 ++++---- .../bi/messaging/BiProductStatDelta.java | 1 - .../document/BiOrderStatDailyProductDoc.java | 8 ++--- .../document/BiOrderStatDailyShopDoc.java | 1 - .../document/BiOrderStatDailySkuDoc.java | 1 - .../query/BiOrderStatQueryService.java | 4 +-- .../BiOrderStatDailyRepository.java | 3 -- .../service/OrderDailyStatComputeService.java | 23 +++---------- .../OrderStatContributionCalculator.java | 33 +++---------------- .../service/PddOrderConvertService.java | 4 +-- 10 files changed, 21 insertions(+), 71 deletions(-) diff --git a/ecom-dmb-backend/docs/订单同步转换与BI日汇聚设计.md b/ecom-dmb-backend/docs/订单同步转换与BI日汇聚设计.md index 0692a955..5506c904 100644 --- a/ecom-dmb-backend/docs/订单同步转换与BI日汇聚设计.md +++ b/ecom-dmb-backend/docs/订单同步转换与BI日汇聚设计.md @@ -298,7 +298,7 @@ RoutingKey: "channelOrderId": 101, "shopDelta": { "salesAmount": 12.3, "dealOrderCount": 1, "...": "..." }, "productDeltas": [ - { "productId": 1, "categoryId": 2, "metrics": { "salesAmount": 12.3 } } + { "productId": 1, "metrics": { "salesAmount": 12.3 } } ], "skuDeltas": [ { "productId": 1, "productSkuId": 9, "metrics": { "salesAmount": 12.3 } } @@ -387,16 +387,16 @@ RoutingKey: | 集合名 | 唯一键 | |--------|--------| | `bi_order_stat_daily_shop` | `(companyId, shopId, statDate)` | -| `bi_order_stat_daily_product` | `(companyId, shopId, productId, categoryId, statDate)` | +| `bi_order_stat_daily_product` | `(companyId, shopId, productId, statDate)` | | `bi_order_stat_daily_sku` | `(companyId, shopId, productSkuId, statDate)` | -`productId=0` 表示未匹配商品;`categoryId=0` 表示未分类;`productSkuId=0` 表示未绑定 SKU。 -`categoryId` 为订单写入时快照,商品改类目不回算历史(与旧设计一致)。 +`productId=0` 表示未匹配商品;`productSkuId=0` 表示未绑定 SKU。 +品类不落日文档,读侧按商品主数据实时解析 `categoryId`。 建议索引: - shop:`{ companyId: 1, shopId: 1, statDate: 1 }` unique;`{ companyId: 1, statDate: 1 }`;`{ companyId: 1, platformCode: 1, statDate: 1 }` -- product:unique 复合键如上;`{ companyId: 1, categoryId: 1, statDate: 1 }`;`{ companyId: 1, productId: 1, statDate: 1 }` +- product:unique 复合键如上;`{ companyId: 1, productId: 1, statDate: 1 }` - sku:unique 复合键如上;`{ companyId: 1, productId: 1, statDate: 1 }` `spring.data.mongodb.auto-index-creation=false`,索引由迁移脚本或启动显式创建。 @@ -439,7 +439,6 @@ RoutingKey: "productCost": 0, "freightCost": 0, "otherCost": 0, - "statVersion": 0, "dbSnapshotTime": "2026-07-28T16:02:00.123+08:00", "computedAt": "2026-07-28T16:02:05.000+08:00", "computeMode": "FULL" @@ -448,7 +447,7 @@ RoutingKey: #### 商品日文档额外键 -`productId`, `categoryId`(其余指标字段同上)。 +`productId`(其余指标字段同上)。 #### SKU 日文档额外键 @@ -459,7 +458,6 @@ RoutingKey: | 字段 | 说明 | |------|------| | `dbSnapshotTime` | **最新一次全量计算**开启 MySQL 事务快照的时刻;增量用 `internalOrderUpdateTime` 与之比较。**仅全量写入/更新**;增量 `$inc` 不抬高该字段 | -| `statVersion` | 与 `OrderStatusStatSets` 重算批次对齐;配置变更全量 rebuild 时递增 | | `computedAt` | 本次写入完成时间 | | `computeMode` | `FULL` / `INC` | diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/messaging/BiProductStatDelta.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/messaging/BiProductStatDelta.java index 04f8c44c..ecbd4253 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/messaging/BiProductStatDelta.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/messaging/BiProductStatDelta.java @@ -12,6 +12,5 @@ import lombok.NoArgsConstructor; public class BiProductStatDelta { private Long productId; - private Long categoryId; private BiStatMetricDelta metrics; } diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailyProductDoc.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailyProductDoc.java index e531d295..aa70c0ad 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailyProductDoc.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailyProductDoc.java @@ -10,13 +10,13 @@ import java.math.BigDecimal; import java.time.Instant; import java.time.LocalDate; -/** 商品日汇聚。净销售由读侧 deal − refund 推导。 */ +/** 商品日汇聚。净销售由读侧 deal − refund 推导;品类按商品主数据实时解析。 */ @Data @Document(collection = "bi_order_stat_daily_product") @CompoundIndexes({ @CompoundIndex( - name = "uk_company_shop_prod_cat_date", - def = "{'companyId':1,'shopId':1,'productId':1,'categoryId':1,'statDate':1}", + name = "uk_company_shop_prod_date", + def = "{'companyId':1,'shopId':1,'productId':1,'statDate':1}", unique = true) }) public class BiOrderStatDailyProductDoc { @@ -27,7 +27,6 @@ public class BiOrderStatDailyProductDoc { private Long companyId; private Long shopId; private Long productId; - private Long categoryId; private LocalDate statDate; private BigDecimal dealAmount = BigDecimal.ZERO; @@ -41,7 +40,6 @@ public class BiOrderStatDailyProductDoc { private BigDecimal freightCost = BigDecimal.ZERO; private BigDecimal otherCost = BigDecimal.ZERO; - private Integer statVersion = 0; private Instant dbSnapshotTime; private Instant computedAt; private String computeMode; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailyShopDoc.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailyShopDoc.java index c48bdfab..2de2bb5f 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailyShopDoc.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailyShopDoc.java @@ -40,7 +40,6 @@ public class BiOrderStatDailyShopDoc { private BigDecimal freightCost = BigDecimal.ZERO; private BigDecimal otherCost = BigDecimal.ZERO; - private Integer statVersion = 0; /** 最新一次全量计算开启 MySQL 事务快照的时间;增量用内部订单更新时间与之比较 */ private Instant dbSnapshotTime; private Instant computedAt; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailySkuDoc.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailySkuDoc.java index fb36be85..71d708a5 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailySkuDoc.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/model/document/BiOrderStatDailySkuDoc.java @@ -42,7 +42,6 @@ public class BiOrderStatDailySkuDoc { private BigDecimal freightCost = BigDecimal.ZERO; private BigDecimal otherCost = BigDecimal.ZERO; - private Integer statVersion = 0; private Instant dbSnapshotTime; private Instant computedAt; private String computeMode; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/query/BiOrderStatQueryService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/query/BiOrderStatQueryService.java index 4b524252..23bdaeaf 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/query/BiOrderStatQueryService.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/query/BiOrderStatQueryService.java @@ -24,7 +24,7 @@ import java.util.Set; * BI 订单日汇聚读查询:Mongo → 工作台/概览沿用的 {@link OverviewAggRows} 行形态。 *

* 净销售三件套由读侧推导:{@code deal* − refund*},不再读 Mongo sales* 字段。 - * 品类归属按商品主数据实时解析,不使用商品日文档上的 categoryId 快照。 + * 品类归属按商品主数据实时解析,商品日文档不存 categoryId。 */ @Service @RequiredArgsConstructor @@ -147,7 +147,7 @@ public class BiOrderStatQueryService { /** * 商品日文档按 (shopId, productId) 合并区间后,转为工作台多维/四表所需行。 - * platformCode 由店铺主数据补齐;categoryId 按当前商品主数据实时解析(忽略文档快照)。 + * platformCode 由店铺主数据补齐;categoryId 按当前商品主数据实时解析。 * 行上 salesAmount/orderCount/salesQuantitySum 为净销售三件套。 */ public List toShopProductRows( diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/repository/BiOrderStatDailyRepository.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/repository/BiOrderStatDailyRepository.java index 1f33b769..5aaeda83 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/repository/BiOrderStatDailyRepository.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/repository/BiOrderStatDailyRepository.java @@ -155,11 +155,9 @@ public class BiOrderStatDailyRepository { continue; } long productId = pd.getProductId() != null ? pd.getProductId() : 0L; - long categoryId = pd.getCategoryId() != null ? pd.getCategoryId() : 0L; Query pq = Query.query(Criteria.where("companyId").is(companyId) .and("shopId").is(shopId) .and("productId").is(productId) - .and("categoryId").is(categoryId) .and("statDate").is(statDate)); BiOrderStatDailyProductDoc found = mongoTemplate.findOne(pq, BiOrderStatDailyProductDoc.class); if (found == null) { @@ -167,7 +165,6 @@ public class BiOrderStatDailyRepository { doc.setCompanyId(companyId); doc.setShopId(shopId); doc.setProductId(productId); - doc.setCategoryId(categoryId); doc.setStatDate(statDate); applyMetricToProduct(doc, pd.getMetrics()); doc.setComputeMode("INC"); diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/service/OrderDailyStatComputeService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/service/OrderDailyStatComputeService.java index 7f074e8c..fa00bda0 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/service/OrderDailyStatComputeService.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/service/OrderDailyStatComputeService.java @@ -17,9 +17,7 @@ import top.xyquan.ecomdmbbackend.modules.ec.order.model.po.OrderItemPO; import top.xyquan.ecomdmbbackend.modules.ec.order.model.po.OrderPO; import top.xyquan.ecomdmbbackend.modules.ec.order.status.support.OrderStatusStatResolver; import top.xyquan.ecomdmbbackend.modules.ec.order.status.support.OrderStatusStatSets; -import top.xyquan.ecomdmbbackend.modules.ec.product.mapper.ProductMapper; import top.xyquan.ecomdmbbackend.modules.ec.product.sku.mapper.ProductSkuMapper; -import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductPO; import top.xyquan.ecomdmbbackend.modules.ec.product.sku.model.po.ProductSkuPO; import org.springframework.transaction.TransactionDefinition; @@ -45,7 +43,6 @@ public class OrderDailyStatComputeService { private final OrderMapper orderMapper; private final OrderItemMapper orderItemMapper; - private final ProductMapper productMapper; private final ProductSkuMapper productSkuMapper; private final OrderStatusStatResolver orderStatusStatResolver; private final BiOrderStatDailyRepository repository; @@ -146,16 +143,6 @@ public class OrderDailyStatComputeService { productBySku.put(sku.getId(), sku.getProductId() != null ? sku.getProductId() : 0L); } } - Set productIds = productBySku.values().stream() - .filter(id -> id != null && id > 0) - .collect(Collectors.toSet()); - Map categoryByProduct = new HashMap<>(); - if (!productIds.isEmpty()) { - List products = productMapper.selectByIds(productIds); - for (ProductPO p : products) { - categoryByProduct.put(p.getId(), p.getCategoryId() != null ? p.getCategoryId() : 0L); - } - } Metric shop = new Metric(); Map productMap = new HashMap<>(); @@ -201,14 +188,13 @@ public class OrderDailyStatComputeService { for (OrderItemPO item : items) { long skuId = item.getProductSkuId() != null ? item.getProductSkuId() : 0L; long productId = productBySku.getOrDefault(skuId, 0L); - long categoryId = categoryByProduct.getOrDefault(productId, 0L); int qty = OrderStatusStatSets.quantity(item); BigDecimal lineReceived = allocateLine(received, item, items); BigDecimal lineFee = deal ? allocateLine(fee, item, items) : BigDecimal.ZERO; - String pk = companyId + ":" + shopId + ":" + productId + ":" + categoryId; + String pk = companyId + ":" + shopId + ":" + productId; Metric pm = productMap.computeIfAbsent(pk, k -> new Metric()); - productKeys.putIfAbsent(pk, new DimKey(productId, categoryId, 0L)); + productKeys.putIfAbsent(pk, new DimKey(productId, 0L)); if (deal) { pm.dealAmount = pm.dealAmount.add(lineReceived); pm.dealQuantitySum += qty; @@ -230,7 +216,7 @@ public class OrderDailyStatComputeService { String sk = companyId + ":" + shopId + ":" + skuId; Metric sm = skuMap.computeIfAbsent(sk, k -> new Metric()); - skuKeys.putIfAbsent(sk, new DimKey(productId, 0L, skuId)); + skuKeys.putIfAbsent(sk, new DimKey(productId, skuId)); if (deal) { sm.dealAmount = sm.dealAmount.add(lineReceived); sm.dealQuantitySum += qty; @@ -283,7 +269,6 @@ public class OrderDailyStatComputeService { doc.setCompanyId(agg.companyId()); doc.setShopId(agg.shopId()); doc.setProductId(key.productId()); - doc.setCategoryId(key.categoryId()); doc.setStatDate(agg.statDate()); applyMetric(doc, e.getValue()); doc.setDbSnapshotTime(snapshotTime); @@ -384,7 +369,7 @@ public class OrderDailyStatComputeService { BigDecimal otherCost = BigDecimal.ZERO; } - private record DimKey(long productId, long categoryId, long skuId) { + private record DimKey(long productId, long skuId) { } private record DayAgg( diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/support/OrderStatContributionCalculator.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/support/OrderStatContributionCalculator.java index 49d80dad..5478d401 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/support/OrderStatContributionCalculator.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/bi/orderstat/support/OrderStatContributionCalculator.java @@ -8,9 +8,7 @@ import top.xyquan.ecomdmbbackend.modules.bi.messaging.BiStatMetricDelta; import top.xyquan.ecomdmbbackend.modules.ec.order.model.po.OrderItemPO; import top.xyquan.ecomdmbbackend.modules.ec.order.model.po.OrderPO; import top.xyquan.ecomdmbbackend.modules.ec.order.status.support.OrderStatusStatSets; -import top.xyquan.ecomdmbbackend.modules.ec.product.mapper.ProductMapper; import top.xyquan.ecomdmbbackend.modules.ec.product.sku.mapper.ProductSkuMapper; -import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductPO; import top.xyquan.ecomdmbbackend.modules.ec.product.sku.model.po.ProductSkuPO; import org.springframework.stereotype.Component; @@ -29,11 +27,9 @@ import java.util.Map; @Component public class OrderStatContributionCalculator { - private final ProductMapper productMapper; private final ProductSkuMapper productSkuMapper; - public OrderStatContributionCalculator(ProductMapper productMapper, ProductSkuMapper productSkuMapper) { - this.productMapper = productMapper; + public OrderStatContributionCalculator(ProductSkuMapper productSkuMapper) { this.productSkuMapper = productSkuMapper; } @@ -69,24 +65,22 @@ public class OrderStatContributionCalculator { } Map productMap = new HashMap<>(); - Map productKeys = new HashMap<>(); + Map productKeys = new HashMap<>(); Map skuMap = new HashMap<>(); Map skuKeys = new HashMap<>(); Map productBySku = loadProductBySku(safeItems); - Map categoryByProduct = loadCategories(productBySku); for (OrderItemPO item : safeItems) { long skuId = item.getProductSkuId() != null ? item.getProductSkuId() : 0L; long productId = productBySku.getOrDefault(skuId, 0L); - long categoryId = categoryByProduct.getOrDefault(productId, 0L); int qty = OrderStatusStatSets.quantity(item); BigDecimal lineReceived = allocateLine(received, item, safeItems); BigDecimal lineFee = deal ? allocateLine(fee, item, safeItems) : BigDecimal.ZERO; - String pk = productId + ":" + categoryId; + String pk = String.valueOf(productId); BiStatMetricDelta pm = productMap.computeIfAbsent(pk, k -> BiStatMetricDelta.zeros()); - productKeys.putIfAbsent(pk, new long[]{productId, categoryId}); + productKeys.putIfAbsent(pk, productId); if (deal) { pm.setDealAmount(nz(pm.getDealAmount()).add(lineReceived)); pm.setDealQuantitySum(nzi(pm.getDealQuantitySum()) + qty); @@ -146,10 +140,8 @@ public class OrderStatContributionCalculator { List products = new ArrayList<>(); for (Map.Entry e : productMap.entrySet()) { - long[] k = productKeys.get(e.getKey()); products.add(BiProductStatDelta.builder() - .productId(k[0]) - .categoryId(k[1]) + .productId(productKeys.get(e.getKey())) .metrics(e.getValue()) .build()); } @@ -188,21 +180,6 @@ public class OrderStatContributionCalculator { return map; } - private Map loadCategories(Map productBySku) { - Map map = new HashMap<>(); - List ids = productBySku.values().stream() - .filter(id -> id != null && id > 0) - .distinct() - .toList(); - if (ids.isEmpty()) { - return map; - } - for (ProductPO p : productMapper.selectByIds(ids)) { - map.put(p.getId(), p.getCategoryId() != null ? p.getCategoryId() : 0L); - } - return map; - } - private static BigDecimal allocateLine(BigDecimal orderPay, OrderItemPO item, List items) { BigDecimal totalLine = BigDecimal.ZERO; for (OrderItemPO i : items) { diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/order/convert/service/PddOrderConvertService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/order/convert/service/PddOrderConvertService.java index dea3ff41..f83a8047 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/order/convert/service/PddOrderConvertService.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/channel/pdd/order/convert/service/PddOrderConvertService.java @@ -297,17 +297,15 @@ public class PddOrderConvertService { Map pm = prodByDate.computeIfAbsent(d, k -> new HashMap<>()); if (c.getProducts() != null) { for (BiProductStatDelta p : c.getProducts()) { - String key = p.getProductId() + ":" + p.getCategoryId(); + String key = String.valueOf(p.getProductId()); BiStatMetricDelta m = p.getMetrics() != null ? p.getMetrics() : BiStatMetricDelta.zeros(); BiStatMetricDelta delta = subtract ? m.negate() : m; pm.merge(key, BiProductStatDelta.builder() .productId(p.getProductId()) - .categoryId(p.getCategoryId()) .metrics(delta) .build(), (a, b) -> BiProductStatDelta.builder() .productId(a.getProductId()) - .categoryId(a.getCategoryId()) .metrics(a.getMetrics().plus(b.getMetrics())) .build()); }