| | |
| | | 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; |
| | | |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | // 将未使用的优惠券直接过期 |
| | | 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) { |
| | | // 优惠券为空 |
| | | 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); |
| | | // TODO |
| | | 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; |
| | | } |
| | | } |