| | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.quartz.mapper.CollectHISMapper; |
| | | import com.ruoyi.quartz.service.ICollectHISService; |
| | | import com.smartor.common.SpecificArchiveConstants; |
| | | import com.smartor.domain.*; |
| | | import com.smartor.domain.entity.ServiceSubtaskEntity; |
| | | import com.smartor.mapper.*; |
| | | import com.smartor.service.IPatMedOuthospService; |
| | | import com.smartor.service.IPatSpecificArchiveGenerateService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | | import org.apache.commons.lang3.ArrayUtils; |
| | |
| | | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneId; |
| | | import java.util.*; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private IPatMedOuthospService patMedOuthospService; |
| | | |
| | | @Autowired |
| | | private IPatSpecificArchiveGenerateService patSpecificArchiveGenerateService; |
| | | |
| | | @Autowired |
| | | private SysDept2Mapper sysDept2Mapper; |
| | |
| | | |
| | | @Autowired |
| | | private RedisCache redisCache; |
| | | |
| | | /** |
| | | * 用户同步任务运行标记,避免重复点击/重复调用并发跑全量同步 |
| | | */ |
| | | private final AtomicBoolean userSyncRunning = new AtomicBoolean(false); |
| | | |
| | | private static final long SHARDING_THRESHOLD = 5_000_000L; |
| | | private static final String PAT_ARCHIVE_TABLE = "pat_archive"; |
| | |
| | | * @return |
| | | */ |
| | | private Integer syncOuthosp(String startTime, String endTime) { |
| | | return syncOuthosp(startTime, endTime, null, false); |
| | | } |
| | | |
| | | /** |
| | | * 同步门诊信息。 |
| | | * |
| | | * @param startTime 开始时间 |
| | | * @param endTime 结束时间 |
| | | * @param orgid 机构ID,为空时同步全部机构 |
| | | * @param byAdmitDate true=按就诊日期过滤(补采);false=按最后更新时间增量同步 |
| | | */ |
| | | private Integer syncOuthosp(String startTime, String endTime, String orgid, boolean byAdmitDate) { |
| | | Integer count = 0; |
| | | PatMedOuthosp patMedOuthosp = new PatMedOuthosp(); |
| | | patMedOuthosp.setLastStartTime(startTime); |
| | | patMedOuthosp.setLastEndTime(endTime); |
| | | if (byAdmitDate) { |
| | | patMedOuthosp.setBeginTime(parseDateTime(startTime)); |
| | | patMedOuthosp.setEndTime(parseDateTime(endTime)); |
| | | } else { |
| | | patMedOuthosp.setLastStartTime(startTime); |
| | | patMedOuthosp.setLastEndTime(endTime); |
| | | } |
| | | patMedOuthosp.setOrgid(orgid); |
| | | List<PatMedOuthosp> patMedOuthosps = chMapper.selectPatMedOuthospList(patMedOuthosp); |
| | | log.info("同步门诊信息数量" + patMedOuthosps.size()); |
| | | log.info("同步门诊信息数量:{},orgid={},byAdmitDate={}", patMedOuthosps.size(), orgid, byAdmitDate); |
| | | for (PatMedOuthosp patMedOuthosp1 : patMedOuthosps) { |
| | | // 按机构补采时,HIS 视图未返回 orgid 的记录补上查询条件中的机构 |
| | | if (StringUtils.isEmpty(patMedOuthosp1.getOrgid()) && StringUtils.isNotEmpty(orgid)) { |
| | | patMedOuthosp1.setOrgid(orgid); |
| | | } |
| | | PatArchive localArchive = null; |
| | | try { |
| | | //患者基本信息 |
| | | PatArchive patArchive = new PatArchive(); |
| | |
| | | pa1.setPatientno(patMedOuthosp1.getPatno()); |
| | | List<PatArchive> patArchives1 = patArchiveMapper.selectPatArchiveList(pa1); |
| | | if (CollectionUtils.isNotEmpty(patArchives1)) { |
| | | patArchives.get(0).setId(patArchives1.get(0).getId()); |
| | | localArchive = patArchives1.get(0); |
| | | patArchives.get(0).setId(localArchive.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)); |
| | | |
| | | localArchive = patArchives.get(0); |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | | log.info("同患者基本信息:" + ex.getMessage()); |
| | | } |
| | | |
| | | // HIS 患者信息缺失时,仍按患者编号回查本地档案 |
| | | if (localArchive == null && StringUtils.isNotEmpty(patMedOuthosp1.getPatno())) { |
| | | try { |
| | | PatArchive localQuery = new PatArchive(); |
| | | localQuery.setPatientno(patMedOuthosp1.getPatno()); |
| | | List<PatArchive> localArchives = patArchiveMapper.selectPatArchiveList(localQuery); |
| | | if (CollectionUtils.isNotEmpty(localArchives)) { |
| | | localArchive = localArchives.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) { |
| | | if (localArchive != null && localArchive.getId() != null) { |
| | | patMedOuthosp1.setPatid(localArchive.getId()); |
| | | } |
| | | PatMedOuthosp localOuthosp = null; |
| | | try { |
| | | localOuthosp = findLocalOuthosp(patMedOuthosp1); |
| | | } catch (Exception e) { |
| | | log.warn("回查本地门诊记录失败,按新增处理,serialnum={}", patMedOuthosp1.getSerialnum(), e); |
| | | } |
| | | if (localOuthosp != null) { |
| | | patMedOuthosp1.setUpdateTime(new Date()); |
| | | patMedOuthosp1.setId(patMedOuthospsTemp.get(0).getId()); |
| | | patMedOuthosp1.setId(localOuthosp.getId()); |
| | | //市一 院区id对应orgid,机构id为默认的1 |
| | | if(active.equals("hzszlyy")) { |
| | | patMedOuthosp1.setCampusid(patMedOuthosp1.getOrgid()); |
| | |
| | | } |
| | | count += patMedOuthospService.insertPatMedOuthosp(patMedOuthosp1); |
| | | } |
| | | generateSpecificArchive(patMedOuthosp1, localArchive); |
| | | } catch (Exception ex) { |
| | | log.info("同步出入院:" + ex.getMessage()); |
| | | log.info("同步门诊信息:" + ex.getMessage()); |
| | | } |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | /** |
| | | * 回查本地门诊记录,避免重复采集时插入重复数据。 |
| | | */ |
| | | private PatMedOuthosp findLocalOuthosp(PatMedOuthosp record) { |
| | | if (StringUtils.isEmpty(record.getSerialnum()) && StringUtils.isEmpty(record.getOuthospno())) { |
| | | return null; |
| | | } |
| | | PatMedOuthospQueryReq req = new PatMedOuthospQueryReq(); |
| | | req.setSerialnum(record.getSerialnum()); |
| | | req.setOuthospno(record.getOuthospno()); |
| | | req.setOrgid(record.getOrgid()); |
| | | List<PatMedOuthosp> localList = patMedOuthospMapper.callSpQueryOuthosp(req); |
| | | return CollectionUtils.isNotEmpty(localList) ? localList.get(0) : null; |
| | | } |
| | | |
| | | /** |
| | | * 门诊接诊完成记录采集后生成专科档案(仅丽水市妇幼保健院生效)。 |
| | | */ |
| | | private void generateSpecificArchive(PatMedOuthosp patMedOuthosp, PatArchive patArchive) { |
| | | if (!SpecificArchiveConstants.LISHUI_MCH_ORG_ID.equals(patMedOuthosp.getOrgid())) { |
| | | return; |
| | | } |
| | | try { |
| | | int generated = patSpecificArchiveGenerateService.generateByOuthosp(patMedOuthosp, patArchive); |
| | | if (generated > 0) { |
| | | log.info("门诊接诊完成记录生成专科档案成功,outhospno={},生成数量={}", |
| | | patMedOuthosp.getOuthospno(), generated); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("门诊接诊完成记录生成专科档案失败,outhospno={}", patMedOuthosp.getOuthospno(), e); |
| | | } |
| | | } |
| | | |
| | | private Date parseDateTime(String value) { |
| | | if (StringUtils.isEmpty(value)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value); |
| | | } catch (Exception e) { |
| | | try { |
| | | return new SimpleDateFormat("yyyy-MM-dd").parse(value); |
| | | } catch (Exception ex) { |
| | | log.warn("解析门诊采集时间失败:{}", value); |
| | | return null; |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 前端/定时任务传入 yyyy-MM-dd 时,结束时间补齐到当天 23:59:59,避免漏掉当天数据。 |
| | | */ |
| | | private Date normalizeEndTime(Date endTime) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(endTime); |
| | | if (calendar.get(Calendar.HOUR_OF_DAY) == 0 && calendar.get(Calendar.MINUTE) == 0 |
| | | && calendar.get(Calendar.SECOND) == 0 && calendar.get(Calendar.MILLISECOND) == 0) { |
| | | calendar.add(Calendar.DAY_OF_MONTH, 1); |
| | | calendar.add(Calendar.SECOND, -1); |
| | | return calendar.getTime(); |
| | | } |
| | | return endTime; |
| | | } |
| | | |
| | | @Override |
| | | public Integer collectMchOuthospAndGenerateArchive(String orgid, Date startTime, Date endTime) { |
| | | if (!SpecificArchiveConstants.LISHUI_MCH_ORG_ID.equals(orgid)) { |
| | | log.warn("采集门诊接诊完成记录仅支持丽水市妇幼保健院,orgid={}", orgid); |
| | | return 0; |
| | | } |
| | | Date end = endTime != null ? normalizeEndTime(endTime) : new Date(); |
| | | Date start = startTime != null ? startTime : new Date(end.getTime() - 24 * 60 * 60 * 1000L); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | log.info("开始采集丽水妇幼门诊接诊完成记录,时间范围:{} ~ {}", sdf.format(start), sdf.format(end)); |
| | | int count = syncOuthosp(sdf.format(start), sdf.format(end), orgid, true); |
| | | log.info("丽水妇幼门诊接诊完成记录采集完成,时间范围:{} ~ {},处理数量:{}", |
| | | sdf.format(start), sdf.format(end), count); |
| | | return count; |
| | | } |
| | | |
| | |
| | | return size; |
| | | } |
| | | |
| | | /** |
| | | * 异步同步用户信息:接口立即返回,实际同步交给线程池后台执行。 |
| | | * 使用 AtomicBoolean 防止重复提交导致多个全量同步任务并发执行。 |
| | | */ |
| | | @Override |
| | | public boolean selectUserListAsync(SysUser sysUser) { |
| | | if (!userSyncRunning.compareAndSet(false, true)) { |
| | | log.warn("用户信息同步任务正在执行中,忽略本次提交"); |
| | | return false; |
| | | } |
| | | try { |
| | | taskExecutor.execute(() -> { |
| | | long start = System.currentTimeMillis(); |
| | | log.info("用户信息异步同步任务开始执行"); |
| | | try { |
| | | Integer count = selectUserList(sysUser); |
| | | log.info("用户信息异步同步任务执行完成,处理数量:{},耗时:{}ms", |
| | | count, System.currentTimeMillis() - start); |
| | | } catch (Throwable e) { |
| | | log.error("用户信息异步同步任务执行失败,耗时:{}ms", |
| | | System.currentTimeMillis() - start, e); |
| | | } finally { |
| | | userSyncRunning.set(false); |
| | | } |
| | | }); |
| | | return true; |
| | | } catch (RuntimeException e) { |
| | | userSyncRunning.set(false); |
| | | log.error("用户信息异步同步任务提交失败", e); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public Integer selectUserList(SysUser sysUser) { |
| | | List<SysUser> sysUserList = chMapper.selectUserList(sysUser); |
| | |
| | | log.info("selectDeptList的采集到的数量为:{}", sysDepts.size()); |
| | | // count = sysDept2Mapper.batchDept(sysDepts); |
| | | for (SysDept row : sysDepts) { |
| | | SysDept sysDeptTemp = sysDept2Mapper.selectDeptByCode(row.getDeptCode(), null); |
| | | // 科室和病区可能共用同一个 dept_code,必须按 dept_type + orgid 精确定位,否则 selectOne 会返回多条 |
| | | if (StringUtils.isEmpty(row.getDeptCode())) { |
| | | log.warn("selectDeptList跳过deptCode为空的记录:{}", row); |
| | | continue; |
| | | } |
| | | SysDept sysDeptTemp = sysDept2Mapper.selectDeptByCodeAndType(row.getDeptCode(), row.getOrgid(), row.getDeptType()); |
| | | if (sysDeptTemp != null && sysDeptTemp.getDeptName() != null) { |
| | | row.setDeptId(sysDeptTemp.getDeptId()); |
| | | count += sysDept2Mapper.updateDept(row); |