满减满赠促销活动:一系列代码展示

后台-系统设置-扩展变量-手机广告位-内容正文顶部

代码展示
1、接口调用流程图

2、相关代码展示

  以新增满减满赠活动为例

API代码--FullDiscountSellerController

  @RestController

  @RequestMapping("/seller/promotion/full-discounts")

  @Api(description = "满优惠活动相关API")

  @Validated

  public class FullDiscountSellerController {

  @ApiOperation(value = "添加满优惠活动", response = FullDiscountVO.class)

  @PostMapping

  public FullDiscountVO add(@Valid @RequestBody FullDiscountVO fullDiscountVO) {

  this.verifyFullDiscountParam(fullDiscountVO);

  // 获取当前登录的店铺ID

  Seller seller = UserContext.getSeller();

  Long sellerId = seller.getSellerId();

  fullDiscountVO.setSellerId(sellerId);

  this.fullDiscountManager.add(fullDiscountVO);

  return fullDiscountVO;

  }

  }

新增满减满赠活动业务层代码--FullDiscountManagerImpl

  @Override

  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RuntimeException.class, Exception.class, ServiceException.class, NoPermissionException.class})

  public FullDiscountVO add(FullDiscountVO fullDiscountVO) {

  //检测开始时间和结束时间

  PromotionValid.paramValid(fullDiscountVO.getStartTime(),

  fullDiscountVO.getEndTime(),1,null);

  this.verifyTime(fullDiscountVO.getStartTime(),

  fullDiscountVO.getEndTime(), PromotionTypeEnum.FULL_DISCOUNT, null);

  List goodsDTOList = new ArrayList<>();

  //是否是全部商品参与

  if (fullDiscountVO.getRangeType() == 1) {

  PromotionGoodsDTO goodsDTO = new PromotionGoodsDTO();

  goodsDTO.setGoodsId(-1L);

  goodsDTO.setSkuId(-1L);

  goodsDTO.setGoodsName("全部商品");

  goodsDTO.setThumbnail("");

  goodsDTOList.add(goodsDTO);

  fullDiscountVO.setGoodsList(goodsDTOList);

  }

  this.verifyRule(fullDiscountVO.getGoodsList());

  // 查看是否赠送优惠券,是则判断优惠券的使用时间是否大于活动结束时间

  if (fullDiscountVO.getIsSendBonus() != null

  && fullDiscountVO.getIsSendBonus() == 1) {

  Long couponId = fullDiscountVO.getBonusId();

  couponManager.getModel(couponId);

  CouponDO coupon = couponManager.getModel(couponId);

  if(coupon.getEndTime()

  throw new ServiceException(PromotionErrorCode.E401.code(),

  "赠送的优惠券有效时间必须大于活动时间");

  }

  }

  //新建满减满赠活动对象

  FullDiscountDO fullDiscountDO = new FullDiscountDO();

  //从VO中复制相关属性值

  BeanUtils.copyProperties(fullDiscountVO, fullDiscountDO);

  //满减满赠活动信息入库

  fullDiscountMapper.insert(fullDiscountDO);

  //获取活动Id

  Long id = fullDiscountDO.getFdId();

  fullDiscountVO.setFdId(id);

  //设置促销信息

  PromotionDetailDTO detailDTO = new PromotionDetailDTO();

  detailDTO.setStartTime(fullDiscountVO.getStartTime());

  detailDTO.setEndTime(fullDiscountVO.getEndTime());

  detailDTO.setActivityId(fullDiscountVO.getFdId());

  detailDTO.setPromotionType(PromotionTypeEnum.FULL_DISCOUNT.name());

  detailDTO.setTitle(fullDiscountVO.getTitle());

  //将活动商品入库

  this.promotionGoodsManager.add(fullDiscountVO.getGoodsList(), detailDTO);

  cache.put(PromotionCacheKeys.getFullDiscountKey(id), fullDiscountDO);

  //启用延时任务创建促销活动脚本信息

  PromotionScriptMsg promotionScriptMsg = new PromotionScriptMsg();

  promotionScriptMsg.setPromotionId(id);

  promotionScriptMsg.setPromotionName(fullDiscountDO.getTitle());

  promotionScriptMsg.setPromotionType(PromotionTypeEnum.FULL_DISCOUNT);

  promotionScriptMsg.setOperationType(ScriptOperationTypeEnum.CREATE);

  promotionScriptMsg.setEndTime(fullDiscountDO.getEndTime());

  String uniqueKey = "{TIME_TRIGGER_" + PromotionTypeEnum.FULL_DISCOUNT.name()

  + "}_" + id;

  timeTrigger.add(TimeExecute.SELLER_PROMOTION_SCRIPT_EXECUTER,

  promotionScriptMsg, fullDiscountDO.getStartTime(), uniqueKey);

  return fullDiscountVO;

  }

由此方法可以看出:

首先是满减满赠促销活动主体信息入库:

  //满减满赠活动信息入库

  fullDiscountMapper.insert(fullDiscountDO);

其次将参与活动的商品信息入库:

  //将活动商品入库

  this.promotionGoodsManager.add(fullDiscountVO.getGoodsList(), detailDTO);

然后把活动主体信息放入缓存中:

  cache.put(PromotionCacheKeys.getFullDiscountKey(id), fullDiscountDO);

最后启用延时任务创建促销活动脚本信息:

  //启用延时任务创建促销活动脚本信息

  PromotionScriptMsg promotionScriptMsg = new PromotionScriptMsg();

  promotionScriptMsg.setPromotionId(id);

  promotionScriptMsg.setPromotionName(fullDiscountDO.getTitle());

  promotionScriptMsg.setPromotionType(PromotionTypeEnum.FULL_DISCOUNT);

  promotionScriptMsg.setOperationType(ScriptOperationTypeEnum.CREATE);

  promotionScriptMsg.setEndTime(fullDiscountDO.getEndTime());

  String uniqueKey = "{TIME_TRIGGER_" + PromotionTypeEnum.FULL_DISCOUNT.name()

  + "}_" + id;

  timeTrigger.add(TimeExecute.SELLER_PROMOTION_SCRIPT_EXECUTER,

  promotionScriptMsg, fullDiscountDO.getStartTime(), uniqueKey);

满减满赠促销活动脚本生成--PromotionScriptTimeTriggerExecuter

  @Component("promotionScriptTimeTriggerExecuter")

  public class PromotionScriptTimeTriggerExecuter implements TimeTriggerExecuter {

  @Override

  public void execute(Object object) {

  PromotionScriptMsg promotionScriptMsg = (PromotionScriptMsg) object;

  //如果是促销活动开始

  if (ScriptOperationTypeEnum.CREATE

  .equals(promotionScriptMsg.getOperationType())) {

  //创建促销活动脚本

  this.createScript(promotionScriptMsg);

  //促销活动开始后,立马设置一个促销活动结束的流程

  promotionScriptMsg.setOperationType(ScriptOperationTypeEnum.DELETE);

  String uniqueKey = "{TIME_TRIGGER_"

  + promotionScriptMsg.getPromotionType().name()

  + "}_" + promotionScriptMsg.getPromotionId();

  timeTrigger.add(TimeExecute.SELLER_PROMOTION_SCRIPT_EXECUTER,

  promotionScriptMsg, promotionScriptMsg.getEndTime(),

  uniqueKey);

  this.logger.debug("促销活动[" + promotionScriptMsg.getPromotionName()

  + "]开始,id=[" + promotionScriptMsg.getPromotionId()

  + "]");

  } else {

  //删除缓存中的促销脚本数据

  this.deleteScript(promotionScriptMsg);

  this.logger.debug("促销活动[" + promotionScriptMsg.getPromotionName()

  + "]结束,id=[" + promotionScriptMsg.getPromotionId()

  + "]");

  }

  }

  /**

  * 创建促销活动脚本

  * @param promotionScriptMsg 促销活动脚本消息信息

  */

  private void createScript(PromotionScriptMsg promotionScriptMsg) {

  //获取促销活动类型

  PromotionTypeEnum promotionType = promotionScriptMsg.getPromotionType();

  //获取促销活动ID

  Long promotionId = promotionScriptMsg.getPromotionId();

  //获取促销脚本相关数据

  ScriptVO scriptVO = this.getPromotionScript(promotionId, promotionType.name());

  //获取商家ID

  Long sellerId = scriptVO.getSellerId();

  //购物车(店铺)级别缓存key

  String cartCacheKey = CachePrefix.CART_PROMOTION.getPrefix() + sellerId;

  //获取商品参与促销活动的方式 1:全部商品参与,2:部分商品参与

  Integer rangeType = scriptVO.getRangeType();

  //构建促销脚本数据结构

  PromotionScriptVO promotionScriptVO = scriptVO.getPromotionScriptVO();

  //如果是全部商品都参与了促销活动

  if (rangeType.intValue() == 1) {

  //构建新的促销脚本数据

  List scriptList =

  getScriptList(cartCacheKey, promotionScriptVO);

  //将促销脚本数据放入缓存中

  cache.put(cartCacheKey, scriptList);

  } else {

  //获取参与促销活动的商品集合

  List goodsList = scriptVO.getGoodsList();

  //批量放入缓存的数据集合

  Map> cacheMap = new HashMap<>();

  //循环skuID集合,将脚本放入缓存中

  for (PromotionGoodsDO goods : goodsList) {

  PromotionScriptVO newScript = new PromotionScriptVO();

  BeanUtil.copyProperties(promotionScriptVO, newScript);

  //缓存key

  String cacheKey = CachePrefix.SKU_PROMOTION.getPrefix()

  + goods.getSkuId();

  //脚本结构中加入商品skuID

  newScript.setSkuId(goods.getSkuId());

  //构建新的促销脚本数据

  List scriptList = getScriptList(cacheKey, newScript);

  cacheMap.put(cacheKey, scriptList);

  }

  //将sku促销脚本数据批量放入缓存中

  cache.multiSet(cacheMap);

  }

  }

  /**

  * 删除促销活动脚本

  * @param promotionScriptMsg 促销活动脚本消息信息

  */

  private void deleteScript(PromotionScriptMsg promotionScriptMsg) {

  //获取促销活动类型

  PromotionTypeEnum promotionType = promotionScriptMsg.getPromotionType();

  //获取促销活动ID

  Long promotionId = promotionScriptMsg.getPromotionId();

  if (PromotionTypeEnum.FULL_DISCOUNT.equals(promotionType)) {

  //获取满减满赠促销活动详细信息

  FullDiscountVO fullDiscountVO =

  this.promotionGoodsClient.getFullDiscountModel(promotionId);

  //初始化缓存中的促销活动脚本信息

  initScriptCache(fullDiscountVO.getRangeType(),

  fullDiscountVO.getSellerId(), promotionId,

  promotionType.name());

  } else if (PromotionTypeEnum.MINUS.equals(promotionType)) {

  //单品立减促销活动

  //...省略...

  } else if (PromotionTypeEnum.HALF_PRICE.equals(promotionType)) {

  //第二件半价促销活动

  //...省略...

  }

  }

  /**

  * 根据促销活动ID和促销活动类型获取促销脚本数据

  * @param promotionId 促销活动id

  * @param promotionType 促销活动类型

  * @return

  */

  private ScriptVO getPromotionScript(Long promotionId, String promotionType) {

  ScriptVO scriptVO = new ScriptVO();

  if (PromotionTypeEnum.FULL_DISCOUNT.name().equals(promotionType)) {

  //获取满减满赠促销活动详细信息

  FullDiscountVO fullDiscountVO =

  this.promotionGoodsClient.getFullDiscountModel(promotionId);

  //渲染并读取满减满赠促销活动脚本信息

  String script = renderFullDiscountScript(fullDiscountVO);

  //构建促销脚本数据结构

  PromotionScriptVO promotionScriptVO =

  getScript(script, promotionId, true, promotionType,

  getFullDiscountTips(fullDiscountVO));

  scriptVO.setSellerId(fullDiscountVO.getSellerId());

  scriptVO.setRangeType(fullDiscountVO.getRangeType());

  scriptVO.setPromotionScriptVO(promotionScriptVO);

  } else if (PromotionTypeEnum.MINUS.name().equals(promotionType)) {

  //单品立减促销活动

  //...省略...

  } else if (PromotionTypeEnum.HALF_PRICE.name().equals(promotionType)) {

  //第二件半价促销活动

  //...省略...

  }

  //如果是部分商品参与活动,需要查询出参与促销活动的商品skuID集合

  if (scriptVO.getRangeType().intValue() == 2) {

  List goodsList =

  this.promotionGoodsClient.getPromotionGoods(promotionId, promotionType);

  scriptVO.setGoodsList(goodsList);

  }

  return scriptVO;

  }

  /**

  * 渲染并读取满减满赠促销活动脚本信息

  * @param fullDiscountVO 满减满赠促销活动信息

  * @return

  */

  private String renderFullDiscountScript(FullDiscountVO fullDiscountVO) {

  Map model = new HashMap<>();

  Map params = new HashMap<>();

  params.put("startTime", fullDiscountVO.getStartTime().toString());

  params.put("endTime", fullDiscountVO.getEndTime().toString());

  params.put("isFullMinus", fullDiscountVO.getIsFullMinus());

  params.put("isDiscount", fullDiscountVO.getIsDiscount());

  params.put("fullMoney", fullDiscountVO.getFullMoney());

  params.put("minusValue", fullDiscountVO.getMinusValue()

  == null ? 0.00 : fullDiscountVO.getMinusValue());

  params.put("discountValue", fullDiscountVO.getDiscountValue()

  == null ? 0.00 : CurrencyUtil.div(fullDiscountVO.getDiscountValue(),

  10, 2));

  List> giftList = new ArrayList<>();

  //判断是否免邮费

  if (fullDiscountVO.getIsFreeShip().intValue() == 1) {

  Map free = new HashMap<>();

  free.put("type", "freeShip");

  free.put("value", true);

  giftList.add(free);

  }

  //判断是否送积分

  if (fullDiscountVO.getIsSendPoint().intValue() == 1) {

  Map point = new HashMap<>();

  point.put("type", "point");

  point.put("value", fullDiscountVO.getPointValue());

  giftList.add(point);

  }

  //判断是否送赠品

  if (fullDiscountVO.getIsSendGift().intValue() == 1 ) {

  Map gift = new HashMap<>();

  gift.put("type", "gift");

  gift.put("value", fullDiscountVO.getGiftId().toString());

  giftList.add(gift);

  }

  //判断是否送优惠券

  if (fullDiscountVO.getIsSendBonus().intValue() == 1) {

  Map coupon = new HashMap<>();

  coupon.put("type", "coupon");

  coupon.put("value", fullDiscountVO.getBonusId().toString());

  giftList.add(coupon);

  }

  params.put("gift", JsonUtil.objectToJson(giftList));

  model.put("promotionActive", params);

  String path = "full_discount.ftl";

  String script = ScriptUtil.renderScript(path, model);

  logger.debug("生成满减满赠促销活动脚本:" + script);

  return script;

  }

  }

满减满赠促销活动脚本引擎模板文件--full_discount.ftl

  <#--

  验证促销活动是否在有效期内

  @param promotionActive 活动日期对象(内置常量)

  .startTime 获取开始时间

  .endTime 活动结束时间

  @param $currentTime 当前时间(变量)

  @returns {boolean}

  -->

  function validTime(){

  if (${promotionActive.startTime} <= $currentTime && $currentTime <= ${promotionActive.endTime}) {

  return true;

  }

  return false;

  }

  <#--

  减现金或打折活动价格计算

  @param promotionActive 促销活动信息对象(内置常量)

  .fullMoney 优惠门槛金额

  .isFullMinus 是否立减现金 0:否,1:是

  .minusValue 立减金额

  .isDiscount 是否打折 0:否,1:是

  .discountValue 打多少折,例:打8折,值为0.8

  @param $price 参与活动的商品总金额(变量)

  @returns {*}

  -->

  function countPrice() {

  <#--判断商品金额是否满足优惠条件 -->

  if (${promotionActive.fullMoney} <= $price) {

  var resultPrice = $price;

  if (${promotionActive.isFullMinus} == 1) {

  resultPrice = $price - ${promotionActive.minusValue};

  }

  if (${promotionActive.isDiscount} == 1) {

  resultPrice = $price * ${promotionActive.discountValue};

  }

  return resultPrice < 0 ? 0 : resultPrice.toString();

  }

  return $price;

  }

  <#--

  赠送赠品

  @param promotionActive 促销活动信息对象(内置常量)

  .fullMoney 优惠门槛金额

  .gift 赠品信息对象json

  @param $price 参与活动的商品总金额(变量)

  @returns {*}

  -->

  function giveGift() {

  <#-- 判断商品金额是否满足优惠条件 -->

  if (${promotionActive.fullMoney} <= $price) {

  return JSON.stringify(${promotionActive.gift});

  }

  return null;

  }

  以上就是关注满减满赠促销活动所有代码展示了,对此感兴趣的,可以持续关注易族智汇javashop技术文档

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

后台-系统设置-扩展变量-手机广告位-内容正文底部
留言与评论(共有 0 条评论)
   
验证码: