From 0d54ea0de440786f0614870b8c556fba5515a205 Mon Sep 17 00:00:00 2001
From: 陶杰 <1378534974@qq.com>
Date: 星期三, 04 九月 2024 15:38:46 +0800
Subject: [PATCH] 1.优惠券使用增加订单金额判断
---
src/main/java/com/mzl/flower/service/coupon/CouponRecordService.java | 14 ++++---
src/main/java/com/mzl/flower/service/impl/coupon/CouponRecordServiceImpl.java | 50 +++++++++++++++++++++---
2 files changed, 51 insertions(+), 13 deletions(-)
diff --git a/src/main/java/com/mzl/flower/service/coupon/CouponRecordService.java b/src/main/java/com/mzl/flower/service/coupon/CouponRecordService.java
index 9482623..530dc08 100644
--- a/src/main/java/com/mzl/flower/service/coupon/CouponRecordService.java
+++ b/src/main/java/com/mzl/flower/service/coupon/CouponRecordService.java
@@ -6,6 +6,7 @@
import com.mzl.flower.dto.response.coupon.CouponRecordVO;
import com.mzl.flower.entity.coupon.CouponRecordDO;
+import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@@ -75,21 +76,22 @@
boolean checkCurMonVipCouponExists(String couponId, Long customId, LocalDateTime startDateTime,LocalDateTime endDateTime);
+
/**
- * 优惠券使用
- * @param couponId
- * @param orderId
+ * 优惠券的使用
+ * @param couponId 优惠券ID
+ * @param orderId 订单的ID
+ * @param orderMount 订单的金额
* @return
*/
- boolean useCoupon(String couponId,String orderId);
+ boolean useCoupon(String couponId, String orderId, BigDecimal orderMount);
/**
* 优惠券退单
- * @param couponId
* @param orderId
* @return
*/
- boolean cancelCouponUsage(String couponId,String orderId);
+ boolean cancelCouponUsage(String orderId);
/**
* 根据订单号查找优惠券信息
diff --git a/src/main/java/com/mzl/flower/service/impl/coupon/CouponRecordServiceImpl.java b/src/main/java/com/mzl/flower/service/impl/coupon/CouponRecordServiceImpl.java
index 9a58724..ec5067b 100644
--- a/src/main/java/com/mzl/flower/service/impl/coupon/CouponRecordServiceImpl.java
+++ b/src/main/java/com/mzl/flower/service/impl/coupon/CouponRecordServiceImpl.java
@@ -1,6 +1,7 @@
package com.mzl.flower.service.impl.coupon;
import cn.hutool.core.util.IdUtil;
+import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -29,6 +30,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
+import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
@@ -62,7 +64,6 @@
@Autowired
private CustomerService customerService;
-
@Override
@@ -431,24 +432,53 @@
@Transactional
@Override
- public boolean useCoupon(String couponId, String orderId) {
+ public boolean useCoupon(String couponId, String orderId, BigDecimal orderMount) {
// 优惠券为空
if(StringUtils.isBlank(couponId)){
throw new IllegalArgumentException("无效的优惠券");
}
+ if(StringUtils.isBlank(orderId)){
+ throw new IllegalArgumentException("订单id不能为空");
+ }
+ if(orderMount.compareTo(BigDecimal.ZERO)<=0){
+ throw new IllegalArgumentException("订单金额不能小于0");
+ }
+
// 验证优惠券存在且有效
final CouponRecordDO couponRecordDO = baseMapper.selectById(couponId);
- if(null==couponRecordDO || StringUtils.isBlank(couponRecordDO.getOrderId()) ){
+ if(null==couponRecordDO || StringUtils.isNotBlank(couponRecordDO.getOrderId()) ){
throw new IllegalArgumentException("无效的优惠券");
}
+
if(couponRecordDO.getStatus().equals(CouponUsedStatusEnum.USED.getType())){
throw new IllegalArgumentException("优惠券已经被使用");
}
if(couponRecordDO.getStatus().equals(CouponUsedStatusEnum.EXPIRED.getType()) || LocalDateTime.now().isAfter(couponRecordDO.getEffectiveEnd())){
throw new IllegalArgumentException("优惠券已过期");
}
- if(StringUtils.isBlank(orderId)){
- throw new IllegalArgumentException("订单id不能为空");
+
+ // 根据类型判断是无门槛还是满减,如果是无门槛
+ if(couponRecordDO.getCouponDiscountType().equals(CouponTypeEnum.ZERO.getType())){
+ // 无门槛,查看金额是否大于满减值
+ if(orderMount.compareTo(couponRecordDO.getCouponDiscountValue())<0){
+ throw new IllegalArgumentException(String.format("订单金额(%s)小于无门槛的金额(%s)", orderMount, couponRecordDO.getCouponDiscountValue()));
+ }
+ }
+
+ if(couponRecordDO.getCouponDiscountType().equals(CouponTypeEnum.DISCOUNT.getType())){
+ //满减,查看金额是否满足最小订单额
+ if(orderMount.compareTo(couponRecordDO.getMinOrderAmount())<0){
+ throw new IllegalArgumentException(String.format("订单金额(%s)小于最低折扣订单金额(%s)", orderMount, couponRecordDO.getMinOrderAmount()));
+ }
+
+ if(orderMount.compareTo(couponRecordDO.getCouponDiscountValue())<0){
+ throw new IllegalArgumentException(String.format("订单金额(%s)小于折扣的金额(%s)", orderMount, couponRecordDO.getCouponDiscountValue()));
+ }
+ }
+
+ // 查看当前的优惠券是否是当前人员的
+ if(!SecurityUtils.getUserId().equals(couponRecordDO.getUserId())){
+ throw new IllegalArgumentException("优惠券不属于当前人员");
}
// 优惠券使用操作
@@ -457,17 +487,23 @@
couponRecordDO.setOrderId(orderId);
return baseMapper.updateById(couponRecordDO)>0;
+
}
+
@Transactional
@Override
- public boolean cancelCouponUsage(String couponId, String orderId) {
+ public boolean cancelCouponUsage(String orderId) {
// 查询订单使用的优惠券
final CouponRecordDO couponRecordDO = getCouponByOrderId(orderId);
+ if(null==couponRecordDO){
+ throw new IllegalArgumentException("优惠券不存在,无法操作");
+ }
+ log.info("优惠券退回之前:"+ JSON.toJSONString(couponRecordDO));
couponRecordDO.setStatus(CouponUsedStatusEnum.UNUSED.getType());
couponRecordDO.setUsedTime(null);
couponRecordDO.setOrderId(null);
-
+ log.info("优惠券退回之后:"+ JSON.toJSONString(couponRecordDO));
return baseMapper.updateById(couponRecordDO)>0;
}
--
Gitblit v1.9.3