remove: 移除无用mongo字段

This commit is contained in:
2026-09-01 19:50:12 +08:00
parent 67ed4d2ffe
commit abcf1894db
10 changed files with 21 additions and 71 deletions
@@ -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` |
@@ -12,6 +12,5 @@ import lombok.NoArgsConstructor;
public class BiProductStatDelta {
private Long productId;
private Long categoryId;
private BiStatMetricDelta metrics;
}
@@ -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;
@@ -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;
@@ -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;
@@ -24,7 +24,7 @@ import java.util.Set;
* BI 订单日汇聚读查询:Mongo → 工作台/概览沿用的 {@link OverviewAggRows} 行形态。
* <p>
* 净销售三件套由读侧推导:{@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<OverviewAggRows.ShopProductRow> toShopProductRows(
@@ -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");
@@ -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<Long> productIds = productBySku.values().stream()
.filter(id -> id != null && id > 0)
.collect(Collectors.toSet());
Map<Long, Long> categoryByProduct = new HashMap<>();
if (!productIds.isEmpty()) {
List<ProductPO> products = productMapper.selectByIds(productIds);
for (ProductPO p : products) {
categoryByProduct.put(p.getId(), p.getCategoryId() != null ? p.getCategoryId() : 0L);
}
}
Metric shop = new Metric();
Map<String, Metric> 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(
@@ -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<String, BiStatMetricDelta> productMap = new HashMap<>();
Map<String, long[]> productKeys = new HashMap<>();
Map<String, Long> productKeys = new HashMap<>();
Map<String, BiStatMetricDelta> skuMap = new HashMap<>();
Map<String, long[]> skuKeys = new HashMap<>();
Map<Long, Long> productBySku = loadProductBySku(safeItems);
Map<Long, Long> 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<BiProductStatDelta> products = new ArrayList<>();
for (Map.Entry<String, BiStatMetricDelta> 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<Long, Long> loadCategories(Map<Long, Long> productBySku) {
Map<Long, Long> map = new HashMap<>();
List<Long> 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<OrderItemPO> items) {
BigDecimal totalLine = BigDecimal.ZERO;
for (OrderItemPO i : items) {
@@ -297,17 +297,15 @@ public class PddOrderConvertService {
Map<String, BiProductStatDelta> 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());
}