yxh
5 天以前 dc4df9a530016c4fd24f9aebc6ccf1b95310ff1f
smartor/src/main/java/com/smartor/service/impl/ExternalInfoServiceImpl.java
@@ -15,6 +15,7 @@
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
@@ -583,105 +584,149 @@
     */
    @Override
    public Boolean getExternalLeaveHospPatientInfo30Day() {
        String startTime = null;
        String endTime = null;
        Date date = patMedInhospMapper.selectEndtimeMax();
        if (ObjectUtils.isNotEmpty(date)) {
            Date now = new Date();
            // 使用Calendar计算30天前
            Calendar cal = Calendar.getInstance();
            cal.setTime(now);
            cal.add(Calendar.DAY_OF_MONTH, -30);
            Date before30Days = cal.getTime();
        try {
            // 固定开始时间
            String fixedStartTime = "2025-03-25";
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//            startTime = formatter.format(date);
//            startTime = formatter.format(before30Days);
            startTime = "2025-03-25";
            endTime = formatter.format(new Date());
        }
        List<ExternalLeaveHospPatientInfo> externalLeaveHospPatientInfo = externalInfoMapper.getExternalLeaveHospPatientInfo(startTime, endTime);
        for (ExternalLeaveHospPatientInfo externalLeaveHospPatientInfo1 : externalLeaveHospPatientInfo) {
            //先对患者基本信息进行处理
            Long patid = dealPatArchive(externalLeaveHospPatientInfo1.getBrid());
            //新增出院数据
            //根据住院号查询入院信息
            PatMedInhosp pmi = new PatMedInhosp();
            pmi.setInhospno(externalLeaveHospPatientInfo1.getZybm());
            List<PatMedInhosp> patMedInhospList = patMedInhospMapper.selectPatMedInhospList2(pmi);
            log.error("通过住院号:{},查询患者入院的信息为:{}", externalLeaveHospPatientInfo1.getZybm(), patMedInhospList);
//            log.error("externalLeaveHospPatientInfo1的结果为:{}", externalLeaveHospPatientInfo1);
            if (CollectionUtils.isEmpty(patMedInhospList)) {
                //如果不存在入院信息,是不是需要获取一下入院信息,则进行新增()
                List<ExternalInHospPatientInfo> externalInHospPatientInfo1 = externalInfoMapper.getExternalInHospPatientInfo(null, null, externalLeaveHospPatientInfo1.getBrid());
                if (CollectionUtils.isNotEmpty(externalInHospPatientInfo1)) {
                    //先对患者基本信息进行处理
                    Long pid = dealPatArchive(externalInHospPatientInfo1.get(0).getBrid());
                    PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
                    //新增入院数据
                    PatMedInhosp inhosp = new PatMedInhosp();
                    inhosp.setInhospno(externalInHospPatientInfo1.get(0).getZyhm());
                    inhosp.setSerialnum(externalInHospPatientInfo1.get(0).getZyhm());
                    inhosp.setNurseId(externalInHospPatientInfo1.get(0).getZrhsbm());
                    inhosp.setNurseName(externalInHospPatientInfo1.get(0).getZrhs());
                    inhosp.setStarttime(externalInHospPatientInfo1.get(0).getRyrq());
                    inhosp.setDiagname(externalInHospPatientInfo1.get(0).getRyqk());
                    inhosp.setDeptcode(externalLeaveHospPatientInfo1.getCyks());
                    inhosp.setDeptname(externalLeaveHospPatientInfo1.getCyksdm());
                    inhosp.setDrname(externalLeaveHospPatientInfo1.getZrys());
                    inhosp.setDrcode(externalLeaveHospPatientInfo1.getZrysbm());
                    inhosp.setHospitaldistrictid("" + externalInHospPatientInfo1.get(0).getBrbqdm());
                    inhosp.setBedNo("" + externalInHospPatientInfo1.get(0).getBrch());
                    inhosp.setPatid(pid);
                    inhosp.setPatname(patArchive.getName());
                    inhosp.setTelcode(patArchive.getTelcode());
                    inhosp.setPatno(patArchive.getPatientno());
                    inhosp.setOutWayName(externalLeaveHospPatientInfo1.getCyfs());
                    inhosp.setEndtime(externalLeaveHospPatientInfo1.getCysq());
                    inhosp.setLeavediagname(externalLeaveHospPatientInfo1.getCyzd());
                    inhosp.setLeavehospitaldistrictcode(externalLeaveHospPatientInfo1.getCybqdm());
                    inhosp.setLeavehospitaldistrictname(externalLeaveHospPatientInfo1.getCybq());
                    inhosp.setLeaveldeptcode(externalLeaveHospPatientInfo1.getCyksdm());
                    inhosp.setLeaveldeptname(externalLeaveHospPatientInfo1.getCyks());
            String today = formatter.format(new Date());
                    inhosp.setSchemestatus(2L);
                    inhosp.setInhospstate("1");
                    inhosp.setCreateTime(new Date());
//                    log.error("新增的patMedInhosp为:{}", inhosp);
                    int i = patMedInhospMapper.insertPatMedInhosp(inhosp);
                    log.error("新增的patMedInhosp结果为:{}", i);
            log.info("开始分批查询出院患者信息,总时间范围:{} 至 {}", fixedStartTime, today);
                } else {
                    log.error("该病人不存在入院信息,病人ID:{}", externalLeaveHospPatientInfo1.getBrid());
                }
            // 计算需要查询的次数
            Calendar startCal = Calendar.getInstance();
            Calendar endCal = Calendar.getInstance();
            Calendar currentCal = Calendar.getInstance();
            } else if (CollectionUtils.isNotEmpty(patMedInhospList)) {
                //如果存在入院信息,则进行修改
                pmi = patMedInhospList.get(0);
                pmi.setOutWayName(externalLeaveHospPatientInfo1.getCyfs());
                pmi.setEndtime(externalLeaveHospPatientInfo1.getCysq());
                pmi.setLeavediagname(externalLeaveHospPatientInfo1.getCyzd());
                pmi.setLeaveldeptcode(externalLeaveHospPatientInfo1.getCyksdm());
                pmi.setLeaveldeptname(externalLeaveHospPatientInfo1.getCyks());
                pmi.setDeptname(externalLeaveHospPatientInfo1.getCyks());
                pmi.setDeptcode(externalLeaveHospPatientInfo1.getCyksdm());
                pmi.setLeavehospitaldistrictcode(externalLeaveHospPatientInfo1.getCybqdm());
                pmi.setLeavehospitaldistrictname(externalLeaveHospPatientInfo1.getCybq());
//                pmi.setSchemestatus(2L);
                pmi.setInhospstate("1");
//                pmi.setUpdateTime(new Date());
                pmi.setNurseId(externalLeaveHospPatientInfo1.getZrhsbm());
                pmi.setNurseName(externalLeaveHospPatientInfo1.getZrhs());
                pmi.setDrcode(externalLeaveHospPatientInfo1.getZrysbm());
                pmi.setDrname(externalLeaveHospPatientInfo1.getZrys());
                log.error("修改的pmi结果为:{}", pmi);
                patMedInhospMapper.updatePatMedInhosp(pmi);
            try {
                startCal.setTime(formatter.parse(fixedStartTime));
                endCal.setTime(formatter.parse(today));
            } catch (ParseException e) {
                log.error("日期解析失败:{}", e.getMessage(), e);
                return false;
            }
            int totalDays = (int) ((endCal.getTimeInMillis() - startCal.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            int batchSize = 10; // 每批10天
            int totalBatches = (int) Math.ceil((double) totalDays / batchSize);
            log.info("总共需要查询 {} 天,分为 {} 批次,每批 {} 天", totalDays, totalBatches, batchSize);
            int totalSuccessCount = 0;
            int totalFailCount = 0;
            // 分批查询
            for (int batch = 0; batch < totalBatches; batch++) {
                // 计算当前批次的开始和结束时间
                Calendar batchStartCal = (Calendar) startCal.clone();
                batchStartCal.add(Calendar.DAY_OF_MONTH, batch * batchSize);
                Calendar batchEndCal = (Calendar) batchStartCal.clone();
                batchEndCal.add(Calendar.DAY_OF_MONTH, batchSize - 1);
                // 确保不超过今天
                if (batchEndCal.after(endCal)) {
                    batchEndCal = (Calendar) endCal.clone();
                }
                String batchStartTime = formatter.format(batchStartCal.getTime());
                String batchEndTime = formatter.format(batchEndCal.getTime());
                log.info("开始查询第 {} 批次,时间范围:{} 至 {}", batch + 1, batchStartTime, batchEndTime);
                try {
                    // 查询当前批次的外部出院患者信息
                    List<ExternalOutHospInfo> externalOutHospInfos = externalInfoMapper.getExternalOutHospInfo(batchStartTime, batchEndTime);
                    log.info("第 {} 批次查询到出院患者信息数量:{}", batch + 1, externalOutHospInfos.size());
                    int batchSuccessCount = 0;
                    int batchFailCount = 0;
                    // 处理当前批次的数据
                    for (ExternalOutHospInfo externalOutHospInfo : externalOutHospInfos) {
                        try {
                            // 处理患者档案
                            Long patid = dealPatArchive(externalOutHospInfo.getBrid());
                            PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patid);
                            // 创建门诊记录对象
                            PatMedOuthosp patMedOuthosp = new PatMedOuthosp();
                            // 设置基本信息
                            patMedOuthosp.setOuthospno(externalOutHospInfo.getJzkh());
                            patMedOuthosp.setSerialnum(externalOutHospInfo.getJzkh());
                            patMedOuthosp.setPatid(patid);
                            // 设置患者姓名
                            if (ObjectUtils.isNotEmpty(patArchive)) {
                                patMedOuthosp.setPatname(patArchive.getName());
                            }
                            // 设置诊断信息
                            patMedOuthosp.setIcd10code(externalOutHospInfo.getZddm());
                            patMedOuthosp.setDiagname(externalOutHospInfo.getZdmc());
                            // 设置科室信息
                            patMedOuthosp.setDeptcode(String.valueOf(externalOutHospInfo.getKsdm()));
                            patMedOuthosp.setDeptname(externalOutHospInfo.getKsmc());
                            // 设置医生信息
                            patMedOuthosp.setDrcode(String.valueOf(externalOutHospInfo.getYsdm()));
                            patMedOuthosp.setDrname(externalOutHospInfo.getYsmc());
                            // 设置其他信息
                            patMedOuthosp.setHpi(externalOutHospInfo.getXbs());
                            patMedOuthosp.setMainsuit(externalOutHospInfo.getZs());
                            patMedOuthosp.setCategory(externalOutHospInfo.getLb());
                            patMedOuthosp.setAdmitdate(externalOutHospInfo.getJzrq());
                            // 设置时间字段
                            Date now = new Date();
                            patMedOuthosp.setUploadTime(now);
                            patMedOuthosp.setCreateTime(now);
                            patMedOuthosp.setUpdateTime(now);
                            // 设置默认值
                            patMedOuthosp.setDelFlag("0");  // 删除标志
                            patMedOuthosp.setIsupload(0L);   // 上传标志
                            // 插入数据
                            int result = patMedOuthospMapper.insertPatMedOuthosp(patMedOuthosp);
                            if (result > 0) {
                                batchSuccessCount++;
                                totalSuccessCount++;
                            } else {
                                batchFailCount++;
                                totalFailCount++;
                            }
                        } catch (Exception e) {
                            batchFailCount++;
                            totalFailCount++;
                            log.error("处理患者门诊记录时发生异常,患者ID:{},门诊号:{},异常信息:{}",
                                    externalOutHospInfo.getBrid(), externalOutHospInfo.getJzkh(), e.getMessage(), e);
                        }
                    }
                    log.info("第 {} 批次处理完成,成功:{},失败:{}", batch + 1, batchSuccessCount, batchFailCount);
                    // 每批次处理完后稍作延迟,避免对数据库造成过大压力
                    if (batch < totalBatches - 1) {
                        Thread.sleep(1000); // 延迟1秒
                    }
                } catch (Exception e) {
                    log.error("第 {} 批次查询处理时发生异常:{}", batch + 1, e.getMessage(), e);
                    totalFailCount++;
                }
            }
            log.info("所有批次处理完成,总计成功:{},失败:{}", totalSuccessCount, totalFailCount);
            return true;
        } catch (Exception e) {
            log.error("获取出院患者信息时发生异常:{}", e.getMessage(), e);
            return false;
        }
        return null;
    }