Pu Zhibing
2025-03-31 93d3fdf628fcd574d948e7b7c0eb5f341a68d3a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.ruoyi.account.task;
 
 
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.api.model.TAppUserVipDetail;
import com.ruoyi.account.service.TAppUserService;
import com.ruoyi.account.service.TAppUserVipDetailService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
 
 
/**
 * 定时任务工具类
 */
@Component
public class TaskUtil {
    
    @Resource
    private TAppUserVipDetailService tAppUserVipDetailService;
    
    @Resource
    private TAppUserService appUserService;
    
    
    /**
     * 每隔一分钟去处理的定时任务
     */
    @Scheduled(fixedRate = 1000 * 60)
    public void sendVipCoupon() {
        try {
            //会员优惠次数使用完后重新续次数
            LocalDateTime currentDate = LocalDateTime.now();
            List<TAppUserVipDetail> recentDetails = tAppUserVipDetailService.lambdaQuery()
                    .le(TAppUserVipDetail::getStartTime, currentDate)
                    .ge(TAppUserVipDetail::getEndTime, currentDate)
                    .ne(TAppUserVipDetail::getVipType, 1)
                    .le(TAppUserVipDetail::getMonthEndTime, currentDate)
                    .orderByDesc(TAppUserVipDetail::getStartTime).list();
            for (TAppUserVipDetail recentDetail : recentDetails) {
                tAppUserVipDetailService.giveVipCoupun(recentDetail);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
    }
    
    @Scheduled(fixedRate = 1000 * 60)
    public void changeVipId() {
        LocalDateTime currentDate = LocalDateTime.now();
        List<TAppUserVipDetail> recentDetails = tAppUserVipDetailService.lambdaQuery()
                .le(TAppUserVipDetail::getStartTime, currentDate)
                .ge(TAppUserVipDetail::getEndTime, currentDate)
                .orderByDesc(TAppUserVipDetail::getStartTime).list();
        for (TAppUserVipDetail recentDetail : recentDetails) {
            TAppUser byId = appUserService.getById(recentDetail.getAppUserId());
            if (byId != null) {
                byId.setVipId(recentDetail.getVipId());
                appUserService.updateById(byId);
            }
        }
        
    }
    
    
}