|
|
@@ -12,6 +12,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.sqx.common.constant.MyConstant;
|
|
|
import com.sqx.common.exception.SqxException;
|
|
|
import com.sqx.common.utils.PageUtils;
|
|
|
+import com.sqx.modules.activity.bo.ShopActivityBO;
|
|
|
import com.sqx.modules.activity.dao.ActivityDao;
|
|
|
import com.sqx.modules.activity.dto.ActivityDTO;
|
|
|
import com.sqx.modules.activity.dto.ActivityOfFullReductionDTO;
|
|
|
@@ -22,12 +23,13 @@ import com.sqx.modules.activity.entity.Activity;
|
|
|
import com.sqx.modules.activity.entity.ActivityShop;
|
|
|
import com.sqx.modules.activity.enums.ActivityTypeEnum;
|
|
|
import com.sqx.modules.activity.enums.FullActivityTypeEnum;
|
|
|
+import com.sqx.modules.activity.enums.LimitTypeEnum;
|
|
|
+import com.sqx.modules.activity.enums.SuitTypeEnum;
|
|
|
import com.sqx.modules.activity.service.ActivityGoodsService;
|
|
|
import com.sqx.modules.activity.service.ActivityService;
|
|
|
import com.sqx.modules.activity.service.ActivityShopService;
|
|
|
import com.sqx.modules.activity.util.ActivityUtil;
|
|
|
import com.sqx.modules.activity.vo.ActivityVO;
|
|
|
-import com.sqx.modules.activity.vo.GoodsActivityVO;
|
|
|
import com.sqx.modules.activity.vo.OrderSuitActivityVO;
|
|
|
import com.sqx.modules.order.entity.OrderGoods;
|
|
|
import com.sqx.modules.order.entity.TbOrder;
|
|
|
@@ -161,6 +163,23 @@ public class ActivityServiceImpl extends ServiceImpl<ActivityDao, Activity> impl
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public List<ShopActivityBO> getActivityListByShopIdAndGoodsId(Long shopId, Long goodsId) {
|
|
|
+ List<ShopActivityBO> shopActivityBOS = activityShopService.getShopActivityBOByShopId(shopId);
|
|
|
+
|
|
|
+ // 满减活动过滤
|
|
|
+ return shopActivityBOS.stream().filter(shopActivityBO -> {
|
|
|
+ // 满减优惠
|
|
|
+ if (ActivityTypeEnum.FULL.getTypeCode().equals(shopActivityBO.getType())) {
|
|
|
+ // 查询符合条件的商品
|
|
|
+ List<Long> applyGoodsIds = activityGoodsService.getByActivityShopId(shopActivityBO.getActivityShopId());
|
|
|
+ return applyGoodsIds.contains(goodsId);
|
|
|
+ } else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public List<OrderSuitActivityVO> getOrderSuitActivity(Long orderId) {
|
|
|
TbOrder order = orderService.getById(orderId);
|
|
|
if (ObjectUtil.isNull(order)) {
|
|
|
@@ -172,76 +191,20 @@ public class ActivityServiceImpl extends ServiceImpl<ActivityDao, Activity> impl
|
|
|
throw new SqxException("订单数据异常,请联系管理员");
|
|
|
}
|
|
|
|
|
|
- List<Long> goodsIds = orderGoodsList.stream().map(OrderGoods::getGoodsId).collect(Collectors.toList());
|
|
|
-
|
|
|
- // 获取当前订单内的商品符合的条件的活动
|
|
|
- List<GoodsActivityVO> goodsActivityVOList = activityGoodsService.getActivityListByGoodsId(goodsIds);
|
|
|
-
|
|
|
- // 计算各活动优惠价格
|
|
|
- return goodsActivityVOList.stream()
|
|
|
- .map(e -> {
|
|
|
- String type = e.getType();
|
|
|
- // 符合当前活动的商品id集合
|
|
|
- List<Long> curGoodsIds = e.getGoodsIds();
|
|
|
-
|
|
|
- // 符合当前活动的商品id集合
|
|
|
- List<String> curGoodsNames = orderGoodsList.stream()
|
|
|
- .filter(orderGoods -> curGoodsIds.contains(orderGoods.getGoodsId()))
|
|
|
- .map(OrderGoods::getGoodsName)
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- // 当前商品总价
|
|
|
- double curGoodsTotalPrice = orderGoodsList.stream()
|
|
|
- .filter(orderGoods -> curGoodsIds.contains(orderGoods.getGoodsId()))
|
|
|
- .mapToDouble(orderGoods ->
|
|
|
- orderGoods.getGoodsPrice().multiply(new BigDecimal(orderGoods.getGoodsNum().toString()))
|
|
|
- .doubleValue())
|
|
|
- .sum();
|
|
|
-
|
|
|
- // 优惠金额
|
|
|
- double discountAmount = 0.0;
|
|
|
-
|
|
|
- // 计算活动优惠金额
|
|
|
- if (ActivityTypeEnum.TIME.getTypeCode().equals(type)) {
|
|
|
- ActivityOfTimeIntervalDTO curTimeTimeActivity = ActivityUtil.getCurTimeTimeActivity(e.getTimeIntervalInfos());
|
|
|
- Double discountContent = curTimeTimeActivity.getDiscountContent();
|
|
|
-
|
|
|
- discountAmount = curGoodsTotalPrice - (curGoodsTotalPrice * discountContent);
|
|
|
- } else if (ActivityTypeEnum.FULL.getTypeCode().equals(type)) {
|
|
|
- ActivityOfFullReductionDTO fullReductionInfo = e.getFullReductionInfo();
|
|
|
-
|
|
|
- // 判断当前商品总价满足最低满减金额
|
|
|
- if (curGoodsTotalPrice - fullReductionInfo.getMinAmount() < 0) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- if (FullActivityTypeEnum.DISCOUNT.getTypeCode().equals(fullReductionInfo.getType())) {
|
|
|
- discountAmount = curGoodsTotalPrice - (curGoodsTotalPrice * fullReductionInfo.getDiscountContent());
|
|
|
- } else {
|
|
|
- Double discountContent = fullReductionInfo.getDiscountContent();
|
|
|
-
|
|
|
- // 如果满减金额大于当前商品的总金额,则优惠金额为当前商品的总金额,否则优惠金额为当前配置的金额
|
|
|
- if (discountContent > curGoodsTotalPrice) {
|
|
|
- discountAmount = curGoodsTotalPrice;
|
|
|
- } else {
|
|
|
- discountAmount = discountContent;
|
|
|
- }
|
|
|
- }
|
|
|
- } else if (ActivityTypeEnum.GLOBAL.getTypeCode().equals(type)) {
|
|
|
- ActivityOfGlobalDiscountDTO globalDiscountsInfo = e.getGlobalDiscountsInfo();
|
|
|
-
|
|
|
- discountAmount = curGoodsTotalPrice - (curGoodsTotalPrice * globalDiscountsInfo.getDiscountRate());
|
|
|
- }
|
|
|
-
|
|
|
- OrderSuitActivityVO vo = new OrderSuitActivityVO();
|
|
|
- vo.setActivityId(e.getId());
|
|
|
- vo.setActivityTitle(e.getTitle());
|
|
|
- vo.setActivityType(type);
|
|
|
- vo.setDiscountAmount(discountAmount);
|
|
|
- vo.setGoodsIds(curGoodsIds);
|
|
|
- vo.setGoodsNames(curGoodsNames);
|
|
|
- return vo;
|
|
|
- })
|
|
|
+ // 订单所在店铺id
|
|
|
+ Long shopId = order.getShopId();
|
|
|
+
|
|
|
+ // 获取当前店铺参加的活动
|
|
|
+ List<ShopActivityBO> curShopActivityBOs = activityShopService.getShopActivityBOByShopId(shopId);
|
|
|
+
|
|
|
+ // 活动限制过滤
|
|
|
+ curShopActivityBOs = curShopActivityBOs.stream()
|
|
|
+ .filter(shopActivityBO -> activityLimitFilter(shopActivityBO, order))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 计算当前订单适用的所有可用活动优惠价格
|
|
|
+ return curShopActivityBOs.stream()
|
|
|
+ .map(shopActivityBO -> calcOrderSuitActivity(shopActivityBO, orderGoodsList))
|
|
|
.filter(Objects::nonNull)
|
|
|
.sorted(Comparator.comparing(OrderSuitActivityVO::getDiscountAmount))
|
|
|
.collect(Collectors.toList());
|
|
|
@@ -374,4 +337,125 @@ public class ActivityServiceImpl extends ServiceImpl<ActivityDao, Activity> impl
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 活动限制过滤
|
|
|
+ * @param shopActivityBO 店铺活动信息
|
|
|
+ * @param order 订单信息
|
|
|
+ * @return 订单是否适用当前活动
|
|
|
+ */
|
|
|
+ private boolean activityLimitFilter(ShopActivityBO shopActivityBO, TbOrder order) {
|
|
|
+ // 是否仅针对第一次下单
|
|
|
+ if (SuitTypeEnum.FIRST_ORDER.getTypeCode().equals(shopActivityBO.getSuitType())) {
|
|
|
+ // 查询用户在该店铺消费次数
|
|
|
+ int historyCount = orderService.countByShopIdAndUserId(order.getShopId(), order.getUserId());
|
|
|
+ return historyCount == 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 是否有数量限制
|
|
|
+ if (LimitTypeEnum.NUM_LIMIT.getTypeCode().equals(shopActivityBO.getLimitType())) {
|
|
|
+ // 每天限制的数量
|
|
|
+ Integer limitValue = shopActivityBO.getLimitValue();
|
|
|
+
|
|
|
+ int curDateCount = orderService.countByShopIdAndUserIdAndCurDate(order.getShopId(), order.getUserId());
|
|
|
+ return limitValue.intValue() >= curDateCount;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 计算当前订单商品适用的活动优惠价格
|
|
|
+ * @param shopActivityBO 店铺活动信息
|
|
|
+ * @param orderGoodsList 订单商品列表
|
|
|
+ * @return 订单适用活动信息
|
|
|
+ */
|
|
|
+ private OrderSuitActivityVO calcOrderSuitActivity(ShopActivityBO shopActivityBO, List<OrderGoods> orderGoodsList) {
|
|
|
+ String type = shopActivityBO.getType();
|
|
|
+
|
|
|
+ // 当前参与活动商品总价
|
|
|
+ double curSuitGoodsTotalPrice = 0.0;
|
|
|
+ // 符合当前活动的商品id集合
|
|
|
+ List<Long> applyGoodsIds;
|
|
|
+ // 符合当前活动的商品name集合
|
|
|
+ List<String> applyGoodsNames;
|
|
|
+
|
|
|
+ // 满减优惠
|
|
|
+ if (ActivityTypeEnum.FULL.getTypeCode().equals(type)) {
|
|
|
+ // 查询符合条件的商品
|
|
|
+ applyGoodsIds = activityGoodsService.getByActivityShopId(shopActivityBO.getActivityShopId());
|
|
|
+
|
|
|
+ orderGoodsList.stream()
|
|
|
+ .filter(orderGoods -> applyGoodsIds.contains(orderGoods.getGoodsId()))
|
|
|
+ .mapToDouble(orderGoods -> orderGoods.getGoodsPrice().multiply(new BigDecimal(orderGoods.getGoodsNum().toString())).doubleValue())
|
|
|
+ .sum();
|
|
|
+
|
|
|
+ applyGoodsNames = orderGoodsList.stream()
|
|
|
+ .filter(orderGoods -> applyGoodsIds.contains(orderGoods.getGoodsId()))
|
|
|
+ .map(OrderGoods::getGoodsName)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ // 全场优惠和时段优惠所有的商品均参与活动
|
|
|
+ else {
|
|
|
+ applyGoodsIds = orderGoodsList.stream()
|
|
|
+ .map(OrderGoods::getId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ curSuitGoodsTotalPrice = orderGoodsList.stream()
|
|
|
+ .mapToDouble(orderGoods -> orderGoods.getGoodsPrice().multiply(new BigDecimal(orderGoods.getGoodsNum().toString())).doubleValue())
|
|
|
+ .sum();
|
|
|
+
|
|
|
+ applyGoodsNames = orderGoodsList.stream()
|
|
|
+ .map(OrderGoods::getGoodsName)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 优惠金额
|
|
|
+ double discountAmount = 0.0;
|
|
|
+
|
|
|
+ // 计算活动优惠金额
|
|
|
+ if (ActivityTypeEnum.TIME.getTypeCode().equals(type)) {
|
|
|
+ ActivityOfTimeIntervalDTO curTimeTimeActivity = ActivityUtil.getCurTimeTimeActivity(shopActivityBO.getTimeIntervalInfos());
|
|
|
+
|
|
|
+ if (ObjectUtil.isNull(curTimeTimeActivity)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Double discountContent = curTimeTimeActivity.getDiscountContent();
|
|
|
+ discountAmount = curSuitGoodsTotalPrice - (curSuitGoodsTotalPrice * discountContent);
|
|
|
+ } else if (ActivityTypeEnum.FULL.getTypeCode().equals(type)) {
|
|
|
+ ActivityOfFullReductionDTO fullReductionInfo = shopActivityBO.getFullReductionInfo();
|
|
|
+
|
|
|
+ // 判断当前商品总价满足最低满减金额
|
|
|
+ if (curSuitGoodsTotalPrice - fullReductionInfo.getMinAmount() < 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (FullActivityTypeEnum.DISCOUNT.getTypeCode().equals(fullReductionInfo.getType())) {
|
|
|
+ discountAmount = curSuitGoodsTotalPrice - (curSuitGoodsTotalPrice * fullReductionInfo.getDiscountContent());
|
|
|
+ } else {
|
|
|
+ Double discountContent = fullReductionInfo.getDiscountContent();
|
|
|
+
|
|
|
+ // 如果满减金额大于当前商品的总金额,则优惠金额为当前商品的总金额,否则优惠金额为当前配置的金额
|
|
|
+ if (discountContent > curSuitGoodsTotalPrice) {
|
|
|
+ discountAmount = curSuitGoodsTotalPrice;
|
|
|
+ } else {
|
|
|
+ discountAmount = discountContent;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (ActivityTypeEnum.GLOBAL.getTypeCode().equals(type)) {
|
|
|
+ ActivityOfGlobalDiscountDTO globalDiscountsInfo = shopActivityBO.getGlobalDiscountsInfo();
|
|
|
+
|
|
|
+ discountAmount = curSuitGoodsTotalPrice - (curSuitGoodsTotalPrice * globalDiscountsInfo.getDiscountRate());
|
|
|
+ }
|
|
|
+
|
|
|
+ OrderSuitActivityVO vo = new OrderSuitActivityVO();
|
|
|
+ vo.setActivityId(shopActivityBO.getActivityId());
|
|
|
+ vo.setActivityTitle(shopActivityBO.getTitle());
|
|
|
+ vo.setActivityType(type);
|
|
|
+ vo.setDiscountAmount(discountAmount);
|
|
|
+ vo.setGoodsIds(applyGoodsIds);
|
|
|
+ vo.setGoodsNames(applyGoodsNames);
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
}
|