|
|
@@ -0,0 +1,174 @@
|
|
|
+package com.etotem.cfc.pricing;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 购物车套餐 offer 检测核心算法(纯计算,无外部依赖,便于单测)。
|
|
|
+ * <p>
|
|
|
+ * 规则:
|
|
|
+ * 1. 商品行按 (productId, skuId) 聚合为「池」;
|
|
|
+ * 2. 多套餐候选时取节省额最大者,同节省额取先出现(确定性);
|
|
|
+ * 3. 节省额 = 该套消费的基础款单价合计 − 套餐价,仅 >0 才触发(避免会员价低于套餐价反而更贵);
|
|
|
+ * 4. 超量覆盖:计算组数 groups = min over children( floor(haveQty / needQty) );
|
|
|
+ * 实际消耗 groups 组后,行数量相应扣减,余量保留;
|
|
|
+ * 5. 脏行防御:child 的 childSkuId 不在 lines 中 → 0 补差值不命中该套餐;
|
|
|
+ * 6. 同一套餐 comoboSkuId 相同只报一次。
|
|
|
+ */
|
|
|
+public class ComboOfferCalculator {
|
|
|
+
|
|
|
+ /** 池内一行:某 (productId, skuId) 剩余数量与下单口径单价 */
|
|
|
+ private static class PoolEntry {
|
|
|
+ final Long productId;
|
|
|
+ final Long skuId;
|
|
|
+ int quantity;
|
|
|
+ final int unitPrice;
|
|
|
+
|
|
|
+ PoolEntry(Long productId, Long skuId, int quantity, int unitPrice) {
|
|
|
+ this.productId = productId;
|
|
|
+ this.skuId = skuId;
|
|
|
+ this.quantity = quantity;
|
|
|
+ this.unitPrice = unitPrice;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 聚合:同 (productId, skuId) 归并数量;单价取该聚合内首个非零值
|
|
|
+ */
|
|
|
+ private Map<String, PoolEntry> aggregate(List<LineItem> lines) {
|
|
|
+ Map<String, PoolEntry> pool = new LinkedHashMap<>();
|
|
|
+ for (LineItem line : lines) {
|
|
|
+ if (line == null || line.getProductId() == null || line.getQuantity() == null || line.getQuantity() <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String key = line.getProductId() + "|" + (line.getSkuId() == null ? "" : line.getSkuId());
|
|
|
+ int unitPrice = safePrice(line.getUnitPrice());
|
|
|
+ PoolEntry entry = pool.get(key);
|
|
|
+ if (entry == null) {
|
|
|
+ pool.put(key, new PoolEntry(line.getProductId(), line.getSkuId(), line.getQuantity(), unitPrice));
|
|
|
+ } else {
|
|
|
+ entry.quantity += line.getQuantity();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return pool;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 线性安全价(null/负数视为 0) */
|
|
|
+ private static int safePrice(Integer price) {
|
|
|
+ return price == null || price < 0 ? 0 : price;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 计算 key:productId skuId 组合 */
|
|
|
+ private static String key(Long productId, Long skuId) {
|
|
|
+ return productId + "|" + (skuId == null ? "" : skuId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 试凑一套套餐:不修改池,返回可覆盖的组数;任一基础款不足则返回 0。
|
|
|
+ * 同一 PoolEntry 被不同 child 消费(跨 child 共行),用 taken 防重复扣。
|
|
|
+ */
|
|
|
+ private int maxGroups(Map<String, PoolEntry> pool, ComboOfferDef def) {
|
|
|
+ int groups = Integer.MAX_VALUE;
|
|
|
+ Map<PoolEntry, Integer> taken = new HashMap<>();
|
|
|
+ for (ComboOfferDef.ComboChild child : def.getChildren()) {
|
|
|
+ if (child == null || child.getChildProductId() == null || child.getChildSkuId() == null) {
|
|
|
+ return 0; // 子项缺失(脏行防御),不命中该套餐
|
|
|
+ }
|
|
|
+ int needQty = child.getQuantity() == null || child.getQuantity() <= 0 ? 1 : child.getQuantity();
|
|
|
+ String k = key(child.getChildProductId(), child.getChildSkuId());
|
|
|
+ PoolEntry entry = pool.get(k);
|
|
|
+ if (entry == null) {
|
|
|
+ return 0; // 该基础款根本不在池中(脏行防御)
|
|
|
+ }
|
|
|
+ int already = taken.getOrDefault(entry, 0);
|
|
|
+ int available = entry.quantity - already;
|
|
|
+ if (available <= 0) {
|
|
|
+ return 0; // 已耗尽,不命中
|
|
|
+ }
|
|
|
+ int take = Math.min(available, needQty);
|
|
|
+ taken.put(entry, already + take);
|
|
|
+ int haveGroups = available / needQty; // floor division: full groups from this child
|
|
|
+ if (haveGroups < groups) {
|
|
|
+ groups = haveGroups;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return groups <= 0 ? 0 : groups;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算订单内套餐优惠。
|
|
|
+ *
|
|
|
+ * @param lines 下单/结算口径商品行(productId/skuId 为解析后身份,unitPrice 为结算单价,单位分)
|
|
|
+ * @param defs 套餐 offer 定义(product_combo_items 按套餐SKU 聚合)
|
|
|
+ * @return ComboOfferResult:active=false 表示不可替换;active=true 表示命中并可替换
|
|
|
+ */
|
|
|
+ public ComboOfferResult compute(List<LineItem> lines, List<ComboOfferDef> defs) {
|
|
|
+ if (lines == null || lines.isEmpty() || defs == null || defs.isEmpty()) {
|
|
|
+ return ComboOfferResult.inactive();
|
|
|
+ }
|
|
|
+ Map<String, PoolEntry> pool = aggregate(lines);
|
|
|
+ ComboOfferDef bestDef = null;
|
|
|
+ int bestSaving = 0;
|
|
|
+ int bestGroups = 0;
|
|
|
+ int bestOriginalTotal = 0;
|
|
|
+ Set<Long> seenComboSkuIds = new HashSet<>();
|
|
|
+
|
|
|
+ for (ComboOfferDef def : defs) {
|
|
|
+ // 同一套餐 comboSkuId 相同只报一次(取先出现)
|
|
|
+ if (def.getComboSkuId() == null || !seenComboSkuIds.add(def.getComboSkuId())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (def.getComboPrice() == null || def.getChildren() == null || def.getChildren().isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ int groups = maxGroups(pool, def);
|
|
|
+ if (groups <= 0) {
|
|
|
+ continue; // 基础款不足或脏行防御,跳过该套餐
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算每组原价:按 needQty × 对应 pool 单价 累加
|
|
|
+ int perGroupOriginal = 0;
|
|
|
+ boolean allValid = true;
|
|
|
+ for (ComboOfferDef.ComboChild child : def.getChildren()) {
|
|
|
+ if (child == null) { allValid = false; break; }
|
|
|
+ Long pid = child.getChildProductId();
|
|
|
+ Long sid = child.getChildSkuId();
|
|
|
+ if (pid == null || sid == null) { allValid = false; break; }
|
|
|
+ int needQty = child.getQuantity() == null || child.getQuantity() <= 0 ? 1 : child.getQuantity();
|
|
|
+ String k = key(pid, sid);
|
|
|
+ PoolEntry entry = pool.get(k);
|
|
|
+ if (entry == null) { allValid = false; break; } // 脏行防御
|
|
|
+ int unitPrice = entry.unitPrice;
|
|
|
+ perGroupOriginal += unitPrice * needQty;
|
|
|
+ }
|
|
|
+ if (!allValid) continue;
|
|
|
+
|
|
|
+ int originalTotal = perGroupOriginal * groups;
|
|
|
+ int comboTotal = safePrice(def.getComboPrice()) * groups;
|
|
|
+ int saving = originalTotal - comboTotal;
|
|
|
+
|
|
|
+ if (saving <= 0) continue; // 套餐价≥总价,取原价结算(Review Focus 1)
|
|
|
+
|
|
|
+ // 严格大于:节省额相同取先出现(确定性)
|
|
|
+ if (saving > bestSaving) {
|
|
|
+ bestSaving = saving;
|
|
|
+ bestDef = def;
|
|
|
+ bestGroups = groups;
|
|
|
+ bestOriginalTotal = originalTotal;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bestDef == null) {
|
|
|
+ return ComboOfferResult.inactive();
|
|
|
+ }
|
|
|
+
|
|
|
+ ComboOfferResult result = new ComboOfferResult();
|
|
|
+ result.setActive(true);
|
|
|
+ result.setComboSkuId(bestDef.getComboSkuId());
|
|
|
+ result.setComboName(bestDef.getComboName());
|
|
|
+ result.setGroups(bestGroups);
|
|
|
+ result.setOriginalTotal(bestOriginalTotal);
|
|
|
+ result.setComboPrice(safePrice(bestDef.getComboPrice()) * bestGroups);
|
|
|
+ result.setSaving(bestSaving);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+}
|