From 74ec634d3ecd024e71188cbdfd9a7118c2c645fe Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期二, 17 六月 2025 18:35:39 +0800
Subject: [PATCH] 代码提交

---
 ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java |  812 ++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 564 insertions(+), 248 deletions(-)

diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java
index 347f135..74436c8 100644
--- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java
+++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/task/RyTask.java
@@ -1,8 +1,6 @@
 package com.ruoyi.quartz.task;
 
-import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
-import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.reflect.TypeToken;
@@ -10,19 +8,22 @@
 import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.enums.MsgLSEnum;
 import com.ruoyi.common.enums.ServiceFromEnum;
-import com.ruoyi.common.utils.DtoConversionUtils;
+import com.ruoyi.common.enums.VisitSendStateEnum;
+import com.ruoyi.common.enums.WxGZHEnum;
+import com.ruoyi.common.exception.base.BaseException;
 import com.ruoyi.common.utils.HttpUtil;
+import com.ruoyi.common.utils.OkHttpExample;
 import com.ruoyi.common.utils.RSAPublicKeyExample;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.http.HttpUtils;
-import com.smartor.common.SendService;
+import com.smartor.common.LSHospTokenUtil;
 import com.smartor.domain.*;
 import com.smartor.mapper.ServiceSubtaskMapper;
 import com.smartor.mapper.ServiceTaskMapper;
 import com.smartor.mapper.SysUserImportMapper;
 import com.smartor.service.*;
-import com.sun.org.apache.bcel.internal.generic.NEW;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.ObjectUtils;
@@ -35,12 +36,12 @@
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Type;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.security.MessageDigest;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.LocalDate;
@@ -50,6 +51,8 @@
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.stream.Collectors;
+
+import static cn.hutool.core.convert.Convert.toHex;
 
 /**
  * 瀹氭椂浠诲姟璋冨害娴嬭瘯
@@ -98,9 +101,6 @@
     @Value("${pub_key}")
     private String pub_key;
 
-    @Value("${thirdWXUrl}")
-    private String thirdWXUrl;
-
     @Value("${req_path}")
     private String req_path;
 
@@ -139,6 +139,12 @@
 
     @Value("${hosp_info_url}")
     private String hospInfoUrl;
+
+    @Value("${appid}")
+    private String appid;
+
+    @Value("${server.port}")
+    private String port;
 
     public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
         System.out.println(StringUtils.format("鎵ц澶氬弬鏂规硶锛� 瀛楃涓茬被鍨媨}锛屽竷灏旂被鍨媨}锛岄暱鏁村瀷{}锛屾诞鐐瑰瀷{}锛屾暣褰}", s, b, l, d, i));
@@ -200,31 +206,41 @@
 
 
     /**
-     * 澶勭悊his鏁版嵁
+     * 澶勭悊HIS鏁版嵁閲囬泦
+     * 瀹氭椂浠诲姟锛氶噰闆嗘偅鑰呬俊鎭�佸嚭闄€�侀棬璇婃暟鎹�
      */
     public void dealHisData() {
-        //灏嗘偅鑰呬俊鎭�佸嚭闄€�侀棬璇婃暟鎹叏閮ㄩ噰闆�
-        // 鑾峰彇褰撳墠鏃ユ湡鍜屾椂闂�
-        LocalDateTime endTime = LocalDateTime.now();
-        endTime = endTime.with(LocalTime.MIN);
-//        String dateStr = "2024/11/30 00:00:00";
-//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
-//        LocalDateTime endTime = LocalDateTime.parse(dateStr, formatter);
-        // 鑾峰彇鍓嶄竴澶╃殑鏃ユ湡鍜屾椂闂�
-        LocalDateTime startTime = endTime.minusDays(1);
-        startTime = startTime.with(LocalTime.MIN);
-        getInHospInfo("0", startTime, endTime);
-        getInHospInfo("1", startTime, endTime);
-
-        //璋冪敤dealOupHospInfo鏁版嵁澶勭悊鏂规硶锛屽鐞嗗畬鍚庯紝灏嗏�滃嚭闄㈣〃鐨刢heck_flag=1鈥�
-        //鑾峰彇褰撳墠鏃ユ湡 锛堢簿纭埌澶╋級寰�鍓嶆帹鐩稿簲澶╂暟锛堝湪鎸囧畾鐨勫熀纭�妯℃澘涓壘瀵瑰簲鐨勫ぉ鏁帮級锛�
-        //寰幆鎮h�呬俊鎭紝鏍规嵁鎮h��"鍑洪櫌璇婃柇ICD鍊尖�濊幏鍙栧埌鐩稿綋鐨勬ā鏉匡紙鑾峰彇鍙栫浉搴旂殑澶╂暟锛�;
-        //鏍规嵁鏌ヨ鍑烘潵鐨勬ā鏉匡紝鍘诲浐瀹氫换鍔★紙鍥哄畾浠诲姟锛屽凡缁忓皢妯℃澘涔熷浐瀹氫簡锛夊幓鍖归厤妯℃澘
-        //鏍规嵁鍖归厤鍑烘潵鐨勬ā鏉垮拰浠诲姟涓庢偅鑰呬俊鎭紝灏嗘暟鎹彃鍏ュ埌瀛愪换鍔¤〃涓�
+        log.info("銆恉ealHisData銆戝紑濮嬫墽琛孒IS鏁版嵁閲囬泦浠诲姟");
+        long startTime = System.currentTimeMillis();
+        
+        try {
+            // 鑾峰彇鏄ㄥぉ0鐐瑰埌浠婂ぉ0鐐圭殑鏃堕棿鑼冨洿
+            LocalDateTime todayZero = LocalDateTime.now().with(LocalTime.MIN);
+            LocalDateTime yesterdayZero = todayZero.minusDays(1);
+            
+            log.info("銆恉ealHisData銆戦噰闆嗘椂闂磋寖鍥达細{} ~ {}", yesterdayZero, todayZero);
+            
+            // 閲囬泦鍏ラ櫌鏁版嵁
+            log.info("銆恉ealHisData銆戝紑濮嬮噰闆嗗叆闄㈡暟鎹�");
+            getInHospInfo("0", yesterdayZero, todayZero);
+            log.info("銆恉ealHisData銆戝叆闄㈡暟鎹噰闆嗗畬鎴�");
+            
+            // 閲囬泦鍑洪櫌鏁版嵁
+            log.info("銆恉ealHisData銆戝紑濮嬮噰闆嗗嚭闄㈡暟鎹�");
+            getInHospInfo("1", yesterdayZero, todayZero);
+            log.info("銆恉ealHisData銆戝嚭闄㈡暟鎹噰闆嗗畬鎴�");
+            
+            long endTime = System.currentTimeMillis();
+            log.info("銆恉ealHisData銆慔IS鏁版嵁閲囬泦浠诲姟瀹屾垚锛屾�昏�楁椂锛歿}ms", endTime - startTime);
+            
+        } catch (Exception e) {
+            log.error("銆恉ealHisData銆慔IS鏁版嵁閲囬泦寮傚父", e);
+            // 杩欓噷鍙互娣诲姞鍛婅閫氱煡閫昏緫锛屾瘮濡傚彂閫侀偖浠舵垨鐭俊
+        }
     }
 
     public void dealHisData2() {
-        //灏嗘偅鑰呬俊鎭�佸嚭闄€�侀棬璇婃暟鎹叏閮ㄩ噰闆�
+        //灏嗘偅鑰呬俊鎭�佸嚭鍏ラ櫌鏁版嵁鍏ㄩ儴閲囬泦
         // 鎸囧畾鐨勫紑濮嬫棩鏈�
         LocalDate endDate = LocalDate.of(2025, 3, 3);
         // 褰撳墠鏃ユ湡
@@ -245,11 +261,13 @@
      * 澶勭悊鎮h�呬俊鎭紝杩涘叆瀛愪换鍔¤〃
      */
     public void dealOutHospInfo() {
-        //鍑洪櫌琛�
-        iPatMedInhospService.dealOutHospInfo();
-        //闂ㄨ瘖琛�
-        if(visitHosp!=1) {
-            iPatMedOuthospService.dealOutpatientInfo();
+        if (port.equals("8095")) {
+            //鍑洪櫌琛�
+            iPatMedInhospService.dealOutHospInfo();
+            //闂ㄨ瘖琛�
+            if (visitHosp != 1) {
+                iPatMedOuthospService.dealOutpatientInfo();
+            }
         }
     }
 
@@ -258,22 +276,24 @@
      * 澶勭悊鎵嬫湳淇℃伅锛岃繘鍏ュ瓙浠诲姟琛�
      */
     public void dealOperationInfo() {
-        iPatMedOperationService.dealOperationInfo();
+        if (port.equals("8095")) iPatMedOperationService.dealOperationInfo();
+
     }
 
     /**
      * 澶勭悊鎮h�呬俊鎭紝杩涘叆瀛愪换鍔¤〃(寰俊灏忕▼搴�)
      */
     public void dealOutHospInfoXHC() {
-        PatMedInhosp patMedInhosp = new PatMedInhosp();
-        //鑾峰彇鏈鐞嗙殑鏁版嵁
-        List<ServicePatientTemp> servicePatientTemps = iServicePatientTempService.selectServicePatientTempList(new ServicePatientTemp());
-
+        if (port.equals("8095")) {
+            PatMedInhosp patMedInhosp = new PatMedInhosp();
+            //鑾峰彇鏈鐞嗙殑鏁版嵁
+            List<ServicePatientTemp> servicePatientTemps = iServicePatientTempService.selectServicePatientTempList(new ServicePatientTemp());
+        }
     }
 
 
     /**
-     * 闀挎湡浠诲姟鎵ц锛堢數璇濓級
+     * 闀挎湡浠诲姟鎵ц
      */
     public void longTaskSend() {
         //鑾峰彇浠诲姟淇℃伅
@@ -281,7 +301,7 @@
         st.setDelFlag("0");
         st.setLongTask(1);
         List<ServiceTask> serviceTasks = serviceTaskMapper.selectServiceTaskList(st);
-        log.error("serviceTasks杩涙潵浜嗗悧锛焮}", serviceTasks);
+        log.info("銆恖ongTaskSend銆戣幏鍙栧埌{}涓暱鏈熶换鍔�", serviceTasks.size());
         for (ServiceTask serviceTask : serviceTasks) {
             CommonTaskcallMQ commonTaskcallMQ = new CommonTaskcallMQ();
             commonTaskcallMQ.setTaskid(serviceTask.getTaskid());
@@ -298,34 +318,26 @@
      * @return
      */
     public boolean isDateToday(Date date) {
-        //濡傛灉缁欑殑鏃ユ湡灏忎簬褰撳墠鏃ユ湡锛屼篃鐩存帴鍙戦��
-        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
-        if (localDateTime.isBefore(LocalDateTime.now())) {
-            return true;
+        if (date == null) {
+            return false;
         }
-        // 鑾峰彇褰撳墠鏃ユ湡
-        Calendar calendarNow = Calendar.getInstance();
-        int todayYear = calendarNow.get(Calendar.YEAR);
-        int todayMonth = calendarNow.get(Calendar.MONTH);
-        int todayDay = calendarNow.get(Calendar.DAY_OF_MONTH);
-
-        // 鍒涘缓涓�涓� Calendar 瀹炰緥鐢ㄤ簬妫�鏌ョ殑鏃ユ湡
-        Calendar calendarToCheck = Calendar.getInstance();
-        calendarToCheck.setTime(date);
-
-        // 姣旇緝骞淬�佹湀鍜屾棩
-        return (calendarToCheck.get(Calendar.YEAR) == todayYear && calendarToCheck.get(Calendar.MONTH) == todayMonth && calendarToCheck.get(Calendar.DAY_OF_MONTH) == todayDay);
+        // 灏� Date 杞负 LocalDate
+        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
+        // 鑾峰彇浠婂ぉ鐨� LocalDate
+        LocalDate today = LocalDate.now();
+        // 濡傛灉鏃ユ湡鏃╀簬鎴栫瓑浜庝粖澶╋紝杩斿洖 true
+        return !localDate.isAfter(today);
     }
 
     public void sfHandlle(CommonTaskcallMQ commonTaskcallMQ, String ip) {
-        log.error("sfHandlle杩涙潵浜嗗悧锛焮}", commonTaskcallMQ);
+        log.info("銆恠fHandlle銆戝紑濮嬪鐞嗕换鍔★紝浠诲姟ID锛歿}", commonTaskcallMQ.getTaskid());
         //鍒ゆ柇涓�涓媍ommonTaskcallMQ涓殑stopstate鏄惁涓巌vr_task涓殑涓�鑷达紝涓嶄竴鑷达紝鍒欒鏄庢槸鏆傚仠浜�
         ServiceTask ivrTask1 = serviceTaskMapper.selectServiceTaskByTaskid(commonTaskcallMQ.getTaskid());
 
         if (StringUtils.isNotEmpty(commonTaskcallMQ.getPreachform())) {
             //濡備綍浠诲姟鍙戦�佹柟寮忎笉涓虹┖
             String[] split = commonTaskcallMQ.getPreachform().split(",");
-            log.error("split杩涙潵浜嗗悧锛焮}", split);
+            log.info("銆恠fHandlle銆戜换鍔″彂閫佹柟寮忥細{}", Arrays.toString(split));
             for (String serviceFrom : split) {
                 String descByCode = ServiceFromEnum.getDescByCode(Integer.valueOf(serviceFrom));
                 //閫氳繃浠诲姟ID鎷垮埌鎮h�呬俊鎭�
@@ -333,6 +345,15 @@
                 ivrTaskcall.setTaskid(commonTaskcallMQ.getTaskid());
                 ivrTaskcall.setSendstate(2L);
                 List<ServiceSubtask> subtaskList = iServiceSubtaskService.selectServiceSubtaskList(ivrTaskcall);
+                //涓芥按鏈夌殑鍖婚櫌杩樻病娉曞仛闀挎湡浠诲姟闅忚
+                List<ServiceSubtask> subtaskList2 = new ArrayList<>();
+                if (visitHosp == 2) {
+                    for (ServiceSubtask serviceSubtask : subtaskList) {
+                        Boolean b = VisitSendStateEnum.getDescByCode(serviceSubtask.getOrgid());
+                        if (b) subtaskList2.add(serviceSubtask);
+                    }
+                    subtaskList = subtaskList2;
+                }
 
                 // 鎸夌収 endtime 绂诲綋鍓嶆椂闂存渶杩戠殑鎺掑簭
                 List<ServiceSubtask> selectServiceSubtaskList = subtaskList.stream().sorted(Comparator.comparing(subtask -> Math.abs(subtask.getEndtime().getTime() - new Date().getTime()))).collect(Collectors.toList());
@@ -343,14 +364,14 @@
                     for (ServiceSubtask serviceSubtask : selectServiceSubtaskList) {
                         ServiceTask ivrTask = serviceTaskMapper.selectServiceTaskByTaskid(serviceSubtask.getTaskid());
                         if (ivrTask.getSendState() != null && ivrTask.getSendState() == 3 || ivrTask.getSendState() != null && ivrTask.getSendState() == 4) {
-                            //濡備綍浠诲姟琚�滄殏鍋溾�濇垨鈥滅粓姝⑩��
+                            //濡備綍浠诲姟琚�"鏆傚仠"鎴�"缁堟"
                             break;
                         }
                         if (org.apache.commons.lang3.StringUtils.isEmpty(serviceSubtask.getPhone())) {
                             serviceSubtask.setRemark("鎵嬫満鍙蜂负绌�");
                             serviceSubtask.setSendstate(4L);
                             serviceSubtask.setResult("error");
-                            serviceSubtask.setFinishtime(new Date());
+//                            serviceSubtask.setFinishtime(new Date());
                             serviceSubtaskMapper.updateServiceSubtask(serviceSubtask);
                             continue;
                         }
@@ -382,6 +403,7 @@
                     if (list.size() > 0) {
 //                        redisCache.setCacheListLeft("cache-0", list);
                         redisCache.setCacheListLeftAndDistinct("cache-0", list);
+                        log.info("銆恠fHandlle銆戠數璇濅换鍔″凡鍔犲叆闃熷垪锛屾暟閲忥細{}", list.size());
                     }
                 } else if (descByCode.equals("澶氬獟浣�")) {
                     //澶氬獟浣�
@@ -389,7 +411,7 @@
                     //绾歌川
                 } else if (descByCode.equals("寰俊灏忕▼搴�")) {
                     //寰俊灏忕▼搴�
-                    log.error("ivrTask1鐨勫�间负锛歿}", ivrTask1);
+                    log.info("銆恠fHandlle銆戝鐞嗗井淇″皬绋嬪簭浠诲姟锛屼换鍔′俊鎭細{}", ivrTask1);
                     if (ObjectUtils.isNotEmpty(ivrTask1) && ivrTask1.getPatCycle() == 1) {
                         //璇存槑璇ヤ换鍔$殑鎮h�呮槸寰幆鎵ц鐨�
                         ServiceSubtaskVO serviceSubtaskVO = new ServiceSubtaskVO();
@@ -434,7 +456,7 @@
                     RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
                     String taskId = rsaPublicKeyExample.encryptedData(ivrTask1.getTaskid().toString(), pub_key);
                     for (ServiceSubtask serviceSubtask : selectServiceSubtaskList) {
-                        log.error("鐭俊闀挎湡浠诲姟鍙戦��---------杩涙潵浜嗗悧锛焮}", serviceSubtask);
+                        log.info("銆恠fHandlle銆戝鐞嗙煭淇¢暱鏈熶换鍔★紝鎮h�匢D锛歿}", serviceSubtask.getPatid());
                         boolean dateToday = isDateToday(serviceSubtask.getLongSendTime());
                         if (dateToday) {
                             try {
@@ -445,7 +467,7 @@
                                     serviceSubtask.setResult("error");
                                     serviceSubtask.setRemark(patArchive.getNotrequiredreason());
                                     serviceSubtask.setSendstate(4L);
-                                    serviceSubtask.setFinishtime(new Date());
+//                                    serviceSubtask.setFinishtime(new Date());
                                     iServiceSubtaskService.insertServiceSubtask(serviceSubtask);
                                     continue;
                                 }
@@ -467,10 +489,36 @@
                                 sendMagParam.setPhone(serviceSubtask.getPhone());
                                 sendMagParam.setUrl(ip + ":" + req_path + "/wt?p=" + format);
                                 sendMagParam.setContent("銆愭柊鍗庡尰闄€�戞偍濂斤紝閭�璇锋偍濉啓鍑洪櫌璋冩煡琛紝璇风偣鍑�" + sendMagParam.getUrl() + "濉啓銆傛劅璋㈡偍閰嶅悎锛�");
-                                Map<String, String> req = new HashMap<>();
-                                req.put("phone", sendMagParam.getPhone());
-                                req.put("content", sendMagParam.getContent());
-                                String isSuccess = HttpUtil.postJsonRequest(xhsmsPath, new Gson().toJson(req));
+                                String isSuccess = null;
+                                if (visitHosp == 1) {
+                                    Map<String, String> req = new HashMap<>();
+                                    req.put("phone", sendMagParam.getPhone());
+                                    req.put("content", sendMagParam.getContent());
+                                    isSuccess = HttpUtil.postJsonRequest(xhsmsPath, new Gson().toJson(req));
+                                } else if (visitHosp == 2) {
+                                    //涓芥按鐨勭煭淇″彂閫佹柟寮�
+                                    Map<String, String> map = MsgLSEnum.getAddressByCode(serviceSubtask.getOrgid());
+                                    String token = LSHospTokenUtil.getToken(serviceSubtask.getOrgid());
+                                    if (ObjectUtils.isNotEmpty(map) && StringUtils.isEmpty(map.get("address"))) {
+                                        ServiceSubtask ss = new ServiceSubtask();
+                                        ss.setResult("error");
+                                        ss.setRemark("鐭俊鍙戦�佸け璐�,璇ユ満鏋勬病鏈夐厤缃煭淇″湴鍧�");
+                                        ss.setSendstate(5L);
+                                        ss.setId(serviceSubtask.getId());
+//                                        ss.setFinishtime(new Date());
+                                        serviceSubtaskMapper.updateServiceSubtask(ss);
+                                        continue;
+                                    }
+                                    log.info("銆恠fHandlle銆戜附姘寸煭淇″彂閫侊紝鍦板潃锛歿}锛屽弬鏁帮細{}锛屾満鏋処D锛歿}", map.get("address"), sendMagParam, serviceSubtask.getOrgid());
+                                    String dxCode = getDXCode(map.get("address"), sendMagParam.getPhone(), sendMagParam.getContent(), map.get("sendPersonId"), map.get("sendPersonName"), MsgLSEnum.getHeaderByCode(serviceSubtask.getOrgid()), token);
+                                    log.info("銆恠fHandlle銆戜附姘寸煭淇″彂閫佺粨鏋滐細{}", dxCode);
+                                    ObjectMapper objectMapper = new ObjectMapper();
+                                    Map<String, Object> textParam = objectMapper.readValue(dxCode, Map.class);
+                                    String code = textParam.get("Code").toString();
+                                    if (code.equals("0")) {
+                                        isSuccess = "true";
+                                    }
+                                }
                                 //浠诲姟鍙戦�佽褰�
                                 ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
                                 serviceSubtaskRecord.setTaskid(serviceSubtask.getTaskid().toString());
@@ -496,23 +544,23 @@
                                 iServiceSubtaskService.updateServiceSubtask(serviceSubtask);
                             } catch (Exception e) {
                                 String guid = UUID.randomUUID().toString();
-                                log.error("闀挎湡浠诲姟锛岀煭淇″彂閫佸け璐ュ師鍥狅細{},{}", e.getMessage(), guid);
+                                log.error("銆恠fHandlle銆戦暱鏈熶换鍔$煭淇″彂閫佸け璐ワ紝鍘熷洜锛歿}锛孏UID锛歿}", e.getMessage(), guid);
                                 serviceSubtask.setResult("error");
                                 serviceSubtask.setRemark("绯荤粺閿欒");
                                 serviceSubtask.setSendstate(5L);
-                                serviceSubtask.setFinishtime(new Date());
+//                                serviceSubtask.setFinishtime(new Date());
                                 serviceSubtask.setGuid(guid);
                                 iServiceSubtaskService.updateServiceSubtask(serviceSubtask);
                             }
                         }
                     }
                 } else if (descByCode.equals("鍏紬鍙�")) {
-                    log.error("鍏紬鍙疯繘鏉ヤ簡鍚楋紵{}", descByCode);
+                    log.info("銆恠fHandlle銆戝鐞嗗叕浼楀彿浠诲姟锛屼换鍔′俊鎭細{}", ivrTask1);
                     //鍏紬鍙�
                     RSAPublicKeyExample rsaPublicKeyExample = new RSAPublicKeyExample();
                     String taskId = rsaPublicKeyExample.encryptedData(ivrTask1.getTaskid().toString(), pub_key);
                     for (ServiceSubtask serviceSubtask : selectServiceSubtaskList) {
-                        log.error("serviceSubtask---------杩涙潵浜嗗悧锛焮}", serviceSubtask);
+                        //  log.error("serviceSubtask---------杩涙潵浜嗗悧锛焮}", serviceSubtask);
                         boolean dateToday = isDateToday(serviceSubtask.getLongSendTime());
                         if (dateToday) {
                             try {
@@ -523,16 +571,41 @@
                                     serviceSubtask.setResult("error");
                                     serviceSubtask.setRemark(patArchive.getNotrequiredreason());
                                     serviceSubtask.setSendstate(4L);
-                                    serviceSubtask.setFinishtime(new Date());
+//                                    serviceSubtask.setFinishtime(new Date());
                                     iServiceSubtaskService.insertServiceSubtask(serviceSubtask);
                                     continue;
                                 }
                                 String patid = rsaPublicKeyExample.encryptedData(serviceSubtask.getPatid().toString(), pub_key);
                                 String url = null;
                                 url = ip + ":" + req_path + "/outsideChainwt?param1=" + taskId + "&param2=" + patid + "&param3=" + URLEncoder.encode(ivrTask1.getTaskName(), StandardCharsets.UTF_8.toString()) + "&param5=false";
-                                String wxCode = getWXCode(serviceSubtask.getSfzh(), url, serviceSubtask.getTaskName(), serviceSubtask.getTaskDesc(), "鏃�");
-                                Map<String, Object> map = JSONObject.parseObject(wxCode, Map.class);
+//                                String wxCode = getWXCode(serviceSubtask.getSfzh(), url, serviceSubtask.getTaskName(), serviceSubtask.getTaskDesc(), "鏃�");
 
+                                //鑾峰彇寰俊鍏紬鍙疯姹備俊鎭牴鎹満鏋処D
+                                List<String> wxqqxx = WxGZHEnum.getDescByCode(patArchive.getOrgid());
+                                if (CollectionUtils.isEmpty(wxqqxx) || wxqqxx.size() < 4) {
+                                    ServiceSubtask ss = new ServiceSubtask();
+                                    ss.setResult("error");
+                                    ss.setRemark("璇ユ満鏋勭殑鍏紬鍙烽厤缃俊鎭笉鍏紝鏃犳硶閫氳繃鍏紬鍙峰彂閫�");
+                                    ss.setSendstate(5L);
+                                    ss.setId(serviceSubtask.getId());
+//                                    ss.setFinishtime(new Date());
+                                    serviceSubtaskMapper.updateServiceSubtask(ss);
+                                    continue;
+                                }
+
+                                if (StringUtils.isEmpty(patArchive.getPatidHis())) {
+                                    ServiceSubtask ss = new ServiceSubtask();
+                                    ss.setResult("error");
+                                    ss.setRemark("鍏紬鍙峰彂閫佸け璐�,his绯荤粺鐨勬偅鑰卛d涓虹┖");
+                                    ss.setSendstate(5L);
+                                    ss.setId(serviceSubtask.getId());
+//                                    ss.setFinishtime(new Date());
+                                    serviceSubtaskMapper.updateServiceSubtask(ss);
+                                    continue;
+                                }
+                                String wxCode = getWXCode(serviceSubtask.getSfzh(), url, serviceSubtask.getTaskName(), serviceSubtask.getTaskDesc(), patArchive.getTelcode(), serviceSubtask.getSendname(), patArchive.getPatidHis(), wxqqxx);
+
+                                Map<String, Object> map = JSONObject.parseObject(wxCode, Map.class);
                                 //浠诲姟鍙戦�佽褰�
                                 ServiceSubtaskRecord serviceSubtaskRecord = new ServiceSubtaskRecord();
                                 serviceSubtaskRecord.setTaskid(serviceSubtask.getTaskid().toString());
@@ -550,6 +623,7 @@
                                 if ((Boolean) map.get("succ") == true) {
                                     serviceSubtask.setResult("success");
                                 } else {
+                                    log.error("銆恠fHandlle銆戝叕浼楀彿鍙戦�佸け璐ワ紝鎮h�呬俊鎭細{}锛岄敊璇俊鎭細{}", serviceSubtask, map.get("msg"));
                                     serviceSubtask.setResult("error");
                                     serviceSubtask.setSendstate(5L);
                                     serviceSubtask.setRemark(map.get("msg").toString());
@@ -557,11 +631,11 @@
                                 iServiceSubtaskService.updateServiceSubtask(serviceSubtask);
                             } catch (Exception e) {
                                 String guid = UUID.randomUUID().toString();
-                                log.error("闀挎湡浠诲姟锛屽叕浼楀彿鍙戦�佸け璐ュ師鍥狅細{},{}", e.getMessage(), guid);
+                                log.error("銆恠fHandlle銆戦暱鏈熶换鍔″叕浼楀彿鍙戦�佸け璐ワ紝鍘熷洜锛歿}锛孏UID锛歿}", e.getMessage(), guid);
                                 serviceSubtask.setResult("error");
                                 serviceSubtask.setRemark("绯荤粺閿欒");
                                 serviceSubtask.setSendstate(5L);
-                                serviceSubtask.setFinishtime(new Date());
+//                                serviceSubtask.setFinishtime(new Date());
                                 serviceSubtask.setGuid(guid);
                                 iServiceSubtaskService.updateServiceSubtask(serviceSubtask);
                             }
@@ -575,186 +649,382 @@
 
 
     /**
-     * 鑾峰嚭闄㈡偅鑰呬俊鎭�
-     * cry    鍑哄叆闄細 0 鍏ラ櫌   1 鍑洪櫌
+     * 鑾峰彇鎮h�呭嚭鍏ラ櫌淇℃伅
+     * @param cry 鍑哄叆闄㈢被鍨嬶細0-鍏ラ櫌锛�1-鍑洪櫌
+     * @param startTime 寮�濮嬫椂闂�
+     * @param endTime 缁撴潫鏃堕棿
      */
     @Transactional
     public void getInHospInfo(String cry, LocalDateTime startTime, LocalDateTime endTime) {
-        // 瀹氫箟鏃ユ湡鏃堕棿鏍煎紡
+        String typeName = "0".equals(cry) ? "鍏ラ櫌" : "鍑洪櫌";
+        log.info("銆恎etInHospInfo銆戝紑濮嬭幏鍙杮}鎮h�呬俊鎭紝鏃堕棿鑼冨洿锛歿} ~ {}", typeName, startTime, endTime);
+        long startTimeMillis = System.currentTimeMillis();
+        
+        try {
+            // 绗竴姝ワ細鏋勫缓璇锋眰鍙傛暟骞惰皟鐢℉IS鎺ュ彛
+            List<ThiedInhospInfo> thiedInhospInfoList = fetchHisData(cry, startTime, endTime);
+            
+            if (CollectionUtils.isEmpty(thiedInhospInfoList)) {
+                log.info("銆恎etInHospInfo銆憑}鎮h�呬俊鎭负绌猴紝璺宠繃澶勭悊", typeName);
+                return;
+            }
+            
+            log.info("銆恎etInHospInfo銆戣幏鍙栧埌{}鏉}鎮h�呬俊鎭�", thiedInhospInfoList.size(), typeName);
+            
+            // 绗簩姝ワ細淇濆瓨鍘熷鏁版嵁鍒版枃浠讹紙鐢ㄤ簬澶囦唤锛�
+            saveRawDataToFile(cry, thiedInhospInfoList);
+            
+            // 绗笁姝ワ細澶勭悊鎮h�呮暟鎹�
+            processPatientData(thiedInhospInfoList, cry);
+            
+            long endTimeMillis = System.currentTimeMillis();
+            log.info("銆恎etInHospInfo銆憑}鎮h�呬俊鎭鐞嗗畬鎴愶紝鑰楁椂锛歿}ms", typeName, endTimeMillis - startTimeMillis);
+            
+        } catch (Exception e) {
+            log.error("銆恎etInHospInfo銆戣幏鍙杮}鎮h�呬俊鎭紓甯�", typeName, e);
+            throw new RuntimeException("鑾峰彇" + typeName + "鎮h�呬俊鎭け璐�", e);
+        }
+    }
+    
+    /**
+     * 绗竴姝ワ細璋冪敤HIS鎺ュ彛鑾峰彇鏁版嵁
+     */
+    private List<ThiedInhospInfo> fetchHisData(String cry, LocalDateTime startTime, LocalDateTime endTime) {
+        try {
+            // 鏋勫缓璇锋眰鍙傛暟
+            Map<String, String> requestParams = buildRequestParams(cry, startTime, endTime);
+            
+            // 鏋勫缓璇锋眰澶�
+            Map<String, String> headers = buildRequestHeaders(startTime);
+            
+            // 鍙戦�丠TTP璇锋眰
+            String result = HttpUtils.sendPostByHeader(hospInfoUrl, new Gson().toJson(requestParams), headers);
+            
+            // 瑙f瀽鍝嶅簲鏁版嵁
+            return parseResponseData(result);
+            
+        } catch (Exception e) {
+            log.error("銆恌etchHisData銆戣皟鐢℉IS鎺ュ彛寮傚父", e);
+            throw e;
+        }
+    }
+    
+    /**
+     * 鏋勫缓璇锋眰鍙傛暟
+     */
+    private Map<String, String> buildRequestParams(String cry, LocalDateTime startTime, LocalDateTime endTime) {
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
         Map<String, String> req = new HashMap<>();
-        if (cry.equals("1")) {
+        
+        if ("1".equals(cry)) {
+            // 鍑洪櫌鍙傛暟
             req.put("cashStartDate", startTime.format(formatter));
             req.put("cashEndDate", endTime.format(formatter));
             req.put("currStatus", "4");
-        } else if (cry.equals("0")) {
+        } else {
+            // 鍏ラ櫌鍙傛暟
             req.put("admissStartDate", startTime.format(formatter));
             req.put("admissEndDate", endTime.format(formatter));
             req.put("currStatus", "2");
         }
+        
+        return req;
+    }
+    
+    /**
+     * 鏋勫缓璇锋眰澶�
+     */
+    private Map<String, String> buildRequestHeaders(LocalDateTime startTime) {
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
         Map<String, String> header = new HashMap<>();
         header.put("x-hcsb-serviceno", "IS002347");
         header.put("x-hcsb-version", "2.0");
         header.put("x-hcsb-sourcesyscode", "14");
         header.put("x-hcsb-token", "ArGE2JnHtxG/Zx5nrnGY4eOfMUJGGJokAJHigG1BrCY=");
         header.put("x-hcsb-msgdate", startTime.format(formatter));
-        String result = HttpUtils.sendPostByHeader(hospInfoUrl, new Gson().toJson(req), header);
-        //鏁版嵁瑙f瀽
-        Gson gson = new Gson();
-        JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
-        JsonArray resultArray = jsonObject.getAsJsonObject("data").getAsJsonArray("result");
-        Type resultType = new TypeToken<List<ThiedInhospInfo>>() {
-        }.getType();
-        List<ThiedInhospInfo> thiedInhospInfoList = gson.fromJson(resultArray, resultType);
-
-        // 鍒涘缓涓�涓柊鏂囦欢
-        String filename = null;
-        if (cry.equals("0")) filename = "鍏ラ櫌淇℃伅" + System.currentTimeMillis();
-        if (cry.equals("1")) filename = "鍑洪櫌淇℃伅" + System.currentTimeMillis();
-        File file = new File("D:\\public\\HIS鏁版嵁閲囬泦\\" + filename);
-        if (!file.exists()) {
-            // 濡傛灉鏂囦欢涓嶅瓨鍦紝鍒欏垱寤烘柊鏂囦欢
-            try {
-                if (file.createNewFile()) {
-                    System.out.println("鎴愬姛鍒涘缓浜嗘柊鏂囦欢!");
-                } else {
-                    System.out.println("澶辫触锛佹枃浠舵棤娉曞垱寤猴紒");
-                }
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
-        // 灏嗘暟鎹啓鍏ュ埌鏂囦欢涓�
+        return header;
+    }
+    
+    /**
+     * 瑙f瀽鍝嶅簲鏁版嵁
+     */
+    private List<ThiedInhospInfo> parseResponseData(String result) {
         try {
-            FileWriter writer = new FileWriter(file);
-            writer.write(result);
-        } catch (IOException e) {
-            e.printStackTrace();
+            Gson gson = new Gson();
+            JsonObject jsonObject = gson.fromJson(result, JsonObject.class);
+            JsonArray resultArray = jsonObject.getAsJsonObject("data").getAsJsonArray("result");
+            Type resultType = new TypeToken<List<ThiedInhospInfo>>() {}.getType();
+            return gson.fromJson(resultArray, resultType);
+        } catch (Exception e) {
+            log.error("銆恜arseResponseData銆戣В鏋愬搷搴旀暟鎹紓甯革紝鍘熷鏁版嵁锛歿}", result, e);
+            throw e;
         }
-
-        if (CollectionUtils.isNotEmpty(thiedInhospInfoList)) {
-            for (ThiedInhospInfo thiedInhospInfo : thiedInhospInfoList) {
-                //閫氳繃韬唤璇佸彿鏌ヨ璇ユ偅鑰呭湪鎮h�呰〃鏄惁瀛樺湪
-                PatArchive patArchive = new PatArchive();
-                List<PatArchive> patArchives = null;
-                patArchive.setIdcardno(thiedInhospInfo.getPatiIdCardNo().trim());
-                if (StringUtils.isNotEmpty(thiedInhospInfo.getPatiIdCardNo().trim())) {
-                    patArchives = patArchiveService.selectPatArchiveList(patArchive);
+    }
+    
+    /**
+     * 绗簩姝ワ細淇濆瓨鍘熷鏁版嵁鍒版枃浠�
+     */
+    private void saveRawDataToFile(String cry, List<ThiedInhospInfo> dataList) {
+        try {
+            String typeName = "0".equals(cry) ? "鍏ラ櫌淇℃伅" : "鍑洪櫌淇℃伅";
+            String filename = typeName + "_" + System.currentTimeMillis() + ".json";
+            String filePath = "D:\\public\\HIS鏁版嵁閲囬泦\\" + filename;
+            
+            File file = new File(filePath);
+            File parentDir = file.getParentFile();
+            if (!parentDir.exists()) {
+                parentDir.mkdirs();
+            }
+            
+            if (!file.exists()) {
+                file.createNewFile();
+            }
+            
+            // 灏嗘暟鎹啓鍏ユ枃浠�
+            try (FileWriter writer = new FileWriter(file)) {
+                writer.write(new Gson().toJson(dataList));
+            }
+            
+            log.info("銆恠aveRawDataToFile銆戝師濮嬫暟鎹凡淇濆瓨鍒版枃浠讹細{}", filePath);
+            
+        } catch (Exception e) {
+            log.error("銆恠aveRawDataToFile銆戜繚瀛樺師濮嬫暟鎹埌鏂囦欢寮傚父", e);
+            // 鏂囦欢淇濆瓨澶辫触涓嶅奖鍝嶄富娴佺▼锛屽彧璁板綍鏃ュ織
+        }
+    }
+    
+    /**
+     * 绗笁姝ワ細澶勭悊鎮h�呮暟鎹�
+     */
+    private void processPatientData(List<ThiedInhospInfo> thiedInhospInfoList, String cry) {
+        int successCount = 0;
+        int skipCount = 0;
+        int errorCount = 0;
+        
+        for (ThiedInhospInfo thiedInhospInfo : thiedInhospInfoList) {
+            try {
+                if (processSinglePatient(thiedInhospInfo, cry)) {
+                    successCount++;
                 } else {
-                    //鎮h�呬俊鎭负绌猴紝鐩存帴 涓嬩竴涓�
-                    continue;
+                    skipCount++;
                 }
-                if (CollectionUtils.isEmpty(patArchives) || patArchives.size() == 0) {
-                    patArchive.setPatientno(thiedInhospInfo.getPatiMediaNo());
-                    patArchive.setSourcefrom(2);
-                    patArchive.setPattype("2");
-                    if (StringUtils.isNotEmpty(thiedInhospInfo.getOutWayId()) && thiedInhospInfo.getOutWayId().equals("4")) {
-                        //濡傛灉涓�4灏辨槸姝讳骸
-                        patArchive.setNotrequiredFlag("1");
-                        patArchive.setNotrequiredreason(thiedInhospInfo.getOutWayName());
-                    }
-                    if (cry.equals("1") && StringUtils.isEmpty(thiedInhospInfo.getOutWayId())) {
-                        //濡傛灉涓�4灏辨槸姝讳骸
-                        patArchive.setNotrequiredFlag("1");
-                        patArchive.setNotrequiredreason("鍑洪櫌鏂瑰紡涓虹┖");
-                    }
-                    patArchive.setSex(thiedInhospInfo.getPatiRecordGender().equals("鐢�") ? 1L : 2L);
-                    patArchive.setNation(thiedInhospInfo.getPatiNation());
-                    patArchive.setNativePlace(thiedInhospInfo.getPatiNationality());
-                    patArchive.setPlaceOfResidence(StringUtils.isNotEmpty(thiedInhospInfo.getPatiHomeAddr()) ? thiedInhospInfo.getPatiHomeAddr().replace("null", "") : "");
-                    try {
-                        if (StringUtils.isNotEmpty(thiedInhospInfo.getPatiBirthday()))
-                            patArchive.setBirthdate(new SimpleDateFormat("yyyy-MM-dd").parse(thiedInhospInfo.getPatiBirthday()));
-                    } catch (ParseException e) {
-                        e.printStackTrace();
-                    }
-                    if (thiedInhospInfo.getTreateAge() != null) {
-                        patArchive.setAge(Long.valueOf(thiedInhospInfo.getTreateAge()));
-                    } else {
-                        patArchive.setAge(null);
-                    }
-                    patArchive.setAgeUnit(thiedInhospInfo.getTreatAgeunit());
-                    patArchive.setTelcode(thiedInhospInfo.getCompanyTelNum());
-                    patArchive.setRelativetelcode(thiedInhospInfo.getContactPersonPhone());
-                    patArchive.setDelFlag("0");
-                    patArchive.setCreateTime(new Date());
-                    patArchive.setUpdateTime(new Date());
-                    patArchive.setName(thiedInhospInfo.getPatiRecordName());
-                    patArchiveService.insertPatArchive(patArchive);
-                } else {
-                    patArchive = patArchives.get(0);
-                }
-
-                log.error("鍏ュ弬鐨刾atArchive锛歿}", patArchive);
-                PatMedInhosp patMedInhosp = new PatMedInhosp();
-                patMedInhosp.setPatid(patArchive.getId());
-                patMedInhosp.setPatno(patArchive.getPatientno());
-                patMedInhosp.setPatname(patArchive.getName());
-                patMedInhosp.setNurseId(thiedInhospInfo.getNurseId());
-                patMedInhosp.setNurseName(thiedInhospInfo.getNurseName());
-                patMedInhosp.setSerialnum(thiedInhospInfo.getInpatientId());
-                patMedInhosp.setFuflag("1");
-                if (cry.equals("0")) {
-                    patMedInhosp.setInhospstate("0");
-                } else if (cry.equals("1")) {
-                    patMedInhosp.setInhospstate("1");
-                }
-                patMedInhosp.setHospitalcode(thiedInhospInfo.getAreaId());
-                patMedInhosp.setIcd10code(thiedInhospInfo.getDiagIcd10());
-                patMedInhosp.setDiagname(thiedInhospInfo.getDiagName());
-                try {
-                    if (StringUtils.isNotEmpty(thiedInhospInfo.getAdmissDate()))
-                        patMedInhosp.setStarttime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(thiedInhospInfo.getAdmissDate()));
-                } catch (ParseException e) {
-                    e.printStackTrace();
-                }
-                try {
-                    if (StringUtils.isNotEmpty(thiedInhospInfo.getOutDate()))
-                        patMedInhosp.setEndtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(thiedInhospInfo.getOutDate()));
-                } catch (ParseException e) {
-                    e.printStackTrace();
-                }
-                patMedInhosp.setDeptcode(thiedInhospInfo.getAdmissDeptId());
-                patMedInhosp.setDeptid(thiedInhospInfo.getAdmissDeptId());
-                patMedInhosp.setDeptname(thiedInhospInfo.getAdmissDeptName());
-                patMedInhosp.setBedNo(thiedInhospInfo.getAdmissBedNo());
-                patMedInhosp.setDelFlag("0");
-//                patMedInhosp.setLeaveldeptcode(thiedInhospInfo.getCurrDeptCode());
-                patMedInhosp.setLeaveldeptcode(thiedInhospInfo.getCurrDeptId());
-                patMedInhosp.setLeaveldeptname(thiedInhospInfo.getCurrDeptName());
-                patMedInhosp.setLeavediagname(thiedInhospInfo.getDiagName());
-                patMedInhosp.setLeaveicd10code(thiedInhospInfo.getDiagIcd10());
-                patMedInhosp.setDrname(thiedInhospInfo.getDoctName());
-                patMedInhosp.setDrcode(thiedInhospInfo.getDoctId());
-                patMedInhosp.setOutWayId(thiedInhospInfo.getOutWayId());
-                patMedInhosp.setOutWayName(thiedInhospInfo.getOutWayName());
-                if (cry.equals("0")) {
-                    patMedInhosp.setSchemestatus(1L);
-                } else if (cry.equals("1")) {
-                    patMedInhosp.setSchemestatus(2L);
-                }
-                patMedInhosp.setLeavehospitaldistrictid(thiedInhospInfo.getCurrWardCode());
-                patMedInhosp.setLeavehospitaldistrictcode(thiedInhospInfo.getCurrWardId());
-                patMedInhosp.setLeavehospitaldistrictname(thiedInhospInfo.getCurrWardName());
-                patMedInhosp.setLeaveldeptid(thiedInhospInfo.getCurrDeptCode());
-                patMedInhosp.setOrgid("1");
-                //濡傛灉鏄嚭闄紝鍏堥�氳繃鎮h�呯紪鍙峰拰娴佹按鍙峰幓鏌ヤ竴涓嬶紝鎮h�呮槸鍚﹀瓨鍦紝濡傛灉瀛樺湪锛屽垯杩涜淇敼
-                PatMedInhosp inhosp = new PatMedInhosp();
-                inhosp.setPatno(patArchive.getPatientno());
-                inhosp.setSerialnum(patMedInhosp.getSerialnum());
-                List<PatMedInhosp> patMedInhosps = patMedInhospService.selectPatMedInhospList(inhosp);
-                log.error("鏄惁闇�瑕佹洿鏂帮細{}", patMedInhosps.size());
-                if (CollectionUtils.isNotEmpty(patMedInhosps)) {
-                    patMedInhosp.setInhospid(patMedInhosps.get(0).getInhospid());
-                    log.error("------鎮h�呭嚭鍏ラ櫌淇敼淇℃伅锛歿}", patMedInhosp);
-                    patMedInhospService.updatePatMedInhosp(patMedInhosp);
-                } else {
-                    log.error("----鎮h�呭嚭鍏ラ櫌鏂板淇℃伅锛歿}", patMedInhosp);
-                    patMedInhospService.insertPatMedInhosp(patMedInhosp);
-                }
-
+            } catch (Exception e) {
+                errorCount++;
+                log.error("銆恜rocessPatientData銆戝鐞嗘偅鑰呮暟鎹紓甯革紝鎮h�匢D锛歿}", 
+                    thiedInhospInfo.getPatiMediaNo(), e);
             }
         }
+        
+        log.info("銆恜rocessPatientData銆戞偅鑰呮暟鎹鐞嗗畬鎴愶紝鎴愬姛锛歿}锛岃烦杩囷細{}锛屽け璐ワ細{}", 
+            successCount, skipCount, errorCount);
+    }
+    
+    /**
+     * 澶勭悊鍗曚釜鎮h�呮暟鎹�
+     */
+    private boolean processSinglePatient(ThiedInhospInfo thiedInhospInfo, String cry) {
+        // 楠岃瘉韬唤璇佸彿
+        if (StringUtils.isEmpty(thiedInhospInfo.getPatiIdCardNo())) {
+            log.warn("銆恜rocessSinglePatient銆戞偅鑰呰韩浠借瘉鍙蜂负绌猴紝璺宠繃澶勭悊锛屾偅鑰呯紪鍙凤細{}", 
+                thiedInhospInfo.getPatiMediaNo());
+            return false;
+        }
+        
+        // 澶勭悊鎮h�呮。妗堜俊鎭�
+        PatArchive patArchive = processPatientArchive(thiedInhospInfo, cry);
+        
+        // 澶勭悊鎮h�呬綇闄俊鎭�
+        processPatientInhospInfo(thiedInhospInfo, patArchive, cry);
+        
+        return true;
+    }
+    
+    /**
+     * 澶勭悊鎮h�呮。妗堜俊鎭�
+     */
+    private PatArchive processPatientArchive(ThiedInhospInfo thiedInhospInfo, String cry) {
+        // 鏌ヨ鎮h�呮槸鍚﹀凡瀛樺湪
+        PatArchive queryArchive = new PatArchive();
+        queryArchive.setIdcardno(thiedInhospInfo.getPatiIdCardNo().trim());
+        List<PatArchive> existingArchives = patArchiveService.selectPatArchiveList(queryArchive);
+        
+        // 鏋勫缓鎮h�呮。妗堜俊鎭�
+        PatArchive patArchive = buildPatientArchive(thiedInhospInfo, cry);
+        
+        // 淇濆瓨鎴栨洿鏂版偅鑰呮。妗�
+        if (CollectionUtils.isEmpty(existingArchives)) {
+            patArchiveService.insertPatArchive(patArchive);
+            log.debug("銆恜rocessPatientArchive銆戞柊澧炴偅鑰呮。妗堬紝鎮h�呯紪鍙凤細{}", patArchive.getPatientno());
+        } else {
+            patArchive.setId(existingArchives.get(0).getId());
+            patArchiveService.updateArchive(patArchive);
+            log.debug("銆恜rocessPatientArchive銆戞洿鏂版偅鑰呮。妗堬紝鎮h�呯紪鍙凤細{}", patArchive.getPatientno());
+        }
+        
+        return patArchive;
+    }
+    
+    /**
+     * 鏋勫缓鎮h�呮。妗堜俊鎭�
+     */
+    private PatArchive buildPatientArchive(ThiedInhospInfo thiedInhospInfo, String cry) {
+        PatArchive patArchive = new PatArchive();
+        
+        // 鍩烘湰淇℃伅
+        patArchive.setPatientno(thiedInhospInfo.getPatiMediaNo());
+        patArchive.setIdcardno(thiedInhospInfo.getPatiIdCardNo().trim());
+        patArchive.setName(thiedInhospInfo.getPatiRecordName());
+        patArchive.setSourcefrom(2);
+        patArchive.setPattype("2");
+        
+        // 鎬у埆
+        patArchive.setSex("鐢�".equals(thiedInhospInfo.getPatiRecordGender()) ? 1L : 2L);
+        
+        // 姘戞棌鍜岀睄璐�
+        patArchive.setNation(thiedInhospInfo.getPatiNation());
+        patArchive.setNativePlace(thiedInhospInfo.getPatiNationality());
+        
+        // 灞呬綇鍦板潃
+        String homeAddr = thiedInhospInfo.getPatiHomeAddr();
+        patArchive.setPlaceOfResidence(StringUtils.isNotEmpty(homeAddr) ? homeAddr.replace("null", "") : "");
+        
+        // 鍑虹敓鏃ユ湡
+        if (StringUtils.isNotEmpty(thiedInhospInfo.getPatiBirthday())) {
+            try {
+                patArchive.setBirthdate(new SimpleDateFormat("yyyy-MM-dd").parse(thiedInhospInfo.getPatiBirthday()));
+            } catch (ParseException e) {
+                log.warn("銆恇uildPatientArchive銆戣В鏋愬嚭鐢熸棩鏈熷け璐ワ細{}", thiedInhospInfo.getPatiBirthday(), e);
+            }
+        }
+        
+        // 骞撮緞
+        if (thiedInhospInfo.getTreateAge() != null) {
+            patArchive.setAge(Long.valueOf(thiedInhospInfo.getTreateAge()));
+        }
+        patArchive.setAgeUnit(thiedInhospInfo.getTreatAgeunit());
+        
+        // 鑱旂郴鏂瑰紡
+        patArchive.setTelcode(thiedInhospInfo.getCompanyTelNum());
+        patArchive.setRelativetelcode(thiedInhospInfo.getContactPersonPhone());
+        
+        // 鍑洪櫌鏂瑰紡澶勭悊锛堟浜$瓑鐗规畩鎯呭喌锛�
+        if (StringUtils.isNotEmpty(thiedInhospInfo.getOutWayId()) && "4".equals(thiedInhospInfo.getOutWayId())) {
+            patArchive.setNotrequiredFlag("1");
+            patArchive.setNotrequiredreason(thiedInhospInfo.getOutWayName());
+        }
+        if ("1".equals(cry) && StringUtils.isEmpty(thiedInhospInfo.getOutWayId())) {
+            patArchive.setNotrequiredFlag("1");
+            patArchive.setNotrequiredreason("鍑洪櫌鏂瑰紡涓虹┖");
+        }
+        
+        // 绯荤粺瀛楁
+        patArchive.setDelFlag("0");
+        patArchive.setCreateTime(new Date());
+        patArchive.setUpdateTime(new Date());
+        
+        return patArchive;
+    }
+    
+    /**
+     * 澶勭悊鎮h�呬綇闄俊鎭�
+     */
+    private void processPatientInhospInfo(ThiedInhospInfo thiedInhospInfo, PatArchive patArchive, String cry) {
+        // 鏋勫缓浣忛櫌淇℃伅
+        PatMedInhosp patMedInhosp = buildPatientInhospInfo(thiedInhospInfo, patArchive, cry);
+        
+        // 鏌ヨ鏄惁宸插瓨鍦ㄤ綇闄㈣褰�
+        PatMedInhosp queryInhosp = new PatMedInhosp();
+        queryInhosp.setPatno(patArchive.getPatientno());
+        queryInhosp.setSerialnum(patMedInhosp.getSerialnum());
+        List<PatMedInhosp> existingInhosps = patMedInhospService.selectPatMedInhospList(queryInhosp);
+        
+        // 淇濆瓨鎴栨洿鏂颁綇闄俊鎭�
+        if (CollectionUtils.isNotEmpty(existingInhosps)) {
+            patMedInhosp.setInhospid(existingInhosps.get(0).getInhospid());
+            patMedInhospService.updatePatMedInhosp(patMedInhosp);
+            log.debug("銆恜rocessPatientInhospInfo銆戞洿鏂颁綇闄俊鎭紝鎮h�呯紪鍙凤細{}锛屾祦姘村彿锛歿}", 
+                patArchive.getPatientno(), patMedInhosp.getSerialnum());
+        } else {
+            patMedInhospService.insertPatMedInhosp(patMedInhosp);
+            log.debug("銆恜rocessPatientInhospInfo銆戞柊澧炰綇闄俊鎭紝鎮h�呯紪鍙凤細{}锛屾祦姘村彿锛歿}", 
+                patArchive.getPatientno(), patMedInhosp.getSerialnum());
+        }
+    }
+    
+    /**
+     * 鏋勫缓鎮h�呬綇闄俊鎭�
+     */
+    private PatMedInhosp buildPatientInhospInfo(ThiedInhospInfo thiedInhospInfo, PatArchive patArchive, String cry) {
+        PatMedInhosp patMedInhosp = new PatMedInhosp();
+        
+        // 鎮h�呭熀鏈俊鎭�
+        patMedInhosp.setPatid(patArchive.getId());
+        patMedInhosp.setPatno(patArchive.getPatientno());
+        patMedInhosp.setPatname(patArchive.getName());
+        
+        // 鎶ょ悊淇℃伅
+        patMedInhosp.setNurseId(thiedInhospInfo.getNurseId());
+        patMedInhosp.setNurseName(thiedInhospInfo.getNurseName());
+        
+        // 浣忛櫌淇℃伅
+        patMedInhosp.setSerialnum(thiedInhospInfo.getInpatientId());
+        patMedInhosp.setFuflag("1");
+        patMedInhosp.setInhospstate("0".equals(cry) ? "0" : "1");
+        
+        // 鍖婚櫌鍜岀瀹や俊鎭�
+        patMedInhosp.setHospitalcode(thiedInhospInfo.getAreaId());
+        patMedInhosp.setDeptcode(thiedInhospInfo.getAdmissDeptId());
+        patMedInhosp.setDeptid(thiedInhospInfo.getAdmissDeptId());
+        patMedInhosp.setDeptname(thiedInhospInfo.getAdmissDeptName());
+        patMedInhosp.setBedNo(thiedInhospInfo.getAdmissBedNo());
+        
+        // 璇婃柇淇℃伅
+        patMedInhosp.setIcd10code(thiedInhospInfo.getDiagIcd10());
+        patMedInhosp.setDiagname(thiedInhospInfo.getDiagName());
+        
+        // 鏃堕棿淇℃伅
+        if (StringUtils.isNotEmpty(thiedInhospInfo.getAdmissDate())) {
+            try {
+                patMedInhosp.setStarttime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(thiedInhospInfo.getAdmissDate()));
+            } catch (ParseException e) {
+                log.warn("銆恇uildPatientInhospInfo銆戣В鏋愬叆闄㈡椂闂村け璐ワ細{}", thiedInhospInfo.getAdmissDate(), e);
+            }
+        }
+        
+        if (StringUtils.isNotEmpty(thiedInhospInfo.getOutDate())) {
+            try {
+                patMedInhosp.setEndtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(thiedInhospInfo.getOutDate()));
+            } catch (ParseException e) {
+                log.warn("銆恇uildPatientInhospInfo銆戣В鏋愬嚭闄㈡椂闂村け璐ワ細{}", thiedInhospInfo.getOutDate(), e);
+            }
+        }
+        
+        // 鍑洪櫌淇℃伅
+        patMedInhosp.setLeaveldeptcode(thiedInhospInfo.getCurrDeptId());
+        patMedInhosp.setLeaveldeptname(thiedInhospInfo.getCurrDeptName());
+        patMedInhosp.setLeavediagname(thiedInhospInfo.getDiagName());
+        patMedInhosp.setLeaveicd10code(thiedInhospInfo.getDiagIcd10());
+        patMedInhosp.setOutWayId(thiedInhospInfo.getOutWayId());
+        patMedInhosp.setOutWayName(thiedInhospInfo.getOutWayName());
+        
+        // 鍖荤敓淇℃伅
+        patMedInhosp.setDrname(thiedInhospInfo.getDoctName());
+        patMedInhosp.setDrcode(thiedInhospInfo.getDoctId());
+        
+        // 鐥呭尯淇℃伅
+        patMedInhosp.setLeavehospitaldistrictid(thiedInhospInfo.getCurrWardCode());
+        patMedInhosp.setLeavehospitaldistrictcode(thiedInhospInfo.getCurrWardId());
+        patMedInhosp.setLeavehospitaldistrictname(thiedInhospInfo.getCurrWardName());
+        patMedInhosp.setLeaveldeptid(thiedInhospInfo.getCurrDeptCode());
+        
+        // 鐘舵�佷俊鎭�
+        patMedInhosp.setSchemestatus("0".equals(cry) ? 1L : 2L);
+        
+        // 绯荤粺瀛楁
+        patMedInhosp.setDelFlag("0");
+        patMedInhosp.setOrgid("1");
+        
+        return patMedInhosp;
     }
 
 
@@ -786,26 +1056,72 @@
         return key;
     }
 
-    private String getWXCode(String idcard, String url, String taskName, String taskDesc, String submitprompt) {
+    private String getDXCode(String address, String ShouJiHM, String FaSongNR, String FaSongRID, String FaSongRXM, Map<String, Object> headerMap, String token) {
+        Map<String, Object> map = new LinkedHashMap<>();
+
+        Map<String, Object> YeWuXX = new LinkedHashMap<>();
+        Map<String, Object> DuanXinXX = new LinkedHashMap<>();
+        DuanXinXX.put("ShouJiHM", ShouJiHM);
+        DuanXinXX.put("FaSongNR", FaSongNR);
+        DuanXinXX.put("FaSongRID", FaSongRID);
+        DuanXinXX.put("FaSongRXM", FaSongRXM);
+        YeWuXX.put("DuanXinXX", DuanXinXX);
+
+        map.put("XiaoXiTou", headerMap);
+        map.put("YeWuXX", YeWuXX);
+
+        String body = new Gson().toJson(map);
+
+//        String result = HttpUtils.sendPost(address, body);
+        String result = null;
+        try {
+            result = OkHttpExample.sendPostRequest(address, body, token);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        log.info("銆恎etDXCode銆戠煭淇″彂閫佺粨鏋滐細{}", result);
+
+        JSONObject jsonObject = JSONObject.parseObject(result);
+        String code = (String) jsonObject.toString();
+        return code;
+    }
+
+
+    private String getWXCode(String idcard, String url, String taskName, String taskDesc, String phone, String setPatientname, String setPatientid, List<String> wxqqxx) {
         XinHuaWXReq xinHuaWXReq = new XinHuaWXReq();
         xinHuaWXReq.setIdcard(idcard);
         xinHuaWXReq.setUrl(url);
-        xinHuaWXReq.setKeyword1("娴欎腑鍖诲ぇ浜岄櫌鍑洪櫌闅忚");
-        xinHuaWXReq.setKeyword2(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
-        xinHuaWXReq.setKeyword3(taskName);
-        xinHuaWXReq.setKeyword4("鐤剧梾搴峰娉ㄦ剰浜嬮」");
+        xinHuaWXReq.setPatientid(setPatientid);
+        xinHuaWXReq.setPatientname(setPatientname);
+        xinHuaWXReq.setMobile(phone);
+        xinHuaWXReq.setMedcardno(null);
+        xinHuaWXReq.setTitlename(taskName);
+        xinHuaWXReq.setContent(taskDesc);
+        xinHuaWXReq.setAppid(wxqqxx.get(1));
+        log.info("銆恎etWXCode銆戝井淇″叕浼楀彿璇锋眰鍙傛暟锛歿}", xinHuaWXReq);
+        String body = new Gson().toJson(xinHuaWXReq);
+        String encode = encode(wxqqxx.get(2).concat(body));
+        Map<String, String> headerMap = new HashMap<>();
+        headerMap.put("sign", encode);
+        String result = HttpUtils.sendPostByHeader(wxqqxx.get(3), body, headerMap);
 
-        String result = HttpUtils.sendPost(thirdWXUrl, new Gson().toJson(xinHuaWXReq));
         JSONObject jsonObject = JSONObject.parseObject(result);
-        log.error("jsonObject鐨勬暟鎹负锛歿}", jsonObject.toString());
-//        String code = (String) jsonObject.get("code");
-        return jsonObject.toString();
+        String code = (String) jsonObject.toString();
+        return code;
     }
 
-//    public static void main(String[] args) {
-//        LocalDateTime originalDateTime = LocalDateTime.now();
-//        LocalDateTime startOfDay = originalDateTime.with(LocalTime.MIN);
-//        System.out.println("Original DateTime: " + originalDateTime);
-//        System.out.println("Start of Day: " + startOfDay);
-//    }
+
+    public String encode(String arg) {
+        if (arg == null) {
+            arg = "";
+        }
+        MessageDigest md5 = null;
+        try {
+            md5 = MessageDigest.getInstance("MD5");
+            md5.update(arg.getBytes("UTF-8"));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return toHex(md5.digest());
+    }
 }

--
Gitblit v1.9.3