cy
2023-02-09 60890359b3052847fb9e61d4675147aba20fec73
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
package com.integrated.zyyt.controller;
 
import com.integrated.zyyt.service.ZyytService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
 
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.concurrent.Callable;
 
/**
 * @ClassName ZyytController
 * @Author cy
 * @Date 2022/11/15
 * @Description
 * @Version 1.0
 **/
@ControllerAdvice
@Slf4j
@RequestMapping("/api/zyyt")
public class ZyytController {
//    private ExecutorService threadPool = Executors.newFixedThreadPool(6);
 
    @Resource
    private ZyytService zyytService;
 
    @GetMapping("/addStationInfo")
    /**
     * @params date 更新的业务日期
     * @params isForece 1强制更新 2不强制
     *
     */
    public String addStationInfo() {
        zyytService.stationInfoTasks();
        return "执行完毕";
    }
 
    @GetMapping("/shkdrbTasks")
    /**
     * @params date 更新的业务日期 2022-11-16
     * @params isForece 1强制更新 2不强制
     *
     */
    public String shkdrbTasks(String date) {
        zyytService.shkdrbTasks(LocalDate.parse(date));
        return "执行完毕";
    }
 
 
    @GetMapping("/shkdrbTasksYear")
    public String shkdrbTasksYear(int year) throws Exception {
        LocalDate startOfYear = LocalDate.of(year, 1, 1);
        LocalDate endOfYear = LocalDate.of(year, 12, 31);
        LocalDate now = LocalDate.now();
        for (LocalDate tmp = startOfYear; endOfYear.compareTo(tmp) > 0 && now.compareTo(tmp) > 0; tmp = tmp.plus(1, ChronoUnit.DAYS)) {
//            Instant instant = tmp.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();
//            threadPool.submit(new ZyytTask(Date.from(instant), zyytService, "shkdrbTasks"));
            zyytService.shkdrbTasks(tmp);
        }
        return "后台执行中 ,请不要重复请求";
 
    }
 
 
    @GetMapping("/djtjbTasks")
    /**
     * @params date 更新的业务日期
     * @params isForece 1强制更新 2不强制
     *
     */
    public String djtjbTasks(String date) {
        zyytService.djtjbTasks(LocalDate.parse(date));
        return "执行完毕";
    }
 
    @GetMapping("/djtjbTasksYear")
    /**
     * @params date 更新的业务日期
     * @params isForece 1强制更新 2不强制
     *
     */
    public String djtjbTasksYear(int year) throws Exception {
        LocalDate startOfYear = LocalDate.of(year, 1, 1);
        LocalDate endOfYear = LocalDate.of(year, 12, 31);
        LocalDate now = LocalDate.now();
        for (LocalDate tmp = startOfYear; endOfYear.compareTo(tmp) > 0 && now.compareTo(tmp) > 0; tmp = tmp.plus(1, ChronoUnit.DAYS)) {
//            Instant instant = tmp.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();
//            threadPool.submit(new ZyytTask(Date.from(instant), zyytService, "djtjbTasks"));
            zyytService.djtjbTasks(tmp);
        }
        return "123";
    }
 
}
 
@Slf4j
class ZyytTask implements Callable {
    private Date date;
    private ZyytService zyytService;
    private String methodName;
 
    public ZyytTask(Date date, ZyytService zyytService, String methodName) {
        this.date = date;
        this.zyytService = zyytService;
        this.methodName = methodName;
    }
 
    @Override
    public Object call() throws Exception {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
 
        log.info("开始:执行方法{},业务日期为{}", methodName, localDate);
        Method zyytServiceMethod = zyytService.getClass().getMethod(methodName, Date.class);
        zyytServiceMethod.invoke(zyytService, date);
        log.info("结束:执行方法{},业务日期为{}", methodName, localDate);
        return null;
    }
}