zjh
2025-02-19 a61beb3fd82d7964fc64695a291bd87697d7bc2f
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package com.ltkj.web.config.timer;
 
import cn.hutool.core.date.DateUtil;
import com.ltkj.framework.datasource.DynamicDataSourceContextHolder;
import com.ltkj.hosp.domain.TjJcycxm;
import com.ltkj.hosp.mapper.TestMapper;
import com.ltkj.hosp.service.*;
import com.ltkj.hosp.sqlDomain.*;
import com.ltkj.system.service.ISysConfigService;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 * @Company: 西安路泰科技有限公司
 * @Author: zjh
 * @Date: 2024/7/25 15:05
 */
@Slf4j
@Component
@Profile("!dev & !prod & !test")
public class DefaultScheduled {
 
 
    @Resource
    private ITjOrderService orderService;
    @Autowired
    private ISysConfigService configService;
    @Resource
    private LtkjExamJcbgdService jcbgdService;
    @Resource
    private LtkjExamJcsqdService jcsqdService;
    @Resource
    private LtkjHybgdService ltkjHybgdService;
    @Resource
    private LtkjHysqdService ltkjHysqdService;
    @Resource
    private TestMapper testMapper;
    @Resource
    private TjAsyncService asyncService;
    @Autowired
    private TjJcycxmService jcycxmService;
    @Autowired
    private AwsService awsService;
 
 
    private final ReentrantLock reentrantLock = new ReentrantLock();
 
    private ScheduledExecutorService scheduledExecutorService;
    private ScheduledFuture<?> scheduledFuture;
    private static final SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
    @PostConstruct
    private void init() {
        scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduleNextTask(0); // 初次启动立即执行
    }
 
 
    private void scheduleNextTask(long delayInMinutes) {
        if (scheduledFuture != null) {
            scheduledFuture.cancel(false);
        }
 
        Date nextExecutionTime = new Date(System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(delayInMinutes));
        log.info("下次执行任务时间 -> {}", dateTimeFormat.format(nextExecutionTime));
 
        scheduledFuture = scheduledExecutorService.schedule(this::exec, delayInMinutes, TimeUnit.MINUTES);
    }
 
    private void exec() {
        String dsqkssj = configService.selectConfigByKey("dsqkssj");
        String dsqjssj = configService.selectConfigByKey("dsqjssj");
        String yqbm = configService.selectConfigByKey("common_api_service_hospbm");
        boolean panduandangqianshijian = panduandangqianshijian(dsqkssj, dsqjssj);
        log.info("定时器自动同步检验检查结果");
        log.info("当前时间是否在 " + dsqkssj + " 点到 " + dsqjssj + " 点之间: " + panduandangqianshijian);
            String dsrwsjjg = configService.selectConfigByKey("dsrwsjjg");
            boolean lock = reentrantLock.tryLock();
            try {
                if (!panduandangqianshijian) {
                    if (lock) {
                        log.info("任务开始执行时间 -> {}", dateTimeFormat.format(new Date()));
                        long start = System.currentTimeMillis();
                        // 查询任务数据
                        log.info("开始执行同步 执行数量->{}", 0);
                        List<String> jy = orderService.getDingShiTongBuJianYanTjNum();
 
                        List<String> jc = orderService.getDingShiTongBuJianChaTjNum();
 
 
                        if (null != jy && !jy.isEmpty()) {
                            log.info("同步检验人员 执行数量->{}", jy.size());
                            for (String s : jy) {
                                extractedjianyan(s);
                            }
                        }
 
 
                        if (null != jc && !jc.isEmpty()) {
                            log.info("同步检查人员 执行数量->{}", jc.size());
                            for (String s : jc) {
                                extractedjiancha(s);
                            }
                        }
                        Map<String, Object> map = new HashMap<>();
                        testMapper.tjplUpdateDetatilByVi(map);
                        Integer object = (Integer) map.get("total");
                        log.info("执行存储过程后出参->{}", object);
                        if (null != object && object.equals(1)) {
                            if (null != jc && !jc.isEmpty()) {
                                for (String s : jc) {
                                    jcycxmService.deletedByTjh(s);
                                    List<LtkjExamJcsqd> list = jcsqdService.getLtkjJcsqdByTjh(s);
                                    if(null !=list && !list.isEmpty()){
                                        for (LtkjExamJcsqd jcsqd : list) {
                                            if(jcsqd.getJgbx().contains("。")){
                                                String[] split = jcsqd.getJgzt().split("。");
                                                for (String jg : split) {
                                                    if(StringUtil.isNotBlank(jg) && !jg.contains("未见异常")
                                                            && !jg.contains("未见明显异常") && !jg.contains("未见占位")
                                                            && !jg.contains("未见明显")&& !jg.contains("正常")&& !jg.contains("双侧椎间孔无狭窄")){
                                                        TjJcycxm jcycxm=new TjJcycxm();
                                                        jcycxm.setTjh(s);
                                                        jcycxm.setYqid(yqbm);
                                                        String[] split1 = jcsqd.getJcxmid().split(";");
                                                        jcycxm.setProId(split1[0]);
                                                        jcycxm.setProName(jcsqd.getJcxmmc());
                                                        jcycxm.setCreateTime(new Date());
                                                        jcycxm.setJcjg(jg);
                                                        jcycxmService.save(jcycxm);
                                                    }
                                                }
                                            }
                                        }
                                    }
 
                                }
                            }
 
                            long end = System.currentTimeMillis();
                            log.info("同步结束 耗时:{}秒", (end - start) / 1000);
                            // 动态获取下次任务的间隔
                            int minute = Integer.parseInt(dsrwsjjg);
                            if (minute < 5) minute = 5;
                            if (minute > 59) minute = 59;
                            scheduleNextTask(minute);
                        }else {
                            log.info("执行存储过程出参失败");
                            scheduleNextTask(Long.parseLong(dsrwsjjg));
                        }
                    }
                }else {
                    log.info("不在定时器执行时间范围之内!!!");
                    scheduleNextTask(Long.parseLong(dsrwsjjg));
                }
            } catch (Exception e) {
                scheduleNextTask(Long.parseLong(dsrwsjjg));
                log.error("定时器执行报错");
                log.error(String.valueOf(e));
//                e.printStackTrace();
            } finally {
                reentrantLock.unlock();
            }
 
    }
 
    private boolean panduandangqianshijian(String kssj, String jssj) {
        ZonedDateTime now = ZonedDateTime.now();
        LocalTime time = now.toLocalTime();
        return time.isAfter(LocalTime.of(Integer.parseInt(kssj), 0)) && time.isBefore(LocalTime.of(Integer.parseInt(jssj), 0));
    }
 
    private void extractedjianyan(String s) {
        try {
            List<Aws> awsList = testMapper.getWsxmLtkjHysqdByLisViBySLAVEWS(s);
            if(null !=awsList && !awsList.isEmpty()){
                log.info(awsList.toString());
                testMapper.delWsxmjg(s);
                log.info("删除外送项目成功");
                awsService.saveBatch(awsList);
                log.info("保存外送项目成功");
            }
            List<LtkjHysqd> hysqdList = testMapper.getCcXZxYyLtkjHysqdByLisVi(s);
            List<LtkjHysqd> wssq = testMapper.getWsxmLtkjHysqdByLisVi(s);
            if(null !=wssq && !wssq.isEmpty())hysqdList.addAll(wssq);
            if (null != hysqdList && !hysqdList.isEmpty()) {
                for (LtkjHysqd hysqd : hysqdList) {
                    LtkjHysqd jybgid = ltkjHysqdService.getLtkjHysqdByTjhAndTmh(hysqd.getTjh(), hysqd.getTmh());
                    if (null != jybgid) {
                        ltkjHysqdService.deletedLtkjHysqdByTjhAndTmh(hysqd.getTjh(), hysqd.getTmh());
                        ltkjHybgdService.deletedLtkjHybgdByTjhAndTmh(hysqd.getTmh());
                    }
                    if (ltkjHysqdService.save(hysqd)) {
                        List<LtkjHybgd> hybgdList = testMapper.getCcXZxYyLtkjHybgdByLisVi(hysqd.getTmh());
                        List<LtkjHybgd> wsbg = testMapper.getWsxmLtkjHybgdByLisVi(s);
                        if(null !=wsbg && !wsbg.isEmpty())hybgdList.addAll(wsbg);
                        ltkjHybgdService.saveBatch(hybgdList);
                    }
                }
                DynamicDataSourceContextHolder.clearDataSourceType();
            }
        } catch (Exception e) {
            log.info("同步检验数据失败");
//            throw new RuntimeException(e);
            log.error(String.valueOf(e));
        }
 
    }
 
 
    private void extractedjiancha(String s) {
 
        try {
            List<LtkjExamJcsqd> jcsqdList = testMapper.getCcXZxYyPacsLtkjExamJcsqd(s);
            if (null != jcsqdList && !jcsqdList.isEmpty()) {
                DynamicDataSourceContextHolder.clearDataSourceType();
                jcsqdService.deletedLtkjJcsqdByTjhAndTmh(s);
                jcbgdService.deletedLtkjJcbgdByTjhAndTmh(s);
                jcsqdService.saveBatch(jcsqdList);
                List<LtkjExamJcbgd> jcbgdList = testMapper.getCcXZxYyPacsLtkjExamJcbgd(s);
                DynamicDataSourceContextHolder.clearDataSourceType();
                if (null != jcbgdList && !jcbgdList.isEmpty()) jcbgdService.saveBatch(jcbgdList);
                DynamicDataSourceContextHolder.clearDataSourceType();
            }
        } catch (Exception e) {
            log.info("同步检查数据失败");
//            throw new RuntimeException(e);
            log.error(String.valueOf(e));
        }
    }
}