tjx
2025-11-25 ac891b014f5110e01fd19bc537f208ae9e7c689c
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
package com.gs.xky.task;
 
 
import com.gs.xky.service.VwCjScSjTsBbService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.atomic.AtomicBoolean;
 
@Component
@RequiredArgsConstructor
public class ScheduledTasks {
    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
 
    private final VwCjScSjTsBbService vwCjScSjTsBbService;
 
    private final AtomicBoolean exportTaskRunning = new AtomicBoolean(false);
 
//    private final XkyService xkyService;
//    private final ApiService apiService;
//    private final MesStaffService staffService;
//    private final DeliveryNoticeService deliveryNoticeService;
//    private final MesInvItemArnService invItemArnService;
//    private final Executor taskExecutor; // 注入通用线程池
 
    // 用于标记各任务是否正在执行
//    private final AtomicBoolean isDeviceDataRunning = new AtomicBoolean(false);
//    private final AtomicBoolean isCompensateRunning = new AtomicBoolean(false);
//    private final AtomicBoolean isDingTalkRunning = new AtomicBoolean(false);
 
    /**
     * 每天上午9点执行
     * 导出生产数据并发送钉钉消息
     */
    @Async("taskExecutor")
    @Scheduled(cron = "0 0 8,16 * * ?")
    public void exportAndSendProductionDataTask() {
        if (!exportTaskRunning.compareAndSet(false, true)) {
            log.warn("生产数据导出任务正在执行中,跳过本次执行");
            return;
        }
 
        try {
            log.info("开始执行生产数据导出并发送钉钉任务");
            boolean result = vwCjScSjTsBbService.exportAndSendToDingtalk();
 
            if (result) {
                log.info("生产数据导出并发送钉钉任务执行成功");
            } else {
                log.error("生产数据导出并发送钉钉任务执行失败");
            }
        } catch (Exception e) {
            log.error("生产数据导出并发送钉钉任务执行异常", e);
        } finally {
            exportTaskRunning.set(false);
        }
    }
 
//    /**
//     * 每五分钟执行一次
//     * 获取协客云最近的一条记录
//     */
//    @Scheduled(cron = "0 0/5 * * * ?")
//    public void getDeviceRealTimeData() {
//        // 如果任务已经在运行,则跳过本次执行
//        if (!isDeviceDataRunning.compareAndSet(false, true)) {
//            log.info("【getDeviceRealTimeData】上一次任务还在执行中,跳过本次执行");
//            return;
//        }
//
//        log.info("【getDeviceRealTimeData】开始获取设备实时数据");
//
//        // 使用异步执行,避免阻塞调度线程
//        CompletableFuture.runAsync(() -> {
//            try {
//                xkyService.GetSaveDetail();
//                log.info("【getDeviceRealTimeData】获取设备实时数据成功");
//            } catch (IOException e) {
//                log.error("【getDeviceRealTimeData】获取设备实时数据异常: {}", e.getMessage(), e);
//            } finally {
//                isDeviceDataRunning.set(false);
//            }
//        }, taskExecutor);
//    }
//
//    /**
//     * 定时执行补偿逻辑
//     */
//    @Scheduled(cron = "10 3,8,13,18,23,28,33,38,43,48,53,58 * * * ?")
//    public void compensateMethod() {
//        // 如果任务已经在运行,则跳过本次执行
//        if (!isCompensateRunning.compareAndSet(false, true)) {
//            log.info("【compensateMethod】上一次任务还在执行中,跳过本次执行");
//            return;
//        }
//
//        log.info("【compensateMethod】开始执行补偿逻辑");
//
//        // 使用异步执行,避免阻塞调度线程
//        CompletableFuture.runAsync(() -> {
//            try {
//                // 补偿逻辑
//                List<MesInvItemArn> itemArnMinus = invItemArnService.getItemArnMinus();
//                deliveryNoticeService.processMesInvItemArnStatusAsync(itemArnMinus);
//                log.info("【compensateMethod】补偿逻辑执行成功");
//            } catch (Exception e) {
//                log.error("【compensateMethod】补偿逻辑执行异常: {}", e.getMessage(), e);
//            } finally {
//                isCompensateRunning.set(false);
//            }
//        }, taskExecutor);
//    }
//
//    /**
//     * 定时获取钉钉数据
//     */
//    @Scheduled(cron = "0 0/53 * * * ?")
//    public void getDinTalkData() {
//        // 如果任务已经在运行,则跳过本次执行
//        if (!isDingTalkRunning.compareAndSet(false, true)) {
//            log.info("【getDinTalkData】上一次任务还在执行中,跳过本次执行");
//            return;
//        }
//
//        log.info("【getDinTalkData】开始获取钉钉数据");
//
//        // 使用异步执行,避免阻塞调度线程
//        CompletableFuture.runAsync(() -> {
//            try {
//                DingTalkParam dingTalkParam = new DingTalkParam(1);
//                DingTalkResponse<EmployeeInfo> employeeInfoDingTalkResponse =
//                        apiService.sendListRequest(dingTalkParam, EmployeeInfo.class,
//                                "http://192.168.1.64/eHR/eHRExternalService/Service.ashx");
//
//                List<EmployeeInfo> collect = employeeInfoDingTalkResponse.getData().stream()
//                        .filter(s -> "造梦者(浙江)科技有限公司".equals(s.getCUnitName()))
//                        .collect(Collectors.toList());
//
//                log.info("【getDinTalkData】获取到{}条员工数据", collect.size());
//
//                List<List<EmployeeInfo>> partition = ListUtil.partition(collect, 100);
//                partition.forEach(staffService::UpdateStaff);
//
//                log.info("【getDinTalkData】钉钉数据处理完成");
//            } catch (IOException e) {
//                log.error("【getDinTalkData】获取钉钉数据异常: {}", e.getMessage(), e);
//            } finally {
//                isDingTalkRunning.set(false);
//            }
//        }, taskExecutor);
//    }
 
}