业务介绍
商品可以打上特定的标签 如
9.9元秒杀
特价款
5折抢购
标签有限制用券规则 如
禁止用券
禁止使用XX元以上的优惠券
禁止使用店铺券
于是当下单展示可用优惠券列表时 需要基于商品的标签 排除相应的券
为了解耦 调用标签模块的接口 得到禁止用券的规则 如
Map<String,String> getDisableCouponRule(int tagId);
返回结果有
disableAll : true (禁止用券)
disableCouponPriceGreatEqual : 30 (禁止使用30元以上(含)的优惠券)
disableCouponType : shop (禁止使用店铺券)
然后基于返回的规则 排除相应的优惠券 如
if("true".equals(ruleMap.get("disableAll"))){
return empty_list;
}
for (Coupon coupon : couponList){ // 遍历用户优惠券 排除不符合条件的优惠券
if(ruleMap.get("disableCouponPriceGreatEqual") != null){
if(coupon.getPrice() >= parseInt(ruleMap.get("disableCouponPriceGreatEqual"))){
continue;
}
}
if(ruleMap.get("disableCouponType")!=null){
if(coupon.getType() == getTypeIntValue(ruleMap.get("disableCouponType"))){continue;}
}
}
目前业务还算简单 就这么几种规则 万一以后要新增规则 如
小于YY元的优惠券不能使用
增加一个if语句就是了
但是总觉得if语句不够优雅 不知有没什么设计模式或DSL适合这种场景?
这种场景适合使用规则引擎吗? 是不是太重了?
最终决定采用
spring el
表达式来实现标签禁止用券 返回 "true"
新人专享 (类目新客) 禁止使用40元以上的优惠券 couponPrice > 40
特价款 5折抢购 禁止使用店铺优惠券 couponType == 2
代码示例
参考文档
http://www.baeldung.com/sprin...