促销活动第二件半价:业务层面代码设计

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

  第二件半价促销活动属于商家店铺可直接发布的促销活动,同一个商家在同一时间段内只允许创建一个第二件半价促销活动。那么,第二件半价促销活动的代码设计是怎么样的?下面易族智汇javashop为您解析,欢迎阅读参考。

代码设计

1、接口调用流程图

2、相关代码展示

  以新增第二件半价活动为例

  API代码--HalfPriceSellerController

  @RestController

  @RequestMapping("/seller/promotion/half-prices")

  @Api(description = "第二件半价相关API")

  @Validated

  public class HalfPriceSellerController {

  @ApiOperation(value = "添加第二件半价", response = HalfPriceVO.class)

  @PostMapping

  public HalfPriceVO add(@Valid @RequestBody HalfPriceVO halfPrice) {

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

  halfPrice.getRangeType(),halfPrice.getGoodsList());

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

  Seller seller = UserContext.getSeller();

  Long sellerId = seller.getSellerId();

  halfPrice.setSellerId(sellerId);

  this.halfPriceManager.add(halfPrice);

  return halfPrice;

  }

  }

  新增第二件半价活动业务层代码--HalfPriceManagerImpl

  @Service

  public class HalfPriceManagerImpl extends AbstractPromotionRuleManagerImpl implements HalfPriceManager {

  @Override

  @Transactional(propagation = Propagation.REQUIRED,

  rollbackFor = {RuntimeException.class, Exception.class,

  ServiceException.class, NoPermissionException.class})

  public HalfPriceVO add(HalfPriceVO halfPriceVO) {

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

  PromotionValid.paramValid(halfPriceVO.getStartTime(),

  halfPriceVO.getEndTime(), 1, null);

  this.verifyTime(halfPriceVO.getStartTime(),

  halfPriceVO.getEndTime(), PromotionTypeEnum.HALF_PRICE, null);

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

  List goodsDTOList = new ArrayList<>();

  //是否是全部商品参与

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

  PromotionGoodsDTO goodsDTO = new PromotionGoodsDTO();

  goodsDTO.setGoodsId(-1L);

  goodsDTO.setSkuId(-1L);

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

  goodsDTO.setThumbnail("");

  goodsDTOList.add(goodsDTO);

  halfPriceVO.setGoodsList(goodsDTOList);

  }

  this.verifyRule(halfPriceVO.getGoodsList());

  HalfPriceDO halfPriceDO = new HalfPriceDO();

  BeanUtils.copyProperties(halfPriceVO, halfPriceDO);

  this.halfPriceMapper.insert(halfPriceDO);

  Long id = halfPriceDO.getHpId();

  halfPriceVO.setHpId(id);

  PromotionDetailDTO detailDTO = new PromotionDetailDTO();

  detailDTO.setStartTime(halfPriceVO.getStartTime());

  detailDTO.setEndTime(halfPriceVO.getEndTime());

  detailDTO.setActivityId(halfPriceVO.getHpId());

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

  detailDTO.setTitle(halfPriceVO.getTitle());

  //将活动商品入库

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

  cache.put(PromotionCacheKeys.getHalfPriceKey(

  halfPriceVO.getHpId()), halfPriceDO);

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

  PromotionScriptMsg promotionScriptMsg = new PromotionScriptMsg();

  promotionScriptMsg.setPromotionId(id);

  promotionScriptMsg.setPromotionName(halfPriceDO.getTitle());

  promotionScriptMsg.setPromotionType(PromotionTypeEnum.HALF_PRICE);

  promotionScriptMsg.setOperationType(ScriptOperationTypeEnum.CREATE);

  promotionScriptMsg.setEndTime(halfPriceDO.getEndTime());

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

  + "}_" + id;

  timeTrigger.add(TimeExecute.SELLER_PROMOTION_SCRIPT_EXECUTER,

  promotionScriptMsg, halfPriceDO.getStartTime(), uniqueKey);

  return halfPriceVO;

  }

  }

  第二件半价促销活动脚本生成--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)) {

  //单品立减促销活动

  //...省略...

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

  //获取第二件半价促销活动信息

  HalfPriceVO halfPriceVO

  = this.promotionGoodsClient.getHalfPriceFromDB(promotionId);

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

  initScriptCache(halfPriceVO.getRangeType(),

  halfPriceVO.getSellerId(), promotionId,

  promotionType.name());

  }

  }

  /**

  * 根据促销活动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)) {

  //单品立减促销活动

  //...省略...

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

  //获取第二件半价促销活动信息

  HalfPriceVO halfPriceVO

  = this.promotionGoodsClient.getHalfPriceFromDB(promotionId);

  //渲染并读取第二件半价促销活动脚本信息

  String script = renderHalfPriceScript(halfPriceVO);

  String tips = "第二件半价";

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

  PromotionScriptVO promotionScriptVO

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

  scriptVO.setSellerId(halfPriceVO.getSellerId());

  scriptVO.setRangeType(halfPriceVO.getRangeType());

  scriptVO.setPromotionScriptVO(promotionScriptVO);

  }

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

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

  List goodsList

  = this.promotionGoodsClient.getPromotionGoods(promotionId, promotionType);

  scriptVO.setGoodsList(goodsList);

  }

  return scriptVO;

  }

  /**

  * 渲染并读取第二件半价促销活动脚本信息

  * @param halfPriceVO 第二件半价促销活动信息

  * @return

  */

  private String renderHalfPriceScript(HalfPriceVO halfPriceVO) {

  Map model = new HashMap<>();

  Map params = new HashMap<>();

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

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

  model.put("promotionActive", params);

  String path = "half_price.ftl";

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

  logger.debug("生成第二件半价促销活动脚本:" + script);

  return script;

  }

  }

  第二件半价促销活动脚本引擎模板文件—half_price.ftl

  <#--

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

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

  .startTime 获取开始时间

  .endTime 活动结束时间

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

  @returns {boolean}

  -->

  function validTime(){

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

  return true;

  }

  return false;

  }

  <#--

  第二件半价促销活动金额计算

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

  .$price 商品SKU单价

  .$num 商品数量

  @returns {*}

  -->

  function countPrice() {

  <#--获取实际商品总金额-->

  var totalPrice = $sku.$price * $sku.$num;

  <#--应该优惠的金额-->

  var discountPrice = 0.00;

  <#--当商品数量大于1时才计算优惠-->

  if ($sku.$num > 1) {

  <#--如果商品数量对2进行取余得到的结果为0-->

  if ($sku.$num % 2 == 0) {

  <#--优惠金额 = 商品原价的一半 * 商品数量的一半-->

  discountPrice = ($sku.$price / 2) * ($sku.$num / 2);

  }

  <#--如果商品数量对2进行取余得到的结果为1-->

  if ($sku.$num % 2 == 1) {

  <#--优惠金额 = 商品原价的一半 * (商品数量 - 1)的一半-->

  discountPrice = ($sku.$price / 2) * (($sku.$num - 1) / 2);

  }

  }

  <#--最终返回优惠后的总金额 = 实际商品总金额 - 优惠金额-->

  totalPrice = totalPrice - discountPrice;

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

  }

  以上就是易族智汇javashop整理的关于第二件半价促销活动代码设计的全部内容,有需要欢迎咨询在线客服或给我们留言。

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

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

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