Crunchy
2025-06-14 b2f31607cbe26d721cd7514b619162b3e355b1aa
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
package com.wms_admin.timer;
 
import com.wms_admin.server.service.ProductService;
import com.wms_admin.utils.RedisUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
 
import static com.wms_admin.utils.MyUtils.isLastDayOfMonth;
 
@Component
public class Timer {
    @Resource
    private ProductService service;
 
    @Scheduled(cron="0 0 23 * * ?")   //每天23点执行一次
    public void execute() {
        List xList = new ArrayList();
        List yList = new ArrayList();
        SimpleDateFormat formatter = new SimpleDateFormat("M.d");
        String time0 = formatter.format(new Date());
        Integer dayCountData = service.TimerCountWeekDayData();
        Map sevenDaysCount = null;
        try {
            sevenDaysCount = (Map) RedisUtil.get("SevenDaysCount");
        }finally {
            if (sevenDaysCount != null) {
                xList = (List) sevenDaysCount.get("xList");
                yList = (List) sevenDaysCount.get("yList");
                if (xList.size() == 7 && yList.size() == 7) {
                    xList.remove(0);
                    yList.remove(0);
                }
            }
            xList.add(time0);
            yList.add(dayCountData);
            HashMap<String, Object> map = new HashMap<>();
            map.put("xList", xList);
            map.put("yList", yList);
            RedisUtil.set("SevenDaysCount", map);
        }
    }
 
 
    //每个月第一天统计剩余库存
    @Scheduled(cron="0 0 1 1 * ?")   //每月初凌晨1点执行一次
    public void monthlyExecute() {
        List pieDate = new ArrayList();
        List pieNum = new ArrayList();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String datetime = simpleDateFormat.format(new Date());
        int beginningMonthNum = service.count();
        Map beginningMonth = null;
        try {
            beginningMonth = (Map) RedisUtil.get("beginningMonth");
        }finally {
            if (beginningMonth != null){
                pieDate = (List) beginningMonth.get("datetime");
                pieNum = (List) beginningMonth.get("num");
                if (pieDate.size() == 4 && pieNum.size() == 4){
                    pieDate.remove(0);
                    pieNum.remove(0);
                }
            }
            pieDate.add(datetime);
            pieNum.add(beginningMonthNum);
            Map<String, Object> map = new HashMap<>();
            map.put("datetime", pieDate);
            map.put("num", pieNum);
            RedisUtil.set("beginningMonth", map);
        }
    }
 
    //每个月最后一天统计剩余库存
    @Scheduled(cron = "0 0 23 28-31 * ?") //每月最后一天的28~31天都执行此代码
    public void finallyExecute() {
        //将范围缩小到28~31后,每次调用此接口来判断当天是否是当月的最后一天
        boolean flag = isLastDayOfMonth(new Date());
        if (flag){
            List pieDate = new ArrayList();
            List pieNum = new ArrayList();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String datetime = simpleDateFormat.format(new Date());
            int endOfMonthNum = service.count();
            Map endOfMonth = null;
            try {
                endOfMonth = (Map) RedisUtil.get("endOfMonth");
                System.out.println(endOfMonth);
            }finally {
                if (endOfMonth != null){
                    pieDate = (List) endOfMonth.get("datetime");
                    pieNum = (List) endOfMonth.get("num");
                    if (pieDate.size() == 4 && pieNum.size() == 4){
                        pieDate.remove(0);
                        pieNum.remove(0);
                    }
                }
                pieDate.add(datetime);
                pieNum.add(endOfMonthNum);
                Map<String, Object> map = new HashMap<>();
                map.put("datetime", pieDate);
                map.put("num", pieNum);
                RedisUtil.set("endOfMonth", map);
            }
        }
    }
}