Pu Zhibing
11 小时以前 98b09eae533537dc9a5277aa6374bd7d35cfe5c4
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
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 implements ApplicationListener<WebServerInitializedEvent> {
    
    @Resource
    private TAppUserVipDetailService tAppUserVipDetailService;
    
    @Resource
    private TAppUserService appUserService;
    
    private Integer port = null;
    
    
    /**
     * 每隔一分钟去处理的定时任务
     */
    @Scheduled(fixedRate = 1000 * 60)
    public void sendVipCoupon() {
        if(null != port && port == 5200){
            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() {
        if(null != port && port == 5200){
            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);
                }
            }
        }
        
        
    }
    
    
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        port = event.getWebServer().getPort();
        System.out.println("端口号:" + port);
    }
}