| | |
| | | OperationConfigDTO config = operationConfigBiz.getConfig(); |
| | | String yearsStr = config.getRegisterCouponYears(); |
| | | String maxGiftStr = config.getRegisterCouponGiftCount(); |
| | | String couponIdStr = config.getRegisterCouponId(); |
| | | if (StringUtils.isBlank(yearsStr) || StringUtils.isBlank(maxGiftStr) || StringUtils.isBlank(couponIdStr)) { |
| | | String couponIdsStr = config.getRegisterCouponId(); |
| | | if (StringUtils.isBlank(yearsStr) || StringUtils.isBlank(maxGiftStr) || StringUtils.isBlank(couponIdsStr)) { |
| | | return; |
| | | } |
| | | int configYears = Integer.parseInt(yearsStr); |
| | | int maxGiftCount = Integer.parseInt(maxGiftStr); |
| | | int couponId = Integer.parseInt(couponIdStr); |
| | | if (configYears <= 0 || maxGiftCount <= 0) { |
| | | return; |
| | | } |
| | | |
| | | // 2. 校验优惠券存在且启用 |
| | | Coupon coupon = couponMapper.selectById(couponId); |
| | | if (coupon == null || !Constants.equalsInteger(coupon.getIsdeleted(), Constants.ZERO) |
| | | || !Constants.equalsInteger(coupon.getStatus(), Constants.ZERO)) { |
| | | // 2. 解析优惠券ID列表,查询有效优惠券 |
| | | List<Integer> couponIdList = Arrays.stream(couponIdsStr.split(",")) |
| | | .map(String::trim).filter(StringUtils::isNotBlank) |
| | | .map(Integer::parseInt).collect(Collectors.toList()); |
| | | List<Coupon> validCoupons = couponMapper.selectList(new QueryWrapper<Coupon>().lambda() |
| | | .in(Coupon::getId, couponIdList) |
| | | .eq(Coupon::getIsdeleted, Constants.ZERO) |
| | | .eq(Coupon::getStatus, Constants.ZERO)); |
| | | if (CollectionUtils.isEmpty(validCoupons)) { |
| | | return; |
| | | } |
| | | |
| | |
| | | |
| | | for (Member member : members) { |
| | | int alreadyGifted = member.getRegisterCouponGiftCount() != null ? member.getRegisterCouponGiftCount() : 0; |
| | | // 已注册年数(取整) |
| | | int registeredYears = (int) ((now.getTime() - member.getCreateTime().getTime()) / msPerYear); |
| | | // 应赠送总次数 |
| | | int shouldGiftTotal = registeredYears / configYears; |
| | | // 实际还需赠送次数 |
| | | int remainGift = Math.min(shouldGiftTotal, maxGiftCount) - alreadyGifted; |
| | | if (remainGift <= 0) { |
| | | continue; |
| | | } |
| | | |
| | | // 4. 赠送优惠券 |
| | | // 4. 每次赠送所有有效优惠券 |
| | | for (int i = 0; i < remainGift; i++) { |
| | | MemberCoupon mc = new MemberCoupon(); |
| | | mc.setCouponId(couponId); |
| | | mc.setMemberId(member.getId()); |
| | | mc.setStatus(Constants.CouponStatus.waitClaim.getKey()); |
| | | // 推送后领取有效期 |
| | | Calendar validCal = Calendar.getInstance(); |
| | | validCal.add(Calendar.DAY_OF_MONTH, coupon.getPushDays() != null ? coupon.getPushDays() : 7); |
| | | mc.setValidDate(validCal.getTime()); |
| | | // 拷贝优惠券信息 |
| | | mc.setName(coupon.getName()); |
| | | mc.setInfo(coupon.getInfo()); |
| | | mc.setType(coupon.getType()); |
| | | mc.setLimitPrice(coupon.getLimitPrice()); |
| | | mc.setPrice(coupon.getPrice()); |
| | | mc.setGetMethod(coupon.getGetMethod()); |
| | | mc.setCouponType(coupon.getCouponType()); |
| | | mc.setPushDays(coupon.getPushDays()); |
| | | mc.setValidDays(coupon.getValidDays()); |
| | | mc.setIsdeleted(Constants.ZERO); |
| | | mc.setCreateDate(now); |
| | | mc.setEditDate(now); |
| | | memberCouponMapper.insert(mc); |
| | | for (Coupon coupon : validCoupons) { |
| | | MemberCoupon mc = new MemberCoupon(); |
| | | mc.setCouponId(coupon.getId()); |
| | | mc.setMemberId(member.getId()); |
| | | mc.setStatus(Constants.CouponStatus.waitClaim.getKey()); |
| | | Calendar validCal = Calendar.getInstance(); |
| | | validCal.add(Calendar.DAY_OF_MONTH, coupon.getPushDays() != null ? coupon.getPushDays() : 7); |
| | | mc.setValidDate(validCal.getTime()); |
| | | mc.setName(coupon.getName()); |
| | | mc.setInfo(coupon.getInfo()); |
| | | mc.setType(coupon.getType()); |
| | | mc.setLimitPrice(coupon.getLimitPrice()); |
| | | mc.setPrice(coupon.getPrice()); |
| | | mc.setGetMethod(coupon.getGetMethod()); |
| | | mc.setCouponType(coupon.getCouponType()); |
| | | mc.setPushDays(coupon.getPushDays()); |
| | | mc.setValidDays(coupon.getValidDays()); |
| | | mc.setIsdeleted(Constants.ZERO); |
| | | mc.setCreateDate(now); |
| | | mc.setEditDate(now); |
| | | memberCouponMapper.insert(mc); |
| | | } |
| | | } |
| | | |
| | | // 5. 更新会员已赠送次数 |