| | |
| | | // 获取锁,最多等待 10 秒,锁自动释放时间 30 秒 |
| | | if (lock.tryLock(10, 30, TimeUnit.SECONDS)) { |
| | | try { |
| | | // 活动优惠券和积分优惠券需要根据库存来控制- 根据优惠券的发放数量来控制有没有超发 |
| | | if(StringUtils.isNotBlank(couponTemplateDO.getCategory()) && ( |
| | | couponTemplateDO.getCategory().equals(CouponCategoryEnum.ACTIVITY.getStatus()) || couponTemplateDO.getCategory().equals(CouponCategoryEnum.POINT.getStatus()) |
| | | )){ |
| | | // 活动优惠券 根据优惠券的发放数量来控制有没有超发 |
| | | if(StringUtils.isNotBlank(couponTemplateDO.getCategory()) && |
| | | couponTemplateDO.getCategory().equals(CouponCategoryEnum.ACTIVITY.getStatus()) |
| | | ){ |
| | | // 获取当前优惠券已经领取的数量 |
| | | final Integer gainTotal = getExistGainCouponRecordAmountById(couponTemplateDO.getId()); |
| | | if(couponTemplateDO.getCouponAmount().compareTo(gainTotal)<=0){ |
| | | throw new ValidationException("当前优惠券已经领完!"); |
| | | } |
| | | } |
| | | if(StringUtils.isNotBlank(couponTemplateDO.getCategory()) |
| | | && couponTemplateDO.getCategory().equals(CouponCategoryEnum.POINT.getStatus() |
| | | )){ |
| | | // 获取当前优惠券已经领取的数量 |
| | | |
| | | if(couponTemplateDO.getCouponAmount()<=0){ |
| | | throw new ValidationException("当前优惠券已经领完!"); |
| | | }else{ |
| | | couponTemplateDO.setCouponAmount(couponTemplateDO.getCouponAmount()-1); |
| | | // 更新库存 |
| | | couponTemplateService.updateById(couponTemplateDO); |
| | | } |
| | | |
| | | } |
| | | |
| | | // 根据用户领取设置的getLimit 查看当前用户是否已经超领优惠券 |
| | |
| | | } |
| | | |
| | | // 优惠券字段冗余 |
| | | couponRecordDO.setCategory(couponTemplateDO.getGetUserType()); |
| | | couponRecordDO.setCategory(couponTemplateDO.getCategory()); |
| | | couponRecordDO.setCouponCode(couponTemplateDO.getCouponCode()); |
| | | couponRecordDO.setCouponName(couponTemplateDO.getCouponName()); |
| | | couponRecordDO.setCouponDiscountValue(couponTemplateDO.getCouponDiscountValue()); |
| | |
| | | couponRecordDO.setCouponDiscountType(couponTemplateDO.getCouponDiscountType()); |
| | | couponRecordDO.setGetUserType(couponTemplateDO.getGetUserType()); |
| | | couponRecordDO.setPoint(couponTemplateDO.getPoint()); |
| | | couponRecordDO.setMemberId(couponRecordDO.getMemberId()); |
| | | couponRecordDO.setMemberId(couponTemplateDO.getMemberId()); |
| | | couponRecordDO.setImageUrl(couponTemplateDO.getImageUrl()); |
| | | couponRecordDO.setCouponDescription(couponTemplateDO.getCouponDescription()); |
| | | |
| | | |
| | | // 根据优惠券模板来计算优惠券的生效开始时间和结束时间 |
| | |
| | | // 领取后有段时间 领取后有效时间 |
| | | // 根据发放后有效期来设置时间 |
| | | if (couponTemplateDO.getUsageTimeNum() == null || couponTemplateDO.getUsageTimeNum() <= 0) { |
| | | throw new IllegalArgumentException("使用时间数量必须为正整数"); |
| | | throw new ValidationException("使用时间数量必须为正整数"); |
| | | } |
| | | LocalDateTime currentTime = LocalDateTime.now(); |
| | | couponRecordDO.setEffectiveStart(currentTime); |
| | |
| | | } |
| | | } |
| | | |
| | | // 手动设置ID |
| | | couponRecordDO.setId(IdUtil.simpleUUID()); |
| | | return baseMapper.insert(couponRecordDO)>0; |
| | | |
| | | } finally { |
| | |
| | | // 优惠券字段冗余 |
| | | 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()); |
| | |
| | | couponRecordDO.setEffectiveStart(firstDayStart); |
| | | couponRecordDO.setEffectiveEnd(lastDayEnd); |
| | | couponRecordDO.setMemberId(couponTemplateDO.getMemberId()); |
| | | couponRecordDO.setImageUrl(couponTemplateDO.getImageUrl()); |
| | | couponRecordDO.setCouponDescription(couponTemplateDO.getCouponDescription()); |
| | | // 创建信息 |
| | | couponRecordDO.create(); |
| | | if(!checkCurMonVipCouponExists(couponRecordDO.getCouponId(),couponRecordDO.getCustomerId(),firstDayStart,lastDayEnd)){ |
| | |
| | | } |
| | | |
| | | @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); |
| | |
| | | public boolean useCoupon(String couponId, String orderId, BigDecimal orderMount) { |
| | | // 优惠券为空 |
| | | if(StringUtils.isBlank(couponId)){ |
| | | throw new IllegalArgumentException("无效的优惠券"); |
| | | throw new ValidationException("无效的优惠券"); |
| | | } |
| | | if(StringUtils.isBlank(orderId)){ |
| | | throw new IllegalArgumentException("订单id不能为空"); |
| | | throw new ValidationException("订单id不能为空"); |
| | | } |
| | | if(orderMount.compareTo(BigDecimal.ZERO)<=0){ |
| | | throw new IllegalArgumentException("订单金额不能小于0"); |
| | | throw new ValidationException("订单金额不能小于0"); |
| | | } |
| | | |
| | | // 验证优惠券存在且有效 |
| | | final CouponRecordDO couponRecordDO = baseMapper.selectById(couponId); |
| | | if(null==couponRecordDO || StringUtils.isNotBlank(couponRecordDO.getOrderId()) ){ |
| | | throw new IllegalArgumentException("无效的优惠券"); |
| | | throw new ValidationException("无效的优惠券"); |
| | | } |
| | | |
| | | if(couponRecordDO.getStatus().equals(CouponUsedStatusEnum.USED.getType())){ |
| | | throw new IllegalArgumentException("优惠券已经被使用"); |
| | | throw new ValidationException("优惠券已经被使用"); |
| | | } |
| | | if(couponRecordDO.getStatus().equals(CouponUsedStatusEnum.EXPIRED.getType()) || LocalDateTime.now().isAfter(couponRecordDO.getEffectiveEnd())){ |
| | | throw new IllegalArgumentException("优惠券已过期"); |
| | | throw new ValidationException("优惠券已过期"); |
| | | } |
| | | |
| | | // 根据类型判断是无门槛还是满减,如果是无门槛 |
| | | if(couponRecordDO.getCouponDiscountType().equals(CouponTypeEnum.ZERO.getType())){ |
| | | // 无门槛,查看金额是否大于满减值 |
| | | if(orderMount.compareTo(couponRecordDO.getCouponDiscountValue())<0){ |
| | | throw new IllegalArgumentException(String.format("订单金额(%s)小于无门槛的金额(%s)", orderMount, couponRecordDO.getCouponDiscountValue())); |
| | | 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 IllegalArgumentException(String.format("订单金额(%s)小于最低折扣订单金额(%s)", orderMount, couponRecordDO.getMinOrderAmount())); |
| | | throw new ValidationException(String.format("订单金额(%s)小于最低折扣订单金额(%s)", orderMount, couponRecordDO.getMinOrderAmount())); |
| | | } |
| | | |
| | | if(orderMount.compareTo(couponRecordDO.getCouponDiscountValue())<0){ |
| | | throw new IllegalArgumentException(String.format("订单金额(%s)小于折扣的金额(%s)", orderMount, couponRecordDO.getCouponDiscountValue())); |
| | | throw new ValidationException(String.format("订单金额(%s)小于折扣的金额(%s)", orderMount, couponRecordDO.getCouponDiscountValue())); |
| | | } |
| | | } |
| | | |
| | | // 查看当前的优惠券是否是当前人员的 |
| | | if(!SecurityUtils.getUserId().equals(couponRecordDO.getUserId())){ |
| | | throw new IllegalArgumentException("优惠券不属于当前人员"); |
| | | throw new ValidationException("优惠券不属于当前人员"); |
| | | } |
| | | |
| | | // 优惠券使用操作 |
| | |
| | | // 查询订单使用的优惠券 |
| | | final CouponRecordDO couponRecordDO = getCouponByOrderId(orderId); |
| | | if(null==couponRecordDO){ |
| | | throw new IllegalArgumentException("优惠券不存在,无法操作"); |
| | | return false; |
| | | } |
| | | log.info("优惠券退回之前:"+ JSON.toJSONString(couponRecordDO)); |
| | | couponRecordDO.setStatus(CouponUsedStatusEnum.UNUSED.getType()); |
| | |
| | | } |
| | | 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; |
| | | } |
| | | |
| | | @Override |
| | | public boolean expireCouponRecordAll() { |
| | | |
| | | return couponRecordMapperCustom.expireCouponRecordAll(); |
| | | } |
| | | } |