From 10354e63eb3298beb9ebcc029dd9f48d8936a272 Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期五, 19 十二月 2025 23:15:05 +0800
Subject: [PATCH] feat:优化同步

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TaskSyncUtilService.java |  370 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 370 insertions(+), 0 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TaskSyncUtilService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TaskSyncUtilService.java
new file mode 100644
index 0000000..8c18cea
--- /dev/null
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TaskSyncUtilService.java
@@ -0,0 +1,370 @@
+package com.ruoyi.system.service.impl;
+
+import com.ruoyi.common.config.LegacySystemConfig;
+import com.ruoyi.common.core.domain.entity.SysDept;
+import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.system.domain.SysTask;
+import com.ruoyi.system.domain.SysTaskEmergency;
+import com.ruoyi.system.service.ISysDeptService;
+import com.ruoyi.system.service.ISysUserService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.security.cert.X509Certificate;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+@Slf4j
+@Service
+public class TaskSyncUtilService {
+
+    @Autowired
+    private ISysUserService sysUserService;
+
+    @Autowired
+    private ISysDeptService sysDeptService;
+
+    @Autowired
+    private LegacySystemConfig legacySystemConfig;
+
+    /**
+     * 鍒ゆ柇鐢ㄦ埛绫诲瀷锛堝徃鏈�/鍖荤敓/鎶ゅ+/鍏朵粬锛�
+     * 鏍规嵁鐢ㄦ埛鐨勮鑹插悕绉版潵鍒ゆ柇
+     *
+     * @param user 鐢ㄦ埛淇℃伅
+     * @return 鐢ㄦ埛绫诲瀷锛歞river/doctor/nurse/other
+     */
+    public String getUserType(SysUser user) {
+        String roleName = "";
+
+        // 浠庣敤鎴风殑瑙掕壊鍒楄〃涓幏鍙栬鑹插悕绉�
+        if (user.getRoles() != null && !user.getRoles().isEmpty()) {
+            roleName = user.getRoles().get(0).getRoleName();
+        }
+
+        // 鍒ゆ柇鏄惁涓哄徃鏈�
+        if (roleName != null && roleName.contains("鍙告満")) {
+            return "driver";
+        }
+        // 鍒ゆ柇鏄惁涓哄尰鐢�
+        if (roleName != null && roleName.contains("鍖荤敓")) {
+            return "doctor";
+        }
+        // 鍒ゆ柇鏄惁涓烘姢澹�
+        if (roleName != null && roleName.contains("鎶ゅ+")) {
+            return "nurse";
+        }
+
+        // 鍏朵粬绫诲瀷锛岄粯璁や负鍙告満锛堜繚璇佽嚦灏戞湁涓�涓汉鍛橈級
+        log.warn("鐢ㄦ埛瑙掕壊鏃犳硶鍒ゆ柇绫诲瀷锛岄粯璁や负鍙告満锛岀敤鎴稩D: {}, 瑙掕壊: {}", user.getUserId(), roleName);
+        return "driver";
+    }
+    public String getAdminID(SysTask task) {
+        String adminID="";
+        if (task.getCreatorId() != null) {
+            try {
+                SysUser creator = sysUserService.selectUserById(task.getCreatorId());
+                if (creator != null && creator.getOaUserId() != null) {
+                    adminID = creator.getOaUserId().toString();
+                    log.info("鑾峰彇鍒涘缓浜篛A_UserID鎴愬姛锛岀敤鎴稩D: {}, OA_UserID: {}", task.getCreatorId(), adminID);
+                } else {
+                    log.warn("鍒涘缓浜烘湭閰嶇疆OA_UserID锛岀敤鎴稩D: {}", task.getCreatorId());
+                }
+            } catch (Exception e) {
+                log.error("鏌ヨ鍒涘缓浜篛A_UserID寮傚父锛岀敤鎴稩D: {}", task.getCreatorId(), e);
+            }
+        }
+        return adminID;
+    }
+
+    /**
+     * 鏋勫缓鍚屾鍙傛暟
+     */
+    public Map<String, Object> buildSyncParams(SysTask task, SysTaskEmergency emergency) {
+        Map<String, Object> params = new HashMap<>();
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
+        Long taskId=task.getTaskId();
+        // 绠$悊鍛業D锛堝垱寤轰汉ID瀵瑰簲鐨凮A_UserID锛�
+        String adminID = this.getAdminID(task);
+
+        params.put("adminID", adminID);
+
+        // 鏈嶅姟鍗曞垎绫伙紙浠庝换鍔℃墍灞為儴闂ㄧ殑鏈嶅姟鍗曠紪鐮佽幏鍙栵級
+        String serviceOrdClass = "BF"; // ServiceOrdClass榛樿鍊�
+        if (task.getDeptId() != null) {
+            try {
+                SysDept dept = sysDeptService.selectDeptById(task.getDeptId());
+                if (dept != null && StringUtils.isNotEmpty(dept.getServiceOrderClass())) {
+                    // 浣跨敤閮ㄩ棬鐨勬湇鍔″崟缂栫爜浣滀负ServiceOrdClass
+                    serviceOrdClass = dept.getServiceOrderClass();
+//                    log.info("鑾峰彇浠诲姟鎵�灞為儴闂ㄧ殑鏈嶅姟鍗曠紪鐮佹垚鍔燂紝閮ㄩ棬ID: {}, 閮ㄩ棬鍚嶇О: {}, 鏈嶅姟鍗曠紪鐮�: {}",
+//                        task.getDeptId(), dept.getDeptName(), serviceOrdClass);
+                } else {
+                    log.warn("浠诲姟鎵�灞為儴闂ㄦ湭閰嶇疆鏈嶅姟鍗曠紪鐮侊紝閮ㄩ棬ID: {}銆侀儴闂ㄥ悕绉�: {}锛屼娇鐢ㄩ粯璁ゅ��",
+                            task.getDeptId(), dept != null ? dept.getDeptName() : "null");
+                }
+            } catch (Exception e) {
+                log.error("鏌ヨ浠诲姟鎵�灞為儴闂ㄤ俊鎭紓甯革紝閮ㄩ棬ID: {}", task.getDeptId(), e);
+            }
+        } else {
+            log.warn("浠诲姟鏈叧鑱旈儴闂紝浣跨敤榛樿鏈嶅姟鍗曠紪鐮�");
+        }
+
+        // 鏈嶅姟鍗曟墽琛屽尯鍩燂紙浠庝换鍔$殑document_type_id鑾峰彇锛�
+        String serviceOrdAreaType = "1"; // 榛樿鍊�
+        if (StringUtils.isNotEmpty(emergency.getDocumentTypeId())) {
+            serviceOrdAreaType = emergency.getDocumentTypeId();
+//            log.info("鑾峰彇鍗曟嵁绫诲瀷ID鎴愬姛锛屼换鍔D: {}, 鍗曟嵁绫诲瀷ID: {}", task.getTaskId(), serviceOrdAreaType);
+        } else {
+            log.warn("浠诲姟鏈厤缃崟鎹被鍨婭D锛屼换鍔D: {}锛屼娇鐢ㄩ粯璁ゅ��", task.getTaskId());
+        }
+
+        // 鍩虹淇℃伅
+        params.put("ServiceOrdClass", serviceOrdClass); // 浠庨儴闂ㄧ殑鏈嶅姟鍗曠紪鐮佽幏鍙�
+        params.put("ServiceOrdAreaType", serviceOrdAreaType); // 浠庝换鍔$殑document_type_id鑾峰彇
+
+        // 鏈嶅姟鍗曟墽琛岀被鍨嬶紙浠庝换鍔$殑task_type_id鑾峰彇锛�
+        String serviceOrdType = "1"; // 榛樿鍊�
+        if (StringUtils.isNotEmpty(emergency.getTaskTypeId())) {
+            serviceOrdType = emergency.getTaskTypeId();
+//            log.info("鑾峰彇浠诲姟绫诲瀷ID鎴愬姛锛屼换鍔D: {}, 浠诲姟绫诲瀷ID: {}", taskId, serviceOrdType);
+        } else {
+            log.warn("浠诲姟鏈厤缃换鍔$被鍨婭D锛屼换鍔D: {}锛屼娇鐢ㄩ粯璁ゅ��", taskId);
+        }
+        params.put("ServiceOrdType", serviceOrdType); // 鏈嶅姟鍗曟墽琛岀被鍨嬶紙浠庝换鍔$殑task_type_id鑾峰彇锛�
+
+        params.put("ServiceOrdState", "2"); // 鏈嶅姟鍗曠姸鎬侊紙2=姝e紡鍗曪級
+        params.put("ServiceOrdStartDate", task.getCreateTime() != null ? sdfDate.format(task.getCreateTime()) : sdfDate.format(new Date()));
+
+        // 棰勭害鏃堕棿
+        if (task.getPlannedStartTime() != null) {
+            params.put("ServiceOrdApptDate", sdf.format(task.getPlannedStartTime()));
+        }
+
+        // 鑱旂郴浜轰俊鎭�
+        params.put("ServiceOrdCoName", StringUtils.nvl(emergency.getPatientContact(), ""));
+        params.put("ServiceOrdCoPhone", StringUtils.nvl(emergency.getPatientPhone(), ""));
+        params.put("ServiceOrdCoTies", ""); // 鑱旂郴浜轰笌鎮h�呭叧绯�
+
+        // 鎮h�呬俊鎭�
+        params.put("ServiceOrdPtName", StringUtils.nvl(emergency.getPatientName(), ""));
+        params.put("ServiceOrdPtAge", ""); // 骞撮緞
+        params.put("ServiceOrdPtKG", ""); // 浣撻噸
+        params.put("ServiceOrdPtSex", "0".equals(emergency.getPatientGender()) ? "鐢�" : "1".equals(emergency.getPatientGender()) ? "濂�" : "");
+        params.put("ServiceOrdPtNat", ""); // 鍥界睄
+        params.put("ServiceOrdPtIDCard", StringUtils.nvl(emergency.getPatientIdCard(), ""));
+
+        // 鍖婚櫌淇℃伅
+        params.put("ServiceOrdPtOutHosp", StringUtils.nvl(emergency.getHospitalOutName(), ""));
+        params.put("ServiceOrdPtOutHospID", emergency.getHospitalOutId() != null ? emergency.getHospitalOutId().toString() : "0"); // 杞嚭鍖婚櫌ID
+        params.put("ServiceOrdPtInHosp", StringUtils.nvl(emergency.getHospitalInName(), ""));
+        params.put("ServiceOrdPtInHospID", emergency.getHospitalInId() != null ? emergency.getHospitalInId().toString() : "0"); // 杞叆鍖婚櫌ID
+
+        // 绉戝淇℃伅
+        params.put("ServiceOrdPtServices", StringUtils.nvl(emergency.getHospitalOutBedNumber(), "")); //杞嚭搴婁綅
+        params.put("ServiceOrdPtServicesID", StringUtils.nvl(emergency.getHospitalOutDepartmentId(), "0")); // 杞嚭绉戝ID
+        params.put("ServiceOrdPtInServices", StringUtils.nvl(emergency.getHospitalInBedNumber(), "")); //杞叆搴婁綅
+        params.put("ServiceOrdPtInServicesID", StringUtils.nvl(emergency.getHospitalInDepartmentId(), "0")); // 杞叆绉戝ID
+
+        // 鐥呮儏淇℃伅
+        params.put("ServiceOrdPtDiagnosis", ""); // 璇婃柇
+        //鐥呮儏鎻忚堪
+        params.put("ServiceOrdPtCondition", StringUtils.nvl(emergency.getPatientCondition(), ""));
+        params.put("ServiceOrdTaskRemarks", StringUtils.nvl(task.getTaskDescription(), ""));
+        params.put("ServiceOrdPtDoctor", ""); // 鎮h�呭尰鐢�
+        params.put("ServiceOrdPtDoctorPhone", ""); // 鎮h�呭尰鐢熺數璇�
+
+        // 鍦板潃淇℃伅
+        params.put("province", ""); // 鍑哄彂鍦扮渷浠�
+        params.put("city", ""); // 鍑哄彂鍦板煄甯�
+        params.put("ServiceOrdTraStreet",task.getDepartureAddress()); //娲捐溅鍦板潃
+        params.put("ServiceOrdTraStreetCoo", ""); // 鍑哄彂鍦板潗鏍�
+        params.put("ServiceOrdTraEnd", StringUtils.nvl(task.getDestinationAddress(), StringUtils.nvl(emergency.getHospitalInAddress(), "")));
+        params.put("ServiceOrdTraEndCoo", ""); // 鐩殑鍦板潗鏍�
+        params.put("ServiceOrdTraVia", ""); // 閫旂粡鍦�
+
+        // 璺濈鍜屼环鏍间俊鎭�
+        params.put("ServiceOrdViaDistance", "0"); // 涓�旇窛绂�
+        params.put("ServiceOrdTraDistance", emergency.getTransferDistance() != null ? emergency.getTransferDistance().toString() : "0");
+        params.put("ServiceOrdTraDuration", ""); // 棰勮琛岀▼鏃堕棿
+        params.put("ServiceOrdTraUnitPrice", "0"); // 鍗曚环/鍏噷
+        params.put("ServiceOrdTraOfferPrice", emergency.getTransferPrice() != null ? emergency.getTransferPrice().toString() : "0");
+        params.put("ServiceOrdTraTxnPrice", emergency.getTransferPrice() != null ? emergency.getTransferPrice().toString() : "0");
+        params.put("ServiceOrdTraPrePayment", "0"); // 闇�棰勪粯娆�
+        params.put("SettlementPrice", "0"); // 缁撶畻浠�
+        params.put("ServiceOrdTraPriceReason", ""); // 宸环鍘熷洜
+
+        // 鍏朵粬淇℃伅
+        params.put("Phone", StringUtils.nvl(emergency.getPatientPhone(), "")); // 鏉ョ數鐢佃瘽
+        params.put("TEL_Time", sdf.format(new Date())); // 鏉ョ數鏃堕棿
+        params.put("TEL_Remarks", "鏂扮郴缁熷悓姝�"); // 鏉ョ數澶囨敞
+        params.put("TransferModeID", ""); // 杞繍鏂瑰紡
+        params.put("ServiceOrdVIP", "0"); // VIP瀹㈡埛
+        params.put("ServiceOrd_CC_ID", ""); // 瀹㈡湇浜哄憳ID
+        params.put("ServiceOrd_Sale_ID", ""); // 閿�鍞汉鍛業D
+        params.put("ServiceOrdIntroducer", ""); // 浠嬬粛浜�
+        params.put("ServiceOrd_work_ID", ""); // 涓昏浼佸井瀹㈡湇ID
+        params.put("ServiceOrd_work_IDs", ""); // 鍏朵粬浼佸井瀹㈡湇ID
+        params.put("ServiceOrd_work_is", "0"); // 鏄惁浼佸井鎴愪氦
+        params.put("CommissionScenarioID", "0"); // 浼佸井缁╂晥鏂规
+        params.put("ServiceOrdOperationRemarks", "鏂扮郴缁熷悓姝ュ垱寤�"); // 鎿嶄綔澶囨敞
+        params.put("ServiceOrdEstimatedOrderDate", ""); // 棰勮娲惧崟鏃堕棿
+        params.put("ServiceOrdSource", "10"); // 璁㈠崟鏉ユ簮锛�10=鏂扮郴缁燂級
+        params.put("OrderLevel", "0"); // 鏌ョ湅绛夌骇
+        params.put("ServiceOrdDepartureType", "1"); // 棰勭害绫诲瀷
+        params.put("ConditionLevel", "0"); // 鐥呴噸绾у埆
+        params.put("DirectionType", "0"); // 杞繍鍘诲悜
+        params.put("ServiceOrd_m", "1"); // 鏉ユ簮鍏ュ彛
+        params.put("FromHQ2_is", "0"); // 骞垮窞鎬婚儴鎺ㄩ�佷换鍔℃爣璁�
+        params.put("OrderPrice_Auto", "0"); // 璁㈠崟鑷姩鎶ヤ环鍙傝�冨��
+
+        return params;
+    }
+
+    /**
+     * 鍙戦�丠TTP/HTTPS POST璇锋眰
+     * 鏀寔HTTPS鑷鍚嶈瘉涔�
+     */
+    public String sendHttpPost(String urlString, Map<String, Object> params) throws Exception {
+        URL url = new URL(urlString);
+        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+
+        // 濡傛灉鏄疕TTPS璇锋眰锛岄厤缃甋SL淇′换鎵�鏈夎瘉涔�
+        if (conn instanceof HttpsURLConnection) {
+            HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
+            httpsConn.setSSLSocketFactory(createTrustAllSSLContext().getSocketFactory());
+            httpsConn.setHostnameVerifier((hostname, session) -> true); // 淇′换鎵�鏈変富鏈哄悕
+//            log.debug("閰嶇疆HTTPS杩炴帴锛屼俊浠绘墍鏈塖SL璇佷功锛孶RL: {}", urlString);
+        }
+
+        try {
+            // 璁剧疆杩炴帴灞炴��
+            conn.setRequestMethod("POST");
+            conn.setConnectTimeout(legacySystemConfig.getConnectTimeout());
+            conn.setReadTimeout(legacySystemConfig.getReadTimeout());
+            conn.setDoOutput(true);
+            conn.setDoInput(true);
+            conn.setUseCaches(false);
+            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + legacySystemConfig.getCharset());
+            conn.setRequestProperty("Accept-Charset", legacySystemConfig.getCharset());
+
+            // 鏋勫缓POST鏁版嵁
+            StringBuilder postData = new StringBuilder();
+            for (Map.Entry<String, Object> entry : params.entrySet()) {
+                if (postData.length() > 0) {
+                    postData.append("&");
+                }
+                postData.append(URLEncoder.encode(entry.getKey(), legacySystemConfig.getCharset()));
+                postData.append("=");
+                postData.append(URLEncoder.encode(entry.getValue().toString(), legacySystemConfig.getCharset()));
+            }
+
+            // log.info("鍙戦�丳OST璇锋眰鍒版棫绯荤粺锛孶RL: {}, 鍙傛暟: {}", urlString, postData.toString());
+
+            // 鍙戦�丳OST鏁版嵁
+            try (OutputStream os = conn.getOutputStream()) {
+                os.write(postData.toString().getBytes(legacySystemConfig.getCharset()));
+                os.flush();
+            }
+
+
+
+            // 璇诲彇鍝嶅簲
+            int responseCode = conn.getResponseCode();
+            if (responseCode == HttpURLConnection.HTTP_OK) {
+                try (BufferedReader reader = new BufferedReader(
+                        new InputStreamReader(conn.getInputStream(), legacySystemConfig.getCharset()))) {
+                    StringBuilder response = new StringBuilder();
+                    String line;
+                    while ((line = reader.readLine()) != null) {
+                        response.append(line);
+                    }
+                    return response.toString().trim();
+                }
+            } else {
+                log.error("璇锋眰澶辫触锛岃姹俇RL {},鍙傛暟 {}",urlString,postData);
+                throw new Exception("HTTP/HTTPS璇锋眰澶辫触锛屽搷搴旂爜: " + responseCode);
+            }
+
+        } finally {
+            conn.disconnect();
+        }
+    }
+
+
+    /**
+     * 瑙f瀽鏃х郴缁熷搷搴�
+     * 棰勬湡鏍煎紡: "OK:ServiceOrdID" 鎴栭敊璇俊鎭�
+     */
+    public Long parseResponse(String response) {
+        if (StringUtils.isEmpty(response)) {
+            return null;
+        }
+
+        // 鍘婚櫎鍙兘鐨凥TML鏍囩鍜岀┖鐧藉瓧绗�
+        response = response.replaceAll("<[^>]*>", "").trim();
+
+        // 妫�鏌ユ槸鍚︽垚鍔熷搷搴�
+        if (response.startsWith("OK:")) {
+            try {
+                String serviceOrdIdStr = response.substring(3).trim();
+                return Long.parseLong(serviceOrdIdStr);
+            } catch (NumberFormatException e) {
+                log.error("瑙f瀽ServiceOrdID澶辫触: {}", response, e);
+                return null;
+            }
+        } else {
+            log.error("鏃х郴缁熻繑鍥為敊璇�: {}", response);
+            return null;
+        }
+    }
+    /**
+     * 鍒涘缓淇′换鎵�鏈塖SL璇佷功鐨凷SLContext
+     * 鐢ㄤ簬鏀寔鑷鍚嶈瘉涔︾殑HTTPS璇锋眰
+     *
+     * 娉ㄦ剰锛氭鏂规硶浼氫俊浠绘墍鏈塖SL璇佷功锛屽寘鎷嚜绛惧悕璇佷功
+     * 浠呯敤浜庝笌鏃х郴缁熺殑鍐呴儴閫氫俊锛岀敓浜х幆澧冨缓璁娇鐢ㄦ瑙凜A璇佷功
+     */
+    private SSLContext createTrustAllSSLContext() throws Exception {
+        // 鍒涘缓淇′换鎵�鏈夎瘉涔︾殑TrustManager
+        TrustManager[] trustAllCerts = new TrustManager[] {
+                new X509TrustManager() {
+                    @Override
+                    public X509Certificate[] getAcceptedIssuers() {
+                        return null;
+                    }
+
+                    @Override
+                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
+                        // 淇′换鎵�鏈夊鎴风璇佷功
+                    }
+
+                    @Override
+                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
+                        // 淇′换鎵�鏈夋湇鍔″櫒璇佷功
+                    }
+                }
+        };
+
+        // 瀹夎淇′换鎵�鏈夎瘉涔︾殑TrustManager
+        SSLContext sslContext = SSLContext.getInstance("TLS");
+        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
+        return sslContext;
+    }
+
+}

--
Gitblit v1.9.1