From ac70afa4e394626dc03d82def3df1db886bb7c2e Mon Sep 17 00:00:00 2001
From: 陶杰 <1378534974@qq.com>
Date: 星期四, 12 九月 2024 14:43:59 +0800
Subject: [PATCH] 1.活动优惠券-管理端-类型是首页领取的活动优惠券,发布的时候将已经发布的设置为下架,只能留有一个已发布的优惠券 3.活动优惠券-小程序-新增首页弹窗优惠券-如果领取数量没有超过当前优惠券还可以再次领取,超过领取优惠券的话则不返回数据

---
 src/main/java/com/mzl/flower/service/impl/coupon/CouponRecordServiceImpl.java |  168 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 160 insertions(+), 8 deletions(-)

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 2de8630..a5138f9 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,9 +30,11 @@
 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;
+import java.util.Objects;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
@@ -61,7 +64,6 @@
 
     @Autowired
     private CustomerService customerService;
-
 
 
     @Override
@@ -133,7 +135,7 @@
                     }
 
                     // 优惠券字段冗余
-                    couponRecordDO.setCategory(couponTemplateDO.getGetUserType());
+                    couponRecordDO.setCategory(couponTemplateDO.getCategory());
                     couponRecordDO.setCouponCode(couponTemplateDO.getCouponCode());
                     couponRecordDO.setCouponName(couponTemplateDO.getCouponName());
                     couponRecordDO.setCouponDiscountValue(couponTemplateDO.getCouponDiscountValue());
@@ -166,7 +168,7 @@
                             // 领取后有段时间 领取后有效时间
                             // 根据发放后有效期来设置时间
                             if (couponTemplateDO.getUsageTimeNum() == null || couponTemplateDO.getUsageTimeNum() <= 0) {
-                                throw new IllegalArgumentException("使用时间数量必须为正整数");
+                                throw new ValidationException("使用时间数量必须为正整数");
                             }
                             LocalDateTime currentTime = LocalDateTime.now();
                             couponRecordDO.setEffectiveStart(currentTime);
@@ -186,6 +188,8 @@
                         }
                     }
 
+                    // 手动设置ID
+                    couponRecordDO.setId(IdUtil.simpleUUID());
                     return baseMapper.insert(couponRecordDO)>0;
 
                 } finally {
@@ -210,7 +214,7 @@
         // 优惠券字段冗余
         final CouponTemplateDO couponTemplateDO = couponTemplateService.getById(dto.getCouponId());
         if(null!=couponTemplateDO){
-            couponRecordDO.setCategory(couponTemplateDO.getGetUserType());
+            couponRecordDO.setCategory(couponTemplateDO.getCategory());
             couponRecordDO.setCouponCode(couponTemplateDO.getCouponCode());
             couponRecordDO.setCouponName(couponTemplateDO.getCouponName());
             couponRecordDO.setCouponDiscountValue(couponTemplateDO.getCouponDiscountValue());
@@ -248,6 +252,7 @@
     public boolean grantVipCouponRecordList() {
 
         try{
+
             LocalDateTime now = LocalDateTime.now();
             LocalDateTime firstDayStart = now.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0).withNano(0);
             LocalDateTime lastDayEnd = now.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59).withNano(0);
@@ -271,11 +276,16 @@
                     couponRecordDO.setEffectiveStart(firstDayStart);
                     couponRecordDO.setEffectiveEnd(lastDayEnd);
                     couponRecordDO.setMemberId(couponTemplateDO.getMemberId());
-
                     // 创建信息
                     couponRecordDO.create();
-                    return couponRecordDO;
-                }).collect(Collectors.toList());
+                    if(!checkCurMonVipCouponExists(couponRecordDO.getCouponId(),couponRecordDO.getCustomerId(),firstDayStart,lastDayEnd)){
+                        return couponRecordDO;
+                    }else{
+                        return null;
+                    }
+
+                }).filter(Objects::nonNull)
+                        .collect(Collectors.toList());
 
                 // 批量保存等级下的优惠券信息
                 saveBatch(gradeCouponRecordList);
@@ -284,6 +294,9 @@
                 couponTemplateDO.setUsageEndDate(lastDayEnd);
                 couponTemplateDO.setGetStartDate(firstDayStart);
                 couponTemplateDO.setGetEndDate(lastDayEnd);
+
+                // 设置总数为当前会员的人数
+                couponTemplateDO.setCouponAmount(CollectionUtils.isNotEmpty(customerList)?customerList.size():0);
 
                 // 设置默认类型固定
                 couponTemplateDO.setUsageType(CouponUsageTypeEnum.FIXED.getType());
@@ -304,7 +317,7 @@
     }
 
     @Override
-    public boolean expiredCouponRecordByListCurMonth() {
+    public boolean expiredCouponRecordLastMon() {
 
         try{
             LocalDateTime now = LocalDateTime.now();
@@ -382,6 +395,17 @@
     }
 
     @Override
+    public Integer getUserGainCouponRecordAmountByUserId(String couponId, String userId) {
+        QueryWrapper<CouponRecordDO> queryWrapper=new QueryWrapper<>();
+        queryWrapper.lambda().eq(CouponRecordDO::getDeleted, TrueOrFalseEnum.FALSE.isFlag())
+                .eq(StringUtils.isNotBlank(couponId), CouponRecordDO::getCouponId,couponId)
+                .eq(null!=userId,CouponRecordDO::getUserId,userId)
+        ;
+
+        return baseMapper.selectCount(queryWrapper);
+    }
+
+    @Override
     public List<CouponRecordVO> getMineCouponRecordList(QueryMineCouponRecordDTO dto) {
         checkCouponExpired(dto);
         return couponRecordMapperCustom.getMineCouponRecordList(dto);
@@ -395,4 +419,132 @@
         // 将未使用的优惠券直接过期
         couponRecordMapperCustom.checkCouponExpired(dto);
     }
+
+    @Override
+    public boolean checkCurMonVipCouponExists(String couponId, Long customId, LocalDateTime startDateTime, LocalDateTime endDateTime) {
+
+        LocalDateTime now = LocalDateTime.now();
+        LocalDateTime firstDayStart = now.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0).withNano(0);
+        LocalDateTime lastDayEnd = now.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59).withNano(0);
+        if(null==startDateTime){
+            startDateTime=firstDayStart;
+        }
+        if(null==endDateTime){
+            endDateTime=lastDayEnd;
+        }
+
+        QueryWrapper<CouponRecordDO> queryWrapper=new QueryWrapper<>();
+        queryWrapper.lambda().eq(CouponRecordDO::getDeleted,TrueOrFalseEnum.FALSE.isFlag())
+                .eq(CouponRecordDO::getCouponId,couponId)
+                .eq(CouponRecordDO::getCustomerId,customId)
+                .eq(CouponRecordDO::getEffectiveStart,startDateTime)
+                .eq(CouponRecordDO::getEffectiveEnd,endDateTime);
+        return  baseMapper.selectCount(queryWrapper)>0;
+
+    }
+
+    @Transactional
+    @Override
+    public boolean useCoupon(String couponId, String orderId, BigDecimal orderMount) {
+        // 优惠券为空
+        if(StringUtils.isBlank(couponId)){
+            throw new ValidationException("无效的优惠券");
+        }
+        if(StringUtils.isBlank(orderId)){
+            throw new ValidationException("订单id不能为空");
+        }
+        if(orderMount.compareTo(BigDecimal.ZERO)<=0){
+            throw new ValidationException("订单金额不能小于0");
+        }
+
+        // 验证优惠券存在且有效
+        final CouponRecordDO couponRecordDO = baseMapper.selectById(couponId);
+        if(null==couponRecordDO || StringUtils.isNotBlank(couponRecordDO.getOrderId()) ){
+            throw new ValidationException("无效的优惠券");
+        }
+
+        if(couponRecordDO.getStatus().equals(CouponUsedStatusEnum.USED.getType())){
+            throw new ValidationException("优惠券已经被使用");
+        }
+        if(couponRecordDO.getStatus().equals(CouponUsedStatusEnum.EXPIRED.getType()) || LocalDateTime.now().isAfter(couponRecordDO.getEffectiveEnd())){
+            throw new ValidationException("优惠券已过期");
+        }
+
+        // 根据类型判断是无门槛还是满减,如果是无门槛
+        if(couponRecordDO.getCouponDiscountType().equals(CouponTypeEnum.ZERO.getType())){
+            // 无门槛,查看金额是否大于满减值
+            if(orderMount.compareTo(couponRecordDO.getCouponDiscountValue())<0){
+                throw new ValidationException(String.format("订单金额(%s)小于无门槛的金额(%s)", orderMount, couponRecordDO.getCouponDiscountValue()));
+            }
+        }
+
+        if(couponRecordDO.getCouponDiscountType().equals(CouponTypeEnum.DISCOUNT.getType())){
+            //满减,查看金额是否满足最小订单额
+            if(orderMount.compareTo(couponRecordDO.getMinOrderAmount())<0){
+                throw new ValidationException(String.format("订单金额(%s)小于最低折扣订单金额(%s)", orderMount, couponRecordDO.getMinOrderAmount()));
+            }
+
+            if(orderMount.compareTo(couponRecordDO.getCouponDiscountValue())<0){
+                throw new ValidationException(String.format("订单金额(%s)小于折扣的金额(%s)", orderMount, couponRecordDO.getCouponDiscountValue()));
+            }
+        }
+
+        // 查看当前的优惠券是否是当前人员的
+        if(!SecurityUtils.getUserId().equals(couponRecordDO.getUserId())){
+            throw new ValidationException("优惠券不属于当前人员");
+        }
+
+        // 优惠券使用操作
+        couponRecordDO.setStatus(CouponUsedStatusEnum.USED.getType());
+        couponRecordDO.setUsedTime(LocalDateTime.now());
+        couponRecordDO.setOrderId(orderId);
+
+        return baseMapper.updateById(couponRecordDO)>0;
+
+    }
+
+
+    @Transactional
+    @Override
+    public boolean cancelCouponUsage(String orderId) {
+        // 查询订单使用的优惠券
+        final CouponRecordDO couponRecordDO = getCouponByOrderId(orderId);
+        if(null==couponRecordDO){
+            return false;
+        }
+        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;
+    }
+
+    @Override
+    public List<CouponRecordDO> getCouponListByOrderId(String orderId) {
+        QueryWrapper<CouponRecordDO> queryWrapper=new QueryWrapper<>();
+        queryWrapper.lambda().eq(CouponRecordDO::getDeleted,TrueOrFalseEnum.FALSE.isFlag())
+                .eq(CouponRecordDO::getOrderId,orderId);
+
+        return baseMapper.selectList(queryWrapper);
+    }
+
+    @Override
+    public CouponRecordDO getCouponByOrderId(String orderId) {
+        final List<CouponRecordDO> couponRecordDOList = getCouponListByOrderId(orderId);
+        if(CollectionUtils.isNotEmpty(couponRecordDOList)){
+            return couponRecordDOList.get(0);
+        }
+        return null;
+    }
+
+    @Override
+    public boolean checkUserCouponExists(String couponId, Long customId) {
+        QueryWrapper<CouponRecordDO> queryWrapper=new QueryWrapper<>();
+        queryWrapper.lambda().eq(CouponRecordDO::getDeleted,TrueOrFalseEnum.FALSE.isFlag())
+                .eq(CouponRecordDO::getCouponId,couponId)
+                .eq(CouponRecordDO::getCustomerId,customId)
+        ;
+        return baseMapper.selectCount(queryWrapper)>0;
+    }
 }

--
Gitblit v1.9.3