From 2a3d4e4c082403aeb31d4c1c499a6107ffa835af Mon Sep 17 00:00:00 2001
From: sinake <sinake1@qq.com>
Date: 星期六, 13 九月 2025 14:22:55 +0800
Subject: [PATCH] 同步数据接口和随访率改成只有已完成

---
 ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/CollectHISServiceImpl.java |  266 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 241 insertions(+), 25 deletions(-)

diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/CollectHISServiceImpl.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/CollectHISServiceImpl.java
index 954d2b6..d5101e7 100644
--- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/CollectHISServiceImpl.java
+++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/service/impl/CollectHISServiceImpl.java
@@ -6,11 +6,13 @@
 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;
@@ -19,6 +21,7 @@
 import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.util.*;
+import java.util.stream.Collectors;
 
 @Slf4j
 @Service
@@ -56,6 +59,9 @@
     @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";
@@ -78,10 +84,194 @@
 //        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");
+            //鍚屾鎮h�呭熀鏈俊鎭�
+//            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;
+    }
+
+    /**
+     * 鍚屾鎮h�呭熀鏈俊鎭�
+     * @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("鍙悓鎮h�呭熀鏈俊鎭暟閲�" + 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("鍙悓鎮h�呭熀鏈俊鎭�:"+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 {
+                //鎮h�呭熀鏈俊鎭�
+                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 {
+                //鎮h�呭熀鏈俊鎭�
+                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());
@@ -126,7 +316,10 @@
 
     @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();
 
@@ -192,40 +385,63 @@
     public Integer selectIcd10List(Icd10 icd10) {
         List<Icd10> icd10s = chMapper.selectIcd10List(icd10);
         log.info("selectIcd10List鐨勯噰闆嗗埌鐨勬暟閲忎负锛歿}", icd10s.size());
-        int i = icd10Mapper.batchIcd10(icd10s);
-        return i;
+        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 = sysUser2Mapper.batchUser(sysUserList);
+        int i =0;// sysUser2Mapper.batchUser(sysUserList);
         for (SysUser sysUser1 : sysUserList) {
-            log.info("sysUser1鐨処D涓猴細{}", sysUser1.getUserId());
-            log.info("sysUser1鐨凥ISUSERID涓猴細{}", sysUser1.getHisUserId());
-            //鏂板鐢ㄦ埛涓庤鑹�
-            SysUserRole yhyjsxx = chMapper.yhyjsxx(sysUser1);
-            if (yhyjsxx == null) continue;
-            yhyjsxx.setUserId(sysUser1.getUserId());
-            List<SysUserRole> userRoleList = new ArrayList<>();
-            userRoleList.add(yhyjsxx);
-            sysUserRoleMapper.batchUserRole(userRoleList);
+//            log.info("sysUser1鐨処D涓猴細{}", sysUser1.getUserId());
+//            log.info("sysUser1鐨凥ISUSERID涓猴細{}", 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);
+            }
 
             //鏂板鐢ㄦ埛涓庨儴闂�
-            if (StringUtils.isEmpty(sysUser1.getHisUserId())) continue;
-            SysUserDept sysUserDept = chMapper.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);
+            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;

--
Gitblit v1.9.3