当前位置: 首页 > news >正文

帮企网站建设代运营博达网站建设

帮企网站建设代运营,博达网站建设,网页设计制作个人网站,cdn网站加速一、计算逻辑的类结构图 在这张图里,顶层接口 RuleTemplate 定义了 calculate 方法,抽象模板类 AbstractRuleTemplate 将通用的模板计算逻辑在 calculate 方法中实现,同时它还定义了一个抽象方法 calculateNewPrice 作为子类的扩展点。各个具…

一、计算逻辑的类结构图

 在这张图里,顶层接口 RuleTemplate 定义了 calculate 方法,抽象模板类 AbstractRuleTemplate 将通用的模板计算逻辑在 calculate 方法中实现,同时它还定义了一个抽象方法 calculateNewPrice 作为子类的扩展点。各个具体的优惠计算类通过继承 AbstractRuleTemplate,并实现 calculateNewPrice 来编写自己的优惠计算方式。

二、代码实现

1、RuleTemplate.java

public interface RuleTemplate {// 计算优惠券ShoppingCart calculate(ShoppingCart settlement);
}

2、AbstractRuleTemplate.java

public ShoppingCart calculate(ShoppingCart order) {// 获取订单总价Long orderTotalAmount = getTotalPrice(order.getProducts());// 获取以shopId为维度的总价统计Map<Long, Long> sumAmount = getTotalPriceGroupByShop(order.getProducts());CouponTemplateInfo template = order.getCouponInfos().get(0).getTemplate();// 最低消费限制Long threshold = template.getRule().getDiscount().getThreshold();// 优惠金额或者打折比例Long quota = template.getRule().getDiscount().getQuota();// 如果优惠券未指定shopId,则shopTotalAmount=orderTotalAmount// 如果指定了shopId,则shopTotalAmount=对应门店下商品总价Long shopId = template.getShopId();Long shopTotalAmount = (shopId == null) ? orderTotalAmount : sumAmount.get(shopId);// 如果不符合优惠券使用标准, 则直接按原价走,不使用优惠券if (shopTotalAmount == null || shopTotalAmount < threshold) {log.debug("Totals of amount not meet");order.setCost(orderTotalAmount);order.setCouponInfos(Collections.emptyList());return order;}// 子类中实现calculateNewPrice计算新的价格Long newCost = calculateNewPrice(orderTotalAmount, shopTotalAmount, quota);if (newCost < minCost()) {newCost = minCost();}order.setCost(newCost);log.debug("original price={}, new price={}", orderTotalAmount, newCost);return order;
}

3、子类

MoneyOffTemplate.java

@Slf4j
@Component
public class MoneyOffTemplate extends AbstractRuleTemplate implements RuleTemplate {@Overrideprotected Long calculateNewPrice(Long totalAmount, Long shopAmount, Long quota) {// benefitAmount是扣减的价格// 如果当前门店的商品总价<quota,那么最多只能扣减shopAmount的钱数Long benefitAmount = shopAmount < quota ? shopAmount : quota;return totalAmount - benefitAmount;}    
}

4、工厂类

CouponTemplateFactory.java

@Component
@Slf4j
public class CouponTemplateFactory {@Autowiredprivate MoneyOffTemplate moneyOffTemplate;@Autowiredprivate DiscountTemplate discountTemplate;@Autowiredprivate RandomReductionTemplate randomReductionTemplate;@Autowiredprivate LonelyNightTemplate lonelyNightTemplate;@Autowiredprivate DummyTemplate dummyTemplate;@Autowiredprivate AntiPauTemplate antiPauTemplate;public RuleTemplate getTemplate(ShoppingCart order) {// 不使用优惠券if (CollectionUtils.isEmpty(order.getCouponInfos())) {// dummy模板直接返回原价,不进行优惠计算return dummyTemplate;}// 获取优惠券的类别// 目前每个订单只支持单张优惠券CouponTemplateInfo template = order.getCouponInfos().get(0).getTemplate();CouponType category = CouponType.convert(template.getType());switch (category) {// 订单满减券case MONEY_OFF:return moneyOffTemplate;// 随机立减券case RANDOM_DISCOUNT:return randomReductionTemplate;// 午夜下单优惠翻倍case LONELY_NIGHT_MONEY_OFF:return lonelyNightTemplate;// 打折券case DISCOUNT:return discountTemplate;case ANTI_PUA:return antiPauTemplate;// 未知类型的券模板default:return dummyTemplate;}}}

5、使用

CouponCalculationServiceImpl.java

    @Autowiredprivate CouponTemplateFactory couponProcessorFactory;// 优惠券结算// 这里通过Factory类决定使用哪个底层Rule,底层规则对上层透明@Overridepublic ShoppingCart calculateOrderPrice(@RequestBody ShoppingCart cart) {log.info("calculate order price: {}", JSON.toJSONString(cart));RuleTemplate ruleTemplate = couponProcessorFactory.getTemplate(cart);return ruleTemplate.calculate(cart);}

http://www.yayakq.cn/news/767228/

相关文章:

  • 老外做牛排的视频网站如何在微信上开小程序
  • 做网站的设计流程网站建设流程html
  • 程序员自己做网站线上营销推广方案有哪些
  • 上海网站制作公司哪做棋牌网站违法
  • 网站开发费待摊年限天津如何做百度的网站
  • 门窗企业网站建设电商网站建设简单代码网页
  • 商务网站开发的的基本流程wordpress 显示备案
  • 唐山模板建站系统这是我自己做的网站吗
  • 创建购物网站多少钱项目网络中可以表示一个活动
  • sae网站备案云服务器建网站
  • 免费购物网站云南建设人才网站首页
  • iis网站权限怎么设置上海网络推广的方法
  • html怎么做网站后台建一个网站首先要怎么做
  • 专业的网站建设托管广西建设厅考试网站首页
  • 网站设计公司西安php怎么用来做网站
  • html手机网站单位做网站费用怎么记账
  • 小白建站软件网站和网站的app
  • 手机响应式网站建设公司一般网站建设用什么样的代码
  • html5 手机网站开发wordpress 主题购买
  • wordpress图片站模板下载怎么建设淘客自己的网站
  • 深圳贸易网站建设专业团队图片原图
  • 湖南企业网站定制做网站能申报只是产权么
  • 网站后台编辑器控件下载免费ppt模板下载爱ppt
  • 广州官网建站php个人网站源码带音乐
  • 定制高端网站建设设计济南百度竞价代运营
  • discuz注册seo的方法
  • 东莞寮步网站建设网络公司公司网站要什么做
  • 广州网站建设定制方案南京市网站建设公司
  • 北海公司做网站网站制作好了怎么上传
  • 网页设计制作网站图片西安英文网站制作