无关风月
2024-11-14 8a025d3351fe4c6087c0dbf430624f4349e33d69
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.xinquan.user.utils;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinquan.system.api.domain.AppUser;
import com.xinquan.system.api.domain.AppUserEnergyRecord;
import com.xinquan.system.api.domain.AppUserTree;
import com.xinquan.system.api.domain.NoticeRecord;
import com.xinquan.user.service.AppUserEnergyRecordService;
import com.xinquan.user.service.AppUserService;
import com.xinquan.user.service.AppUserTreeService;
import com.xinquan.user.service.NoticeRecordService;
import org.springframework.scheduling.annotation.EnableScheduling;
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.time.temporal.ChronoUnit;
import java.util.List;
 
 
/**
 * 定时任务工具类
 */
@Component
public class TaskUtil {
    
    @Resource
    private AppUserService appUserService;
    @Resource
    private AppUserEnergyRecordService appUserEnergyRecordService;
    @Resource
    private AppUserTreeService appUserTreeService;
    @Resource
    private NoticeRecordService noticeRecordService;
 
    /**
     * 每天凌晨12点执行的定时任务
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void taskLastDay() {
        try {
            System.err.println("每天晚上凌晨执行的任务");
            LocalDateTime now = LocalDateTime.now();
            List<AppUserTree> list1 = appUserTreeService.lambdaQuery().eq(AppUserTree::getSowAgain, 2)
                    .list();
            for (AppUserTree appUserTree : list1) {
                LocalDateTime time = appUserTree.getTime();
                // 计算两个now和time的天数差
                long between = ChronoUnit.DAYS.between(time, now);
                if (between>7){
                    double v = appUserTree.getTotal() * 0.02;
                    if ((appUserTree.getTotal() - (int) v)>0){
                        appUserTree.setTotal(appUserTree.getTotal() - (int) v);
                    }else{
                        appUserTree.setTotal(0);
                    }
                    appUserTree.setStatus(1);
                }else if (between>=1){
                    // 计算两个now和time的小时差
                    long betweenHours = ChronoUnit.HOURS.between(time, now);
                    if (betweenHours>=24){
                        double v = appUserTree.getTotal() * 0.01;
                        if ((appUserTree.getTotal() - (int) v)>0){
                            appUserTree.setTotal(appUserTree.getTotal() - (int) v);
                        }else{
                            appUserTree.setTotal(0);
                        }
                    }
                }
                appUserTreeService.updateById(appUserTree);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now().minusDays(7);
        long between = ChronoUnit.DAYS.between(LocalDateTime.now(),localDateTime);
        System.err.println(between);
    }
 
    @Scheduled(cron = "0 0 7 * * ?")
    public void taskSivenDay() {
        try {
            System.err.println("每天晚上早上7点执行的任务");
            LocalDateTime now = LocalDateTime.now();
            List<AppUser> list = appUserService.lambdaQuery().ne(AppUser::getUserStatus, 3).list();
            for (AppUser appUser : list) {
                if (appUser.getVipExpireTime()!=null && appUser.getVipExpireTime().isAfter(now)){
                    // 计算两个now和time的天数差
                    long between = ChronoUnit.DAYS.between(now,appUser.getVipExpireTime());
                    if (between<=7 &&between>=0){
                        NoticeRecord noticeRecord = new NoticeRecord();
                        noticeRecord.setAppUserId(appUser.getId());
                        noticeRecord.setReadStatus(1);
                        noticeRecord.setNoticeType(1);
                        noticeRecord.setTitle("【会员临期通知】");
                        noticeRecord.setContent("尊敬的泉疗愈会员你好,你的会员即将在"
                        +appUser.getVipExpireTime().toLocalDate()+"到期,到期后将不再享受会员权益,请及时续费");
                        noticeRecordService.save(noticeRecord);
                    }
                }
                AppUserTree one = appUserTreeService.lambdaQuery().eq(AppUserTree::getAppUserId, appUser.getId())
                        .eq(AppUserTree::getSowAgain, 2).one();
                if(one!=null&&(one.getTaskOne()==2||one.getTaskTwo()==2)){
                    NoticeRecord noticeRecord = new NoticeRecord();
                    noticeRecord.setAppUserId(appUser.getId());
                    noticeRecord.setReadStatus(1);
                    noticeRecord.setNoticeType(1);
                    noticeRecord.setTitle("【冥想通知】");
                    noticeRecord.setContent("你今天的冥想任务还未完成,完成冥想后可获得能量值奖励,快去冥想吧!");
                    noticeRecordService.save(noticeRecord);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 每天晚上9点执行的定时任务
    @Scheduled(cron = "0 0 21 * * ?")
    public void taskNineDay() {
        try {
            System.err.println("执行每天晚上9点的定时任务");
            LocalDateTime now = LocalDateTime.now();
            List<AppUser> list = appUserService.lambdaQuery().ne(AppUser::getUserStatus, 3).list();
            for (AppUser appUser : list) {
                AppUserTree one = appUserTreeService.lambdaQuery().eq(AppUserTree::getAppUserId, appUser.getId())
                        .eq(AppUserTree::getSowAgain, 2).one();
                if(one!=null && (one.getTaskOne()==2||one.getTaskTwo()==2)){
                    NoticeRecord noticeRecord = new NoticeRecord();
                    noticeRecord.setAppUserId(appUser.getId());
                    noticeRecord.setReadStatus(1);
                    noticeRecord.setNoticeType(1);
                    noticeRecord.setTitle("【冥想通知】");
                    noticeRecord.setContent("你今天的冥想任务还未完成,完成冥想后可获得能量值奖励,快去冥想吧!");
                    noticeRecordService.save(noticeRecord);
                }
                if (appUser.getSignTime()==null){
                    NoticeRecord noticeRecord = new NoticeRecord();
                    noticeRecord.setAppUserId(appUser.getId());
                    noticeRecord.setReadStatus(1);
                    noticeRecord.setNoticeType(1);
                    noticeRecord.setTitle("【签到通知】");
                    noticeRecord.setContent("你今天还未签到,及时签到可获得能量值奖励,快去签到吧!");
                    noticeRecordService.save(noticeRecord);
                }else if (!appUser.getSignTime().toLocalDate().equals(LocalDateTime.now().toLocalDate())){
                    NoticeRecord noticeRecord = new NoticeRecord();
                    noticeRecord.setAppUserId(appUser.getId());
                    noticeRecord.setReadStatus(1);
                    noticeRecord.setNoticeType(1);
                    noticeRecord.setTitle("【签到通知】");
                    noticeRecord.setContent("你今天还未签到,及时签到可获得能量值奖励,快去签到吧!");
                    noticeRecordService.save(noticeRecord);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}