Commit 2766dd672a098350abd0bee7d291691555222678
1 parent
11ffa45b
优惠券:过期定时任务
Showing
5 changed files
with
96 additions
and
0 deletions
yudao-module-mall/yudao-module-promotion-biz/pom.xml
| ... | ... | @@ -47,6 +47,10 @@ |
| 47 | 47 | </dependency> |
| 48 | 48 | <dependency> |
| 49 | 49 | <groupId>cn.iocoder.boot</groupId> |
| 50 | + <artifactId>yudao-spring-boot-starter-biz-tenant</artifactId> | |
| 51 | + </dependency> | |
| 52 | + <dependency> | |
| 53 | + <groupId>cn.iocoder.boot</groupId> | |
| 50 | 54 | <artifactId>yudao-spring-boot-starter-biz-weixin</artifactId> |
| 51 | 55 | </dependency> |
| 52 | 56 | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/dal/mysql/coupon/CouponMapper.java
| ... | ... | @@ -13,6 +13,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| 13 | 13 | import com.github.yulichang.toolkit.MPJWrappers; |
| 14 | 14 | import org.apache.ibatis.annotations.Mapper; |
| 15 | 15 | |
| 16 | +import java.time.LocalDateTime; | |
| 16 | 17 | import java.util.Collection; |
| 17 | 18 | import java.util.List; |
| 18 | 19 | import java.util.function.Function; |
| ... | ... | @@ -94,4 +95,11 @@ public interface CouponMapper extends BaseMapperX<CouponDO> { |
| 94 | 95 | .or(ww -> ww.eq(CouponDO::getProductScope, PromotionProductScopeEnum.CATEGORY.getScope()) |
| 95 | 96 | .apply(productScopeValuesFindInSetFunc.apply(categoryIds))))); |
| 96 | 97 | } |
| 98 | + | |
| 99 | + default List<CouponDO> selectListByStatusAndValidEndTimeLe(Integer status, LocalDateTime validEndTime) { | |
| 100 | + return selectList(new LambdaQueryWrapperX<CouponDO>() | |
| 101 | + .eq(CouponDO::getStatus, status) | |
| 102 | + .le(CouponDO::getValidEndTime, validEndTime) | |
| 103 | + ); | |
| 104 | + } | |
| 97 | 105 | } | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/job/CouponExpireJob.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.promotion.job; | |
| 2 | + | |
| 3 | +import cn.hutool.core.util.StrUtil; | |
| 4 | +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; | |
| 5 | +import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; | |
| 6 | +import cn.iocoder.yudao.module.promotion.service.coupon.CouponService; | |
| 7 | +import org.springframework.stereotype.Component; | |
| 8 | + | |
| 9 | +import javax.annotation.Resource; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * 优惠券过期 Job | |
| 13 | + * | |
| 14 | + * @author owen | |
| 15 | + */ | |
| 16 | +@Component | |
| 17 | +@TenantJob | |
| 18 | +public class CouponExpireJob implements JobHandler { | |
| 19 | + | |
| 20 | + @Resource | |
| 21 | + private CouponService couponService; | |
| 22 | + | |
| 23 | + @Override | |
| 24 | + public String execute(String param) { | |
| 25 | + int count = couponService.expireCoupon(); | |
| 26 | + return StrUtil.format("过期优惠券 {} 个", count); | |
| 27 | + } | |
| 28 | + | |
| 29 | +} | ... | ... |
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponService.java
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/coupon/CouponServiceImpl.java
| ... | ... | @@ -6,6 +6,7 @@ import cn.hutool.core.collection.ListUtil; |
| 6 | 6 | import cn.hutool.core.map.MapUtil; |
| 7 | 7 | import cn.hutool.core.util.ObjectUtil; |
| 8 | 8 | import cn.hutool.core.util.StrUtil; |
| 9 | +import cn.hutool.extra.spring.SpringUtil; | |
| 9 | 10 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 10 | 11 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 11 | 12 | import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; |
| ... | ... | @@ -21,6 +22,7 @@ import cn.iocoder.yudao.module.promotion.enums.coupon.CouponStatusEnum; |
| 21 | 22 | import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTakeTypeEnum; |
| 22 | 23 | import cn.iocoder.yudao.module.promotion.enums.coupon.CouponTemplateValidityTypeEnum; |
| 23 | 24 | import cn.iocoder.yudao.module.promotion.service.coupon.bo.CouponTakeCountBO; |
| 25 | +import lombok.extern.slf4j.Slf4j; | |
| 24 | 26 | import org.springframework.stereotype.Service; |
| 25 | 27 | import org.springframework.transaction.annotation.Transactional; |
| 26 | 28 | import org.springframework.validation.annotation.Validated; |
| ... | ... | @@ -43,6 +45,7 @@ import static java.util.Arrays.asList; |
| 43 | 45 | * |
| 44 | 46 | * @author 芋道源码 |
| 45 | 47 | */ |
| 48 | +@Slf4j | |
| 46 | 49 | @Service |
| 47 | 50 | @Validated |
| 48 | 51 | public class CouponServiceImpl implements CouponService { |
| ... | ... | @@ -193,6 +196,43 @@ public class CouponServiceImpl implements CouponService { |
| 193 | 196 | matchReqVO.getPrice(), matchReqVO.getSpuIds(), matchReqVO.getCategoryIds()); |
| 194 | 197 | } |
| 195 | 198 | |
| 199 | + @Override | |
| 200 | + public int expireCoupon() { | |
| 201 | + // 1. 查询待过期的优惠券 | |
| 202 | + List<CouponDO> list = couponMapper.selectListByStatusAndValidEndTimeLe( | |
| 203 | + CouponStatusEnum.UNUSED.getStatus(), LocalDateTime.now()); | |
| 204 | + if (CollUtil.isEmpty(list)) { | |
| 205 | + return 0; | |
| 206 | + } | |
| 207 | + | |
| 208 | + // 2. 遍历执行 | |
| 209 | + int count = 0; | |
| 210 | + for (CouponDO coupon : list) { | |
| 211 | + try { | |
| 212 | + boolean success = getSelf().expireCoupon(coupon); | |
| 213 | + if (success) { | |
| 214 | + count++; | |
| 215 | + } | |
| 216 | + } catch (Exception e) { | |
| 217 | + log.error("[expireCoupon][coupon({}) 更新为已过期失败]", coupon.getId(), e); | |
| 218 | + } | |
| 219 | + } | |
| 220 | + return count; | |
| 221 | + } | |
| 222 | + | |
| 223 | + private boolean expireCoupon(CouponDO coupon) { | |
| 224 | + // 更新记录状态 | |
| 225 | + CouponDO updateObj = new CouponDO().setStatus(CouponStatusEnum.EXPIRE.getStatus()); | |
| 226 | + int updateRows = couponMapper.updateByIdAndStatus(coupon.getId(), CouponStatusEnum.UNUSED.getStatus(), updateObj); | |
| 227 | + if (updateRows == 0) { | |
| 228 | + log.error("[expireCoupon][coupon({}) 更新为已过期失败]", coupon.getId()); | |
| 229 | + return false; | |
| 230 | + } | |
| 231 | + | |
| 232 | + log.info("[expireCoupon][coupon({}) 更新为已过期成功]", coupon.getId()); | |
| 233 | + return true; | |
| 234 | + } | |
| 235 | + | |
| 196 | 236 | /** |
| 197 | 237 | * 校验优惠券是否可以领取 |
| 198 | 238 | * |
| ... | ... | @@ -246,4 +286,12 @@ public class CouponServiceImpl implements CouponService { |
| 246 | 286 | userIds.removeIf(userId -> MapUtil.getInt(userTakeCountMap, userId, 0) >= couponTemplate.getTakeLimitCount()); |
| 247 | 287 | } |
| 248 | 288 | |
| 289 | + /** | |
| 290 | + * 获得自身的代理对象,解决 AOP 生效问题 | |
| 291 | + * | |
| 292 | + * @return 自己 | |
| 293 | + */ | |
| 294 | + private CouponServiceImpl getSelf() { | |
| 295 | + return SpringUtil.getBean(getClass()); | |
| 296 | + } | |
| 249 | 297 | } | ... | ... |