优惠券 券使用记录

This commit is contained in:
2025-02-22 11:47:54 +08:00
parent c29f508b4c
commit 97ae7983f1
12 changed files with 229 additions and 159 deletions

View File

@@ -81,8 +81,9 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
@DubboReference
private PointsBasicSettingService pointsBasicService;
@DubboReference
private ShopCouponService couponService;
@DubboReference
private ShopActivateCouponRecordService couponRecordService;
@Resource
private RedisService redisService;
@Resource
@@ -152,38 +153,14 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
if (param.getDiscountRatio().compareTo(BigDecimal.ZERO) <= 0 && param.getDiscountRatio().compareTo(BigDecimal.ONE) > 0) {
throw new ValidateException("生成订单失败,折扣比例不正确");
}
//商品券
//商品券 <商品id数量>
Map<Long, Integer> prodCouponMap = new HashMap<>();
//满减券 满fullCouponAmount 减disCouponAmount
BigDecimal fullAmount = BigDecimal.ZERO;
BigDecimal discountAmount = BigDecimal.ZERO;
if (CollUtil.isNotEmpty(param.getCouponList())) {
//校验优惠券
List<ShopActivateCouponRecord> records = couponRecordService.list(QueryWrapper.create()
.where(ShopActivateCouponRecord::getId).in(param.getCouponList())
.and(ShopActivateCouponRecord::getStatus).eq("not_used"));
if (CollUtil.isEmpty(records)) {
throw new ValidateException("生成订单失败,优惠券信息不存在");
} else if (records.size() != param.getCouponList().size()) {
throw new ValidateException("生成订单失败,优惠券信息不正确");
}
boolean isFullMinus = false;
for (ShopActivateCouponRecord record : records) {
if (record.getType().equals(1)) {
if (isFullMinus) {
throw new ValidateException("生成订单失败,满减券仅可使用一张");
}
fullAmount = record.getFullAmount();
discountAmount = record.getDiscountAmount();
isFullMinus = true;
} else if (record.getType().equals(2)) {
prodCouponMap.compute(record.getProId(), (key, oldValue) -> oldValue == null ? 1 : oldValue + 1);
}
}
}
if (discountAmount.compareTo(param.getFullCouponDiscountAmount()) != 0) {
throw new ValidateException("生成订单失败,满减券减免金额不正确");
}
//校验优惠券
checkCoupon(prodCouponMap, fullAmount, discountAmount, param);
//获取商品信息 计算金额 需要传入优惠券 减去优惠券
List<OrderDetail> orderDetails = cartService.getCartByTableCode(table.getTableCode(), param.isVipPrice() ? 1 : 0, param.getPlaceNum());
//总打包费
@@ -237,6 +214,8 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
//生成订单 //discount_info 所有折扣 几折 折扣金额 满减金额 优惠券金额 积分抵扣金额 抹零
OrderInfo orderInfo = initOrderInfo(param, shopInfo, table);
orderDetailService.createOrderDetails(orderInfo.getId(), orderDetails);
//券消耗
couponService.use(param.getCouponList(), shopUser.getId(), orderInfo.getId());
return orderInfo;
}
@@ -296,6 +275,39 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
orderDetails = resultList;
}
/**
* 校验优惠券可用性 回填优惠券信息
*/
private void checkCoupon(Map<Long, Integer> prodCouponMap, BigDecimal fullAmount, BigDecimal discountAmount, OrderInfoAddDTO param) {
if (CollUtil.isNotEmpty(param.getCouponList())) {
//校验优惠券
List<ShopActivateCouponRecord> records = couponRecordService.list(QueryWrapper.create()
.where(ShopActivateCouponRecord::getId).in(param.getCouponList())
.and(ShopActivateCouponRecord::getStatus).eq(0));
if (CollUtil.isEmpty(records)) {
throw new ValidateException("生成订单失败,优惠券信息不存在");
} else if (records.size() != param.getCouponList().size()) {
throw new ValidateException("生成订单失败,优惠券信息不正确");
}
boolean isFullMinus = false;
for (ShopActivateCouponRecord record : records) {
if (record.getType().equals(1)) {
if (isFullMinus) {
throw new ValidateException("生成订单失败,满减券仅可使用一张");
}
fullAmount = record.getFullAmount();
discountAmount = record.getDiscountAmount();
isFullMinus = true;
} else if (record.getType().equals(2)) {
prodCouponMap.compute(record.getProId(), (key, oldValue) -> oldValue == null ? 1 : oldValue + 1);
}
}
}
if (discountAmount.compareTo(param.getFullCouponDiscountAmount()) != 0) {
throw new ValidateException("生成订单失败,满减券减免金额不正确");
}
}
@Override
@Transactional
public void payCallBackOrder(@NotBlank String orderNo, @NotNull JSONObject resultJson) {
@@ -338,7 +350,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
//更新会员余额 并生成流水
Long flowId = shopUserService.updateMoney(shopUser.getShopId(), shopUserMoneyEditDTO);
//会员活动
activateService.giveActivate(shopUser.getShopId(), shopUser.getId(),
activateService.giveActivate(shopUser,
new BigDecimal(czgCallBackDto.getAmount() / 100L), flowId);
}

View File

@@ -28,6 +28,7 @@ import com.czg.system.enums.SysParamCodeEnum;
import com.czg.system.service.SysParamsService;
import com.czg.utils.AssertUtil;
import com.czg.utils.MD5Util;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import lombok.NonNull;
@@ -64,6 +65,8 @@ public class PayServiceImpl implements PayService {
private ShopUserFlowService userFlowService;
@DubboReference
private ShopActivateCouponRecordService inRecordService;
@DubboReference
private ShopCouponService couponService;
@Resource
private RabbitPublisher rabbitPublisher;
@Resource
@@ -243,7 +246,7 @@ public class PayServiceImpl implements PayService {
//更新会员余额 并生成流水
Long flowId = shopUserService.updateMoney(shopUser.getShopId(), shopUserMoneyEditDTO);
//会员活动
shopActivateService.giveActivate(shopUser.getShopId(), shopUser.getId(), payParam.getAmount(), flowId);
shopActivateService.giveActivate(shopUser, payParam.getAmount(), flowId);
return CzgResult.success();
}
@@ -342,7 +345,7 @@ public class PayServiceImpl implements PayService {
}
Long refPaymentId = null;
if (!refPayParam.isCashRefund()) {
OrderPayment payment = null;
OrderPayment payment;
if (inFlow.getRelationId() != null) {
payment = paymentService.getById(inFlow.getRelationId());
} else {
@@ -379,11 +382,11 @@ public class PayServiceImpl implements PayService {
.eq(ShopUserFlow::getId, giftFlow.getId())
.update();
}
inRecordService.updateChain()
.set(ShopActivateCouponRecord::getStatus, "not_used")
//移除优惠券
inRecordService.remove(QueryWrapper.create()
.eq(ShopActivateCouponRecord::getSourceFlowId, inFlow.getId())
.update();
.eq(ShopActivateCouponRecord::getSource, "activate")
.eq(ShopActivateCouponRecord::getStatus, 0));
return CzgResult.success();
}