Commit a38a035f by 章家宝

feat:【优惠券需求】小程序订单优惠券代码完善

1 parent 49ef4c65
...@@ -224,7 +224,16 @@ public class CouponVerificationService { ...@@ -224,7 +224,16 @@ public class CouponVerificationService {
throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "优惠券已过期或未生效"); throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "优惠券已过期或未生效");
} }
// 5. 校验优惠券是否已被其他订单使用(未退回) // 5. 校验订单是否已有优惠券(一个订单只能使用一张优惠券)
CouponOrderUsage existingOrderUsage = couponOrderUsageRepository
.findByOrderIdAndDeleteFlag(orderId, DeleteFlag.NOT_DELETED.getCode())
.orElse(null);
if (existingOrderUsage != null && existingOrderUsage.getUsageStatus() == 1) {
throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR,
"该订单已使用优惠券,不可重复使用");
}
// 6. 校验优惠券是否已被其他订单使用(未退回)
CouponOrderUsage existingUsage = couponOrderUsageRepository CouponOrderUsage existingUsage = couponOrderUsageRepository
.findByCouponIdAndDeleteFlag(couponId, DeleteFlag.NOT_DELETED.getCode()).orElse(null); .findByCouponIdAndDeleteFlag(couponId, DeleteFlag.NOT_DELETED.getCode()).orElse(null);
if (existingUsage != null && existingUsage.getUsageStatus() == 1) { if (existingUsage != null && existingUsage.getUsageStatus() == 1) {
...@@ -232,17 +241,17 @@ public class CouponVerificationService { ...@@ -232,17 +241,17 @@ public class CouponVerificationService {
throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "优惠券已被其他订单使用"); throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, "优惠券已被其他订单使用");
} }
// 6. 校验使用门槛 // 7. 校验使用门槛
if (coupon.getThresholdAmount() != null && if (coupon.getThresholdAmount() != null &&
transactionAmount.compareTo(coupon.getThresholdAmount()) < 0) { transactionAmount.compareTo(coupon.getThresholdAmount()) < 0) {
throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR,
"订单金额未达到优惠券使用门槛,门槛金额:" + coupon.getThresholdAmount() + "元"); "订单金额未达到优惠券使用门槛,门槛金额:" + coupon.getThresholdAmount() + "元");
} }
// 7. 计算实际抵扣金额 // 8. 计算实际抵扣金额
BigDecimal actualDeduction = calculateActualDeduction(coupon, transactionAmount); BigDecimal actualDeduction = calculateActualDeduction(coupon, transactionAmount);
// 8. 对于阶梯券,校验是否达到阶梯门槛 // 9. 对于阶梯券,校验是否达到阶梯门槛
if (CouponTypeEnum.TIERED_AMOUNT.getCode().equals(coupon.getCouponType())) { if (CouponTypeEnum.TIERED_AMOUNT.getCode().equals(coupon.getCouponType())) {
if (actualDeduction.compareTo(BigDecimal.ZERO) <= 0) { if (actualDeduction.compareTo(BigDecimal.ZERO) <= 0) {
throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR, throw new BusinessCodeException(ErrorCodeEnum.COMMON_ERROR,
...@@ -250,13 +259,13 @@ public class CouponVerificationService { ...@@ -250,13 +259,13 @@ public class CouponVerificationService {
} }
} }
// 9. 更新优惠券状态为已核销 // 10. 更新优惠券状态为已核销
coupon.setStatus(CouponUserStatusEnum.VERIFIED.getCode()); coupon.setStatus(CouponUserStatusEnum.VERIFIED.getCode());
coupon.setNuclearTime(now); coupon.setNuclearTime(now);
coupon.setNuclearOrderId(orderId); coupon.setNuclearOrderId(orderId);
couponAccountRepository.save(coupon); couponAccountRepository.save(coupon);
// 10. 创建订单使用记录 // 11. 创建订单使用记录
CouponOrderUsage usage = CouponOrderUsage.builder() CouponOrderUsage usage = CouponOrderUsage.builder()
.usageId(generateUsageId()) .usageId(generateUsageId())
.orderId(orderId) .orderId(orderId)
...@@ -275,7 +284,7 @@ public class CouponVerificationService { ...@@ -275,7 +284,7 @@ public class CouponVerificationService {
usage.setCreateTime(now); usage.setCreateTime(now);
couponOrderUsageRepository.save(usage); couponOrderUsageRepository.save(usage);
// 11. 创建核销记录 // 12. 创建核销记录
CouponVerificationRecord record = CouponVerificationRecord.builder() CouponVerificationRecord record = CouponVerificationRecord.builder()
.recordId(generateRecordId()) .recordId(generateRecordId())
.couponId(couponId) .couponId(couponId)
...@@ -402,13 +411,23 @@ public class CouponVerificationService { ...@@ -402,13 +411,23 @@ public class CouponVerificationService {
* @param orderId 订单 ID * @param orderId 订单 ID
* @param orderPrice 订单成交价(车款) * @param orderPrice 订单成交价(车款)
* @param commissionPrice 订单佣金 * @param commissionPrice 订单佣金
* @return 优惠券列表(可用券按优先级排序在前,不可用券在后) * @return 优惠券列表(如果订单已使用优惠券,则返回该优惠券并标识已使用;否则可用券按优先级排序在前,不可用券在后)
*/ */
public List<OrderCouponRespVO> getOrderCoupons(Integer accountId, Integer orderId, public List<OrderCouponRespVO> getOrderCoupons(Integer accountId, Integer orderId,
BigDecimal orderPrice, BigDecimal commissionPrice) { BigDecimal orderPrice, BigDecimal commissionPrice) {
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
// 查询用户所有待使用的优惠券(未过期、未核销、未退回) // 1. 先查询该订单是否已使用优惠券
CouponOrderUsage orderUsage = couponOrderUsageRepository
.findByOrderIdAndDeleteFlag(orderId, DeleteFlag.NOT_DELETED.getCode())
.orElse(null);
// 如果订单已使用优惠券且未退回,直接返回该优惠券信息
if (orderUsage != null && orderUsage.getUsageStatus() == 1) {
return buildUsedCouponResponse(orderUsage);
}
// 2. 查询用户所有待使用的优惠券(未过期、未核销、未退回)
List<CouponAccount> allCoupons = couponAccountRepository.findAvailableCoupons( List<CouponAccount> allCoupons = couponAccountRepository.findAvailableCoupons(
accountId, CouponUserStatusEnum.UNUSED.getCode(), "N", now); accountId, CouponUserStatusEnum.UNUSED.getCode(), "N", now);
...@@ -461,6 +480,41 @@ public class CouponVerificationService { ...@@ -461,6 +480,41 @@ public class CouponVerificationService {
} }
/** /**
* 构建订单已使用优惠券的响应
*/
private List<OrderCouponRespVO> buildUsedCouponResponse(CouponOrderUsage orderUsage) {
OrderCouponRespVO vo = new OrderCouponRespVO();
vo.setCouponId(orderUsage.getCouponId());
vo.setTemplateName(orderUsage.getTemplateName());
vo.setFaceAmount(orderUsage.getFaceAmount());
vo.setActualDeduction(orderUsage.getActualDeduction());
vo.setAvailable(false);
vo.setUnavailableReason("优惠券已被当前订单使用");
vo.setUsedByCurrentOrder(true);
vo.setRecommended(false);
// 填充已使用优惠券信息
OrderCouponRespVO.UsedCouponInfo usedInfo = new OrderCouponRespVO.UsedCouponInfo();
usedInfo.setCouponId(orderUsage.getCouponId());
usedInfo.setTemplateName(orderUsage.getTemplateName());
usedInfo.setFaceAmount(orderUsage.getFaceAmount());
usedInfo.setActualDeduction(orderUsage.getActualDeduction());
usedInfo.setUsageTime(orderUsage.getUsageTime());
// 查询优惠券详情获取抵扣类型
CouponAccount coupon = couponAccountRepository
.findByCouponIdAndDeleteFlag(orderUsage.getCouponId(), DeleteFlag.NOT_DELETED.getCode())
.orElse(null);
if (coupon != null) {
usedInfo.setDeductionType(coupon.getDeductionType());
}
vo.setUsedCouponInfo(usedInfo);
return List.of(vo);
}
/**
* 转换为订单优惠券 VO * 转换为订单优惠券 VO
*/ */
private OrderCouponRespVO convertToOrderCouponVO(CouponAccount coupon, BigDecimal orderPrice, private OrderCouponRespVO convertToOrderCouponVO(CouponAccount coupon, BigDecimal orderPrice,
......
package com.glsx.usedcar.module.coupon.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 订单优惠券列表响应 VO
*/
@Data
@Schema(description = "订单优惠券列表响应")
public class OrderCouponRespVO implements Serializable {
@Serial
private static final long serialVersionUID = 1234567890123456790L;
@Schema(description = "优惠券实例 ID")
private String couponId;
@Schema(description = "优惠券模板名称")
private String templateName;
@Schema(description = "优惠券类型 (1:固定面额券;2:阶梯满减券)")
private Integer couponType;
@Schema(description = "抵扣类型 (1:抵扣车款;2:抵扣佣金)")
private Integer deductionType;
@Schema(description = "券面额/可抵扣金额(元)")
private BigDecimal faceAmount;
@Schema(description = "使用门槛(元)")
private BigDecimal thresholdAmount;
@Schema(description = "实际抵扣金额(元)")
private BigDecimal actualDeduction;
@Schema(description = "有效期开始时间")
private LocalDateTime validStartTime;
@Schema(description = "有效期结束时间")
private LocalDateTime validEndTime;
@Schema(description = "领取时间")
private LocalDateTime receiveTime;
@Schema(description = "是否可用 (true:可用;false:不可用)")
private Boolean available;
@Schema(description = "不可用原因(可用时为null)")
private String unavailableReason;
@Schema(description = "是否推荐使用(实际抵扣金额最高的券)")
private Boolean recommended;
@Schema(description = "是否已被当前订单使用 (true:已使用;false:未使用)")
private Boolean usedByCurrentOrder;
@Schema(description = "当前订单已使用的优惠券信息(当usedByCurrentOrder=true时有值)")
private UsedCouponInfo usedCouponInfo;
/**
* 当前订单已使用的优惠券信息
*/
@Data
@Schema(description = "当前订单已使用的优惠券信息")
public static class UsedCouponInfo implements Serializable {
@Serial
private static final long serialVersionUID = 1234567890123456791L;
@Schema(description = "优惠券实例 ID")
private String couponId;
@Schema(description = "优惠券模板名称")
private String templateName;
@Schema(description = "券面额(元)")
private BigDecimal faceAmount;
@Schema(description = "实际抵扣金额(元)")
private BigDecimal actualDeduction;
@Schema(description = "抵扣类型 (1:抵扣车款;2:抵扣佣金)")
private Integer deductionType;
@Schema(description = "使用时间")
private LocalDateTime usageTime;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!