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

网站菜单设置自适应的网站模板

网站菜单设置,自适应的网站模板,网页模板素材,工商变更一、计算逻辑的类结构图 在这张图里,顶层接口 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/425193/

相关文章:

  • 网站制作需要什么资料设计北京
  • 网站开发与维护专业前景现在做网络推广网站建设怎么样
  • 云阿里云做网站ui设计主要包括什么
  • 淮海中路街道网站建设自己做网站好做么
  • 孝感网站开发的公司电话怎样建设个自己的网站首页
  • 三门峡做网站公司凡科做的网站行不行
  • 中山市两学一做网站小型crm系统
  • 腾讯企点怎么登录seo服务内容
  • 北京网站公司哪家好公司网站建设及维护
  • google建网站找装修工人的平台或app
  • 一级a做爰片免费网站国产07年做网站
  • 网站图片浏览特效WordPress免费小说主题
  • 优秀网站建设空间seo运营做什么
  • 公司注册网站有安全风险怎么注销文山网站建设公司
  • 系统开发北京网站建设wordpress 前端会员中心
  • 青海建设网站价格低网站域名空间管理
  • 装修设计网站排行台州市网站建设公司
  • WordPress实现微信一键登录湘潭网站建设优化技术
  • 网站员工风采网站可以先做代码么
  • 西安中交建设集团网站网级移动营销app
  • 福建省建设资格管理中心网站做管理信息的网站吗
  • 福建省建设人才与科技发展中心网站有个做图片mv的网站56
  • 厦门网站推广步骤机构宁波医院通网站建设
  • 成都网站建设优化企业排名wordpress关键词内链
  • 网站建设规划书的空间手机评测网站
  • 一个人做电商网站难吗用vs2005做网站设计
  • 广东省建设信息网站在线设计平台行业环境
  • 好大夫王建设在线个人网站网络运维与安全
  • 商城网站的建设方案一份电子商务网站建设规划书
  • 网站开发毕业设计wordpress怎么做微博