docs/superpowers/specs/2026-10-30-product-bundle-autopricing-design.md(已获用户批准)cfclubproduct_bundle_items 表新增 bundle_sku_id 列(套餐 SKU 锚点,套餐价 = 该 SKU 的 price)。bundle_sku_id 非空 = 定价套餐行;为空 = 既有出库联动行(InventoryService 不动)。child_product_id + 可选 child_sku_id,quantity 为组成数量)数量凑够 → 该部分自动按套餐价结算。ProductOrderService 服务端重算套餐优惠计入 moneyAmount;前端传入的 discountAmount 语义改为“规则优惠(不含套餐)”,套餐部分一律服务端算。CartService.computeGiftPromo 的读取与结算;保留 ProductOrderService 基于 gift_membership_original_threshold 的单笔订单送会员权益与 gift_membership_threshold 累计送会员(均不动)。dto/CartItemDTO.java:58-71):giftOriginalTotal/threshold/payPrice/discount/active/remaining。本次新增 BundlePromo 嵌套类,giftPromo 字段保留但置空(避免旧小程序读 giftPromo.active 出错)。service/CartService.java:411-490):读取 gift_membership_original_threshold / gift_membership_pay_price,算一口价。整段替换为套餐定价。调用点在 CartService 购物车列表方法(约 130-135,dto.setGiftPromo(...))。依赖注入在 45-51 行(productSkuService/productGiftRuleService/sysConfigService 等)。memberPrice 仅展示不参与 unitPrice。购物车当前非会员口径。unitPrice = getEligibleMemberPrice(product, buyerId);有 sku 则 linked→换 product 用 linked.price,legacy→sku.price;totalAmount += unitPrice*qty;OrderItem 记录 product.getId()(解析后 id)、item.getSkuId()、unitPrice。multiDiscountAmount = dto.getDiscountAmount()(客户端)→ +couponDiscount(couponService.apply,服务端校验)→ moneyAmount = max(0, totalAmount − pointsCost − multiDiscountAmount)。bundleDiscount = bundlePricingService.compute(lines).getDiscount(),multiDiscountAmount += bundleDiscount。gift_membership_original_threshold)不碰。BaseMapper<ProductBundleItem>(无自定义方法)。加载定价行用 selectList(new QueryWrapper<ProductBundleItem>().isNotNull("bundle_sku_id"))。id/bundleProductId/childProductId/childSkuId/quantity/createdAt。加 bundleSkuId。id/productId/skuCode/specs/price/.../label/...。套餐锚点名取 label(非空)否则 specs;价取 price。DatabaseInitializer 有 columnExists(table, column)(5373)辅助;ALTER TABLE ... ADD COLUMN ... 示例见 158-202。src/test/java/com/etotem/cfc/unit/*Test.java,JUnit5 + Mockito(config 测试已用)。纯算法类直接实例化。this.giftPromo = result.giftPromo(约 316);totalAmount()(约 177-184)if giftPromo.active → originalTotalAmount - giftPromo.discount;gift-promo-section(96-112);写 storage checkoutGiftPromo(约 424)。checkoutGiftPromo(648);gift-promo-section(229-240);giftPromoDiscount computed(576-580);提交 discountAmount: discountInfo.discountAmount + giftPromoDiscount(约 1152)。__tests__/utils/*.test.js(纯函数)。product_bundle_items.bundle_sku_idTDD 说明:迁移幂等性无法纯单测;沿用现有 DatabaseInitializer 迁移模式(列存在检查),部署后验证“重复执行不报错”。
改动:
cfc-backend/src/main/java/com/etotem/cfc/config/DatabaseInitializer.java:新增幂等迁移块(参照现有 columnExists 模式):
if (!columnExists("product_bundle_items", "bundle_sku_id")) {
jdbcTemplate.execute("ALTER TABLE product_bundle_items ADD COLUMN bundle_sku_id BIGINT NULL COMMENT '套餐SKU锚点:非空=定价套餐行(套餐价取该SKU.price);空=出库联动行'");
}
同步 schema.sql(cfc-backend 资源目录,找到 product_bundle_items 建表处补列)。
entity/ProductBundleItem.java 加 private Long bundleSkuId;。
提交:feat(bundle): product_bundle_items 新增 bundle_sku_id 列(定价套餐锚点,迁移幂等)
BundlePricingCalculator(TDD 核心)新建 cfc-backend/src/main/java/com/etotem/cfc/pricing/BundlePricingCalculator.java(无状态、可实例化)。
数据结构(放同包 bundle 下或作为嵌套静态类,随实现定):
LineItem:{ Long productId; Long skuId; Integer quantity; Integer unitPrice; }BundleChild:{ Long childProductId; Long childSkuId; Integer quantity; }BundleDef:{ Long bundleSkuId; String bundleName; Integer bundlePrice; List<BundleChild> children; }BundleHit:{ Long bundleSkuId; String bundleName; Integer times; Integer originalTotal; Integer bundlePrice; Integer saving; }BundleDiscountResult:{ boolean active; Integer originalTotal; Integer payPrice; Integer discount; List<BundleHit> bundleList; }公开方法:BundleDiscountResult compute(List<LineItem> lines, List<BundleDef> defs)
算法(贪心省最多):
(productId, skuId) 归总 quantity;unitPrice 取该组行内(相同 sku 的行 unitPrice 一致;skuId==null 组取 product 级 unitPrice)。childSkuId!=null 精确匹配 sku;==null 按 productId 聚合所有 sku 行)≥ child.quantity。可凑时算节省额:按“消费顺序”(遍历池中匹配行、从前往后扣减)取 child.quantity 个的 Σ(unitPrice) 之和,再对全体 child 求和后减 bundlePrice。仅节省额 > 0 记候选。hit.times++、discount += saving、originalTotal += 原价合计、payPrice += bundlePrice。active = discount > 0;空 defs/空 lines → inactive。单元测试 src/test/java/com/etotem/cfc/unit/BundlePricingCalculatorTest.java(先写测试):
提交:feat(bundle): BundlePricingCalculator 贪心省最多套餐匹配算法 + 单测
BundlePricingService(Spring 装载定义 + 委托)新建 cfc-backend/src/main/java/com/etotem/cfc/service/BundlePricingService.java(@Service)。
注入:ProductBundleItemMapper、ProductSkuService、ProductMapper(供 sku 不存在时的降级表现)。
方法:
BundleDiscountResult compute(List<LineItem> lines):
productBundleItemMapper.selectList(new QueryWrapper<ProductBundleItem>().isNotNull("bundle_sku_id"))。bundleSkuId 分组 → BundleDef;bundlePrice = sku.price;bundleName = sku.label 非空 ? label : specs;skus 不存在该 id 则跳过该 def(防御)。BundlePricingCalculator.compute(lines, defs)。单元测试(Mockito mock mapper + skuService):
提交:feat(bundle): BundlePricingService 装载定价套餐定义并委托计算
MemberPriceResolver新建 cfc-backend/src/main/java/com/etotem/cfc/service/MemberPriceResolver.java(@Service),把 ProductOrderService.getEligibleMemberPrice(2213-2260)原样抽出:注入 UserMapper、MembershipService。
方法:
int resolveEligibleMemberPrice(Product product, Long buyerId)(原逻辑,行为不变)。ResolvedLine resolve(Product product, ProductSku sku, Long buyerId):返回 { Product product(解析后); int unitPrice; }——linked sku → 换 product 用 linkedProduct.price;legacy sku → sku.price;无 sku → resolveEligibleMemberPrice(product, buyerId)。与 createMultiItem 767-782 同口径。改造:ProductOrderService 的 getEligibleMemberPrice 私有方法改为委托 resolver(行为保持,多处调用点不变);createMultiItem 的中间解析逻辑改调 resolver.resolve(...)(保持同行为)。
单元测试 MemberPriceResolverTest:memberPrice+等级达标/不达标、minDiscountLevel、linked sku、legacy sku.price、无 sku。
提交:refactor(order): 抽出 MemberPriceResolver 共享下单价格口径(行为不变)
bundlePromo(替换一口价)改动 dto/CartItemDTO.java:新增嵌套类 BundleItemHit(bundleSkuId/bundleName/times/originalTotal/bundlePrice/saving)与 BundlePromo(active/originalTotal/payPrice/discount/bundleList)+ 字段 private BundlePromo bundlePromo;。GiftPromo 保留。
改动 service/CartService.java:
computeGiftPromo(411-490)中读取 gift_membership_original_threshold / gift_membership_pay_price 及一口价计算,整体替换为 computeBundlePromo(List<Cart> cartList, Map<Long,Product> productMap, Long userId):
LineItem:resolver 解析 sku/linked/member 得到下单口径 unitPrice(保证展示=实付),skuId 传原始 cart.getSkuId()(bundle 匹配用原始身份;linked 转发产品才用解析后 id——与下单一致,取 resolver 返回的解析后 product.getId() 作 productId、skuId 用解析后的 skuId:linked sku 在订单里 skuId 仍是原 skuId,故 LineItem.skuId = cart.getSkuId(),productId = 解析后 product id)。BundleDiscountResult r = bundlePricingService.compute(lines) → dto.setBundlePromo(...);dto.setGiftPromo(buildEmptyGiftPromo())。computeBundlePromo。需要 userId(列表方法已有当前用户上下文)。sysConfigService 若不再使用可保留注入(或剔除,随实现)。单元测试:可用 Mockito 对 CartService 最小装配验证 bundlePromo 绑定与 giftPromo 置空(mock mapper/resolver/bundlePricingService)。
提交:feat(bundle): 购物车返回 bundlePromo,移除一口价(threshold/payPrice)结算
改动 service/ProductOrderService.java:
createMultiItem(742-819 循环内):收集 List<LineItem> lines(new LineItem(product.getId(), item.getSkuId(), item.getQuantity(), unitPrice),product 为解析后)。multiDiscountAmount 处(约 838):int bundleDiscount = bundlePricingService.compute(lines).getDiscount(); → multiDiscountAmount = (dto.getDiscountAmount() ?? 0) + couponDiscount + bundleDiscount;(语义:dto.getDiscountAmount() 现仅含规则优惠)。create()(227-310):同样收集单行(解析后 productId、dto.getSkuId()、dto.getQuantity()、最终 unitPrice)并在 discountAmount 处 + bundleDiscount,保证“商品页直接购买多件”也享受套餐价。BundlePricingService。单元测试(新 OrderBundlePricingTest,Mockito mock bundlePricingService + 现有依赖桩):
moneyAmount = totalAmount − pointsCost − 规则优惠 − 套餐优惠(套餐优惠=服务端计算值)。discountAmount 若含“套餐部分”,服务端以其重算值为准(mock 返回固定 bundleDiscount,断言只计入一次)。gift_membership_original_threshold 单笔达标赠会员)仍生效(该逻辑未触碰,作回归断言)。提交:feat(bundle): 下单后端权威重算套餐优惠,前端折扣仅规则优惠
新建 utils/bundle-pricing.js(纯函数,供展示汇总与单测):
summarizeBundlePromo(bundlePromo) → { originalTotal, payPrice, saving, rows: [{name, times, saving, ...}] }(防御空值)。新建 __tests__/utils/bundle-pricing.test.js:空值/正常/多行汇总。
cart.vue:
result.bundlePromo(替换 316 行 giftPromo)。totalAmount()(177-184):if bundlePromo && bundlePromo.active && discount>0 → originalTotalAmount − bundlePromo.discount。bundleList:套餐名 ×times,原价合计 → 套餐价,省 X 元)+ 合计“已按套餐价结算,共优惠 Y 元”。无命中不显示。checkoutBundlePromo(424 行替换 checkoutGiftPromo)。checkout.vue:
checkoutBundlePromo(648 行)。giftPromoDiscount (576) → bundlePromoDiscount(读 bundlePromo.discount)。discountAmount: (discountInfo ? discountInfo.discountAmount : 0)——去掉 + giftPromoDiscount(套餐优惠由后端权威重算)。calcAmount/合计展示扣减 bundlePromoDiscount。注意:小程序改动需你自行构建(npm run build:mp-weixin 或既有流程)后在微信开发者工具上传;后端部署即可生效(旧小程序在过渡期看到 giftPromo.active=false、不吃亏,见设计“过渡安全”说明)。
提交(在远程 cfc-frontend 内):feat(bundle): 购物车/结算页套餐优惠展示,提交仅传规则优惠
cd /app/cfc/cfc-backend && mvn -q test(跑全部单测,含新增);mvn clean package -Dmaven.test.skip=true -Dmaven.compile.fork=true。git status、git fetch && git pull --rebase origin cfclub、备份 /app/pro/cfc、健康基线 curl。systemctl restart cfc-backend → 日志检查(Tomcat started、0 ERROR)。/api/cart/list 返回 bundlePromo;giftPromo.active=false。moneyAmount = 服务端重算值(与展示一致);篡改前端 discountAmount 无效。## 验证记录 或口头汇报。cart/list 开始返回 bundlePromo、giftPromo 置空;下单后端权威重算。旧小程序:giftPromo.active=false → 不显示一口价、total 不减 → 提交的 discountAmount 无套餐部分 → 服务端补套餐优惠,实付=服务端值(安全,不会出现“旧前端多打折”)→ 请尽快构建上传新小程序以保证展示一致。bundle_sku_id 配置走 SQL(示例见设计文档 §6)。attachBundleMatches(技术债保留)、不改 InventoryService 出库联动、不动送会员权益触发。