@pisell/pisellos 2.1.58 → 2.1.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/model/index.d.ts +1 -0
- package/dist/model/index.js +1 -0
- package/dist/model/strategy/adapter/index.d.ts +3 -0
- package/dist/model/strategy/adapter/index.js +4 -0
- package/dist/model/strategy/adapter/type.d.ts +28 -0
- package/dist/model/strategy/adapter/type.js +1 -0
- package/dist/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
- package/dist/model/strategy/adapter/walletPass/evaluator.js +496 -0
- package/dist/model/strategy/adapter/walletPass/example.d.ts +4 -0
- package/dist/model/strategy/adapter/walletPass/example.js +258 -0
- package/dist/model/strategy/adapter/walletPass/index.d.ts +32 -0
- package/dist/model/strategy/adapter/walletPass/index.js +182 -0
- package/dist/model/strategy/adapter/walletPass/locales.d.ts +1 -0
- package/dist/model/strategy/adapter/walletPass/locales.js +23 -0
- package/dist/model/strategy/adapter/walletPass/type.d.ts +163 -0
- package/dist/model/strategy/adapter/walletPass/type.js +1 -0
- package/dist/model/strategy/adapter/walletPass/utils.d.ts +50 -0
- package/dist/model/strategy/adapter/walletPass/utils.js +965 -0
- package/dist/model/strategy/index.d.ts +94 -0
- package/dist/model/strategy/index.js +551 -0
- package/dist/model/strategy/strategy-example.d.ts +5 -0
- package/dist/model/strategy/strategy-example.js +331 -0
- package/dist/model/strategy/type.d.ts +228 -0
- package/dist/model/strategy/type.js +94 -0
- package/dist/modules/Cart/types.d.ts +2 -0
- package/dist/modules/Cart/utils/cartProduct.js +11 -0
- package/dist/modules/Discount/types.d.ts +17 -0
- package/dist/modules/Payment/index.d.ts +2 -1
- package/dist/modules/Payment/index.js +10 -7
- package/dist/modules/Payment/utils.js +3 -0
- package/dist/modules/Payment/walletpass.d.ts +23 -0
- package/dist/modules/Payment/walletpass.js +191 -95
- package/dist/modules/Product/index.d.ts +1 -1
- package/dist/modules/Rules/index.d.ts +8 -3
- package/dist/modules/Rules/index.js +152 -26
- package/dist/modules/Rules/types.d.ts +1 -0
- package/dist/modules/Summary/types.d.ts +2 -0
- package/dist/modules/Summary/utils.d.ts +6 -0
- package/dist/modules/Summary/utils.js +21 -0
- package/dist/plugins/window.d.ts +2 -0
- package/dist/solution/BookingByStep/index.d.ts +1 -0
- package/dist/solution/BookingTicket/index.d.ts +1 -1
- package/dist/solution/Checkout/index.js +2 -0
- package/dist/solution/ShopDiscount/index.d.ts +1 -0
- package/dist/solution/ShopDiscount/index.js +11 -1
- package/dist/solution/ShopDiscount/types.d.ts +1 -0
- package/dist/solution/ShopDiscount/utils.js +43 -14
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -1
- package/lib/model/index.d.ts +1 -0
- package/lib/model/index.js +23 -0
- package/lib/model/strategy/adapter/index.d.ts +3 -0
- package/lib/model/strategy/adapter/index.js +45 -0
- package/lib/model/strategy/adapter/type.d.ts +28 -0
- package/lib/model/strategy/adapter/type.js +17 -0
- package/lib/model/strategy/adapter/walletPass/evaluator.d.ts +84 -0
- package/lib/model/strategy/adapter/walletPass/evaluator.js +420 -0
- package/lib/model/strategy/adapter/walletPass/example.d.ts +4 -0
- package/lib/model/strategy/adapter/walletPass/example.js +207 -0
- package/lib/model/strategy/adapter/walletPass/index.d.ts +32 -0
- package/lib/model/strategy/adapter/walletPass/index.js +142 -0
- package/lib/model/strategy/adapter/walletPass/locales.d.ts +1 -0
- package/lib/model/strategy/adapter/walletPass/locales.js +51 -0
- package/lib/model/strategy/adapter/walletPass/type.d.ts +163 -0
- package/lib/model/strategy/adapter/walletPass/type.js +17 -0
- package/lib/model/strategy/adapter/walletPass/utils.d.ts +50 -0
- package/lib/model/strategy/adapter/walletPass/utils.js +660 -0
- package/lib/model/strategy/index.d.ts +94 -0
- package/lib/model/strategy/index.js +413 -0
- package/lib/model/strategy/strategy-example.d.ts +5 -0
- package/lib/model/strategy/strategy-example.js +318 -0
- package/lib/model/strategy/type.d.ts +228 -0
- package/lib/model/strategy/type.js +44 -0
- package/lib/modules/Cart/types.d.ts +2 -0
- package/lib/modules/Cart/utils/cartProduct.js +9 -0
- package/lib/modules/Discount/types.d.ts +17 -0
- package/lib/modules/Payment/index.d.ts +2 -1
- package/lib/modules/Payment/index.js +1 -0
- package/lib/modules/Payment/utils.js +3 -0
- package/lib/modules/Payment/walletpass.d.ts +23 -0
- package/lib/modules/Payment/walletpass.js +94 -17
- package/lib/modules/Product/index.d.ts +1 -1
- package/lib/modules/Rules/index.d.ts +8 -3
- package/lib/modules/Rules/index.js +370 -192
- package/lib/modules/Rules/types.d.ts +1 -0
- package/lib/modules/Summary/types.d.ts +2 -0
- package/lib/modules/Summary/utils.d.ts +6 -0
- package/lib/modules/Summary/utils.js +15 -0
- package/lib/plugins/window.d.ts +2 -0
- package/lib/solution/BookingByStep/index.d.ts +1 -0
- package/lib/solution/BookingTicket/index.d.ts +1 -1
- package/lib/solution/BookingTicket/index.js +0 -6
- package/lib/solution/Checkout/index.js +2 -0
- package/lib/solution/ShopDiscount/index.d.ts +1 -0
- package/lib/solution/ShopDiscount/index.js +6 -0
- package/lib/solution/ShopDiscount/types.d.ts +1 -0
- package/lib/solution/ShopDiscount/utils.js +22 -9
- package/package.json +1 -1
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/model/strategy/adapter/walletPass/evaluator.ts
|
|
30
|
+
var evaluator_exports = {};
|
|
31
|
+
__export(evaluator_exports, {
|
|
32
|
+
WalletPassEvaluator: () => WalletPassEvaluator
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(evaluator_exports);
|
|
35
|
+
var import__ = require("../../index");
|
|
36
|
+
var import_utils = require("./utils");
|
|
37
|
+
var import_locales = require("./locales");
|
|
38
|
+
var import_decimal = __toESM(require("decimal.js"));
|
|
39
|
+
var defaultStrategyMetadataCustom = {
|
|
40
|
+
maxDeductionAmount: 99999,
|
|
41
|
+
maxUsagePerOrder: 0,
|
|
42
|
+
allowCrossProduct: true,
|
|
43
|
+
applicableProductLimit: 0,
|
|
44
|
+
deductTaxAndFee: true
|
|
45
|
+
};
|
|
46
|
+
var WalletPassEvaluator = class {
|
|
47
|
+
constructor() {
|
|
48
|
+
this.strategyConfigs = [];
|
|
49
|
+
this.locale = "en";
|
|
50
|
+
this.engine = new import__.StrategyEngine({
|
|
51
|
+
debug: false,
|
|
52
|
+
enableTrace: false
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
setStrategyConfigs(strategyConfigs) {
|
|
56
|
+
this.strategyConfigs = strategyConfigs;
|
|
57
|
+
}
|
|
58
|
+
getStrategyConfigs() {
|
|
59
|
+
return this.strategyConfigs;
|
|
60
|
+
}
|
|
61
|
+
setLocale(locale) {
|
|
62
|
+
this.locale = locale;
|
|
63
|
+
}
|
|
64
|
+
getText(key) {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
return (_b = (_a = import_locales.locales) == null ? void 0 : _a[this.locale]) == null ? void 0 : _b[key];
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* 搜索券的格式
|
|
70
|
+
* @param input 输入
|
|
71
|
+
* @returns 返回搜索券的格式
|
|
72
|
+
*/
|
|
73
|
+
searchVoucherFormat(input) {
|
|
74
|
+
const results = this.evaluate(input);
|
|
75
|
+
return results.map((item) => {
|
|
76
|
+
var _a, _b;
|
|
77
|
+
if (item.isApplicable) {
|
|
78
|
+
return {
|
|
79
|
+
...item.voucher,
|
|
80
|
+
config: {
|
|
81
|
+
...(_a = item == null ? void 0 : item.strategyResult) == null ? void 0 : _a.config.metadata.custom
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
} else {
|
|
85
|
+
return {
|
|
86
|
+
...item.voucher,
|
|
87
|
+
config: {
|
|
88
|
+
...(_b = item == null ? void 0 : item.strategyResult) == null ? void 0 : _b.config.metadata.custom
|
|
89
|
+
},
|
|
90
|
+
reason: item.reason,
|
|
91
|
+
reasonCode: item.reasonCode
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 评估可用的 vouchers
|
|
98
|
+
*/
|
|
99
|
+
evaluate(input) {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
const { orderTotalAmount, products, vouchers, strategyConfigs } = input;
|
|
102
|
+
const productIds = products.map((p) => p.product_id);
|
|
103
|
+
const results = [];
|
|
104
|
+
for (const voucher of vouchers) {
|
|
105
|
+
const applicableStrategies = this.findApplicableStrategies(
|
|
106
|
+
voucher,
|
|
107
|
+
strategyConfigs || this.strategyConfigs
|
|
108
|
+
);
|
|
109
|
+
if (applicableStrategies.length === 0) {
|
|
110
|
+
results.push({
|
|
111
|
+
voucher,
|
|
112
|
+
isApplicable: true,
|
|
113
|
+
canUseCount: 0,
|
|
114
|
+
maxDeduction: 0,
|
|
115
|
+
deductTaxAndFee: true,
|
|
116
|
+
applicableProductIds: [],
|
|
117
|
+
reason: "",
|
|
118
|
+
reasonCode: "",
|
|
119
|
+
strategyResult: {
|
|
120
|
+
config: {
|
|
121
|
+
metadata: {
|
|
122
|
+
custom: {
|
|
123
|
+
...defaultStrategyMetadataCustom
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const strategy = applicableStrategies[0];
|
|
132
|
+
const applicableProductIds = (0, import_utils.getApplicableProductIds)(voucher);
|
|
133
|
+
const { applicableTotal, applicableCount } = this.calculateApplicableProducts(
|
|
134
|
+
products,
|
|
135
|
+
applicableProductIds,
|
|
136
|
+
((_b = (_a = strategy == null ? void 0 : strategy.metadata) == null ? void 0 : _a.custom) == null ? void 0 : _b.deductTaxAndFee) ?? true
|
|
137
|
+
);
|
|
138
|
+
const context = {
|
|
139
|
+
entities: {
|
|
140
|
+
voucher,
|
|
141
|
+
products,
|
|
142
|
+
order: {
|
|
143
|
+
orderTotalAmount,
|
|
144
|
+
products
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
attributes: {
|
|
148
|
+
orderTotalAmount,
|
|
149
|
+
applicableProductTotalAmount: applicableTotal,
|
|
150
|
+
applicableProductCount: applicableCount,
|
|
151
|
+
voucherProductId: voucher.product_id,
|
|
152
|
+
productIds
|
|
153
|
+
},
|
|
154
|
+
metadata: {
|
|
155
|
+
timestamp: Date.now()
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const evaluationResult = this.engine.evaluate(strategy, context);
|
|
159
|
+
const businessConfig = strategy.metadata.custom || {};
|
|
160
|
+
const maxDeduction = businessConfig.maxDeductionAmount || 0;
|
|
161
|
+
const canUseCount = businessConfig.maxUsagePerOrder || 0;
|
|
162
|
+
const deductTaxAndFee = businessConfig.deductTaxAndFee ?? true;
|
|
163
|
+
if (evaluationResult.applicable) {
|
|
164
|
+
results.push({
|
|
165
|
+
voucher,
|
|
166
|
+
isApplicable: true,
|
|
167
|
+
canUseCount,
|
|
168
|
+
maxDeduction,
|
|
169
|
+
deductTaxAndFee,
|
|
170
|
+
applicableProductIds: applicableProductIds || [],
|
|
171
|
+
strategyResult: evaluationResult
|
|
172
|
+
});
|
|
173
|
+
} else {
|
|
174
|
+
let reasonCode = "not_meet_the_required_conditions";
|
|
175
|
+
if (evaluationResult.code) {
|
|
176
|
+
const codeMapping = {
|
|
177
|
+
INSUFFICIENT_AMOUNT: "not_meet_the_required_conditions",
|
|
178
|
+
USAGE_LIMIT_EXCEEDED: "usage_limit_reached",
|
|
179
|
+
CHANNEL_NOT_ALLOWED: "not_available_for_this_channel",
|
|
180
|
+
ORDER_TYPE_NOT_ALLOWED: "not_valid_for_this_order_type",
|
|
181
|
+
DEDUCTION_LIMIT_EXCEEDED: "exceeds_the_maximum_deduction_limit"
|
|
182
|
+
};
|
|
183
|
+
reasonCode = codeMapping[evaluationResult.code] || "not_meet_the_required_conditions";
|
|
184
|
+
}
|
|
185
|
+
results.push({
|
|
186
|
+
voucher,
|
|
187
|
+
isApplicable: false,
|
|
188
|
+
canUseCount: 0,
|
|
189
|
+
maxDeduction: 0,
|
|
190
|
+
deductTaxAndFee: false,
|
|
191
|
+
applicableProductIds: [],
|
|
192
|
+
reason: this.getText(reasonCode),
|
|
193
|
+
reasonCode,
|
|
194
|
+
strategyResult: evaluationResult
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return results;
|
|
199
|
+
}
|
|
200
|
+
/** 检查代金券/折扣卡是否可用 并返回config */
|
|
201
|
+
checkVoucherAvailability(input) {
|
|
202
|
+
var _a, _b, _c, _d, _e;
|
|
203
|
+
const { orderTotalAmount, products, vouchers } = input;
|
|
204
|
+
const results = this.evaluate(input);
|
|
205
|
+
const isAvailable = ((_a = results[0]) == null ? void 0 : _a.isApplicable) || false;
|
|
206
|
+
return {
|
|
207
|
+
isAvailable,
|
|
208
|
+
config: {
|
|
209
|
+
...(_e = (_d = (_c = (_b = results[0]) == null ? void 0 : _b.strategyResult) == null ? void 0 : _c.config) == null ? void 0 : _d.metadata) == null ? void 0 : _e.custom
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* 获取推荐券
|
|
215
|
+
* @param input 输入
|
|
216
|
+
* @returns 返回推荐券列表
|
|
217
|
+
*/
|
|
218
|
+
getRecommendedVouchers(input) {
|
|
219
|
+
const { orderTotalAmount, products, vouchers } = input;
|
|
220
|
+
const noApplicableVoucher = [];
|
|
221
|
+
const applicableVoucher = [];
|
|
222
|
+
vouchers.forEach((voucher) => {
|
|
223
|
+
if (voucher.unified_available_status === 1) {
|
|
224
|
+
applicableVoucher.push(voucher);
|
|
225
|
+
} else {
|
|
226
|
+
noApplicableVoucher.push(voucher);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
const results = this.evaluate({
|
|
230
|
+
...input,
|
|
231
|
+
vouchers: applicableVoucher
|
|
232
|
+
});
|
|
233
|
+
const newVouchers = [];
|
|
234
|
+
results.forEach((item) => {
|
|
235
|
+
var _a, _b, _c;
|
|
236
|
+
if (item.isApplicable) {
|
|
237
|
+
newVouchers.push({
|
|
238
|
+
...item.voucher,
|
|
239
|
+
config: {
|
|
240
|
+
...defaultStrategyMetadataCustom,
|
|
241
|
+
...((_c = (_b = (_a = item == null ? void 0 : item.strategyResult) == null ? void 0 : _a.config) == null ? void 0 : _b.metadata) == null ? void 0 : _c.custom) || {}
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
} else {
|
|
245
|
+
noApplicableVoucher.push({
|
|
246
|
+
...item.voucher,
|
|
247
|
+
_unified_available_status: 0,
|
|
248
|
+
reason: item.reason,
|
|
249
|
+
reasonCode: item.reasonCode
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
const { recommended, transformList } = this.processVouchers(
|
|
254
|
+
newVouchers,
|
|
255
|
+
orderTotalAmount,
|
|
256
|
+
products
|
|
257
|
+
);
|
|
258
|
+
const recommendedAmount = recommended.reduce((sum, item) => {
|
|
259
|
+
return new import_decimal.default(sum).add(item._available_max_amount || 0).toNumber();
|
|
260
|
+
}, 0);
|
|
261
|
+
return {
|
|
262
|
+
// 推荐券总金额
|
|
263
|
+
recommendedAmount,
|
|
264
|
+
// 推荐券列表
|
|
265
|
+
recommended,
|
|
266
|
+
// 格式化后的可用券列表
|
|
267
|
+
transformList,
|
|
268
|
+
// 后端不可用券列表 和前端基于策略返回的 不可用列表 合并
|
|
269
|
+
noApplicableVoucher
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
enhanceWithReason(voucher) {
|
|
273
|
+
if (voucher._unified_available_status === 0 && voucher.reasonCode) {
|
|
274
|
+
return {
|
|
275
|
+
...voucher,
|
|
276
|
+
reason: this.getText(voucher.reasonCode)
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
return voucher;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* 优惠券处理函数
|
|
283
|
+
* @param applicableVouchers 可用的券列表
|
|
284
|
+
* @param orderTotalAmount 订单总金额
|
|
285
|
+
* @param products 订单商品列表
|
|
286
|
+
* @returns 返回推荐券列表和全部列表,每个券包含 _available_max_amount 和 _unified_available_status
|
|
287
|
+
*/
|
|
288
|
+
processVouchers(vouchers, orderTotalAmount, products) {
|
|
289
|
+
const result = (0, import_utils.processVouchers)(vouchers, orderTotalAmount, products);
|
|
290
|
+
return {
|
|
291
|
+
recommended: result.recommended.map(
|
|
292
|
+
(item) => this.enhanceWithReason(item)
|
|
293
|
+
),
|
|
294
|
+
transformList: result.transformList.map(
|
|
295
|
+
(item) => this.enhanceWithReason(item)
|
|
296
|
+
)
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* 重新计算优惠券状态(基于已选券的增量计算)
|
|
301
|
+
* @param allVouchers 所有原始券列表
|
|
302
|
+
* @param selectedVouchers 已选中的券列表(按选中顺序)
|
|
303
|
+
* @param orderTotalAmount 订单总金额
|
|
304
|
+
* @param products 订单商品列表
|
|
305
|
+
* @returns 返回更新后的所有券列表和已选券的详细抵扣信息
|
|
306
|
+
*/
|
|
307
|
+
recalculateVouchers(allVouchers, selectedVouchers, orderTotalAmount, products) {
|
|
308
|
+
const result = (0, import_utils.recalculateVouchers)(
|
|
309
|
+
allVouchers,
|
|
310
|
+
selectedVouchers,
|
|
311
|
+
orderTotalAmount,
|
|
312
|
+
products
|
|
313
|
+
);
|
|
314
|
+
return {
|
|
315
|
+
allWithUpdatedStatus: result.allWithUpdatedStatus.map(
|
|
316
|
+
(item) => this.enhanceWithReason(item)
|
|
317
|
+
),
|
|
318
|
+
selectedWithDetails: result.selectedWithDetails.map(
|
|
319
|
+
(item) => this.enhanceWithReason(item)
|
|
320
|
+
)
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* 查找适用于指定 voucher 的策略配置
|
|
325
|
+
*/
|
|
326
|
+
findApplicableStrategies(voucher, strategyConfigs) {
|
|
327
|
+
return strategyConfigs.filter((config) => {
|
|
328
|
+
return this.checkVoucherInStrategy(voucher.product_id, config);
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* 检查策略中是否包含指定的 voucher ID
|
|
333
|
+
*/
|
|
334
|
+
checkVoucherInStrategy(voucherProductId, config) {
|
|
335
|
+
const rules = config.conditions.rules;
|
|
336
|
+
for (const rule of rules) {
|
|
337
|
+
if ("operator" in rule && "rules" in rule) {
|
|
338
|
+
if (this.checkVoucherInConditionGroup(voucherProductId, rule)) {
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
} else {
|
|
342
|
+
const conditionRule = rule;
|
|
343
|
+
if (conditionRule.field === "voucherProductId" && conditionRule.operator === "in" && Array.isArray(conditionRule.value)) {
|
|
344
|
+
if (conditionRule.value.includes(voucherProductId)) {
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (conditionRule.field === "voucherProductId" && (conditionRule.operator === "=" || conditionRule.operator === "==")) {
|
|
349
|
+
if (conditionRule.value === voucherProductId) {
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* 递归检查条件组中是否包含 voucher ID
|
|
359
|
+
*/
|
|
360
|
+
checkVoucherInConditionGroup(voucherProductId, group) {
|
|
361
|
+
for (const rule of group.rules) {
|
|
362
|
+
if ("operator" in rule && "rules" in rule) {
|
|
363
|
+
if (this.checkVoucherInConditionGroup(voucherProductId, rule)) {
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
} else {
|
|
367
|
+
if (rule.field === "voucherProductId" && rule.operator === "in" && Array.isArray(rule.value)) {
|
|
368
|
+
if (rule.value.includes(voucherProductId)) {
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (rule.field === "voucherProductId" && (rule.operator === "=" || rule.operator === "==")) {
|
|
373
|
+
if (rule.value === voucherProductId) {
|
|
374
|
+
return true;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* 计算适用商品的总金额和数量
|
|
383
|
+
*/
|
|
384
|
+
calculateApplicableProducts(products, applicableProductIds, deductTaxAndFee) {
|
|
385
|
+
let total = 0;
|
|
386
|
+
let count = 0;
|
|
387
|
+
if (applicableProductIds !== null && applicableProductIds.length === 0) {
|
|
388
|
+
return { applicableTotal: 0, applicableCount: 0 };
|
|
389
|
+
}
|
|
390
|
+
products.forEach((product) => {
|
|
391
|
+
const productQuantity = (0, import_utils.getProductQuantity)(product);
|
|
392
|
+
const isMainProductApplicable = applicableProductIds === null || applicableProductIds.includes(product.product_id);
|
|
393
|
+
if (isMainProductApplicable) {
|
|
394
|
+
total += (0, import_utils.getMainProductPrice)(product, deductTaxAndFee) * productQuantity;
|
|
395
|
+
count += productQuantity;
|
|
396
|
+
}
|
|
397
|
+
if (product.product_bundle && product.product_bundle.length > 0) {
|
|
398
|
+
product.product_bundle.forEach((bundleItem) => {
|
|
399
|
+
if ((0, import_utils.getBundleItemIsOriginalPrice)(bundleItem)) {
|
|
400
|
+
const isBundleItemApplicable = applicableProductIds === null || applicableProductIds.includes(bundleItem.bundle_product_id);
|
|
401
|
+
if (isBundleItemApplicable) {
|
|
402
|
+
const bundleItemQuantity = bundleItem.num * productQuantity;
|
|
403
|
+
total += (0, import_utils.getBundleItemPrice)(
|
|
404
|
+
bundleItem,
|
|
405
|
+
productQuantity,
|
|
406
|
+
deductTaxAndFee
|
|
407
|
+
);
|
|
408
|
+
count += bundleItemQuantity;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
return { applicableTotal: total, applicableCount: count };
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
418
|
+
0 && (module.exports = {
|
|
419
|
+
WalletPassEvaluator
|
|
420
|
+
});
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// src/model/strategy/adapter/walletPass/example.ts
|
|
2
|
+
var import_evaluator = require("./evaluator");
|
|
3
|
+
var products = [
|
|
4
|
+
{
|
|
5
|
+
product_id: 60734,
|
|
6
|
+
price: 80,
|
|
7
|
+
quantity: 1,
|
|
8
|
+
name: "商品A"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
product_id: 60735,
|
|
12
|
+
price: 50,
|
|
13
|
+
quantity: 2,
|
|
14
|
+
name: "商品B"
|
|
15
|
+
}
|
|
16
|
+
];
|
|
17
|
+
var vouchers = [
|
|
18
|
+
{
|
|
19
|
+
id: "VOUCHER_001",
|
|
20
|
+
amount: 10,
|
|
21
|
+
balance: 10,
|
|
22
|
+
type: "cash_voucher"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: "VOUCHER_002",
|
|
26
|
+
amount: 20,
|
|
27
|
+
balance: 20,
|
|
28
|
+
type: "cash_voucher"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: "VOUCHER_003",
|
|
32
|
+
amount: 50,
|
|
33
|
+
balance: 50,
|
|
34
|
+
type: "cash_voucher"
|
|
35
|
+
}
|
|
36
|
+
];
|
|
37
|
+
var strategyConfigs = [
|
|
38
|
+
// 策略1: 满100减10,适用于 VOUCHER_001
|
|
39
|
+
{
|
|
40
|
+
metadata: {
|
|
41
|
+
id: "STRATEGY_001",
|
|
42
|
+
name: "满100减10",
|
|
43
|
+
type: "wallet_pass",
|
|
44
|
+
custom: {
|
|
45
|
+
maxDeductionAmount: 10,
|
|
46
|
+
// 最大抵扣10元
|
|
47
|
+
maxUsagePerOrder: 1,
|
|
48
|
+
// 每单最多用1张
|
|
49
|
+
deductTaxAndFee: true,
|
|
50
|
+
// 抵扣税费
|
|
51
|
+
applicableProductIds: [60734, 60735]
|
|
52
|
+
// 适用于这两个商品
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
conditions: {
|
|
56
|
+
operator: "and",
|
|
57
|
+
rules: [
|
|
58
|
+
// 匹配 voucher ID
|
|
59
|
+
{
|
|
60
|
+
type: "operator",
|
|
61
|
+
field: "voucherId",
|
|
62
|
+
operator: "in",
|
|
63
|
+
value: ["VOUCHER_001"]
|
|
64
|
+
},
|
|
65
|
+
// 适用商品总额需 >= 100
|
|
66
|
+
{
|
|
67
|
+
type: "operator",
|
|
68
|
+
field: "applicableProductTotal",
|
|
69
|
+
operator: ">=",
|
|
70
|
+
value: 100
|
|
71
|
+
}
|
|
72
|
+
],
|
|
73
|
+
actionIds: ["deduct_10"]
|
|
74
|
+
},
|
|
75
|
+
actions: [
|
|
76
|
+
{
|
|
77
|
+
id: "deduct_10",
|
|
78
|
+
type: "DEDUCT_AMOUNT",
|
|
79
|
+
value: 10,
|
|
80
|
+
target: "applicable_products",
|
|
81
|
+
priority: 1
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
},
|
|
85
|
+
// 策略2: 满150减20,适用于 VOUCHER_002
|
|
86
|
+
{
|
|
87
|
+
metadata: {
|
|
88
|
+
id: "STRATEGY_002",
|
|
89
|
+
name: "满150减20",
|
|
90
|
+
type: "wallet_pass",
|
|
91
|
+
custom: {
|
|
92
|
+
maxDeductionAmount: 20,
|
|
93
|
+
maxUsagePerOrder: 1,
|
|
94
|
+
deductTaxAndFee: true,
|
|
95
|
+
applicableProductIds: [60734, 60735]
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
conditions: {
|
|
99
|
+
operator: "and",
|
|
100
|
+
rules: [
|
|
101
|
+
{
|
|
102
|
+
type: "operator",
|
|
103
|
+
field: "voucherId",
|
|
104
|
+
operator: "in",
|
|
105
|
+
value: ["VOUCHER_002"]
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
type: "operator",
|
|
109
|
+
field: "applicableProductTotal",
|
|
110
|
+
operator: ">=",
|
|
111
|
+
value: 150
|
|
112
|
+
}
|
|
113
|
+
],
|
|
114
|
+
actionIds: ["deduct_20"]
|
|
115
|
+
},
|
|
116
|
+
actions: [
|
|
117
|
+
{
|
|
118
|
+
id: "deduct_20",
|
|
119
|
+
type: "DEDUCT_AMOUNT",
|
|
120
|
+
value: 20,
|
|
121
|
+
target: "applicable_products",
|
|
122
|
+
priority: 1
|
|
123
|
+
}
|
|
124
|
+
]
|
|
125
|
+
},
|
|
126
|
+
// 策略3: 满200减50,适用于 VOUCHER_003
|
|
127
|
+
{
|
|
128
|
+
metadata: {
|
|
129
|
+
id: "STRATEGY_003",
|
|
130
|
+
name: "满200减50",
|
|
131
|
+
type: "wallet_pass",
|
|
132
|
+
custom: {
|
|
133
|
+
maxDeductionAmount: 50,
|
|
134
|
+
maxUsagePerOrder: 2,
|
|
135
|
+
deductTaxAndFee: false,
|
|
136
|
+
applicableProductIds: [60734, 60735]
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
conditions: {
|
|
140
|
+
operator: "and",
|
|
141
|
+
rules: [
|
|
142
|
+
{
|
|
143
|
+
type: "operator",
|
|
144
|
+
field: "voucherId",
|
|
145
|
+
operator: "in",
|
|
146
|
+
value: ["VOUCHER_003"]
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
type: "operator",
|
|
150
|
+
field: "applicableProductTotal",
|
|
151
|
+
operator: ">=",
|
|
152
|
+
value: 200
|
|
153
|
+
}
|
|
154
|
+
],
|
|
155
|
+
actionIds: ["deduct_50"]
|
|
156
|
+
},
|
|
157
|
+
actions: [
|
|
158
|
+
{
|
|
159
|
+
id: "deduct_50",
|
|
160
|
+
type: "DEDUCT_AMOUNT",
|
|
161
|
+
value: 50,
|
|
162
|
+
target: "applicable_products",
|
|
163
|
+
priority: 1
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
}
|
|
167
|
+
];
|
|
168
|
+
function runExample() {
|
|
169
|
+
console.log("========== WalletPass 评估器示例 ==========\n");
|
|
170
|
+
const evaluator = new import_evaluator.WalletPassEvaluator();
|
|
171
|
+
const results = evaluator.evaluate({
|
|
172
|
+
orderTotalAmount: 180,
|
|
173
|
+
products,
|
|
174
|
+
vouchers,
|
|
175
|
+
strategyConfigs
|
|
176
|
+
});
|
|
177
|
+
console.log("订单信息:");
|
|
178
|
+
console.log(`- 总金额: ${180} 元`);
|
|
179
|
+
console.log(`- 商品: ${products.map((p) => `${p.name}(${p.quantity}件)`).join(", ")}
|
|
180
|
+
`);
|
|
181
|
+
console.log("评估结果:\n");
|
|
182
|
+
results.forEach((result, index) => {
|
|
183
|
+
console.log(`Voucher ${index + 1}: ${result.voucher.id}`);
|
|
184
|
+
console.log(` 面额: ${result.voucher.amount} 元`);
|
|
185
|
+
console.log(` 可用: ${result.isApplicable ? "✅ 是" : "❌ 否"}`);
|
|
186
|
+
if (result.isApplicable) {
|
|
187
|
+
console.log(` 最大抵扣: ${result.maxDeduction} 元`);
|
|
188
|
+
console.log(` 每单可用: ${result.canUseCount} 张`);
|
|
189
|
+
console.log(` 抵扣税费: ${result.deductTaxAndFee ? "是" : "否"}`);
|
|
190
|
+
console.log(` 适用商品: ${result.applicableProductIds.join(", ")}`);
|
|
191
|
+
} else {
|
|
192
|
+
console.log(` 原因: ${result.reason}`);
|
|
193
|
+
console.log(` 原因码: ${result.reasonCode}`);
|
|
194
|
+
}
|
|
195
|
+
console.log("");
|
|
196
|
+
});
|
|
197
|
+
const availableVouchers = results.filter((r) => r.isApplicable);
|
|
198
|
+
console.log(`
|
|
199
|
+
共 ${availableVouchers.length} 张可用`);
|
|
200
|
+
if (availableVouchers.length > 0) {
|
|
201
|
+
console.log("可用 voucher ID:");
|
|
202
|
+
availableVouchers.forEach((v) => {
|
|
203
|
+
console.log(` - ${v.voucher.id}: 最多抵扣 ${v.maxDeduction} 元`);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
runExample();
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { EvaluationResult, RuntimeContext } from '../../type';
|
|
2
|
+
import { BusinessAdapter } from '../type';
|
|
3
|
+
import { WalletPassBusinessData, WalletPassTransformResult } from './type';
|
|
4
|
+
/**
|
|
5
|
+
* Wallet Pass 适配器
|
|
6
|
+
* 用于将 wallet pass 业务数据转换为策略引擎可识别的格式
|
|
7
|
+
*/
|
|
8
|
+
export default class WalletPassAdapter implements BusinessAdapter {
|
|
9
|
+
name: string;
|
|
10
|
+
version: string;
|
|
11
|
+
/**
|
|
12
|
+
* 准备运行时上下文
|
|
13
|
+
* 将业务数据转换为策略引擎可识别的 RuntimeContext
|
|
14
|
+
*/
|
|
15
|
+
prepareContext(businessData: WalletPassBusinessData): RuntimeContext;
|
|
16
|
+
/**
|
|
17
|
+
* 转换执行结果
|
|
18
|
+
* 将策略引擎的通用结果转换为业务层需要的格式
|
|
19
|
+
*/
|
|
20
|
+
transformResult(result: EvaluationResult, businessData?: WalletPassBusinessData): WalletPassTransformResult;
|
|
21
|
+
/**
|
|
22
|
+
* 格式化配置
|
|
23
|
+
*/
|
|
24
|
+
formatConfig(result: EvaluationResult, businessData?: WalletPassBusinessData): {
|
|
25
|
+
result: EvaluationResult;
|
|
26
|
+
businessData?: WalletPassBusinessData;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* 计算适用商品的总金额和数量
|
|
30
|
+
*/
|
|
31
|
+
private calculateApplicableProducts;
|
|
32
|
+
}
|