sinake
3 天以前 19dde21187ef9edfdb38f60fbe764bddf3d2466d
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package com.ruoyi.quartz.service.impl;
 
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.SysUserDept;
import com.ruoyi.common.core.domain.entity.SysUserRole;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.quartz.mapper.CollectHISMapper;
import com.ruoyi.quartz.mapper.UtilsMapper;
import com.ruoyi.quartz.service.ICollectHISService;
import com.smartor.domain.*;
import com.smartor.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class CollectHISServiceImpl implements ICollectHISService {
    @Autowired
    private CollectHISMapper chMapper;
 
    @Autowired
    private SysUser2Mapper sysUser2Mapper;
 
    @Autowired
    private SysUserRole2Mapper sysUserRoleMapper;
 
    @Autowired
    private SysDept2Mapper sysDept2Mapper;
 
    @Autowired
    private SysUserDeptMapper sysUserDeptMapper;
 
    @Autowired
    private Icd10Mapper icd10Mapper;
 
    @Autowired
    private PatMedOuthospMapper patMedOuthospMapper;
 
    @Autowired
    private PatMedInhospMapper patMedInhospMapper;
 
    @Autowired
    private PatArchiveMapper patArchiveMapper;
 
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;
 
    @Autowired
    private ShardingMapper shardingMapper;
 
    @Autowired
    private UtilsMapper utilsMapper;
 
    private static final long SHARDING_THRESHOLD = 5_000_000L;
    private static final String PAT_ARCHIVE_TABLE = "pat_archive";
    private static final String PAT_MED_INHOSP_TABLE = "pat_med_inhosp";
 
 
    @Override
    public List<PatArchive> selectPatArchiveList(PatArchive patArchive) {
        return chMapper.selectPatArchiveList(patArchive);
    }
 
//    @Override
//    public Integer selectPatMedInhospList(PatMedInhosp patMedInhosp) {
//        // 异步执行整个数据同步任务
//        taskExecutor.execute(() -> {
//            log.info("开始执行数据同步任务...");
//            syncPatArchivesInOrder(); // 先同步患者
//            syncPatMedInhosp();       // 再同步出入院
//            log.info("数据同步任务提交完毕。");
//        });
//        return 0; // 返回0表示任务已提交到后台执行
//    }
 
    /**
     * 同步任务
     * @param
     * @return
     */
    @Override
    public Integer sync() {
        Integer count = 0;
        List<Map<String, Object>> syncList = utilsMapper.getList("SELECT id,syncName,DATE_FORMAT(ADDDATE(startTime, INTERVAL -1 MINUTE), '%Y-%m-%d %H:%i:%s') startTime  FROM sys_sync_time  where state=0");
        if (syncList.size() > 0) {
            String endTime = chMapper.getSqlString("select sysdate from healthy_inhosp where rownum=1");
            //同步患者基本信息
//            List<Map<String, Object>> syncTemp = syncList.stream().filter(row -> row.get("syncName").equals("pat_archive")).collect(Collectors.toList());
//            if (syncTemp.size() > 0) {
//                count+=syncArchive(syncTemp.get(0).get("startTime") + "", endTime);
//                utilsMapper.updateSql("update sys_sync_time set startTime='"+endTime+"' where syncName='pat_archive' ");
//            }
 
            //同步住院病人信息
            List<Map<String, Object>> syncTemp = syncList.stream().filter(row -> row.get("syncName").equals("pat_med_inhosp")).collect(Collectors.toList());
            if (syncTemp.size() > 0) {
                count+=syncMedInhosp(syncTemp.get(0).get("startTime") + "", endTime);
                utilsMapper.updateSql("update sys_sync_time set startTime='"+endTime+"' where syncName='pat_med_inhosp' ");
            }
 
            //同步门诊信息
            syncTemp = syncList.stream().filter(row -> row.get("syncName").equals("pat_med_outhosp")).collect(Collectors.toList());
            if (syncTemp.size() > 0) {
                count+=syncOuthosp(syncTemp.get(0).get("startTime") + "", endTime);
                utilsMapper.updateSql("update sys_sync_time set startTime='"+endTime+"' where syncName='pat_med_outhosp' ");
            }
 
 
        } else {
            log.info("同步任务时间sys_sync_time表中没有需要同步的任务");
        }
        return count;
    }
 
    /**
     * 同步患者基本信息
     * @param
     * @return
     */
    private  Integer syncArchive(String startTime,String endTime) {
        Integer count = 0;
        PatArchive pa = new PatArchive();
        pa.setLastStartTime(startTime);
        pa.setLastEndTime(endTime);
        List<PatArchive> patArchives = chMapper.selectPatArchiveList(pa);
        log.info("可同患者基本信息数量" + patArchives.size());
        for (PatArchive pm : patArchives) {
            try {
                PatArchive pa1 = new PatArchive();
                pa1.setPatientno(pm.getPatientno());
                List<PatArchive> patArchives1 = patArchiveMapper.selectPatArchiveList(pa1);
                if (CollectionUtils.isNotEmpty(patArchives1)) {
                    pm.setId(patArchives1.get(0).getId());
                    pm.setUpdateTime(new Date());
                    count += patArchiveMapper.updatePatArchive(pm);
                } else {
                    pm.setCreateTime(new Date());
                    count += patArchiveMapper.insertPatArchiveSingle(pm);
 
                }
            }catch (Exception ex){
                log.info("可同患者基本信息:"+ex.getMessage());
            }
        }
 
        return count;
    }
 
    /**
     * 同步出入院信息
     * @param
     * @return
     */
    private  Integer syncMedInhosp(String startTime,String endTime) {
        Integer count = 0;
        PatMedInhosp patMedInhosp = new PatMedInhosp();
        patMedInhosp.setLastStartTime(startTime);
        patMedInhosp.setLastEndTime(endTime);
        List<PatMedInhosp> patMedInhospList = chMapper.selectPatMedInhospList(patMedInhosp);
        log.info("可同步出入院数量" + patMedInhospList.size());
        for (PatMedInhosp pm : patMedInhospList) {
            try {
                //患者基本信息
                PatArchive patArchive = new PatArchive();
                patArchive.setPatientno(pm.getPatno());
                List<PatArchive> patArchives = chMapper.selectPatArchiveList(patArchive);
                if (patArchives.size() > 0) {
                    PatArchive pa1 = new PatArchive();
                    pa1.setPatientno(pm.getPatno());
                    List<PatArchive> patArchives1 = patArchiveMapper.selectPatArchiveList(pa1);
                    if (CollectionUtils.isNotEmpty(patArchives1)) {
                        patArchives.get(0).setId(patArchives1.get(0).getId());
                        patArchives.get(0).setUpdateTime(new Date());
                        count += patArchiveMapper.updatePatArchive(patArchives.get(0));
                    } else {
                        patArchives.get(0).setCreateTime(new Date());
                        count += patArchiveMapper.insertPatArchiveSingle(patArchives.get(0));
 
                    }
                }
            }catch (Exception ex) {
                log.info("同患者基本信息:" + ex.getMessage());
            }
 
            try {
                //同步出入院
                PatMedInhosp pmi = new PatMedInhosp();
                pmi.setSerialnum(pm.getSerialnum());
                List<PatMedInhosp> patMedInhospList1 = patMedInhospMapper.selectPatMedInhospListBySerialnum(pmi);
                if (patMedInhospList1.size() == 0) {
                    pm.setCreateTime(new Date());
                    count += patMedInhospMapper.insertPatMedInhosp(pm);
                } else {
                    pm.setInhospid(patMedInhospList1.get(0).getInhospid());
                    pm.setUpdateTime(new Date());
                    count += patMedInhospMapper.updatePatMedInhosp(pm);
                }
            } catch (Exception ex) {
                log.info("同步出入院:" + ex.getMessage());
            }
        }
        return count;
    }
 
    /**
     * 同步门诊信息
     * @param
     * @return
     */
    private  Integer syncOuthosp(String startTime,String endTime) {
        Integer count = 0;
        PatMedOuthosp patMedOuthosp = new PatMedOuthosp();
        patMedOuthosp.setLastStartTime(startTime);
        patMedOuthosp.setLastEndTime(endTime);
        List<PatMedOuthosp> patMedOuthosps = chMapper.selectPatMedOuthospList(patMedOuthosp);
        log.info("同步门诊信息数量" + patMedOuthosps.size());
        for (PatMedOuthosp patMedOuthosp1 : patMedOuthosps) {
            try {
                //患者基本信息
                PatArchive patArchive = new PatArchive();
                patArchive.setPatientno(patMedOuthosp1.getPatno());
                List<PatArchive> patArchives = chMapper.selectPatArchiveList(patArchive);
                if (patArchives.size() > 0) {
                    PatArchive pa1 = new PatArchive();
                    pa1.setPatientno(patMedOuthosp1.getPatno());
                    List<PatArchive> patArchives1 = patArchiveMapper.selectPatArchiveList(pa1);
                    if (CollectionUtils.isNotEmpty(patArchives1)) {
                        patArchives.get(0).setId(patArchives1.get(0).getId());
                        patArchives.get(0).setUpdateTime(new Date());
                        count += patArchiveMapper.updatePatArchive(patArchives.get(0));
                    } else {
                        patArchives.get(0).setCreateTime(new Date());
                        count += patArchiveMapper.insertPatArchiveSingle(patArchives.get(0));
 
                    }
                }
            }catch (Exception ex) {
                log.info("同患者基本信息:" + ex.getMessage());
            }
 
            try {
                PatMedOuthosp patMedOuthospTemp = new PatMedOuthosp();
                patMedOuthospTemp.setSerialnum(patMedOuthosp1.getSerialnum());
                List<PatMedOuthosp> patMedOuthospsTemp = chMapper.selectPatMedOuthospList(patMedOuthosp);
                if (patMedOuthospsTemp.size() > 0) {
                    patMedOuthosp1.setUpdateTime(new Date());
                    patMedOuthosp1.setId(patMedOuthospsTemp.get(0).getId());
                    count += patMedOuthospMapper.updatePatMedOuthosp(patMedOuthosp1);
                } else {
                    patMedOuthosp1.setCreateTime(new Date());
                    count += patMedOuthospMapper.insertPatMedOuthosp(patMedOuthosp1);
                }
            } catch (Exception ex) {
                log.info("同步出入院:" + ex.getMessage());
            }
        }
        return count;
    }
 
    @Override
    public Integer selectPatMedInhospList(PatMedInhosp patMedInhosp) {
        List<PatMedInhosp> patMedInhospList = chMapper.selectPatMedInhospList(patMedInhosp);
        log.info("可同步病人数量"+patMedInhospList.size());
        for (PatMedInhosp pm : patMedInhospList) {
            PatArchive patArchive = new PatArchive();
            patArchive.setPatientno(pm.getPatno());
            List<PatArchive> patArchives = chMapper.selectPatArchiveList(patArchive);
            if (CollectionUtils.isEmpty(patArchives)) {
                //空了直接丢掉
                continue;
            }
            PatArchive pa = new PatArchive();
            pa.setPatientno(patArchives.get(0).getPatientno());
            List<PatArchive> patArchives1 = patArchiveMapper.selectPatArchiveList(pa);
            if (CollectionUtils.isNotEmpty(patArchives1)) {
                pm.setPatid(patArchives1.get(0).getId());
            } else {
                patArchiveMapper.insertPatArchiveSingle(patArchives.get(0));
                pm.setPatid(patArchives.get(0).getId());
            }
            if (!Objects.isNull(patMedInhosp.getStartInHospTime()) && !Objects.isNull(patMedInhosp.getEndInHospTime())) {
                //入院
                pm.setInhospstate("0");
                pm.setCreateTime(new Date());
                patMedInhospMapper.insertPatMedInhosp(pm);
            } else if (!Objects.isNull(patMedInhosp.getStartOutHospTime()) && !Objects.isNull(patMedInhosp.getEndOutHospTime())) {
                //出院
                pm.setInhospstate("1");
                pm.setUpdateTime(new Date());
                PatMedInhosp pmi = new PatMedInhosp();
                pmi.setSerialnum(pm.getSerialnum());
                List<PatMedInhosp> patMedInhospList1 = patMedInhospMapper.selectPatMedInhospList(pmi);
                if (CollectionUtils.isEmpty(patMedInhospList1)) {
                    pm.setCreateTime(new Date());
                    patMedInhospMapper.insertPatMedInhosp(pm);
                } else {
                    pm.setInhospid(patMedInhospList1.get(0).getInhospid());
                    patMedInhospMapper.updatePatMedInhosp(pm);
                }
 
            }
        }
        return 0;
    }
 
    @Override
    public Boolean hnDataGather(HnDataGatherVO hnDataGatherVO) {
        if(ObjectUtils.isEmpty(hnDataGatherVO.getStartTime()) )
            hnDataGatherVO.setStartTime(new Date());
        if(ObjectUtils.isEmpty(hnDataGatherVO.getEndTime()) )
            hnDataGatherVO.setEndTime(new Date());
        LocalDate startDate = hnDataGatherVO.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endDate = hnDataGatherVO.getEndTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
 
        Integer po = null;
        // 循环处理每一天
        for (LocalDate currentDate = startDate; !currentDate.isAfter(endDate); currentDate = currentDate.plusDays(1)) {
            PatMedInhosp dailyCondition = new PatMedInhosp();
            //处理入院
            LocalDateTime dayStart = currentDate.atStartOfDay();
            LocalDateTime dayEnd = currentDate.atTime(23, 59, 59);
            dailyCondition.setStartInHospTime(Date.from(dayStart.atZone(ZoneId.systemDefault()).toInstant()));
            dailyCondition.setEndInHospTime(Date.from(dayEnd.atZone(ZoneId.systemDefault()).toInstant()));
            selectPatMedInhospList(dailyCondition);
 
            //处理出院
            dailyCondition.setStartInHospTime(null);
            dailyCondition.setEndInHospTime(null);
            dailyCondition.setStartOutHospTime(Date.from(dayStart.atZone(ZoneId.systemDefault()).toInstant()));
            dailyCondition.setEndOutHospTime(Date.from(dayEnd.atZone(ZoneId.systemDefault()).toInstant()));
            selectPatMedInhospList(dailyCondition);
 
            //处理门诊
            PatMedOuthosp patMedOuthosp = new PatMedOuthosp();
            patMedOuthosp.setBeginTime(Date.from(dayStart.atZone(ZoneId.systemDefault()).toInstant()));
            patMedOuthosp.setEndTime(Date.from(dayEnd.atZone(ZoneId.systemDefault()).toInstant()));
            selectPatMedOuthospList(patMedOuthosp);
        }
        return true;
    }
 
 
    @Override
    public Integer selectPatMedOuthospList(PatMedOuthosp patMedOuthosp) {
        List<PatMedOuthosp> patMedOuthosps = chMapper.selectPatMedOuthospList(patMedOuthosp);
        log.info("selectPatMedOuthospList的采集到的数量为:{}", patMedOuthosps.size());
        Integer i = null;
        for (PatMedOuthosp patMedOuthosp1 : patMedOuthosps) {
            //获取患者基本信息
            PatArchive patArchive = new PatArchive();
            patArchive.setPatientno(patMedOuthosp1.getPatno());
            List<PatArchive> patArchives = chMapper.selectPatArchiveList(patArchive);
 
            //根据patno判断本地患者基本信息是否存在
            PatArchive pa = new PatArchive();
            pa.setPatientno(patMedOuthosp1.getPatno());
            List<PatArchive> patArchives1 = patArchiveMapper.selectPatArchiveList(pa);
            if (CollectionUtils.isNotEmpty(patArchives1)) {
                patMedOuthosp1.setPatid(patArchives1.get(0).getId());
            } else {
                if (CollectionUtils.isEmpty(patArchives)) {
                    continue;
                }
                patArchiveMapper.insertPatArchiveSingle(patArchives.get(0));
                patMedOuthosp1.setPatid(patArchives.get(0).getId());
            }
            i = patMedOuthospMapper.insertPatMedOuthosp(patMedOuthosp1);
        }
 
        return i;
    }
 
    @Override
    public Integer selectIcd10List(Icd10 icd10) {
        List<Icd10> icd10s = chMapper.selectIcd10List(icd10);
        log.info("selectIcd10List的采集到的数量为:{}", icd10s.size());
        int size = 0;
        if (icd10s.size() > 500) {
            int i = icd10s.size() / 500;
            for (int a = 0; a < i + 1; a++) {
                if (a >= 0 && a < i) {
                    List<Icd10> icd10Temp = icd10s.subList(a * 500, (a + 1) * 500);
                    size += icd10Mapper.batchIcd10(icd10Temp);
                } else {
                    List<Icd10> icd10Temp = icd10s.subList(i * 500, icd10s.size());
                    if (icd10Temp != null && icd10Temp.size() != 0) {
                        size += icd10Mapper.batchIcd10(icd10Temp);
                    }
                }
            }
        } else {
            size += icd10Mapper.batchIcd10(icd10s);
        }
        return size;
    }
 
    @Override
    public Integer selectUserList(SysUser sysUser) {
        List<SysUser> sysUserList = chMapper.selectUserList(sysUser);
        log.info("sysUserList的采集到的数量为:{}", sysUserList.size());
        int i =0;// sysUser2Mapper.batchUser(sysUserList);
        for (SysUser sysUser1 : sysUserList) {
//            log.info("sysUser1的ID为:{}", sysUser1.getUserId());
//            log.info("sysUser1的HISUSERID为:{}", sysUser1.getHisUserId());
//            //新增用户与角色
//            SysUserRole yhyjsxx = chMapper.yhyjsxx(sysUser1);
//            if (yhyjsxx != null&&yhyjsxx.getUserId()!=null) {
//                yhyjsxx.setUserId(sysUser1.getUserId());
//                List<SysUserRole> userRoleList = new ArrayList<>();
//                userRoleList.add(yhyjsxx);
//                sysUserRoleMapper.batchUserRole(userRoleList);
//            }
            if (StringUtils.isEmpty(sysUser1.getHisUserId())) continue;
            SysUser suTemp=new SysUser();
            suTemp.setHisUserId(sysUser1.getHisUserId());
            List<SysUser> usersTemp= sysUser2Mapper.selectUserList(suTemp);
            if (usersTemp.size()>0) {
                sysUser1.setUserId(usersTemp.get(0).getUserId());
                sysUser2Mapper.updateUser(sysUser1);
            }else{
                sysUser2Mapper.insertUser(sysUser1);
                usersTemp= sysUser2Mapper.selectUserList(suTemp);
            }
 
            //新增用户与部门
            List<SysUserDept> sysUserDept = chMapper.yhyksxx(sysUser1);
            for(SysUserDept sud :sysUserDept) {
                if (usersTemp.size()>0) {
                    sud.setUserId(usersTemp.get(0).getUserId());
                    sysUserDeptMapper.insertSysUserDept(sud);
                }
 
            }
        }
 
        return i;
    }
 
    @Override
    public Integer selectDeptList(SysDept dept) {
        List<SysDept> sysDepts = chMapper.selectDeptList(dept);
        log.info("selectDeptList的采集到的数量为:{}", sysDepts.size());
        int i = sysDept2Mapper.batchDept(sysDepts);
        return i;
    }
 
 
    /**
     * 检查并执行分表的核心方法
     *
     * @param tableName 要检查的表名
     */
    private synchronized void checkAndShard(String tableName) {
        long currentCount = shardingMapper.getTableCount(tableName);
 
        if (currentCount >= SHARDING_THRESHOLD) {
            log.warn("表 '{}' 已达到分表阈值 {},准备执行分表操作...", tableName, SHARDING_THRESHOLD);
 
            String newTableName = tableName + "_" + java.time.LocalDate.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy_MM"));
            Long maxId = shardingMapper.getMaxId(tableName);
            if (maxId == null) maxId = 0L;
 
            shardingMapper.renameTable(tableName, newTableName);
            log.info("已将表 '{}' 重命名为 '{}'", tableName, newTableName);
 
            shardingMapper.createLikeTable(tableName, newTableName);
            log.info("已创建新表 '{}'", tableName);
 
            shardingMapper.setAutoIncrement(tableName, maxId + 1);
            log.info("已设置新表 '{}' 的自增ID起始值为 {}", tableName, maxId + 1);
 
            log.info("表 '{}' 的分表操作完成!", tableName);
        }
    }
 
}