UNPKG

@pisell/pisellos

Version:

一个可扩展的前端模块化SDK框架,支持插件系统

208 lines (207 loc) 4.95 kB
// src/model/strategy/adapter/walletPass/example.ts var import_evaluator = require("./evaluator"); var products = [ { product_id: 60734, price: 80, quantity: 1, name: "商品A" }, { product_id: 60735, price: 50, quantity: 2, name: "商品B" } ]; var vouchers = [ { id: "VOUCHER_001", amount: 10, balance: 10, type: "cash_voucher" }, { id: "VOUCHER_002", amount: 20, balance: 20, type: "cash_voucher" }, { id: "VOUCHER_003", amount: 50, balance: 50, type: "cash_voucher" } ]; var strategyConfigs = [ // 策略1: 满100减10,适用于 VOUCHER_001 { metadata: { id: "STRATEGY_001", name: "满100减10", type: "wallet_pass", custom: { maxDeductionAmount: 10, // 最大抵扣10元 maxUsagePerOrder: 1, // 每单最多用1张 deductTaxAndFee: true, // 抵扣税费 applicableProductIds: [60734, 60735] // 适用于这两个商品 } }, conditions: { operator: "and", rules: [ // 匹配 voucher ID { type: "operator", field: "voucherId", operator: "in", value: ["VOUCHER_001"] }, // 适用商品总额需 >= 100 { type: "operator", field: "applicableProductTotal", operator: ">=", value: 100 } ], actionIds: ["deduct_10"] }, actions: [ { id: "deduct_10", type: "DEDUCT_AMOUNT", value: 10, target: "applicable_products", priority: 1 } ] }, // 策略2: 满150减20,适用于 VOUCHER_002 { metadata: { id: "STRATEGY_002", name: "满150减20", type: "wallet_pass", custom: { maxDeductionAmount: 20, maxUsagePerOrder: 1, deductTaxAndFee: true, applicableProductIds: [60734, 60735] } }, conditions: { operator: "and", rules: [ { type: "operator", field: "voucherId", operator: "in", value: ["VOUCHER_002"] }, { type: "operator", field: "applicableProductTotal", operator: ">=", value: 150 } ], actionIds: ["deduct_20"] }, actions: [ { id: "deduct_20", type: "DEDUCT_AMOUNT", value: 20, target: "applicable_products", priority: 1 } ] }, // 策略3: 满200减50,适用于 VOUCHER_003 { metadata: { id: "STRATEGY_003", name: "满200减50", type: "wallet_pass", custom: { maxDeductionAmount: 50, maxUsagePerOrder: 2, deductTaxAndFee: false, applicableProductIds: [60734, 60735] } }, conditions: { operator: "and", rules: [ { type: "operator", field: "voucherId", operator: "in", value: ["VOUCHER_003"] }, { type: "operator", field: "applicableProductTotal", operator: ">=", value: 200 } ], actionIds: ["deduct_50"] }, actions: [ { id: "deduct_50", type: "DEDUCT_AMOUNT", value: 50, target: "applicable_products", priority: 1 } ] } ]; function runExample() { console.log("========== WalletPass 评估器示例 ==========\n"); const evaluator = new import_evaluator.WalletPassEvaluator(); const results = evaluator.evaluate({ orderTotalAmount: 180, products, vouchers, strategyConfigs }); console.log("订单信息:"); console.log(`- 总金额: ${180} 元`); console.log(`- 商品: ${products.map((p) => `${p.name}(${p.quantity}件)`).join(", ")} `); console.log("评估结果:\n"); results.forEach((result, index) => { console.log(`Voucher ${index + 1}: ${result.voucher.id}`); console.log(` 面额: ${result.voucher.amount} 元`); console.log(` 可用: ${result.isApplicable ? "✅ 是" : "❌ 否"}`); if (result.isApplicable) { console.log(` 最大抵扣: ${result.maxDeduction} 元`); console.log(` 每单可用: ${result.canUseCount} 张`); console.log(` 抵扣税费: ${result.deductTaxAndFee ? "是" : "否"}`); console.log(` 适用商品: ${result.applicableProductIds.join(", ")}`); } else { console.log(` 原因: ${result.reason}`); console.log(` 原因码: ${result.reasonCode}`); } console.log(""); }); const availableVouchers = results.filter((r) => r.isApplicable); console.log(` 共 ${availableVouchers.length} 张可用`); if (availableVouchers.length > 0) { console.log("可用 voucher ID:"); availableVouchers.forEach((v) => { console.log(` - ${v.voucher.id}: 最多抵扣 ${v.maxDeduction} 元`); }); } } runExample();