UNPKG

@pisell/pisellos

Version:

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

143 lines (141 loc) 5.26 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/model/strategy/adapter/walletPass/index.ts var walletPass_exports = {}; __export(walletPass_exports, { default: () => WalletPassAdapter }); module.exports = __toCommonJS(walletPass_exports); var import_utils = require("./utils"); var WalletPassAdapter = class { constructor() { this.name = "WalletPassAdapter"; this.version = "1.0.0"; } /** * 准备运行时上下文 * 将业务数据转换为策略引擎可识别的 RuntimeContext */ prepareContext(businessData) { var _a, _b; const { orderTotalAmount, products, voucher, strategyConfig } = businessData; const productIds = products.map((p) => p.product_id); const applicableProductIds = (0, import_utils.getApplicableProductIds)(voucher); const deductTaxAndFee = ((_b = (_a = strategyConfig == null ? void 0 : strategyConfig.metadata) == null ? void 0 : _a.custom) == null ? void 0 : _b.deductTaxAndFee) ?? true; const { applicableTotal, applicableCount } = this.calculateApplicableProducts( products, applicableProductIds, deductTaxAndFee ); return { entities: { voucher, products, order: { orderTotalAmount, products } }, attributes: { // 订单消费金额 orderTotalAmount, // 适用商品金额 applicableProductTotalAmount: applicableTotal, // 适用商品购买数量 applicableProductCount: applicableCount, // 当前评估的 voucher ID voucherProductId: voucher.product_id, // 订单中的商品ID productIds }, metadata: { timestamp: Date.now() } }; } /** * 转换执行结果 * 将策略引擎的通用结果转换为业务层需要的格式 */ transformResult(result, businessData) { const strategyConfig = businessData == null ? void 0 : businessData.strategyConfig; const businessConfig = (strategyConfig == null ? void 0 : strategyConfig.metadata.custom) || {}; const voucher = businessData == null ? void 0 : businessData.voucher; const applicableProductIds = voucher ? (0, import_utils.getApplicableProductIds)(voucher) : null; if (!result.applicable) { return { isApplicable: false, canUseCount: 0, maxDeduction: 0, deductTaxAndFee: false, applicableProductIds: [], reason: result.message, reasonCode: result.code, strategyResult: result }; } const maxDeduction = businessConfig.maxDeductionAmount || 0; const canUseCount = businessConfig.maxUsagePerOrder || 0; const deductTaxAndFee = businessConfig.deductTaxAndFee || false; return { isApplicable: true, canUseCount, maxDeduction, deductTaxAndFee, applicableProductIds: applicableProductIds || [], strategyResult: result }; } /** * 格式化配置 */ formatConfig(result, businessData) { return { result, businessData }; } /** * 计算适用商品的总金额和数量 */ calculateApplicableProducts(products, applicableProductIds, deductTaxAndFee = true) { let total = 0; let count = 0; if (applicableProductIds !== null && applicableProductIds.length === 0) { return { applicableTotal: 0, applicableCount: 0 }; } products.forEach((product) => { const productQuantity = (0, import_utils.getProductQuantity)(product); const isMainProductApplicable = applicableProductIds === null || applicableProductIds.includes(product.product_id); if (isMainProductApplicable) { total += (0, import_utils.getMainProductPrice)(product, deductTaxAndFee) * productQuantity; count += productQuantity; } if (product.product_bundle && product.product_bundle.length > 0) { product.product_bundle.forEach((bundleItem) => { if ((0, import_utils.getBundleItemIsOriginalPrice)(bundleItem)) { const isBundleItemApplicable = applicableProductIds === null || applicableProductIds.includes(bundleItem.bundle_product_id); if (isBundleItemApplicable) { const bundleItemQuantity = bundleItem.num * productQuantity; total += (0, import_utils.getBundleItemPrice)(bundleItem, productQuantity, deductTaxAndFee); count += bundleItemQuantity; } } }); } }); return { applicableTotal: total, applicableCount: count }; } };