|  |  | 
 |  |  |  | 
 |  |  | import com.ruoyi.common.core.domain.entity.SysDept; | 
 |  |  | import com.ruoyi.common.core.domain.entity.SysUser; | 
 |  |  | import com.ruoyi.common.core.redis.RedisCache; | 
 |  |  | import com.ruoyi.common.exception.base.BaseException; | 
 |  |  | import com.ruoyi.common.utils.DateUtils; | 
 |  |  | import com.ruoyi.common.utils.DtoConversionUtils; | 
 |  |  | import com.ruoyi.common.core.domain.entity.SysUserDept; | 
 |  |  | import com.ruoyi.common.core.domain.entity.SysUserRole; | 
 |  |  | import com.ruoyi.common.utils.StringUtils; | 
 |  |  | import com.smartor.domain.*; | 
 |  |  | import com.smartor.mapper.*; | 
 |  |  | import com.smartor.service.IHNGatherPatArchiveService; | 
 |  |  | import com.smartor.service.IPatArchiveService; | 
 |  |  | import com.smartor.service.IPatMedInhospService; | 
 |  |  | import com.smartor.service.IPatMedPhysicalService; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.apache.commons.collections4.CollectionUtils; | 
 |  |  | import org.apache.commons.lang3.ObjectUtils; | 
 |  |  | import org.apache.poi.hssf.usermodel.HSSFDateUtil; | 
 |  |  | import org.apache.poi.ss.usermodel.DataFormatter; | 
 |  |  | import org.apache.poi.ss.usermodel.Row; | 
 |  |  | import org.apache.poi.ss.usermodel.Sheet; | 
 |  |  | import org.apache.poi.ss.usermodel.Workbook; | 
 |  |  | import org.apache.poi.xssf.usermodel.XSSFWorkbook; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  | import org.springframework.web.multipart.MultipartFile; | 
 |  |  |  | 
 |  |  | import java.io.FileOutputStream; | 
 |  |  | import java.text.ParseException; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.time.LocalDate; | 
 |  |  | import java.time.LocalDateTime; | 
 |  |  | import java.time.ZoneId; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.concurrent.ExecutorService; | 
 |  |  | import java.util.concurrent.Executors; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * 患者档案Service业务层处理 | 
 |  |  | 
 |  |  |     private SysUser2Mapper sysUser2Mapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SysUserRole2Mapper sysUserRoleMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SysDept2Mapper sysDept2Mapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SysUserDeptMapper sysUserDeptMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private Icd10Mapper icd10Mapper; | 
 |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private PatArchiveMapper patArchiveMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private ThreadPoolTaskExecutor taskExecutor; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private ShardingMapper shardingMapper; | 
 |  |  |  | 
 |  |  |     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 hnGatherPatArchiveMapper.selectPatArchiveList(patArchive); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | //    @Override | 
 |  |  | //    public Integer selectPatMedInhospList(PatMedInhosp patMedInhosp) { | 
 |  |  | //        // 异步执行整个数据同步任务 | 
 |  |  | //        taskExecutor.execute(() -> { | 
 |  |  | //            log.info("开始执行数据同步任务..."); | 
 |  |  | //            syncPatArchivesInOrder(); // 先同步患者 | 
 |  |  | //            syncPatMedInhosp();       // 再同步出入院 | 
 |  |  | //            log.info("数据同步任务提交完毕。"); | 
 |  |  | //        }); | 
 |  |  | //        return 0; // 返回0表示任务已提交到后台执行 | 
 |  |  | //    } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Integer selectPatMedInhospList(PatMedInhosp patMedInhosp) { | 
 |  |  |         Long count = hnGatherPatArchiveMapper.selectPatMedInhospListCount(null); | 
 |  |  |         Long aa = (count + 1000 - 1) / 1000; | 
 |  |  |         //进行分页查询 | 
 |  |  |         for (int i = 0; i <= aa; i++) { | 
 |  |  |             patMedInhosp.setPs(1000); | 
 |  |  |             patMedInhosp.setPn(1000 * i); | 
 |  |  |             List<PatMedInhosp> patMedInhospList = hnGatherPatArchiveMapper.selectPatMedInhospList(patMedInhosp); | 
 |  |  |             for (PatMedInhosp pm : patMedInhospList) { | 
 |  |  |                 String patno = pm.getPatno(); | 
 |  |  |                 PatArchive patArchive = new PatArchive(); | 
 |  |  |                 //先去我们自己的表里通过patno查询该患者是否存在 | 
 |  |  |                 patArchive.setPatientno(patno); | 
 |  |  |                 List<PatArchive> patArchives = patArchiveMapper.selectPatArchiveList(patArchive); | 
 |  |  |                 if (CollectionUtils.isEmpty(patArchives)) { | 
 |  |  |                     //不存在,则通过patno去HIS表hzjbxx查询 | 
 |  |  |                     List<PatArchive> patArchives1 = hnGatherPatArchiveMapper.selectPatArchiveList(patArchive); | 
 |  |  |                     //把查询出来的数据存到pat_archive表 | 
 |  |  |                     PatArchive pa = patArchives1.get(0); | 
 |  |  |                     pa.setId(null); | 
 |  |  |                     patArchiveMapper.insertPatArchiveSingle(pa); | 
 |  |  |                     pm.setPatid(pa.getId()); | 
 |  |  |                 } else { | 
 |  |  |                     //直接将patid放到出入院表中 | 
 |  |  |                     pm.setPatid(patArchives.get(0).getId()); | 
 |  |  |                 } | 
 |  |  |         List<PatMedInhosp> patMedInhospList = hnGatherPatArchiveMapper.selectPatMedInhospList(patMedInhosp); | 
 |  |  |         for (PatMedInhosp pm : patMedInhospList) { | 
 |  |  |             PatArchive patArchive = new PatArchive(); | 
 |  |  |             patArchive.setPatientno(pm.getPatno()); | 
 |  |  |             List<PatArchive> patArchives = hnGatherPatArchiveMapper.selectPatArchiveList(patArchive); | 
 |  |  |             if (CollectionUtils.isEmpty(patArchives)) { | 
 |  |  |                 //空了直接丢掉 | 
 |  |  |                 continue; | 
 |  |  |             } | 
 |  |  |             if (patMedInhospList != null && !patMedInhospList.isEmpty()) { | 
 |  |  |                 patMedInhospMapper.insertPatMedInhospBatch(patMedInhospList); | 
 |  |  |             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 null; | 
 |  |  |         return 0; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public Boolean hnDataGather(HnDataGatherVO hnDataGatherVO) { | 
 |  |  |  | 
 |  |  |         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 = hnGatherPatArchiveMapper.selectPatMedOuthospList(patMedOuthosp); | 
 |  |  |         log.info("selectPatMedOuthospList的采集到的数量为:{}", patMedOuthosps.size()); | 
 |  |  |         int i = patMedOuthospMapper.batchPatMedOuthosp(patMedOuthosps); | 
 |  |  |         Integer i = null; | 
 |  |  |         for (PatMedOuthosp patMedOuthosp1 : patMedOuthosps) { | 
 |  |  |             //获取患者基本信息 | 
 |  |  |             PatArchive patArchive = new PatArchive(); | 
 |  |  |             patArchive.setPatientno(patMedOuthosp1.getPatno()); | 
 |  |  |             List<PatArchive> patArchives = hnGatherPatArchiveMapper.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 selectUserList(SysUser sysUser) { | 
 |  |  |         List<SysUser> sysUserList = hnGatherPatArchiveMapper.selectUserList(sysUser); | 
 |  |  |         if (CollectionUtils.isEmpty(sysUserList)) return null; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         log.info("sysUserList的采集到的数量为:{}", sysUserList.size()); | 
 |  |  |         int i = sysUser2Mapper.batchUser(sysUserList); | 
 |  |  |         for (SysUser sysUser1 : sysUserList) { | 
 |  |  |             log.info("sysUser1的ID为:{}", sysUser1.getUserId()); | 
 |  |  |             log.info("sysUser1的HISUSERID为:{}", sysUser1.getHisUserId()); | 
 |  |  |             //新增用户与角色 | 
 |  |  |             SysUserRole yhyjsxx = hnGatherPatArchiveMapper.yhyjsxx(sysUser1); | 
 |  |  |             if (yhyjsxx == null) continue; | 
 |  |  |             yhyjsxx.setUserId(sysUser1.getUserId()); | 
 |  |  |             List<SysUserRole> userRoleList = new ArrayList<>(); | 
 |  |  |             userRoleList.add(yhyjsxx); | 
 |  |  |             sysUserRoleMapper.batchUserRole(userRoleList); | 
 |  |  |  | 
 |  |  |             //新增用户与部门 | 
 |  |  |             if (StringUtils.isEmpty(sysUser1.getHisUserId())) continue; | 
 |  |  |             SysUserDept sysUserDept = hnGatherPatArchiveMapper.yhyksxx(sysUser1); | 
 |  |  |             if (Objects.isNull(sysUserDept) || sysUserDept.getDeptId() == null) continue; | 
 |  |  |             SysDept dept = new SysDept(); | 
 |  |  |             dept.setHisDeptId(sysUserDept.getDeptId().toString()); | 
 |  |  |             List<SysDept> sysDepts = sysDept2Mapper.selectDeptList(dept); | 
 |  |  |             if (CollectionUtils.isNotEmpty(sysDepts)) { | 
 |  |  |                 sysUserDept.setDeptId(sysDepts.get(0).getDeptId()); | 
 |  |  |                 sysUserDept.setDeptCode(sysDepts.get(0).getDeptCode()); | 
 |  |  |             } | 
 |  |  |             sysUserDept.setUserId(sysUser1.getUserId()); | 
 |  |  |             sysUserDeptMapper.insertSysUserDept(sysUserDept); | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return i; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |     public Integer selectDeptList(SysDept dept) { | 
 |  |  |         List<SysDept> sysDepts = hnGatherPatArchiveMapper.selectDeptList(dept); | 
 |  |  |         log.info("selectDeptList的采集到的数量为:{}", sysDepts.size()); | 
 |  |  |         int i = sysDept2Mapper.batchDept(sysDepts); | 
 |  |  |         int i = 0; | 
 |  |  |         if (CollectionUtils.isNotEmpty(sysDepts)) 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); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  | } | 
 |  |  |  | 
 |  |  |  |