单品立减促销活动:代码设计

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

代码设计

1、接口调用流程图

2、相关代码展示

  以新增单品立减活动为例

API代码--MinusSellerController

  @RestController

  @RequestMapping("/seller/promotion/minus")

  @Api(description = "单品立减相关API")

  @Validated

  public class MinusSellerController {

  @ApiOperation(value = "添加单品立减", response = MinusVO.class)

  @ApiImplicitParam(name = "minus", value = "单品立减信息", required = true, dataType = "MinusVO", paramType = "body")

  @PostMapping

  public MinusVO add(@ApiIgnore @Valid @RequestBody MinusVO minus) {

  PromotionValid.paramValid(minus.getStartTime(),minus.getEndTime(),

  minus.getRangeType(),minus.getGoodsList());

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

  Seller seller = UserContext.getSeller();

  Long sellerId = seller.getSellerId();

  minus.setSellerId(sellerId);

  this.minusManager.add(minus);

  return minus;

  }

  }

新增单品立减活动业务层代码--MinusManagerImpl

  @Service

  public class MinusManagerImpl extends AbstractPromotionRuleManagerImpl implements MinusManager {

  @Override

  @Transactional(propagation = Propagation.REQUIRED,

  rollbackFor = {RuntimeException.class,

  Exception.class, ServiceException.class})

  public MinusVO add(MinusVO minusVO) {

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

  PromotionValid.paramValid(minusVO.getStartTime(),minusVO.getEndTime(),1,null);

  this.verifyTime(minusVO.getStartTime(),minusVO.getEndTime(),

  PromotionTypeEnum.MINUS,null);

  //初步形成商品的DTO列表

  List goodsDTOList = new ArrayList<>();

  //是否是全部商品参与

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

  PromotionGoodsDTO goodsDTO = new PromotionGoodsDTO();

  goodsDTO.setGoodsId(-1L);

  goodsDTO.setSkuId(-1L);

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

  goodsDTO.setThumbnail("path");

  goodsDTOList.add(goodsDTO);

  minusVO.setGoodsList(goodsDTOList);

  }

  //检测活动规则

  this.verifyRule(minusVO.getGoodsList());

  MinusDO minusDO = new MinusDO();

  BeanUtils.copyProperties(minusVO,minusDO);

  minusMapper.insert(minusDO);

  // 获取活动Id

  Long minusId = minusDO.getMinusId();

  minusDO.setMinusId(minusId);

  minusVO.setMinusId(minusId);

  PromotionDetailDTO detailDTO = new PromotionDetailDTO();

  detailDTO.setStartTime(minusVO.getStartTime());

  detailDTO.setEndTime(minusVO.getEndTime());

  detailDTO.setActivityId(minusVO.getMinusId());

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

  detailDTO.setTitle(minusVO.getTitle());

  //将活动商品入库

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

  String minusKey = PromotionCacheKeys.getMinusKey(minusId);

  cache.put(minusKey, minusDO);

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

  PromotionScriptMsg promotionScriptMsg = new PromotionScriptMsg();

  promotionScriptMsg.setPromotionId(minusId);

  promotionScriptMsg.setPromotionName(minusDO.getTitle());

  promotionScriptMsg.setPromotionType(PromotionTypeEnum.MINUS);

  promotionScriptMsg.setOperationType(ScriptOperationTypeEnum.CREATE);

  promotionScriptMsg.setEndTime(minusDO.getEndTime());

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

  + "}_" + minusId;

  timeTrigger.add(TimeExecute.SELLER_PROMOTION_SCRIPT_EXECUTER,

  promotionScriptMsg, minusDO.getStartTime(), uniqueKey);

  return minusVO;

  }

  }

单品立减促销活动脚本生成--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)) {

  //满减满赠促销活动

  //...省略...

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

  //获取单品立减促销活动信息

  MinusVO minusVO = this.promotionGoodsClient.getMinusFromDB(promotionId);

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

  initScriptCache(minusVO.getRangeType(), minusVO.getSellerId(),

  promotionId, promotionType.name());

  } 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)) {

  //满减满赠促销活动

  //...省略...

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

  //获取单品立减促销活动信息

  MinusVO minusVO = this.promotionGoodsClient.getMinusFromDB(promotionId);

  //渲染并读取单品立减促销活动脚本信息

  String script = renderMinusScript(minusVO);

  String tips = "单品立减";

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

  PromotionScriptVO promotionScriptVO

  = getScript(script, promotionId, false, promotionType, tips);

  scriptVO.setSellerId(minusVO.getSellerId());

  scriptVO.setRangeType(minusVO.getRangeType());

  scriptVO.setPromotionScriptVO(promotionScriptVO);

  } 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 minusVO 单品立减促销活动信息

  * @return

  */

  private String renderMinusScript(MinusVO minusVO) {

  Map model = new HashMap<>();

  Map params = new HashMap<>();

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

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

  params.put("singleReductionValue", minusVO.getSingleReductionValue());

  model.put("promotionActive", params);

  String path = "minus.ftl";

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

  logger.debug("生成单品立减促销活动脚本:" + script);

  return script;

  }

  }

单品立减促销活动脚本引擎模板文件--minus.ftl

  <#--

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

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

  .startTime 获取开始时间

  .endTime 活动结束时间

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

  @returns {boolean}

  -->

  function validTime(){

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

  return true;

  }

  return false;

  }

  <#--

  单品立减活动金额计算

  @param promotionActive 单品立减活动信息对象(内置常量)

  .singleReductionValue 立减金额

  @param $sku 商品SKU信息对象(变量)

  .$price 商品SKU单价

  .$num 商品数量

  @returns {*}

  -->

  function countPrice() {

  var resultPrice = ($sku.$price - ${promotionActive.singleReductionValue}) * $sku.$num;

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

  }

  上述就是易族智汇javashop编辑的关于单品立减促销活动的代码设计所有内容,有需要欢迎咨询在线客户或者给我们留言。

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

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

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