陶杰
2024-09-03 0325d5bd06ea7c7230ea2dd9e78a076cbf41afb9
src/main/java/com/mzl/flower/service/impl/coupon/CouponRecordServiceImpl.java
@@ -249,6 +249,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);
@@ -291,6 +292,9 @@
                couponTemplateDO.setGetStartDate(firstDayStart);
                couponTemplateDO.setGetEndDate(lastDayEnd);
                // 设置总数为当前会员的人数
                couponTemplateDO.setCouponAmount(CollectionUtils.isNotEmpty(customerList)?customerList.size():0);
                // 设置默认类型固定
                couponTemplateDO.setUsageType(CouponUsageTypeEnum.FIXED.getType());
@@ -310,7 +314,7 @@
    }
    @Override
    public boolean expiredCouponRecordByListCurMonth() {
    public boolean expiredCouponRecordLastMon() {
        try{
            LocalDateTime now = LocalDateTime.now();
@@ -424,4 +428,64 @@
        return  baseMapper.selectCount(queryWrapper)>0;
    }
    @Transactional
    @Override
    public boolean useCoupon(String couponId, String orderId) {
        // 优惠券为空
        if(StringUtils.isBlank(couponId)){
            throw new IllegalArgumentException("无效的优惠券");
        }
        // 验证优惠券存在且有效
        final CouponRecordDO couponRecordDO = baseMapper.selectById(couponId);
        if(null==couponRecordDO || StringUtils.isBlank(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不能为空");
        }
        // 优惠券使用操作
        couponRecordDO.setStatus(CouponUsedStatusEnum.USED.getType());
        couponRecordDO.setUsedTime(LocalDateTime.now());
        couponRecordDO.setOrderId(orderId);
        return baseMapper.updateById(couponRecordDO)>0;
    }
    @Transactional
    @Override
    public boolean cancelCouponUsage(String couponId, String orderId) {
        // 查询订单使用的优惠券
        final CouponRecordDO couponRecordDO = getCouponByOrderId(orderId);
        couponRecordDO.setStatus(CouponUsedStatusEnum.UNUSED.getType());
        couponRecordDO.setUsedTime(null);
        couponRecordDO.setOrderId(null);
        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;
    }
}