无关风月
2024-11-09 111652d23733d04e379c2454c8b39171596a6b50
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
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.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 {
            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) {
        LocalDate localDate = LocalDateTime.now().toLocalDate();
        System.err.println(localDate);
    }
    // 每天早上7点执行的定时任务
    @Scheduled(cron = "0 0 7 * * ?")
    public void taskSivenDay() {
        try {
            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(appUser.getVipExpireTime(), now);
                    if (between<=7){
                        NoticeRecord noticeRecord = new NoticeRecord();
                        noticeRecord.setAppUserId(appUser.getId());
                        noticeRecord.setReadStatus(1);
                        noticeRecord.setNoticeType(1);
                        noticeRecord.setTitle("【会员临期通知】");
                        noticeRecord.setContent("尊敬的心泉疗愈会员你好,你的会员即将在"
                        +appUser.getVipExpireTime().toLocalDate()+"到期,到期后将不再享受会员权益,请及时续费");
                        noticeRecordService.save(noticeRecord);
                    }
 
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}