| | |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.core.utils.bean.BeanUtils; |
| | | import com.ruoyi.common.core.utils.uuid.IdUtils; |
| | | import com.ruoyi.common.redis.service.RedisService; |
| | | import com.ruoyi.member.domain.dto.*; |
| | | import com.ruoyi.member.domain.pojo.coupon.*; |
| | | import com.ruoyi.member.domain.vo.*; |
| | |
| | | import com.ruoyi.member.service.member.MemberCouponService; |
| | | import com.ruoyi.member.service.member.MemberService; |
| | | import com.ruoyi.system.api.constant.AppErrorConstant; |
| | | import com.ruoyi.system.api.constant.DelayTaskEnum; |
| | | import com.ruoyi.system.api.domain.dto.*; |
| | | import com.ruoyi.system.api.domain.poji.config.DelayTask; |
| | | import com.ruoyi.system.api.domain.poji.goods.Goods; |
| | | import com.ruoyi.system.api.domain.poji.member.MemberCoupon; |
| | | import com.ruoyi.system.api.domain.poji.shop.Shop; |
| | | import com.ruoyi.system.api.domain.vo.MerGoodsPriceListVo; |
| | | import com.ruoyi.system.api.service.RemoteActivityService; |
| | | 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 java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | @Resource |
| | | private MemberCouponRecordService memberCouponRecordService; |
| | | |
| | | @Resource |
| | | private RemoteShopService remoteShopService; |
| | | |
| | | @Resource |
| | | private RedisService redisService; |
| | | |
| | | @Resource |
| | | private RemoteConfigService remoteConfigService; |
| | | |
| | | /** |
| | | * @description 平台获取优惠券列表 |
| | |
| | | coupon.setCreateUserId(mgtCouponEditDto.getUserId()); |
| | | coupon.setCouponFrom(1); |
| | | coupon.setAuditStatus(2); |
| | | coupon.setLimitNumber(0); |
| | | coupon.setSendFlag(0); |
| | | //创建优惠券统计 |
| | | couponTotal = new CouponTotal(); |
| | | couponTotal.setCouponId(couponId); |
| | |
| | | .collect(Collectors.toList()); |
| | | couponRelUserService.saveBatch(couponRelUserList); |
| | | } |
| | | this.saveOrUpdate(coupon); |
| | | //处理优惠券发放 |
| | | if(coupon.getSendTimeType()==1){ |
| | | sendCoupon(coupon,relUserIdList); |
| | | coupon.setSendFlag(1); |
| | | this.saveOrUpdate(coupon); |
| | | }else{ |
| | | this.saveOrUpdate(coupon); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | private void sendCouponNow(Coupon coupon, List<Long> relUserIdList){ |
| | | //1.手动领取2.全部用户3.会员用户4非会员用户5自定义 |
| | | Date nowTime = new Date(); |
| | | Integer sendType = coupon.getSendType(); |
| | | List<Long> userIdList = new ArrayList<>(); |
| | | Long shopId = null; |
| | | if(coupon.getCouponFrom()==2 && sendType==3){ |
| | | shopId = coupon.getShopId(); |
| | | } |
| | | if(sendType >= 2 && sendType <= 4){ |
| | | userIdList = memberService.listIdBySendType(sendType,shopId); |
| | | }else if(sendType == 5){ |
| | | //关联类型1.用户管理筛选2.活动管理筛选 |
| | | if(coupon.getRelationType()==2){ |
| | | Integer relationActivityType = coupon.getRelationActivityType(); |
| | | //关联活动类型1秒杀活动 |
| | | switch(relationActivityType){ |
| | | case 1: |
| | | userIdList = remoteActivityService.listUserIdByActivityId(coupon.getRelationActivityId()).getData(); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | }else if(coupon.getRelationType()==1){ |
| | | userIdList = relUserIdList; |
| | | } |
| | | } |
| | | 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); |
| | | memberCoupon.setShopId(coupon.getShopId()); |
| | | memberCoupon.setCouponType(coupon.getCouponType()); |
| | | memberCoupon.setCouponStatus(coupon.getCouponStatus()); |
| | | memberCoupon.setCouponName(coupon.getCouponName()); |
| | | memberCoupon.setSendType(coupon.getSendType()); |
| | | memberCoupon.setSendTimeType(coupon.getSendTimeType()); |
| | | memberCoupon.setSendTime(coupon.getSendTime()); |
| | | memberCoupon.setMoneyThreshold(coupon.getMoneyThreshold()); |
| | | memberCoupon.setDiscountMoney(coupon.getDiscountMoney()); |
| | | memberCoupon.setDiscountPercent(coupon.getDiscountPercent()); |
| | | memberCoupon.setUseScope(coupon.getUseScope()); |
| | | memberCoupon.setValidTimeType(coupon.getValidTimeType()); |
| | | memberCoupon.setValidStartTime(coupon.getValidStartTime()); |
| | | memberCoupon.setValidEndTime(coupon.getValidEndTime()); |
| | | memberCoupon.setValidDay(coupon.getValidDay()); |
| | | memberCoupon.setReceiveTime(nowTime); |
| | | memberCoupon.setRelGoodsIds(coupon.getRelGoodsIds()); |
| | | //有效期处理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); |
| | | } |
| | | memberCouponService.saveBatch(memberCouponList); |
| | | //处理优惠券统计 |
| | | if(sendTotal>0){ |
| | | couponMapper.updateCouponTotal(coupon.getCouponId(),sendTotal,sendPerson); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @description 平台删除优惠券 |
| | | * @author jqs |
| | |
| | | */ |
| | | @Override |
| | | public void timingSendCoupon(){ |
| | | //获取1小时内需要发送的优惠券 |
| | | LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(Coupon::getDelFlag,0); |
| | | queryWrapper.eq(Coupon::getCouponStatus,1); |
| | | queryWrapper.eq(Coupon::getSendTimeType,2); |
| | | queryWrapper.eq(Coupon::getAuditStatus,2); |
| | | queryWrapper.eq(Coupon::getSendTime,DateUtils.getNowDate()); |
| | | queryWrapper.eq(Coupon::getSendFlag,0); |
| | | Date checkTime = DateUtils.addMinutes(new Date(),61); |
| | | queryWrapper.le(Coupon::getSendTime, checkTime); |
| | | List<Coupon> couponList = couponMapper.selectList(queryWrapper); |
| | | if(couponList!=null&&!couponList.isEmpty()){ |
| | | List<CouponRelUser> couponRelUserList; |
| | | List<Long> userIdList = new ArrayList<>(); |
| | | for(Coupon coupon : couponList){ |
| | | if(coupon.getSendType()==5&&coupon.getRelationType()==1){ |
| | | couponRelUserList = couponRelUserService.list(new LambdaQueryWrapper<CouponRelUser>().eq(CouponRelUser::getDelFlag,0).eq(CouponRelUser::getCouponId,coupon.getCouponId())); |
| | | if(couponRelUserList!=null&&!couponRelUserList.isEmpty()){ |
| | | userIdList = couponRelUserList.stream() |
| | | .map(CouponRelUser::getUserId) |
| | | .collect(Collectors.toList()); |
| | | }else{ |
| | | userIdList = new ArrayList<>(); |
| | | } |
| | | for(Coupon coupon : couponList) { |
| | | String couponId = coupon.getCouponId(); |
| | | Date sendTime = coupon.getSendTime(); |
| | | long currentTimeMillis = System.currentTimeMillis(); |
| | | long timeDifference = currentTimeMillis - sendTime.getTime(); |
| | | DelayTask delayTask = remoteConfigService.getDelayTask(DelayTaskEnum.COUPON_SEND_DELAY_TASK.getCode()+"-"+couponId).getData(); |
| | | if(delayTask==null){ |
| | | //创建延时任务控制优惠券定时发放 |
| | | redisService.setCacheObject(DelayTaskEnum.COUPON_SEND_DELAY_TASK.getCode()+"-"+couponId,sendTime,timeDifference, TimeUnit.MILLISECONDS); |
| | | delayTask = new DelayTask(); |
| | | delayTask.setDelFlag(0); |
| | | delayTask.setCreateTime(new Date()); |
| | | delayTask.setEndTime(sendTime); |
| | | delayTask.setRedisKey(DelayTaskEnum.COUPON_SEND_DELAY_TASK.getCode()+"-"+couponId); |
| | | remoteConfigService.addDelayTask(delayTask); |
| | | } |
| | | //发送优惠券 |
| | | sendCoupon(coupon, userIdList); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @description 定时发送优惠券 |
| | | * @author jqs |
| | | * @date 2023/7/12 12:01 |
| | | * @param couponId |
| | | * @return void |
| | | */ |
| | | @Override |
| | | public void autoTimeSendCoupon(String couponId){ |
| | | List<CouponRelUser> couponRelUserList; |
| | | List<Long> userIdList = new ArrayList<>(); |
| | | Coupon coupon = this.getById(couponId); |
| | | if(coupon.getSendType()==5&&coupon.getRelationType()==1){ |
| | | couponRelUserList = couponRelUserService.list(new LambdaQueryWrapper<CouponRelUser>().eq(CouponRelUser::getDelFlag,0).eq(CouponRelUser::getCouponId,coupon.getCouponId())); |
| | | if(couponRelUserList!=null&&!couponRelUserList.isEmpty()){ |
| | | userIdList = couponRelUserList.stream() |
| | | .map(CouponRelUser::getUserId) |
| | | .collect(Collectors.toList()); |
| | | }else{ |
| | | userIdList = new ArrayList<>(); |
| | | } |
| | | } |
| | | //发送优惠券 |
| | | sendCouponNow(coupon, userIdList); |
| | | } |
| | | |
| | | /** |
| | |
| | | coupon.setAuditReason(mgtCouponAuditDto.getAuditReason()); |
| | | coupon.setUpdateTime(new Date()); |
| | | coupon.setUpdateUserId(mgtCouponAuditDto.getUserId()); |
| | | this.saveOrUpdate(coupon); |
| | | //审核通过后判断是否立即发放 |
| | | if(coupon.getCouponStatus()==2&&coupon.getSendTimeType()==1){ |
| | | List<CouponRelUser> couponRelUserList = couponRelUserService.list(new LambdaQueryWrapper<CouponRelUser>().eq(CouponRelUser::getDelFlag,0).eq(CouponRelUser::getCouponId,coupon.getCouponId())); |
| | |
| | | } |
| | | //发放优惠券 |
| | | sendCoupon(coupon, userIdList); |
| | | coupon.setSendFlag(1); |
| | | } |
| | | this.saveOrUpdate(coupon); |
| | | } |
| | | |
| | | /** |
| | |
| | | coupon.setCreateTime(new Date()); |
| | | coupon.setCreateUserId(merCouponEditDto.getUserId()); |
| | | coupon.setCouponFrom(2); |
| | | coupon.setSendFlag(0); |
| | | coupon.setLimitNumber(merCouponEditDto.getLimitNumber()); |
| | | //创建优惠券统计 |
| | | couponTotal = new CouponTotal(); |
| | | couponTotal.setCouponId(couponId); |
| | |
| | | if(!getAbleCouponPageVoList.isEmpty()){ |
| | | String relGoodsIds; |
| | | List<Goods> goodsList; |
| | | List<String> goodsLimitList; |
| | | List<String> goodsLimitList = null; |
| | | Shop shop = null; |
| | | for(AppGetAbleCouponPageVo appGetAbleCouponPageVo : getAbleCouponPageVoList){ |
| | | relGoodsIds = appGetAbleCouponPageVo.getRelGoodsIds(); |
| | | goodsList = remoteGoodsService.listGoodsByGoodsId(relGoodsIds).getData(); |
| | | if(!goodsList.isEmpty()){ |
| | | goodsLimitList = goodsList.stream().map(goods -> { |
| | | String goodsName = goods.getGoodsName(); |
| | | goodsName = "限制" + goodsName + "适用"; |
| | | return goodsName; |
| | | }).collect(Collectors.toList()); |
| | | appGetAbleCouponPageVo.setGoodsLimitList(goodsLimitList); |
| | | if(StringUtils.isNotBlank(relGoodsIds)){ |
| | | goodsList = remoteGoodsService.listGoodsByGoodsId(relGoodsIds).getData(); |
| | | if(!goodsList.isEmpty()){ |
| | | goodsLimitList = goodsList.stream().map(goods -> { |
| | | String goodsName = goods.getGoodsName(); |
| | | goodsName = "限制" + goodsName + "适用"; |
| | | return goodsName; |
| | | }).collect(Collectors.toList()); |
| | | } |
| | | }else{ |
| | | goodsLimitList = new ArrayList<>(); |
| | | if(appGetAbleCouponPageVo.getShopId()!=null){ |
| | | if(shop!=null&&shop.getShopId().equals(appGetAbleCouponPageVo.getShopId())){ |
| | | }else{ |
| | | shop = remoteShopService.getShop(appGetAbleCouponPageVo.getShopId()).getData(); |
| | | } |
| | | goodsLimitList.add(shop.getShopName()+"通用"); |
| | | }else{ |
| | | goodsLimitList.add("全场通用"); |
| | | } |
| | | } |
| | | appGetAbleCouponPageVo.setGoodsLimitList(goodsLimitList); |
| | | } |
| | | |
| | | } |
| | | return getAbleCouponPageVoList; |
| | | } |