@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
331 lines (314 loc) • 7.25 kB
JavaScript
/**
* 策略模型使用示例
*/
import { createStrategyEngine, evaluate } from "./index";
// ============================================
// 示例1: 简单的代金券(单层条件)
// ============================================
var voucherConfig = {
metadata: {
id: 'VOUCHER_10',
name: '10元代金券',
type: 'wallet_pass',
calculationType: 'single'
},
conditions: {
operator: 'and',
rules: [{
type: 'operator',
dimension: 'scope_product',
field: 'productId',
operator: 'in',
value: [60734, 60735]
}, {
type: 'operator',
dimension: 'scope_channel',
field: 'channel',
operator: 'in',
value: ['pos', 'mini_program']
}, {
type: 'operator',
dimension: 'transaction',
field: 'applicableProductTotal',
operator: '>=',
value: 100
}, {
type: 'operator',
dimension: 'limit_usage',
field: 'perUserUsedCount',
operator: '<',
value: 1
}],
actionIds: ['deduct_10']
},
actions: [{
id: 'deduct_10',
type: 'DEDUCT_AMOUNT',
value: 10,
valueType: 'number',
valueUnit: '元',
target: 'applicable_products',
priority: 1,
config: {
allowCrossProduct: true,
deductTaxAndFee: true
}
}]
};
// 运行时上下文
var voucherContext = {
entities: {
order: {
id: 'ORDER_001',
total: 150,
items: [{
productId: 60734,
quantity: 2,
price: 75
}]
},
customer: {
id: 'USER_001',
level: 'vip'
}
},
attributes: {
productId: 60734,
channel: 'pos',
applicableProductTotal: 150,
perUserUsedCount: 0
},
metadata: {
timestamp: Date.now(),
locale: 'zh-CN'
}
};
// ============================================
// 示例2: 阶梯式满减(嵌套条件)
// ============================================
var tieredDiscountConfig = {
metadata: {
id: 'TIERED_DISCOUNT',
name: '阶梯满减',
type: 'promotion',
calculationType: 'single'
},
conditions: {
operator: 'or',
rules: [
// 档位1:100-199 减10
{
operator: 'and',
rules: [{
type: 'operator',
dimension: 'transaction',
field: 'orderTotal',
operator: '>=',
value: 100
}, {
type: 'operator',
dimension: 'transaction',
field: 'orderTotal',
operator: '<',
value: 200
}],
actionIds: ['deduct_10']
},
// 档位2:200-299 减25
{
operator: 'and',
rules: [{
type: 'operator',
dimension: 'transaction',
field: 'orderTotal',
operator: '>=',
value: 200
}, {
type: 'operator',
dimension: 'transaction',
field: 'orderTotal',
operator: '<',
value: 300
}],
actionIds: ['deduct_25']
},
// 档位3:>= 300 减40
{
operator: 'and',
rules: [{
type: 'operator',
dimension: 'transaction',
field: 'orderTotal',
operator: '>=',
value: 300
}],
actionIds: ['deduct_40']
}],
actionIds: []
},
actions: [{
id: 'deduct_10',
type: 'DEDUCT_AMOUNT',
value: 10,
target: 'order',
priority: 1
}, {
id: 'deduct_25',
type: 'DEDUCT_AMOUNT',
value: 25,
target: 'order',
priority: 2
}, {
id: 'deduct_40',
type: 'DEDUCT_AMOUNT',
value: 40,
target: 'order',
priority: 3
}]
};
var tieredContext = {
entities: {
order: {
id: 'ORDER_002',
total: 250
}
},
attributes: {
orderTotal: 250
},
metadata: {
timestamp: Date.now()
}
};
// ============================================
// 示例3: 使用 Code 模式的条件
// ============================================
var codeConditionConfig = {
metadata: {
id: 'CODE_CONDITION_EXAMPLE',
name: '代码条件示例',
type: 'custom'
},
conditions: {
operator: 'and',
rules: [
// 使用 code 模式
{
type: 'code',
code: 'attributes.orderTotal >= 100 && entities.customer.level === "vip"'
},
// 混合使用 operator 模式
{
type: 'operator',
field: 'productCount',
operator: '>=',
value: 2
}],
actionIds: ['vip_discount']
},
actions: [{
id: 'vip_discount',
type: 'DISCOUNT_RATE',
value: 0.85,
target: 'order',
priority: 1
}]
};
var codeContext = {
entities: {
customer: {
id: 'USER_002',
level: 'vip'
},
order: {
id: 'ORDER_003',
total: 150
}
},
attributes: {
orderTotal: 150,
productCount: 3
},
metadata: {
timestamp: Date.now()
}
};
// ============================================
// 执行示例
// ============================================
function runExamples() {
console.log('====== 策略模型使用示例 ======\n');
// 创建引擎实例(开启调试和追踪)
var engine = createStrategyEngine({
debug: true,
enableTrace: true
});
// 示例1: 代金券
console.log('示例1: 10元代金券');
var result1 = engine.evaluate(voucherConfig, voucherContext);
console.log('结果:', JSON.stringify(result1, null, 2));
console.log('\n');
// 示例2: 阶梯满减
console.log('示例2: 阶梯满减(订单金额250)');
var result2 = engine.evaluate(tieredDiscountConfig, tieredContext);
console.log('结果:', JSON.stringify(result2, null, 2));
console.log('\n');
// 示例3: Code 模式
console.log('示例3: Code 模式条件');
var result3 = engine.evaluate(codeConditionConfig, codeContext);
console.log('结果:', JSON.stringify(result3, null, 2));
console.log('\n');
// 快速评估(不需要创建引擎实例)
console.log('示例4: 快速评估');
var result4 = evaluate(voucherConfig, voucherContext);
console.log('适用:', result4.applicable);
console.log('匹配动作数:', result4.matchedActions.length);
console.log('\n');
// 注册自定义运算符
console.log('示例5: 自定义运算符');
engine.registerOperator('divisible_by', function (fieldValue, compareValue) {
return Number(fieldValue) % Number(compareValue) === 0;
});
var customOperatorConfig = {
metadata: {
id: 'CUSTOM_OP',
name: '自定义运算符',
type: 'test'
},
conditions: {
operator: 'and',
rules: [{
type: 'operator',
field: 'amount',
operator: 'divisible_by',
value: 10
}],
actionIds: ['bonus']
},
actions: [{
id: 'bonus',
type: 'ADD_POINTS',
value: 100,
target: 'customer'
}]
};
var customContext = {
entities: {},
attributes: {
amount: 50
},
metadata: {
timestamp: Date.now()
}
};
var result5 = engine.evaluate(customOperatorConfig, customContext);
console.log('结果:', result5.applicable ? '适用' : '不适用');
console.log('匹配动作:', result5.matchedActions);
// 调试信息
console.log('\n引擎调试信息:', engine.getDebugInfo());
}
// 运行示例(如果直接执行此文件)
if (require.main === module) {
runExamples();
}
export { runExamples };