| | |
| | | package com.ruoyi.member.service.impl.coupon; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | |
| | | import com.ruoyi.system.api.service.RemoteConfigService; |
| | | import com.ruoyi.system.api.service.RemoteGoodsService; |
| | | import com.ruoyi.system.api.service.RemoteShopService; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.concurrent.CompletableFuture; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | |
| | | CouponTotal couponTotal; |
| | | if(StringUtils.isNotBlank(mgtCouponEditDto.getCouponId())){ |
| | | coupon = this.getById(mgtCouponEditDto.getCouponId()); |
| | | couponTotal = couponTotalService.getById(mgtCouponEditDto.getCouponId()); |
| | | if(couponTotal.getSendCount()!=null&&couponTotal.getSendCount()>0){ |
| | | throw new ServiceException(AppErrorConstant.COUPON_SENT_EDIT); |
| | | // couponTotal = couponTotalService.getById(mgtCouponEditDto.getCouponId()); |
| | | // if(couponTotal.getSendCount()!=null&&couponTotal.getSendCount()>0){ |
| | | // throw new ServiceException(AppErrorConstant.COUPON_SENT_EDIT); |
| | | // } |
| | | /** |
| | | * 一:不可修改 |
| | | * 1、优惠券类型 |
| | | * 2、发放方式 |
| | | * 3、优惠券金额 |
| | | * 4、使用期限类型 |
| | | * 5、有效时间段:开始时间不能修改,结束时间可以修改 |
| | | * 二:部分可修改 |
| | | * 1、发放方式为指定发放时 |
| | | * 定时发放:已到发放时间不可修改 |
| | | * 立即发放:不可修改 |
| | | * 2、适用范围 |
| | | * 全部商品:不可修改 |
| | | * 部分商品:只能增加商品 |
| | | * 3、发放数量:只能增加 |
| | | * 不限制:不可修改 |
| | | * 4、用户可领数量:只能增加 |
| | | * 三:可修改 |
| | | * 1、优惠券名称 |
| | | * 2、宣传海报 |
| | | * 四:发放对象 |
| | | * 1、未到发放时间都可以修改 |
| | | * 2、已到发放时间,只有自定义时可修改,且只能增加用户 |
| | | */ |
| | | if(!coupon.getCouponType().equals(mgtCouponEditDto.getCouponType())){ |
| | | throw new ServiceException("不能修改优惠券类型"); |
| | | } |
| | | if(!coupon.getSendType().equals(mgtCouponEditDto.getSendType())){ |
| | | throw new ServiceException("不能修改优惠券发放方式"); |
| | | } |
| | | if(null != mgtCouponEditDto.getMoneyThreshold() && coupon.getMoneyThreshold().compareTo(mgtCouponEditDto.getMoneyThreshold()) != 0){ |
| | | throw new ServiceException("不能修改优惠券金额"); |
| | | } |
| | | if(null != mgtCouponEditDto.getDiscountMoney() && coupon.getDiscountMoney().compareTo(mgtCouponEditDto.getDiscountMoney()) != 0){ |
| | | throw new ServiceException("不能修改优惠券金额"); |
| | | } |
| | | if(null != mgtCouponEditDto.getDiscountPercent() && coupon.getDiscountPercent().compareTo(mgtCouponEditDto.getDiscountPercent()) != 0){ |
| | | throw new ServiceException("不能修改优惠券折扣"); |
| | | } |
| | | if(coupon.getSendType() == 2 && coupon.getSendTimeType() == 2 && mgtCouponEditDto.getSendTimeType() == 2 && System.currentTimeMillis() >= coupon.getSendTime().getTime() && null != mgtCouponEditDto.getValidTimeType() && !coupon.getValidTimeType().equals(mgtCouponEditDto.getValidTimeType())){ |
| | | throw new ServiceException("不能修改优惠券适用期限类型"); |
| | | } |
| | | if(coupon.getSendType() == 2 && coupon.getSendTimeType() == 2 && mgtCouponEditDto.getSendTimeType() == 2 && System.currentTimeMillis() >= coupon.getSendTime().getTime() && null != mgtCouponEditDto.getValidTimeType() && coupon.getValidTimeType() == 1 && !coupon.getValidStartTime().equals(mgtCouponEditDto.getValidStartTime())){ |
| | | throw new ServiceException("不能修改优惠券有效期开始时间"); |
| | | } |
| | | if(coupon.getSendType() == 2 && coupon.getSendTimeType() == 1 && mgtCouponEditDto.getSendTimeType() != 1){ |
| | | throw new ServiceException("不能修改优惠券发放方式"); |
| | | } |
| | | if(coupon.getSendType() == 2 && coupon.getSendTimeType() == 2 && mgtCouponEditDto.getSendTimeType() != 2 && System.currentTimeMillis() <= coupon.getSendTime().getTime()){ |
| | | throw new ServiceException("不能修改优惠券发放方式"); |
| | | } |
| | | if(coupon.getSendType() == 2 && coupon.getSendTimeType() == 2 && mgtCouponEditDto.getSendTimeType() == 2 && System.currentTimeMillis() >= coupon.getSendTime().getTime()){ |
| | | throw new ServiceException("不能修改优惠券发放时间"); |
| | | } |
| | | if(coupon.getSendType() == 2 && coupon.getSendTimeType() == 2 && mgtCouponEditDto.getSendTimeType() == 2 && System.currentTimeMillis() >= coupon.getSendTime().getTime() && coupon.getUseScope() == 1 && !coupon.getUseScope().equals(mgtCouponEditDto.getUseScope())){ |
| | | throw new ServiceException("不能修改优惠券适用范围"); |
| | | } |
| | | |
| | | if(coupon.getUseScope() == 2 && coupon.getUseScope().equals(mgtCouponEditDto.getUseScope())){ |
| | | List<String> relGoodsIdList = mgtCouponEditDto.getRelGoodsIdList(); |
| | | List<CouponRelGoods> list = couponRelGoodsService.list(new QueryWrapper<CouponRelGoods>().eq("coupon_id", coupon.getCouponId()).eq("del_flag", 0)); |
| | | for (CouponRelGoods couponRelGoods : list) { |
| | | if(!relGoodsIdList.contains(couponRelGoods.getGoodsId())){ |
| | | throw new ServiceException("不能删除优惠券适用商品"); |
| | | } |
| | | } |
| | | } |
| | | if(coupon.getSendLimitFlag() == 0 && mgtCouponEditDto.getSendLimitFlag() != 0){ |
| | | throw new ServiceException("不能修改优惠券发放数量限制"); |
| | | } |
| | | if(coupon.getSendLimitFlag() == 1 && mgtCouponEditDto.getSendLimitFlag() == 1 && coupon.getSendLimitNumber().compareTo(mgtCouponEditDto.getSendLimitNumber()) > 0){ |
| | | throw new ServiceException("不能修改优惠券发放数量限制"); |
| | | } |
| | | if(coupon.getLimitNumber().compareTo(mgtCouponEditDto.getLimitNumber()) > 0){ |
| | | throw new ServiceException("只能增加优惠券领取数量"); |
| | | } |
| | | List<CouponRelUser> list = couponRelUserService.list(new QueryWrapper<CouponRelUser>().eq("coupon_id", coupon.getCouponId()).eq("del_flag", 0)); |
| | | List<Long> relUserIdList = mgtCouponEditDto.getRelUserIdList(); |
| | | for (CouponRelUser couponRelUser : list) { |
| | | if(!relUserIdList.contains(couponRelUser.getUserId())){ |
| | | throw new ServiceException("只能增加优惠发放对象"); |
| | | } |
| | | } |
| | | if(relUserIdList.size() != list.size() && coupon.getSendType() == 2 && coupon.getSendTimeType() == 1){ |
| | | throw new ServiceException("只能修改优惠发放对象"); |
| | | } |
| | | if(relUserIdList.size() != list.size() && coupon.getSendType() == 2 && coupon.getSendTimeType() == 2 && System.currentTimeMillis() <= coupon.getSendTime().getTime()){ |
| | | throw new ServiceException("只能修改优惠发放对象"); |
| | | } |
| | | |
| | | couponRelUserService.deleteCouponRelByCouponId(coupon.getCouponId()); |
| | | couponRelGoodsService.deleteCouponRelByCouponId(coupon.getCouponId()); |
| | | |
| | | }else{ |
| | | coupon = new Coupon(); |
| | | String couponId = IdUtils.simpleUUID(); |
| | |
| | | coupon.setRelGoodsIds(relGoodsIds); |
| | | } |
| | | //判断是否指定用户 |
| | | List<Long> relUserIdList = null; |
| | | List<Long> relUserIdList; |
| | | if(coupon.getSendTarget()==5&&coupon.getRelationType()==1&&!mgtCouponEditDto.getRelUserIdList().isEmpty()){ |
| | | relUserIdList = mgtCouponEditDto.getRelUserIdList(); |
| | | List<CouponRelUser> couponRelUserList = new ArrayList<>(); |
| | |
| | | } |
| | | couponRelUserService.saveBatch(couponRelUserList); |
| | | coupon.setRelUserIds(userIdSj.toString()); |
| | | } else { |
| | | relUserIdList = null; |
| | | } |
| | | //处理优惠券发放 |
| | | if(coupon.getSendType()==2&&coupon.getSendTimeType()==1){ |
| | | sendCoupon(coupon,relUserIdList); |
| | | if(coupon.getSendFlag() == 0 && coupon.getSendType()==2&&coupon.getSendTimeType()==1){ |
| | | CompletableFuture.runAsync(()->sendCoupon(coupon,relUserIdList)); |
| | | coupon.setSendFlag(1); |
| | | this.saveOrUpdate(coupon); |
| | | }else{ |
| | |
| | | * @param relUserIdList |
| | | * @return void |
| | | */ |
| | | @Async |
| | | protected void sendCoupon(Coupon coupon, List<Long> relUserIdList){ |
| | | //1.手动领取2.全部用户3.会员用户4非会员用户5自定义 |
| | | Date nowTime = new Date(); |
| | |
| | | userIdList = relUserIdList; |
| | | } |
| | | } |
| | | //商户发送全部,则发送没有绑定的会员及绑定了当前商户端会员 |
| | | if(coupon.getCouponFrom() == 2 && sendTarget == 2){ |
| | | shopId = coupon.getShopId(); |
| | | userIdList = memberService.listIdBySendType2(shopId); |
| | | } |
| | | |
| | | |
| | | if(userIdList!=null&&!userIdList.isEmpty()){ |
| | | List<MemberCoupon> memberCouponList = new ArrayList<>(); |
| | | MemberCoupon memberCoupon; |
| | | Integer sendTotal = 0; |
| | | Integer sendPerson = 0; |
| | | String memberCouponId; |
| | | Integer sendUserTotal; |
| | | for(Long userId : userIdList){ |
| | | if(coupon.getCouponFrom()==2){ |
| | | for(int i=0;i<coupon.getLimitNumber();i++){ |
| | | memberCouponId = IdUtils.simpleUUID(); |
| | | memberCoupon = new MemberCoupon(); |
| | | memberCoupon.setId(memberCouponId); |
| | | memberCoupon.setDelFlag(0); |
| | | memberCoupon.setCouponId(coupon.getCouponId()); |
| | | memberCoupon.setUserId(userId); |
| | |
| | | memberCouponRecordService.updateMemberCouponRecord(coupon,userId,1); |
| | | } |
| | | }else{ |
| | | memberCouponId = IdUtils.simpleUUID(); |
| | | memberCoupon = new MemberCoupon(); |
| | | memberCoupon.setId(memberCouponId); |
| | | memberCoupon.setDelFlag(0); |
| | | memberCoupon.setCouponId(coupon.getCouponId()); |
| | | memberCoupon.setUserId(userId); |
| | | //memberCoupon.setShopId(coupon.getShopId()); |
| | | memberCoupon.setCouponType(coupon.getCouponType()); |
| | | if(coupon.getCouponType()==1){ |
| | | memberCoupon.setMoneyThreshold(coupon.getMoneyThreshold()); |
| | | memberCoupon.setDiscountMoney(coupon.getDiscountMoney()); |
| | | }else if(coupon.getCouponType()==2){ |
| | | memberCoupon.setDiscountPercent(coupon.getDiscountPercent()); |
| | | }else if(coupon.getCouponType()==3){ |
| | | memberCoupon.setDiscountMoney(coupon.getDiscountMoney()); |
| | | }else{ |
| | | memberCoupon.setRelGoodsIds(coupon.getRelGoodsIds()); |
| | | } |
| | | memberCoupon.setCouponStatus(coupon.getCouponStatus()); |
| | | memberCoupon.setCouponName(coupon.getCouponName()); |
| | | memberCoupon.setSendType(coupon.getSendType()); |
| | | memberCoupon.setSendTarget(coupon.getSendTarget()); |
| | | memberCoupon.setSendTimeType(coupon.getSendTimeType()); |
| | | memberCoupon.setSendTime(coupon.getSendTime()); |
| | | memberCoupon.setUseScope(coupon.getUseScope()); |
| | | if(memberCoupon.getUseScope()==2){ |
| | | memberCoupon.setRelGoodsIds(coupon.getRelGoodsIds()); |
| | | } |
| | | memberCoupon.setValidTimeType(coupon.getValidTimeType()); |
| | | memberCoupon.setValidStartTime(coupon.getValidStartTime()); |
| | | memberCoupon.setValidEndTime(coupon.getValidEndTime()); |
| | | memberCoupon.setValidDay(coupon.getValidDay()); |
| | | memberCoupon.setCouponFrom(coupon.getCouponFrom()); |
| | | memberCoupon.setReceiveTime(nowTime); |
| | | //有效期处理1.时间段2.领取之日起 |
| | | if(coupon.getValidTimeType()==1){ |
| | | memberCoupon.setDeadlineTime(coupon.getValidEndTime()); |
| | | }else if(coupon.getValidTimeType()==2){ |
| | | memberCoupon.setDeadlineTime(DateUtils.addDays(nowTime,coupon.getValidDay())); |
| | | } |
| | | memberCouponList.add(memberCoupon); |
| | | sendTotal = sendTotal + 1; |
| | | if(coupon.getSendType()==1){ |
| | | sendUserTotal = memberCouponService.totalMemberCouponByUserAndCoupon(userId,coupon.getCouponId()); |
| | | if(sendUserTotal!=null&&sendUserTotal>0){ |
| | | for (Integer i = 0; i < coupon.getLimitNumber(); i++) { |
| | | memberCoupon = new MemberCoupon(); |
| | | memberCoupon.setDelFlag(0); |
| | | memberCoupon.setCouponId(coupon.getCouponId()); |
| | | memberCoupon.setUserId(userId); |
| | | //memberCoupon.setShopId(coupon.getShopId()); |
| | | memberCoupon.setCouponType(coupon.getCouponType()); |
| | | if(coupon.getCouponType()==1){ |
| | | memberCoupon.setMoneyThreshold(coupon.getMoneyThreshold()); |
| | | memberCoupon.setDiscountMoney(coupon.getDiscountMoney()); |
| | | }else if(coupon.getCouponType()==2){ |
| | | memberCoupon.setDiscountPercent(coupon.getDiscountPercent()); |
| | | }else if(coupon.getCouponType()==3){ |
| | | memberCoupon.setDiscountMoney(coupon.getDiscountMoney()); |
| | | }else{ |
| | | sendPerson = sendPerson + 1; |
| | | memberCoupon.setRelGoodsIds(coupon.getRelGoodsIds()); |
| | | } |
| | | memberCoupon.setCouponStatus(coupon.getCouponStatus()); |
| | | memberCoupon.setCouponName(coupon.getCouponName()); |
| | | memberCoupon.setSendType(coupon.getSendType()); |
| | | memberCoupon.setSendTarget(coupon.getSendTarget()); |
| | | memberCoupon.setSendTimeType(coupon.getSendTimeType()); |
| | | memberCoupon.setSendTime(coupon.getSendTime()); |
| | | memberCoupon.setUseScope(coupon.getUseScope()); |
| | | if(memberCoupon.getUseScope()==2){ |
| | | memberCoupon.setRelGoodsIds(coupon.getRelGoodsIds()); |
| | | } |
| | | memberCoupon.setValidTimeType(coupon.getValidTimeType()); |
| | | memberCoupon.setValidStartTime(coupon.getValidStartTime()); |
| | | memberCoupon.setValidEndTime(coupon.getValidEndTime()); |
| | | memberCoupon.setValidDay(coupon.getValidDay()); |
| | | memberCoupon.setCouponFrom(coupon.getCouponFrom()); |
| | | memberCoupon.setReceiveTime(nowTime); |
| | | //有效期处理1.时间段2.领取之日起 |
| | | if(coupon.getValidTimeType()==1){ |
| | | memberCoupon.setDeadlineTime(coupon.getValidEndTime()); |
| | | }else if(coupon.getValidTimeType()==2){ |
| | | memberCoupon.setDeadlineTime(DateUtils.addDays(nowTime,coupon.getValidDay())); |
| | | } |
| | | memberCouponList.add(memberCoupon); |
| | | sendTotal = sendTotal + 1; |
| | | if(coupon.getSendType()==1){ |
| | | sendUserTotal = memberCouponService.totalMemberCouponByUserAndCoupon(userId,coupon.getCouponId()); |
| | | if(sendUserTotal!=null&&sendUserTotal>0){ |
| | | }else{ |
| | | sendPerson = sendPerson + 1; |
| | | } |
| | | } |
| | | memberCouponRecordService.updateMemberCouponRecord(coupon,userId,1); |
| | | } |
| | | memberCouponRecordService.updateMemberCouponRecord(coupon,userId,1); |
| | | } |
| | | } |
| | | memberCouponService.saveBatch(memberCouponList); |
| | |
| | | userIdList = relUserIdList; |
| | | } |
| | | } |
| | | //商户发送全部,则发送没有绑定的会员及绑定了当前商户端会员 |
| | | if(coupon.getCouponFrom() == 2 && sendTarget == 2){ |
| | | shopId = coupon.getShopId(); |
| | | userIdList = memberService.listIdBySendType2(shopId); |
| | | } |
| | | |
| | | if(relUserIdList!=null&&!relUserIdList.isEmpty()){ |
| | | List<MemberCoupon> memberCouponList = new ArrayList<>(); |
| | | MemberCoupon memberCoupon; |
| | | Integer sendTotal = 0; |
| | | Integer sendPerson = 0; |
| | | String memberCouponId; |
| | | Integer sendUserTotal; |
| | | for(Long userId : userIdList){ |
| | | memberCouponId = IdUtils.simpleUUID(); |
| | | memberCoupon = new MemberCoupon(); |
| | | memberCoupon.setId(memberCouponId); |
| | | memberCoupon.setDelFlag(0); |
| | | memberCoupon.setCouponId(coupon.getCouponId()); |
| | | memberCoupon.setUserId(userId); |
| | |
| | | public void deleteMgtCoupon(MgtBaseGetDto mgtBaseGetDto){ |
| | | Coupon coupon = this.getById(mgtBaseGetDto.getId()); |
| | | CouponTotal couponTotal = couponTotalService.getById(mgtBaseGetDto.getId()); |
| | | if(couponTotal.getSendCount()!=null&&couponTotal.getSendCount()>0){ |
| | | throw new ServiceException(AppErrorConstant.COUPON_SENT_DEL); |
| | | } |
| | | // if(couponTotal.getSendCount()!=null&&couponTotal.getSendCount()>0){ |
| | | // throw new ServiceException(AppErrorConstant.COUPON_SENT_DEL); |
| | | // } |
| | | coupon.setCouponStatus(-1); |
| | | coupon.setDelFlag(1); |
| | | coupon.setUpdateUserId(mgtBaseGetDto.getUserId()); |
| | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | String useLimit = ""; |
| | | for(MgtCouponAuditPageVo mgtCouponAuditPageVo : mgtCouponAuditPageVoList){ |
| | | useLimit = null; |
| | | //处理使用限制 |
| | | String couponType = mgtCouponAuditPageVo.getCouponType(); |
| | | if(couponType.equals("满减")){ |
| | | BigDecimal moneyThreshold = mgtCouponAuditPageVo.getMoneyThreshold(); |
| | | BigDecimal discountMoney = mgtCouponAuditPageVo.getDiscountMoney(); |
| | | useLimit = "满" + moneyThreshold.toString() + "减" + discountMoney.toString() + "元"; |
| | | } |
| | | if(couponType.equals("折扣")){ |
| | | BigDecimal discountPercent = mgtCouponAuditPageVo.getDiscountPercent(); |
| | | useLimit = discountPercent.toString() + "折"; |
| | | } |
| | | if(couponType.equals("代金")){ |
| | | BigDecimal discountMoney = mgtCouponAuditPageVo.getDiscountMoney(); |
| | | useLimit = discountMoney.toString(); |
| | | } |
| | | mgtCouponAuditPageVo.setUseLimit(useLimit); |
| | | } |
| | | return mgtCouponAuditPageVoList; |
| | | } |
| | |
| | | Coupon coupon; |
| | | List<MemberCoupon> memberCouponList = new ArrayList<>(); |
| | | MemberCoupon memberCoupon; |
| | | String memberCouponId; |
| | | |
| | | // 遍历生日礼物发送DTO列表 |
| | | for(BirthdayGiftSendDto birthdayGiftSendDto : giftSendDtoList){ |
| | |
| | | // 根据优惠券数量生成会员优惠券列表 |
| | | for(int i=0;i<birthdayGiftSendDto.getCouponNumber();i++){ |
| | | memberCoupon = new MemberCoupon(); |
| | | memberCouponId = IdUtils.simpleUUID(); |
| | | memberCoupon.setId(memberCouponId); |
| | | memberCoupon.setDelFlag(0); |
| | | memberCoupon.setCouponId(couponId); |
| | | memberCoupon.setUserId(userId); |
| | |
| | | } |
| | | //生成用户优惠券 |
| | | MemberCoupon memberCoupon = new MemberCoupon(); |
| | | String memberCouponId = IdUtils.simpleUUID(); |
| | | memberCoupon.setId(memberCouponId); |
| | | memberCoupon.setDelFlag(0); |
| | | memberCoupon.setCouponId(couponId); |
| | | memberCoupon.setUserId(userId); |
| | |
| | | }else if(coupon.getValidTimeType()==2){ |
| | | memberCoupon.setDeadlineTime(DateUtils.addDays(new Date(),coupon.getValidDay())); |
| | | } |
| | | memberCouponService.save(memberCoupon); |
| | | memberCouponRecordService.updateMemberCouponRecord(coupon,userId,1); |
| | | memberCouponService.insert(memberCoupon); |
| | | } |
| | | |
| | | /** |