订单模块
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
package com.atguigu.ssyx.activity.api;
|
||||
|
||||
import com.atguigu.ssyx.activity.service.ActivityInfoService;
|
||||
import com.atguigu.ssyx.activity.service.CouponInfoService;
|
||||
import com.atguigu.ssyx.model.activity.CouponInfo;
|
||||
import com.atguigu.ssyx.model.order.CartInfo;
|
||||
import com.atguigu.ssyx.vo.order.CartInfoVo;
|
||||
import com.atguigu.ssyx.vo.order.OrderConfirmVo;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -26,6 +32,17 @@ public class ActivityApiController {
|
||||
@Resource
|
||||
private ActivityInfoService activityInfoService;
|
||||
|
||||
@Autowired
|
||||
private CouponInfoService couponInfoService;
|
||||
|
||||
@ApiOperation("获取购物车中满足条件优惠券和活动")
|
||||
@PostMapping("inner/findCartActivityAndCoupon/{userId}")
|
||||
public OrderConfirmVo findCartActivityAndCoupon(
|
||||
@RequestBody List<CartInfo> cartInfoList, @PathVariable("userId") Long userId) {
|
||||
return activityInfoService.findCartActivityAndCoupon(cartInfoList, userId);
|
||||
|
||||
}
|
||||
|
||||
@ApiOperation(value = "根据skuId列表获取促销信息")
|
||||
@PostMapping("inner/findActivity")
|
||||
public Map<Long, List<String>> findActivity(@RequestBody List<Long> skuIdList) {
|
||||
@@ -34,7 +51,29 @@ public class ActivityApiController {
|
||||
|
||||
@ApiOperation("根据skuID获取营销数据和优惠卷")
|
||||
@GetMapping("inner/findActivityAndCoupon/{skuId}/{userId}")
|
||||
public Map<String, Object> findActivityAndCoupon(@PathVariable Long skuId, @PathVariable Long userId) {
|
||||
public Map<String, Object> findActivityAndCoupon(
|
||||
@PathVariable("skuId") Long skuId, @PathVariable("userId") Long userId) {
|
||||
return activityInfoService.findActivityAndCoupon(skuId, userId);
|
||||
}
|
||||
|
||||
@ApiOperation("获取购物车对应规则数据")
|
||||
@PostMapping("inner/findCartActivityList")
|
||||
List<CartInfoVo> findCartActivityList(@RequestBody List<CartInfo> cartInfoList) {
|
||||
return activityInfoService.findCartActivityList(cartInfoList);
|
||||
}
|
||||
|
||||
@ApiOperation("获取购物车对应优惠券数据")
|
||||
@PostMapping("inner/findRangeSkuIdList")
|
||||
CouponInfo findRangeSkuIdList(@RequestBody List<CartInfo> cartInfoList, @PathVariable("couponId") Long couponId) {
|
||||
return couponInfoService.findRangeSkuIdList(cartInfoList, couponId);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "更新优惠卷使用状态")
|
||||
@GetMapping("inner/updateCouponInfoUseStatus/{couponId}/{userId}/{orderId}")
|
||||
public Boolean updateCouponInfoUseStatus(@PathVariable("couponId") Long couponId,
|
||||
@PathVariable("userId") Long userId,
|
||||
@PathVariable("orderId") Long orderId) {
|
||||
couponInfoService.updateCouponInfoUseStatus(couponId, userId, orderId);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package com.atguigu.ssyx.activity.mapper;
|
||||
|
||||
import com.atguigu.ssyx.model.activity.ActivityInfo;
|
||||
import com.atguigu.ssyx.model.activity.ActivityRule;
|
||||
import com.atguigu.ssyx.model.activity.ActivitySku;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import feign.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
@@ -33,5 +34,13 @@ public interface ActivityInfoMapper extends BaseMapper<ActivityInfo> {
|
||||
* @param skuId
|
||||
* @return
|
||||
*/
|
||||
List<ActivityRule> findActivityRule(Long skuId);
|
||||
List<ActivityRule> findActivityRule(@Param("skuId") Long skuId);
|
||||
|
||||
/**
|
||||
* 根据所有skuId列表获取参与活动
|
||||
*
|
||||
* @param skuIdList
|
||||
* @return
|
||||
*/
|
||||
List<ActivitySku> selectCartActivity(@Param("skuIdList") List<Long> skuIdList);
|
||||
}
|
||||
|
@@ -26,4 +26,12 @@ public interface CouponInfoMapper extends BaseMapper<CouponInfo> {
|
||||
*/
|
||||
List<CouponInfo> selectCouponInfoList(
|
||||
@Param("skuId") Long id, @Param("categoryId") Long categoryId, @Param("userId") Long userId);
|
||||
|
||||
/**
|
||||
* 根据userId获取用户全部优惠卷
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
List<CouponInfo> selectCartCouponInfoList(@Param("userId") Long userId);
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package com.atguigu.ssyx.activity.mapper;
|
||||
|
||||
import com.atguigu.ssyx.model.activity.CouponUse;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -11,6 +12,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
* @author atguigu
|
||||
* @since 2023-09-17
|
||||
*/
|
||||
@Repository
|
||||
public interface CouponUseMapper extends BaseMapper<CouponUse> {
|
||||
|
||||
}
|
||||
|
@@ -2,8 +2,11 @@ package com.atguigu.ssyx.activity.service;
|
||||
|
||||
|
||||
import com.atguigu.ssyx.model.activity.ActivityInfo;
|
||||
import com.atguigu.ssyx.model.order.CartInfo;
|
||||
import com.atguigu.ssyx.model.product.SkuInfo;
|
||||
import com.atguigu.ssyx.vo.activity.ActivityRuleVo;
|
||||
import com.atguigu.ssyx.vo.order.CartInfoVo;
|
||||
import com.atguigu.ssyx.vo.order.OrderConfirmVo;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
@@ -69,4 +72,21 @@ public interface ActivityInfoService extends IService<ActivityInfo> {
|
||||
* @return
|
||||
*/
|
||||
Map<String, Object> findActivityAndCoupon(Long skuId, Long userId);
|
||||
|
||||
/**
|
||||
* 获取购物车中满足条件优惠券和活动
|
||||
*
|
||||
* @param cartInfoList
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
OrderConfirmVo findCartActivityAndCoupon(List<CartInfo> cartInfoList, Long userId);
|
||||
|
||||
/**
|
||||
* 获取购物车对应规则数据
|
||||
*
|
||||
* @param cartInfoList
|
||||
* @return
|
||||
*/
|
||||
List<CartInfoVo> findCartActivityList(List<CartInfo> cartInfoList);
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package com.atguigu.ssyx.activity.service;
|
||||
|
||||
import com.atguigu.ssyx.model.activity.CouponInfo;
|
||||
import com.atguigu.ssyx.model.order.CartInfo;
|
||||
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
@@ -68,4 +69,31 @@ public interface CouponInfoService extends IService<CouponInfo> {
|
||||
*/
|
||||
|
||||
List<CouponInfo> findCouponInfoList(Long skuId, Long userId);
|
||||
|
||||
/**
|
||||
* 获取购物车可以使用优惠卷列表
|
||||
*
|
||||
* @param cartInfoList
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
List<CouponInfo> findCartCouponInfo(List<CartInfo> cartInfoList, Long userId);
|
||||
|
||||
/**
|
||||
* 获取购物车对应优惠券数据
|
||||
*
|
||||
* @param cartInfoList
|
||||
* @param couponId
|
||||
* @return
|
||||
*/
|
||||
CouponInfo findRangeSkuIdList(List<CartInfo> cartInfoList, Long couponId);
|
||||
|
||||
/**
|
||||
* 更新优惠卷使用状态
|
||||
*
|
||||
* @param couponId
|
||||
* @param userId
|
||||
* @param orderId
|
||||
*/
|
||||
void updateCouponInfoUseStatus(Long couponId, Long userId, Long orderId);
|
||||
}
|
||||
|
@@ -11,8 +11,11 @@ import com.atguigu.ssyx.model.activity.ActivityInfo;
|
||||
import com.atguigu.ssyx.model.activity.ActivityRule;
|
||||
import com.atguigu.ssyx.model.activity.ActivitySku;
|
||||
import com.atguigu.ssyx.model.activity.CouponInfo;
|
||||
import com.atguigu.ssyx.model.order.CartInfo;
|
||||
import com.atguigu.ssyx.model.product.SkuInfo;
|
||||
import com.atguigu.ssyx.vo.activity.ActivityRuleVo;
|
||||
import com.atguigu.ssyx.vo.order.CartInfoVo;
|
||||
import com.atguigu.ssyx.vo.order.OrderConfirmVo;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
@@ -23,10 +26,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -245,6 +246,265 @@ public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, Act
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取购物车中满足条件优惠券和活动
|
||||
*
|
||||
* @param cartInfoList
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public OrderConfirmVo findCartActivityAndCoupon(List<CartInfo> cartInfoList, Long userId) {
|
||||
//1 获取购物车,每个购物项参与活动,根据活动规则分组,
|
||||
//一个规则对应多个商品
|
||||
List<CartInfoVo> cartInfoVoList = this.findCartActivityList(cartInfoList);
|
||||
|
||||
//2 计算参与活动之后金额
|
||||
BigDecimal activityReduceAmount = cartInfoVoList.stream()
|
||||
.filter(cartInfoVo -> cartInfoVo.getActivityRule() != null)
|
||||
.map(cartInfoVo -> cartInfoVo.getActivityRule()
|
||||
.getReduceAmount())
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
//3 获取购物车可以使用优惠卷列表
|
||||
List<CouponInfo> couponInfoList = couponInfoService.findCartCouponInfo(cartInfoList, userId);
|
||||
|
||||
//4 计算商品使用优惠卷之后金额,一次只能使用一张优惠卷
|
||||
BigDecimal couponReduceAmount = new BigDecimal(0);
|
||||
if (!CollectionUtils.isEmpty(couponInfoList)) {
|
||||
couponReduceAmount = couponInfoList.stream()
|
||||
.filter(couponInfo -> couponInfo.getIsOptimal()
|
||||
.intValue() == 1)
|
||||
.map(couponInfo -> couponInfo.getAmount())
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
}
|
||||
|
||||
//5 计算没有参与活动,没有使用优惠卷原始金额
|
||||
BigDecimal originalTotalAmount = cartInfoList.stream()
|
||||
.filter(cartInfo -> cartInfo.getIsChecked() == 1)
|
||||
.map(cartInfo -> cartInfo.getCartPrice()
|
||||
.multiply(new BigDecimal(cartInfo.getSkuNum())))
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
|
||||
//6 最终金额
|
||||
BigDecimal totalAmount = originalTotalAmount.subtract(activityReduceAmount)
|
||||
.subtract(couponReduceAmount);
|
||||
|
||||
//7 封装需要数据到OrderConfirmVo,返回
|
||||
OrderConfirmVo orderTradeVo = new OrderConfirmVo();
|
||||
orderTradeVo.setCarInfoVoList(cartInfoVoList);
|
||||
orderTradeVo.setActivityReduceAmount(activityReduceAmount);
|
||||
orderTradeVo.setCouponInfoList(couponInfoList);
|
||||
orderTradeVo.setCouponReduceAmount(couponReduceAmount);
|
||||
orderTradeVo.setOriginalTotalAmount(originalTotalAmount);
|
||||
orderTradeVo.setTotalAmount(totalAmount);
|
||||
return orderTradeVo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取购物车对应规则数据
|
||||
*
|
||||
* @param cartInfoList
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<CartInfoVo> findCartActivityList(List<CartInfo> cartInfoList) {
|
||||
//创建最终返回集合
|
||||
List<CartInfoVo> cartInfoVoList = new ArrayList<>();
|
||||
//获取所有skuId
|
||||
List<Long> skuIdList = cartInfoList.stream()
|
||||
.map(CartInfo::getSkuId)
|
||||
.collect(Collectors.toList());
|
||||
//根据所有skuId列表获取参与活动
|
||||
List<ActivitySku> activitySkuList = baseMapper.selectCartActivity(skuIdList);
|
||||
//根据活动进行分组,每个活动里面有哪些skuId信息
|
||||
//map里面key是分组字段 活动id
|
||||
// value是每组里面sku列表数据,set集合
|
||||
Map<Long, Set<Long>> activityIdToSkuIdListMap = activitySkuList.stream()
|
||||
.collect(Collectors.groupingBy(ActivitySku::getActivityId, Collectors.mapping(ActivitySku::getSkuId, Collectors.toSet())));
|
||||
|
||||
//获取活动里面规则数据
|
||||
//key是活动id value是活动里面规则列表数据
|
||||
Map<Long, List<ActivityRule>> activityIdToActivityRuleListMap = new HashMap<>();
|
||||
//所有活动id
|
||||
Set<Long> activityIdSet = activitySkuList.stream()
|
||||
.map(ActivitySku::getActivityId)
|
||||
.collect(Collectors.toSet());
|
||||
if (!CollectionUtils.isEmpty(activityIdSet)) {
|
||||
//activity_rule表
|
||||
LambdaQueryWrapper<ActivityRule> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.orderByDesc(ActivityRule::getConditionAmount, ActivityRule::getConditionNum);
|
||||
wrapper.in(ActivityRule::getActivityId, activityIdSet);
|
||||
List<ActivityRule> activityRuleList = activityRuleMapper.selectList(wrapper);
|
||||
|
||||
//封装到activityIdToActivityRuleListMap里面
|
||||
//根据活动id进行分组
|
||||
activityIdToActivityRuleListMap = activityRuleList.stream()
|
||||
.collect(Collectors.groupingBy(activityRule -> activityRule.getActivityId()));
|
||||
}
|
||||
|
||||
//有活动的购物项skuId
|
||||
Set<Long> activitySkuIdSet = new HashSet<>();
|
||||
if (!CollectionUtils.isEmpty(activityIdToSkuIdListMap)) {
|
||||
//遍历activityIdToSkuIdListMap集合
|
||||
Iterator<Map.Entry<Long, Set<Long>>> iterator = activityIdToSkuIdListMap.entrySet()
|
||||
.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<Long, Set<Long>> entry = iterator.next();
|
||||
//活动id
|
||||
Long activityId = entry.getKey();
|
||||
//每个活动对应skuId列表
|
||||
Set<Long> currentActivitySkuIdSet = entry.getValue();
|
||||
//获取当前活动对应的购物项列表
|
||||
List<CartInfo> currentActivityCartInfoList = cartInfoList.stream()
|
||||
.filter(cartInfo -> currentActivitySkuIdSet.contains(cartInfo.getSkuId()))
|
||||
.collect(Collectors.toList());
|
||||
//计数购物项总金额和总数量
|
||||
BigDecimal activityTotalAmount = this.computeTotalAmount(currentActivityCartInfoList);
|
||||
int activityTotalNum = this.computeCartNum(currentActivityCartInfoList);
|
||||
|
||||
//计算活动对应规则
|
||||
//根据activityId获取活动对应规则
|
||||
List<ActivityRule> currentActivityRuleList = activityIdToActivityRuleListMap.get(activityId);
|
||||
ActivityType activityType = currentActivityRuleList.get(0)
|
||||
.getActivityType();
|
||||
//判断活动类型:满减和打折
|
||||
ActivityRule activityRule = null;
|
||||
if (activityType == ActivityType.FULL_REDUCTION) {//满减"
|
||||
activityRule = this.computeFullReduction(activityTotalAmount, currentActivityRuleList);
|
||||
} else {//满量
|
||||
activityRule = this.computeFullDiscount(activityTotalNum, activityTotalAmount, currentActivityRuleList);
|
||||
}
|
||||
|
||||
//CartInfoVo封装
|
||||
CartInfoVo cartInfoVo = new CartInfoVo();
|
||||
cartInfoVo.setActivityRule(activityRule);
|
||||
cartInfoVo.setCartInfoList(currentActivityCartInfoList);
|
||||
cartInfoVoList.add(cartInfoVo);
|
||||
|
||||
//记录哪些购物项参与活动
|
||||
activitySkuIdSet.addAll(currentActivitySkuIdSet);
|
||||
}
|
||||
}
|
||||
|
||||
//没有活动购物项skuId
|
||||
//获取哪些skuId没有参加活动
|
||||
skuIdList.removeAll(activitySkuIdSet);
|
||||
if (!CollectionUtils.isEmpty(skuIdList)) {
|
||||
//skuId对应购物项
|
||||
Map<Long, CartInfo> skuIdCartInfoMap = cartInfoList.stream()
|
||||
.collect(Collectors.toMap(CartInfo::getSkuId, CartInfo -> CartInfo));
|
||||
for (Long skuId : skuIdList) {
|
||||
CartInfoVo cartInfoVo = new CartInfoVo();
|
||||
cartInfoVo.setActivityRule(null);//没有活动
|
||||
|
||||
List<CartInfo> cartInfos = new ArrayList<>();
|
||||
cartInfos.add(skuIdCartInfoMap.get(skuId));
|
||||
cartInfoVo.setCartInfoList(cartInfos);
|
||||
|
||||
cartInfoVoList.add(cartInfoVo);
|
||||
}
|
||||
}
|
||||
|
||||
return cartInfoVoList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算满量打折最优规则
|
||||
* 该活动规则skuActivityRuleList数据,已经按照优惠折扣从大到小排序了
|
||||
*
|
||||
* @param totalNum
|
||||
* @param activityRuleList
|
||||
*/
|
||||
private ActivityRule computeFullDiscount(Integer totalNum, BigDecimal totalAmount, List<ActivityRule> activityRuleList) {
|
||||
ActivityRule optimalActivityRule = null;
|
||||
//该活动规则skuActivityRuleList数据,已经按照优惠金额从大到小排序了
|
||||
for (ActivityRule activityRule : activityRuleList) {
|
||||
//如果订单项购买个数大于等于满减件数,则优化打折
|
||||
if (totalNum.intValue() >= activityRule.getConditionNum()) {
|
||||
BigDecimal skuDiscountTotalAmount = totalAmount.multiply(activityRule.getBenefitDiscount()
|
||||
.divide(new BigDecimal("10")));
|
||||
BigDecimal reduceAmount = totalAmount.subtract(skuDiscountTotalAmount);
|
||||
activityRule.setReduceAmount(reduceAmount);
|
||||
optimalActivityRule = activityRule;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (null == optimalActivityRule) {
|
||||
//如果没有满足条件的取最小满足条件的一项
|
||||
optimalActivityRule = activityRuleList.get(activityRuleList.size() - 1);
|
||||
optimalActivityRule.setReduceAmount(new BigDecimal("0"));
|
||||
optimalActivityRule.setSelectType(1);
|
||||
|
||||
String ruleDesc = "满" +
|
||||
optimalActivityRule.getConditionNum() +
|
||||
"元打" +
|
||||
optimalActivityRule.getBenefitDiscount() +
|
||||
"折,还差" +
|
||||
(totalNum - optimalActivityRule.getConditionNum()) +
|
||||
"件";
|
||||
optimalActivityRule.setRuleDesc(ruleDesc);
|
||||
} else {
|
||||
String ruleDesc = "满" +
|
||||
optimalActivityRule.getConditionNum() +
|
||||
"元打" +
|
||||
optimalActivityRule.getBenefitDiscount() +
|
||||
"折,已减" +
|
||||
optimalActivityRule.getReduceAmount() +
|
||||
"元";
|
||||
optimalActivityRule.setRuleDesc(ruleDesc);
|
||||
optimalActivityRule.setSelectType(2);
|
||||
}
|
||||
return optimalActivityRule;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算满减最优规则
|
||||
* 该活动规则skuActivityRuleList数据,已经按照优惠金额从大到小排序了
|
||||
*
|
||||
* @param totalAmount
|
||||
* @param activityRuleList
|
||||
*/
|
||||
private ActivityRule computeFullReduction(BigDecimal totalAmount, List<ActivityRule> activityRuleList) {
|
||||
ActivityRule optimalActivityRule = null;
|
||||
//该活动规则skuActivityRuleList数据,已经按照优惠金额从大到小排序了
|
||||
for (ActivityRule activityRule : activityRuleList) {
|
||||
//如果订单项金额大于等于满减金额,则优惠金额
|
||||
if (totalAmount.compareTo(activityRule.getConditionAmount()) > -1) {
|
||||
//优惠后减少金额
|
||||
activityRule.setReduceAmount(activityRule.getBenefitAmount());
|
||||
optimalActivityRule = activityRule;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (null == optimalActivityRule) {
|
||||
//如果没有满足条件的取最小满足条件的一项
|
||||
optimalActivityRule = activityRuleList.get(activityRuleList.size() - 1);
|
||||
optimalActivityRule.setReduceAmount(new BigDecimal("0"));
|
||||
optimalActivityRule.setSelectType(1);
|
||||
|
||||
String ruleDesc = "满" +
|
||||
optimalActivityRule.getConditionAmount() +
|
||||
"元减" +
|
||||
optimalActivityRule.getBenefitAmount() +
|
||||
"元,还差" +
|
||||
totalAmount.subtract(optimalActivityRule.getConditionAmount()) +
|
||||
"元";
|
||||
optimalActivityRule.setRuleDesc(ruleDesc);
|
||||
} else {
|
||||
String ruleDesc = "满" +
|
||||
optimalActivityRule.getConditionAmount() +
|
||||
"元减" +
|
||||
optimalActivityRule.getBenefitAmount() +
|
||||
"元,已减" +
|
||||
optimalActivityRule.getReduceAmount() +
|
||||
"元";
|
||||
optimalActivityRule.setRuleDesc(ruleDesc);
|
||||
optimalActivityRule.setSelectType(2);
|
||||
}
|
||||
return optimalActivityRule;
|
||||
}
|
||||
|
||||
//根据skuId获取活动规则数据
|
||||
public List<ActivityRule> findActivityRuleBySkuId(Long skuId) {
|
||||
List<ActivityRule> activityRuleList = baseMapper.findActivityRule(skuId);
|
||||
@@ -255,4 +515,32 @@ public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, Act
|
||||
}
|
||||
return activityRuleList;
|
||||
}
|
||||
|
||||
//计算购物车中金额
|
||||
private BigDecimal computeTotalAmount(List<CartInfo> cartInfoList) {
|
||||
BigDecimal total = new BigDecimal("0");
|
||||
for (CartInfo cartInfo : cartInfoList) {
|
||||
//是否选中
|
||||
if (cartInfo.getIsChecked()
|
||||
.intValue() == 1) {
|
||||
BigDecimal itemTotal = cartInfo.getCartPrice()
|
||||
.multiply(new BigDecimal(cartInfo.getSkuNum()));
|
||||
total = total.add(itemTotal);
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
//计算购物车中总数量
|
||||
private int computeCartNum(List<CartInfo> cartInfoList) {
|
||||
int total = 0;
|
||||
for (CartInfo cartInfo : cartInfoList) {
|
||||
//是否选中
|
||||
if (cartInfo.getIsChecked()
|
||||
.intValue() == 1) {
|
||||
total += cartInfo.getSkuNum();
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
}
|
||||
|
@@ -2,11 +2,16 @@ package com.atguigu.ssyx.activity.service.impl;
|
||||
|
||||
import com.atguigu.ssyx.activity.mapper.CouponInfoMapper;
|
||||
import com.atguigu.ssyx.activity.mapper.CouponRangeMapper;
|
||||
import com.atguigu.ssyx.activity.mapper.CouponUseMapper;
|
||||
import com.atguigu.ssyx.activity.service.CouponInfoService;
|
||||
import com.atguigu.ssyx.client.product.ProductFeignClient;
|
||||
import com.atguigu.ssyx.enums.CouponRangeType;
|
||||
import com.atguigu.ssyx.enums.CouponStatus;
|
||||
import com.atguigu.ssyx.model.activity.CouponInfo;
|
||||
import com.atguigu.ssyx.model.activity.CouponRange;
|
||||
import com.atguigu.ssyx.model.activity.CouponUse;
|
||||
import com.atguigu.ssyx.model.base.BaseEntity;
|
||||
import com.atguigu.ssyx.model.order.CartInfo;
|
||||
import com.atguigu.ssyx.model.product.Category;
|
||||
import com.atguigu.ssyx.model.product.SkuInfo;
|
||||
import com.atguigu.ssyx.vo.activity.CouponRuleVo;
|
||||
@@ -18,9 +23,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -34,12 +38,12 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {
|
||||
|
||||
|
||||
@Autowired
|
||||
CouponRangeMapper couponRangeMapper;
|
||||
|
||||
@Autowired
|
||||
ProductFeignClient productFeignClient;
|
||||
@Autowired
|
||||
private CouponUseMapper couponUseMapper;
|
||||
|
||||
/**
|
||||
* 获取分页列表
|
||||
@@ -55,7 +59,7 @@ public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponI
|
||||
List<CouponInfo> couponInfoList = couponInfoPage.getRecords();
|
||||
couponInfoList.forEach(item -> {
|
||||
item.setCouponTypeString(item.getCouponType()
|
||||
.getComment());
|
||||
.getComment());
|
||||
CouponRangeType rangeType = item.getRangeType();
|
||||
if (rangeType != null) {
|
||||
item.setRangeTypeString(rangeType.getComment());
|
||||
@@ -74,10 +78,10 @@ public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponI
|
||||
public CouponInfo getCouponInfo(Long id) {
|
||||
CouponInfo couponInfo = baseMapper.selectById(id);
|
||||
couponInfo.setCouponTypeString(couponInfo.getCouponType()
|
||||
.getComment());
|
||||
.getComment());
|
||||
if (couponInfo.getRangeType() != null) {
|
||||
couponInfo.setRangeTypeString(couponInfo.getRangeType()
|
||||
.getComment());
|
||||
.getComment());
|
||||
}
|
||||
return couponInfo;
|
||||
}
|
||||
@@ -99,8 +103,8 @@ public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponI
|
||||
//// 如果规则类型 SKU range_id就是skuId值
|
||||
//// 如果规则类型 CATEGORY range_id就是分类Id值
|
||||
List<Long> randIdList = couponRangeList.stream()
|
||||
.map(CouponRange::getRangeId)
|
||||
.collect(Collectors.toList());
|
||||
.map(CouponRange::getRangeId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
//第三步 分别判断封装不同数据
|
||||
@@ -172,4 +176,186 @@ public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponI
|
||||
|
||||
return couponInfoList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取购物车可以使用优惠卷列表
|
||||
*
|
||||
* @param cartInfoList
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<CouponInfo> findCartCouponInfo(List<CartInfo> cartInfoList, Long userId) {
|
||||
//1 根据userId获取用户全部优惠卷
|
||||
//coupon_use coupon_info
|
||||
List<CouponInfo> userAllCouponInfoList = baseMapper.selectCartCouponInfoList(userId);
|
||||
if (CollectionUtils.isEmpty(userAllCouponInfoList)) {
|
||||
return new ArrayList<CouponInfo>();
|
||||
}
|
||||
|
||||
//2 从第一步返回list集合中,获取所有优惠卷id列表
|
||||
List<Long> couponIdList = userAllCouponInfoList.stream()
|
||||
.map(BaseEntity::getId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
//3 查询优惠卷对应的范围 coupon_range
|
||||
//couponRangeList
|
||||
LambdaQueryWrapper<CouponRange> wrapper = new LambdaQueryWrapper<>();
|
||||
// id in (1,2,3)
|
||||
wrapper.in(CouponRange::getCouponId, couponIdList);
|
||||
List<CouponRange> couponRangeList = couponRangeMapper.selectList(wrapper);
|
||||
|
||||
//4 获取优惠卷id 对应skuId列表
|
||||
//优惠卷id进行分组,得到map集合
|
||||
// Map<Long,List<Long>>
|
||||
Map<Long, List<Long>> couponIdToSkuIdMap = this.findCouponIdToSkuIdMap(cartInfoList, couponRangeList);
|
||||
|
||||
//5 遍历全部优惠卷集合,判断优惠卷类型
|
||||
//全场通用 sku和分类
|
||||
BigDecimal reduceAmount = new BigDecimal(0);
|
||||
CouponInfo optimalCouponInfo = null;
|
||||
for (CouponInfo couponInfo : userAllCouponInfoList) {
|
||||
//全场通用
|
||||
if (CouponRangeType.ALL == couponInfo.getRangeType()) {
|
||||
//全场通用
|
||||
//判断是否满足优惠使用门槛
|
||||
//计算购物车商品的总价
|
||||
BigDecimal totalAmount = computeTotalAmount(cartInfoList);
|
||||
if (totalAmount.subtract(couponInfo.getConditionAmount())
|
||||
.doubleValue() >= 0) {
|
||||
couponInfo.setIsSelect(1);
|
||||
}
|
||||
} else {
|
||||
//优惠卷id获取对应skuId列表
|
||||
List<Long> skuIdList = couponIdToSkuIdMap.get(couponInfo.getId());
|
||||
//满足使用范围购物项
|
||||
List<CartInfo> currentCartInfoList = cartInfoList.stream()
|
||||
.filter(cartInfo -> skuIdList.contains(cartInfo.getSkuId()))
|
||||
.collect(Collectors.toList());
|
||||
BigDecimal totalAmount = computeTotalAmount(currentCartInfoList);
|
||||
if (totalAmount.subtract(couponInfo.getConditionAmount())
|
||||
.doubleValue() >= 0) {
|
||||
couponInfo.setIsSelect(1);
|
||||
}
|
||||
}
|
||||
if (couponInfo.getIsSelect()
|
||||
.intValue() == 1 && couponInfo.getAmount()
|
||||
.subtract(reduceAmount)
|
||||
.doubleValue() > 0) {
|
||||
reduceAmount = couponInfo.getAmount();
|
||||
optimalCouponInfo = couponInfo;
|
||||
}
|
||||
|
||||
}
|
||||
//6 返回List<CouponInfo>
|
||||
if (null != optimalCouponInfo) {
|
||||
optimalCouponInfo.setIsOptimal(1);
|
||||
}
|
||||
return userAllCouponInfoList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取购物车对应优惠券数据
|
||||
*
|
||||
* @param cartInfoList
|
||||
* @param couponId
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public CouponInfo findRangeSkuIdList(List<CartInfo> cartInfoList, Long couponId) {
|
||||
//根据优惠卷id基本信息查询
|
||||
CouponInfo couponInfo = baseMapper.selectById(couponId);
|
||||
if (couponInfo == null) {
|
||||
return null;
|
||||
}
|
||||
//根据couponId查询对应CouponRange数据
|
||||
List<CouponRange> couponRangeList = couponRangeMapper.selectList(new LambdaQueryWrapper<CouponRange>().eq(CouponRange::getCouponId, couponId));
|
||||
//对应sku信息
|
||||
Map<Long, List<Long>> couponIdToSkuIdMap = this.findCouponIdToSkuIdMap(cartInfoList, couponRangeList);
|
||||
//遍历map,得到value值,封装到couponInfo对象
|
||||
List<Long> skuIdList = couponIdToSkuIdMap.entrySet()
|
||||
.iterator()
|
||||
.next()
|
||||
.getValue();
|
||||
couponInfo.setSkuIdList(skuIdList);
|
||||
return couponInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新优惠卷使用状态
|
||||
*
|
||||
* @param couponId
|
||||
* @param userId
|
||||
* @param orderId
|
||||
*/
|
||||
@Override
|
||||
public void updateCouponInfoUseStatus(Long couponId, Long userId, Long orderId) {
|
||||
//根据couponId查询优惠卷信息
|
||||
CouponUse couponUse = couponUseMapper.selectOne(
|
||||
new LambdaQueryWrapper<CouponUse>()
|
||||
.eq(CouponUse::getCouponId, couponId)
|
||||
.eq(CouponUse::getUserId, userId)
|
||||
.eq(CouponUse::getOrderId, orderId)
|
||||
);
|
||||
|
||||
//设置修改值
|
||||
couponUse.setCouponStatus(CouponStatus.USED);
|
||||
|
||||
//调用方法修改
|
||||
couponUseMapper.updateById(couponUse);
|
||||
}
|
||||
|
||||
//获取优惠卷id 对应skuId列表
|
||||
private Map<Long, List<Long>> findCouponIdToSkuIdMap(List<CartInfo> cartInfoList, List<CouponRange> couponRangeList) {
|
||||
Map<Long, List<Long>> couponIdToSkuIdMap = new HashMap<>();
|
||||
|
||||
//couponRangeList数据处理,根据优惠卷id分组
|
||||
Map<Long, List<CouponRange>> couponRangeToRangeListMap = couponRangeList.stream()
|
||||
.collect(Collectors.groupingBy(CouponRange::getCouponId));
|
||||
|
||||
//遍历map集合
|
||||
Iterator<Map.Entry<Long, List<CouponRange>>> iterator = couponRangeToRangeListMap.entrySet()
|
||||
.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<Long, List<CouponRange>> entry = iterator.next();
|
||||
Long couponId = entry.getKey();
|
||||
List<CouponRange> rangeList = entry.getValue();
|
||||
|
||||
//创建集合 set
|
||||
Set<Long> skuIdSet = new HashSet<>();
|
||||
for (CartInfo cartInfo : cartInfoList) {
|
||||
for (CouponRange couponRange : rangeList) {
|
||||
//判断
|
||||
if (couponRange.getRangeType() == CouponRangeType.SKU && couponRange.getRangeId()
|
||||
.longValue() == cartInfo.getSkuId()
|
||||
.longValue()) {
|
||||
skuIdSet.add(cartInfo.getSkuId());
|
||||
} else if (couponRange.getRangeType() == CouponRangeType.CATEGORY && couponRange.getRangeId()
|
||||
.longValue() == cartInfo.getCategoryId()
|
||||
.longValue()) {
|
||||
skuIdSet.add(cartInfo.getSkuId());
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
couponIdToSkuIdMap.put(couponId, new ArrayList<>(skuIdSet));
|
||||
}
|
||||
return couponIdToSkuIdMap;
|
||||
}
|
||||
|
||||
//计算购物车商品的总价
|
||||
private BigDecimal computeTotalAmount(List<CartInfo> cartInfoList) {
|
||||
BigDecimal total = new BigDecimal("0");
|
||||
for (CartInfo cartInfo : cartInfoList) {
|
||||
//是否选中
|
||||
if (cartInfo.getIsChecked()
|
||||
.intValue() == 1) {
|
||||
BigDecimal itemTotal = cartInfo.getCartPrice()
|
||||
.multiply(new BigDecimal(cartInfo.getSkuNum()));
|
||||
total = total.add(itemTotal);
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
}
|
||||
|
@@ -13,6 +13,18 @@ spring:
|
||||
url: jdbc:mysql://82.157.68.223:3306/shequ-activity?characterEncoding=utf-8&useSSL=false
|
||||
username: shequ-activity
|
||||
password: shequ-activity
|
||||
redis:
|
||||
host: 82.157.68.223
|
||||
port: 6379
|
||||
database: 0
|
||||
timeout: 1800000
|
||||
password:
|
||||
lettuce:
|
||||
pool:
|
||||
max-active: 20 #最大连接数
|
||||
max-wait: -1 #最大阻塞等待时间(负数表示没限制)
|
||||
max-idle: 5 #最大空闲
|
||||
min-idle: 0 #最小空闲
|
||||
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd HH:mm:ss
|
||||
|
@@ -21,41 +21,6 @@
|
||||
</where>
|
||||
and now() between info.start_time and info.end_time
|
||||
</select>
|
||||
|
||||
<!--//2 根据skuId+分类id+userId查询优惠卷信息-->
|
||||
<select id="selectCouponInfoList" resultMap="CouponInfoMap">
|
||||
select info.id,
|
||||
info.coupon_type,
|
||||
info.coupon_name,
|
||||
info.amount,
|
||||
info.condition_amount,
|
||||
info.start_time,
|
||||
info.end_time,
|
||||
info.range_type,
|
||||
info.range_desc,
|
||||
info.publish_count,
|
||||
info.per_limit,
|
||||
info.use_count,
|
||||
info.receive_count,
|
||||
info.expire_time,
|
||||
info.publish_status,
|
||||
info.create_time,
|
||||
info.update_time,
|
||||
info.is_deleted,
|
||||
cuse.coupon_status
|
||||
from coupon_info info
|
||||
left join coupon_range crange on info.id = crange.coupon_id
|
||||
left join coupon_use cuse on info.id = cuse.coupon_id
|
||||
and cuse.user_id = #{userId}
|
||||
where (
|
||||
info.range_type = 1
|
||||
or (info.range_type = 2 and crange.range_id = #{skuId})
|
||||
or (info.range_type = 3 and crange.range_id = #{categoryId})
|
||||
)
|
||||
and now() between info.start_time and info.end_time
|
||||
order by info.amount desc
|
||||
</select>
|
||||
|
||||
<!--//根据skuId进行查询,查询sku对应活动里面规则列表-->
|
||||
<select id="findActivityRule" resultMap="ActivityRuleMap">
|
||||
select rule.id,
|
||||
|
Reference in New Issue
Block a user