ScheduledTask.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package com.futu.course.cupons;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.futu.course.course.domain.Course;
  5. import com.futu.course.course.mapper.CourseMapper;
  6. import com.futu.course.user.domain.UserCoupon;
  7. import com.futu.course.user.mapper.UserCouponMapper;
  8. import org.springframework.scheduling.annotation.EnableScheduling;
  9. import org.springframework.scheduling.annotation.Scheduled;
  10. import org.springframework.stereotype.Component;
  11. import javax.annotation.Resource;
  12. import java.util.Date;
  13. import java.util.List;
  14. @Component
  15. @EnableScheduling
  16. public class ScheduledTask {
  17. @Resource
  18. private CourseMapper courseMapper;
  19. @Resource
  20. private UserCouponMapper userCouponMapper;
  21. // 定时任务检查优惠券是否过期
  22. @Scheduled(cron = "* * * * * * *")
  23. public void setCourse(){
  24. List<Course> courses = courseMapper.selectList(new QueryWrapper<>(null));
  25. for (Course c:courses){
  26. if (c.getEndTime().before(new Date())){
  27. setUserCoupon(c.getId());
  28. }
  29. }
  30. }
  31. public void setUserCoupon(Long id){
  32. userCouponMapper.delete(new QueryWrapper<UserCoupon>().eq("coupon_id",id));
  33. courseMapper.deleteById(id);
  34. }
  35. }