陈昶聿
10 天以前 579438ab87a88332f3643fbeefb3bbc43c87ae59
ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/CollectHISServiceImpl.java
@@ -9,10 +9,12 @@
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;
@@ -22,6 +24,7 @@
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;
@@ -41,6 +44,9 @@
    @Autowired
    private IPatMedOuthospService patMedOuthospService;
    @Autowired
    private IPatSpecificArchiveGenerateService patSpecificArchiveGenerateService;
    @Autowired
    private SysDept2Mapper sysDept2Mapper;
@@ -277,13 +283,36 @@
     * @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();
@@ -294,26 +323,47 @@
                    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());
@@ -329,14 +379,95 @@
                    }
                    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;
    }
    /**
     * 同步手术信息
     *
     * @param