【功能新增】商城: 满减送活动下单后,赠送积分、优惠劵、包邮

This commit is contained in:
puhui999 2024-08-29 18:19:54 +08:00
parent cccad2c6c1
commit e0d9f7cfba
13 changed files with 135 additions and 41 deletions

View File

@ -96,7 +96,7 @@ public class RewardActivityBaseVO {
@AssertTrue(message = "优惠劵和数量必须一一对应")
@JsonIgnore
public boolean isCouponCountsValid() {
return BooleanUtil.isFalse(givePoint) || CollUtil.size(couponIds) == CollUtil.size(couponCounts);
return BooleanUtil.isFalse(giveCoupon) || CollUtil.size(couponIds) == CollUtil.size(couponCounts);
}
@AssertTrue(message = "赠送的积分不能小于 1")

View File

@ -340,7 +340,7 @@ public class CombinationRecordServiceImpl implements CombinationRecordService {
CombinationRecordStatusEnum.FAILED);
// 2. 订单取消
headAndRecords.forEach(item -> tradeOrderApi.cancelPaidOrder(item.getUserId(), item.getOrderId(),
TradeOrderCancelTypeEnum.COMBINATION_CLOSE));
TradeOrderCancelTypeEnum.COMBINATION_CLOSE.getType()));
}
/**

View File

@ -1,7 +1,6 @@
package cn.iocoder.yudao.module.trade.api.order;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderRespDTO;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderCancelTypeEnum;
import java.util.Collection;
import java.util.List;
@ -34,8 +33,8 @@ public interface TradeOrderApi {
*
* @param userId 用户编号
* @param orderId 订单编号
* @param cancelTypeEnum 取消类型
* @param cancelType 取消类型
*/
void cancelPaidOrder(Long userId, Long orderId, TradeOrderCancelTypeEnum cancelTypeEnum);
void cancelPaidOrder(Long userId, Long orderId, Integer cancelType);
}

View File

@ -60,6 +60,7 @@ public interface ErrorCodeConstants {
ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_TEMPLATE_NOT_FOUND = new ErrorCode(1_011_003_002, "计算快递运费异常,找不到对应的运费模板");
ErrorCode PRICE_CALCULATE_COUPON_NOT_MATCH_NORMAL_ORDER = new ErrorCode(1_011_003_004, "参与秒杀、拼团、砍价的营销商品,无法使用优惠劵");
ErrorCode PRICE_CALCULATE_SECKILL_TOTAL_LIMIT_COUNT = new ErrorCode(1_011_003_005, "参与秒杀的商品,超过了秒杀总限购数量");
ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_TYPE_ILLEGAL = new ErrorCode(1_011_003_006, "计算快递运费异常,配送方式不匹配");
// ========== 物流 Express 模块 1-011-004-000 ==========
ErrorCode EXPRESS_NOT_EXISTS = new ErrorCode(1_011_004_000, "快递公司不存在");

View File

@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.trade.api.order;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderRespDTO;
import cn.iocoder.yudao.module.trade.convert.order.TradeOrderConvert;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderCancelTypeEnum;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderUpdateService;
import jakarta.annotation.Resource;
@ -37,8 +36,8 @@ public class TradeOrderApiImpl implements TradeOrderApi {
}
@Override
public void cancelPaidOrder(Long userId, Long orderId, TradeOrderCancelTypeEnum cancelTypeEnum) {
tradeOrderUpdateService.cancelPaidOrder(userId, orderId, cancelTypeEnum);
public void cancelPaidOrder(Long userId, Long orderId, Integer cancelType) {
tradeOrderUpdateService.cancelPaidOrder(userId, orderId, cancelType);
}
}

View File

@ -160,6 +160,19 @@ public class TradeOrderItemDO extends BaseDO {
*/
private Integer vipPrice;
/**
* 赠送的优惠劵编号的数组
*
* 目的用于后续取消或者售后订单时需要扣减赠送
*/
private List<Long> couponIds;
/**
* 赠送的优惠券数量的数组
*
* 目的用于后续取消或者售后订单时需要扣减赠送
*/
private List<Integer> couponCounts;
// ========== 售后基本信息 ==========
/**

View File

@ -9,7 +9,6 @@ import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderSettle
import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeOrderSettlementRespVO;
import cn.iocoder.yudao.module.trade.controller.app.order.vo.item.AppTradeOrderItemCommentCreateReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderCancelTypeEnum;
import jakarta.validation.constraints.NotNull;
/**
@ -186,14 +185,13 @@ public interface TradeOrderUpdateService {
*/
void updateOrderCombinationInfo(Long orderId, Long activityId, Long combinationRecordId, Long headId);
// TODO @puhui999不传递枚举哈因为 rpc 不好支持
/**
* 取消支付订单
*
* @param userId 用户编号
* @param orderId 订单编号
* @param cancelType 取消类型
* @param cancelType 取消类型
*/
void cancelPaidOrder(Long userId, Long orderId, TradeOrderCancelTypeEnum cancelType);
void cancelPaidOrder(Long userId, Long orderId, Integer cancelType);
}

View File

@ -858,8 +858,11 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
@Override
@Transactional(rollbackFor = Exception.class)
public void cancelPaidOrder(Long userId, Long orderId, TradeOrderCancelTypeEnum cancelType) {
// TODO @puhui999这里校验下 cancelType 只允许拼团关闭
public void cancelPaidOrder(Long userId, Long orderId, Integer cancelType) {
// 1. 这里校验下 cancelType 只允许拼团关闭
if (!TradeOrderCancelTypeEnum.COMBINATION_CLOSE.getType().equals(cancelType)) {
return;
}
// 1.1 检验订单存在
TradeOrderDO order = tradeOrderMapper.selectOrderByIdAndUserId(orderId, userId);
if (order == null) {
@ -876,13 +879,13 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
}
// 2.1 取消订单
cancelOrder0(order, cancelType);
cancelOrder0(order, TradeOrderCancelTypeEnum.COMBINATION_CLOSE);
// 2.2 创建退款单
payRefundApi.createRefund(new PayRefundCreateReqDTO()
.setAppKey(tradeOrderProperties.getPayAppKey()).setUserIp(getClientIP()) // 支付应用
.setMerchantOrderId(String.valueOf(order.getId())) // 支付单号
.setMerchantRefundId(String.valueOf(order.getId()))
.setReason(cancelType.getName()).setPrice(order.getPayPrice()));// 价格信息
.setReason(TradeOrderCancelTypeEnum.COMBINATION_CLOSE.getName()).setPrice(order.getPayPrice()));// 价格信息
}
/**

View File

@ -4,9 +4,9 @@ import cn.iocoder.yudao.module.promotion.api.coupon.CouponApi;
import cn.iocoder.yudao.module.promotion.api.coupon.dto.CouponUseReqDTO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import jakarta.annotation.Resource;
import java.util.List;
/**
@ -30,6 +30,11 @@ public class TradeCouponOrderHandler implements TradeOrderHandler {
.setOrderId(order.getId()));
}
@Override
public void afterPayOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
}
@Override
public void afterCancelOrder(TradeOrderDO order, List<TradeOrderItemDO> orderItems) {
if (order.getCouponId() == null || order.getCouponId() <= 0) {

View File

@ -67,6 +67,21 @@ public class TradePriceCalculateRespBO {
*/
private Long bargainActivityId;
/**
* 是否包邮
*/
private Boolean freeDelivery;
// TODO @puhui999: 订单保存时需要保存
/**
* 赠送的优惠劵编号的数组
*/
private List<Long> couponIds;
/**
* 赠送的优惠券数量的数组
*/
private List<Integer> couponCounts;
/**
* 订单价格
*/
@ -213,8 +228,19 @@ public class TradePriceCalculateRespBO {
*/
private Long categoryId;
// ========== 物流相关字段 =========
/**
* 运费模板 Id
* 配送方式数组
*
* 对应 DeliveryTypeEnum 枚举
*/
private List<Integer> deliveryTypes;
/**
* 物流配置模板编号
*
* 对应 TradeDeliveryExpressTemplateDO id 编号
*/
private Long deliveryTemplateId;
@ -234,7 +260,7 @@ public class TradePriceCalculateRespBO {
private List<ProductPropertyValueDetailRespDTO> properties;
/**
* 使用的积分
* 赠送的积分
*/
private Integer givePoint;

View File

@ -6,8 +6,6 @@ import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.member.api.address.MemberAddressApi;
import cn.iocoder.yudao.module.member.api.address.dto.MemberAddressRespDTO;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.trade.dal.dataobject.config.TradeConfigDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryPickUpStoreDO;
import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryExpressChargeModeEnum;
@ -30,8 +28,7 @@ import java.util.Set;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.PICK_UP_STORE_NOT_EXISTS;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.PRICE_CALCULATE_DELIVERY_PRICE_TEMPLATE_NOT_FOUND;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;
/**
* 运费的 {@link TradePriceCalculator} 实现类
@ -52,19 +49,15 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator {
private DeliveryExpressTemplateService deliveryExpressTemplateService;
@Resource
private TradeConfigService tradeConfigService;
@Resource
private ProductSpuApi productSpuApi;
@Override
public void calculate(TradePriceCalculateReqBO param, TradePriceCalculateRespBO result) {
if (param.getDeliveryType() == null) {
return;
}
// TODO @puhui9991TradePriceCalculateRespBO 传递进来 delveryType 配送方式减少读取2如果不匹配抛出业务异常 = = 不然就不扣钱啦
// 校验是不是存在商品不能门店自提或者不能快递发货的情况就是说配送方式不匹配哈
List<ProductSpuRespDTO> spuList = productSpuApi.getSpuList(convertSet(result.getItems(), OrderItem::getSpuId));
if (anyMatch(spuList, item -> !item.getDeliveryTypes().contains(param.getDeliveryType()))) {
return;
if (anyMatch(result.getItems(), item -> !item.getDeliveryTypes().contains(param.getDeliveryType()))) {
throw exception(PRICE_CALCULATE_DELIVERY_PRICE_TYPE_ILLEGAL);
}
if (DeliveryTypeEnum.PICK_UP.getType().equals(param.getDeliveryType())) {
@ -101,7 +94,12 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator {
return;
}
// 情况二快递模版
// 情况二活动包邮
if (Boolean.TRUE.equals(result.getFreeDelivery())) {
return;
}
// 情况三快递模版
// 2.1 过滤出已选中的商品 SKU
List<OrderItem> selectedItem = filterList(result.getItems(), OrderItem::getSelected);
Set<Long> deliveryTemplateIds = convertSet(selectedItem, OrderItem::getDeliveryTemplateId);

View File

@ -31,8 +31,8 @@ public class TradePriceCalculatorHelper {
List<ProductSpuRespDTO> spuList, List<ProductSkuRespDTO> skuList) {
// 创建 PriceCalculateRespDTO 对象
TradePriceCalculateRespBO result = new TradePriceCalculateRespBO();
result.setType(getOrderType(param));
result.setPromotions(new ArrayList<>());
result.setType(getOrderType(param)).setPromotions(new ArrayList<>())
.setCouponIds(new ArrayList<>()).setCouponCounts(new ArrayList<>());
// 创建它的 OrderItem 属性
result.setItems(new ArrayList<>(param.getItems().size()));
@ -60,7 +60,7 @@ public class TradePriceCalculatorHelper {
.setWeight(sku.getWeight()).setVolume(sku.getVolume());
// spu 信息
orderItem.setSpuName(spu.getName()).setCategoryId(spu.getCategoryId())
.setDeliveryTemplateId(spu.getDeliveryTemplateId())
.setDeliveryTypes(spu.getDeliveryTypes()).setDeliveryTemplateId(spu.getDeliveryTemplateId())
.setGivePoint(spu.getGiveIntegral()).setUsePoint(0);
if (StrUtil.isBlank(orderItem.getPicUrl())) {
orderItem.setPicUrl(spu.getPicUrl());

View File

@ -3,9 +3,11 @@ package cn.iocoder.yudao.module.trade.service.price.calculator;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
import cn.iocoder.yudao.module.promotion.api.reward.RewardActivityApi;
import cn.iocoder.yudao.module.promotion.api.reward.dto.RewardActivityMatchRespDTO;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum;
import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
@ -14,6 +16,8 @@ import jakarta.annotation.Resource;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
@ -61,7 +65,7 @@ public class TradeRewardActivityPriceCalculator implements TradePriceCalculator
if (rule == null) {
TradePriceCalculatorHelper.addNotMatchPromotion(result, orderItems,
rewardActivity.getId(), rewardActivity.getName(), PromotionTypeEnum.REWARD_ACTIVITY.getType(),
getRewardActivityNotMeetTip(rewardActivity));
getRewardActivityNotMeetTip(rewardActivity, orderItems));
return;
}
@ -84,6 +88,26 @@ public class TradeRewardActivityPriceCalculator implements TradePriceCalculator
TradePriceCalculatorHelper.recountPayPrice(orderItem);
}
TradePriceCalculatorHelper.recountAllPrice(result);
// 4.1 记录赠送的积分
if (rule.getGivePoint()) {
List<Integer> dividePoints = TradePriceCalculatorHelper.dividePrice(orderItems, rule.getPoint());
for (int i = 0; i < orderItems.size(); i++) {
TradePriceCalculateRespBO.OrderItem orderItem = orderItems.get(i);
// 商品可能赠送了积分所以这里要加上
orderItem.setGivePoint(orderItem.getGivePoint() + dividePoints.get(i));
}
}
// 4.2 记录订单是否包邮
if (rule.getFreeDelivery()) {
// 只要满足一个活动包邮那么这单就包邮
result.setFreeDelivery(true);
}
// 4.3 记录赠送的优惠券
if (rule.getGiveCoupon()) {
// TODO @puhui999: 需要考虑赠送的优惠券是否重叠重叠则对数量进行累加
result.setCouponIds(rule.getCouponIds()).setCouponCounts(rule.getCouponCounts());
}
}
/**
@ -95,9 +119,21 @@ public class TradeRewardActivityPriceCalculator implements TradePriceCalculator
*/
private List<TradePriceCalculateRespBO.OrderItem> filterMatchActivityOrderItems(TradePriceCalculateRespBO result,
RewardActivityMatchRespDTO rewardActivity) {
// TODO @puhui999是不是得根据类型过滤哈
return filterList(result.getItems(),
orderItem -> CollUtil.contains(rewardActivity.getProductScopeValues(), orderItem.getSpuId()));
// 情况一全部商品都可以参与
if (PromotionProductScopeEnum.isAll(rewardActivity.getProductScope())) {
return result.getItems();
}
// 情况二指定商品参与
if (PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope())) {
return filterList(result.getItems(),
orderItem -> CollUtil.contains(rewardActivity.getProductScopeValues(), orderItem.getSpuId()));
}
// 情况三指定商品类型参与
if (PromotionProductScopeEnum.isCategory(rewardActivity.getProductScope())) {
return filterList(result.getItems(),
orderItem -> CollUtil.contains(rewardActivity.getProductScopeValues(), orderItem.getCategoryId()));
}
return List.of();
}
/**
@ -130,14 +166,30 @@ public class TradeRewardActivityPriceCalculator implements TradePriceCalculator
}
/**
* 获得满减送活动匹配时的提示
* 获得满减送活动匹配时的提示
*
* @param rewardActivity 满减送活动
* @return 提示
*/
private String getRewardActivityNotMeetTip(RewardActivityMatchRespDTO rewardActivity) {
// TODO 芋艿后面再想想应该找第一个规则算下还差多少即可
return "TODO";
private String getRewardActivityNotMeetTip(RewardActivityMatchRespDTO rewardActivity,
List<TradePriceCalculateRespBO.OrderItem> orderItems) {
// 1. 计算数量和价格
Integer count = TradePriceCalculatorHelper.calculateTotalCount(orderItems);
Integer price = TradePriceCalculatorHelper.calculateTotalPayPrice(orderItems);
assert count != null && price != null;
// 2. 构建不满足时的提示信息-按最低档规则算
String meetTip = "满减送:购满 {} {},可以减 {} 元";
List<RewardActivityMatchRespDTO.Rule> rules = new ArrayList<>(rewardActivity.getRules());
rules.sort(Comparator.comparing(RewardActivityMatchRespDTO.Rule::getLimit)); // 按优惠门槛降序
RewardActivityMatchRespDTO.Rule rule = rules.get(0);
if (PromotionConditionTypeEnum.PRICE.getType().equals(rewardActivity.getConditionType())) {
return StrUtil.format(meetTip, rule.getLimit(), "", MoneyUtils.fenToYuanStr(rule.getDiscountPrice()));
}
if (PromotionConditionTypeEnum.COUNT.getType().equals(rewardActivity.getConditionType())) {
return StrUtil.format(meetTip, rule.getLimit(), "", MoneyUtils.fenToYuanStr(rule.getDiscountPrice()));
}
return StrUtil.EMPTY;
}
}