| | |
| | | import com.mzl.flower.service.coupon.CouponTemplateCustomerService; |
| | | import com.mzl.flower.service.coupon.CouponTemplateService2; |
| | | import com.mzl.flower.service.menber.MemberService; |
| | | import com.mzl.flower.service.payment.RedisLockService; |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | |
| | | |
| | | @Autowired |
| | | private MemberService memberService; |
| | | |
| | | @Autowired |
| | | private RedisLockService lockService; |
| | | |
| | | |
| | | |
| | |
| | | |
| | | // 根据发放后有效期来设置时间 |
| | | if (couponTemplateDO.getUsageTimeNum() == null || couponTemplateDO.getUsageTimeNum() <= 0) { |
| | | throw new IllegalArgumentException("使用时间数量必须为正整数"); |
| | | throw new ValidationException("使用时间数量必须为正整数"); |
| | | } |
| | | LocalDateTime currentTime = LocalDateTime.now(); |
| | | couponTemplateDO.setUsageStartDate(currentTime); |
| | |
| | | List<CouponRecordDO> couponUsageDOList = couponTemplateCustomerDOList.stream().map(pointCustomRe -> { |
| | | |
| | | CouponRecordDO couponRecordDO = new CouponRecordDO(); |
| | | |
| | | couponRecordDO.setId(IdUtil.simpleUUID()); |
| | | couponRecordDO.setCouponId(pointCustomRe.getCouponId()); |
| | | couponRecordDO.setCustomerId(pointCustomRe.getCustomId()); |
| | |
| | | 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.create(SecurityUtils.getUserId()); |
| | |
| | | couponRecordService.saveBatch(couponUsageDOList); |
| | | } |
| | | } |
| | | |
| | | // 如果是活动优惠券且领取渠道是Home类型的,那么只能设置当前优惠券为激活状态,其他优惠券是激活状态的设置为下架状态 |
| | | if (StringUtils.isNotBlank(couponTemplateDO.getCategory()) && couponTemplateDO.getCategory().equals(CouponCategoryEnum.ACTIVITY.getStatus()) |
| | | && StringUtils.isNotBlank(couponTemplateDO.getGetType()) && couponTemplateDO.getGetType().equals(CouponGetTypeEnum.HOME.getType()) |
| | | ) { |
| | | |
| | | couponTemplateMapperCustom.expireHomeActivityCouponTemplate(); |
| | | } |
| | | |
| | | return baseMapper.updateById(couponTemplateDO) > 0; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | public synchronized void exchangeCoupon(ExchangeCouponDTO dto) { |
| | | dto.setNum(1); |
| | | public void exchangeCoupon(ExchangeCouponDTO dto) { |
| | | |
| | | final CouponTemplateDO couponTemplateDO = couponTemplateService.getById(dto.getCouponId()); |
| | | if(couponTemplateDO==null){ |
| | | throw new ValidationException("优惠券不存在"); |
| | | String key="EXCHANGE_COUPON:"+dto.getCouponId()+":"+SecurityUtils.getUserId(); |
| | | boolean lock = lockService.getObjectLock(key, ""); |
| | | if(!lock){ |
| | | throw new ValidationException("系统操作频繁,请稍后重试"); |
| | | } |
| | | try { |
| | | dto.setNum(1); |
| | | final CouponTemplateDO couponTemplateDO = couponTemplateService.getById(dto.getCouponId()); |
| | | if(couponTemplateDO==null){ |
| | | throw new ValidationException("优惠券不存在"); |
| | | } |
| | | if(couponTemplateDO.getCouponAmount()<=0 ){ |
| | | throw new ValidationException("优惠券已兑换完"); |
| | | } |
| | | CustomerPoint cp = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>() |
| | | .eq(CustomerPoint::getUserId, SecurityUtils.getUserId())); |
| | | if(cp == null || (cp.getTotalPoint()-cp.getUsedPoint()-cp.getExpiredPoint()- cp.getExpiredPoint()) < couponTemplateDO.getPoint() * dto.getNum()){ |
| | | throw new ValidationException("积分不足"); |
| | | } |
| | | |
| | | CreateCouponRecordDTO recordDTO =new CreateCouponRecordDTO(); |
| | | recordDTO.setCouponId(dto.getCouponId()); |
| | | recordDTO.setUserId(SecurityUtils.getUserId()); |
| | | Customer customer = customerMapper.selectOne(new LambdaQueryWrapper<Customer>() |
| | | .eq(Customer::getUserId, SecurityUtils.getUserId())); |
| | | if(customer == null){ |
| | | throw new ValidationException("商户不存在"); |
| | | } |
| | | recordDTO.setCustomerId(customer.getId()); |
| | | couponRecordService.createCouponRecord(recordDTO); |
| | | |
| | | // //更新优惠券数量 |
| | | // couponTemplateDO.setCouponAmount(couponTemplateDO.getCouponAmount()-dto.getNum()); |
| | | // couponTemplateService.updateById(couponTemplateDO); |
| | | |
| | | //更新积分汇总 |
| | | cp.setUsedPoint(cp.getUsedPoint()+couponTemplateDO.getPoint()); |
| | | customerPointMapper.updateById(cp); |
| | | |
| | | //记录积分明细 |
| | | CustomerPointDetail detail = new CustomerPointDetail(); |
| | | detail.setUserId(customer.getUserId()); |
| | | detail.setCustomerId(customer.getId()); |
| | | detail.setPoint(couponTemplateDO.getPoint()); |
| | | detail.setChangeType(Constants.POINT_CHANGE_TYPE.reduce.name()); |
| | | detail.setType(Constants.POINT_TYPE.exchange.name()); |
| | | detail.setRecordDate(LocalDate.now()); |
| | | detail.setRemarks(couponTemplateDO.getCouponName()); |
| | | detail.create(SecurityUtils.getUserId()); |
| | | customerPointDetailMapper.insert(detail); |
| | | }catch (Exception e){ |
| | | log.error("兑换失败",e); |
| | | throw new ValidationException("兑换失败"); |
| | | }finally { |
| | | lockService.releaseObjectLock(key,""); |
| | | } |
| | | |
| | | CustomerPoint cp = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>() |
| | | .eq(CustomerPoint::getUserId, SecurityUtils.getUserId())); |
| | | if(cp == null || (cp.getTotalPoint()-cp.getUsedPoint()-cp.getExpiredPoint()) < couponTemplateDO.getPoint() * dto.getNum()){ |
| | | throw new ValidationException("积分不足"); |
| | | } |
| | | |
| | | @Override |
| | | public CouponTemplateVO getHomeActivityEffectAlert(QueryActivityEffectCouponDTO dto) { |
| | | |
| | | // 存在用户是空的情况 |
| | | final CouponTemplateVO homeActivityEffectAlert = couponTemplateMapperCustom.getHomeActivityEffectAlert(dto); |
| | | if(null!=homeActivityEffectAlert ){ |
| | | if(StringUtils.isNotBlank(SecurityUtils.getUserId()) ){ |
| | | // 查看当前已经领取了几张 |
| | | final Integer getCnt = couponRecordService.getUserGainCouponRecordAmountByUserId(homeActivityEffectAlert.getId(), SecurityUtils.getUserId()); |
| | | // 如果当前领取的数量小于限制领取的数量的时候,可以再次领取 |
| | | if(null!=homeActivityEffectAlert.getGetLimit() && null!=getCnt |
| | | && homeActivityEffectAlert.getGetLimit().compareTo(getCnt)>0){ |
| | | return homeActivityEffectAlert; |
| | | } |
| | | }else{ |
| | | return homeActivityEffectAlert; |
| | | } |
| | | } |
| | | |
| | | CreateCouponRecordDTO recordDTO =new CreateCouponRecordDTO(); |
| | | recordDTO.setCouponId(dto.getCouponId()); |
| | | recordDTO.setUserId(SecurityUtils.getUserId()); |
| | | Customer customer = customerMapper.selectOne(new LambdaQueryWrapper<Customer>() |
| | | .eq(Customer::getUserId, SecurityUtils.getUserId())); |
| | | if(customer == null){ |
| | | throw new ValidationException("商户不存在"); |
| | | } |
| | | recordDTO.setCustomerId(customer.getId()); |
| | | couponRecordService.createCouponRecord(recordDTO); |
| | | |
| | | //更新积分汇总 |
| | | cp.setUsedPoint(cp.getUsedPoint()+couponTemplateDO.getPoint()); |
| | | customerPointMapper.updateById(cp); |
| | | |
| | | //记录积分明细 |
| | | CustomerPointDetail detail = new CustomerPointDetail(); |
| | | detail.setUserId(customer.getUserId()); |
| | | detail.setCustomerId(customer.getId()); |
| | | detail.setPoint(couponTemplateDO.getPoint()); |
| | | detail.setChangeType(Constants.POINT_CHANGE_TYPE.reduce.name()); |
| | | detail.setType(Constants.POINT_TYPE.exchange.name()); |
| | | detail.setRecordDate(LocalDate.now()); |
| | | detail.setRemarks(couponTemplateDO.getCouponName()); |
| | | detail.create(SecurityUtils.getUserId()); |
| | | customerPointDetailMapper.insert(detail); |
| | | return null; |
| | | } |
| | | |
| | | @Override |