From 364adbc9a93a396b74e154f910c2a0a72bfb1a0f Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期六, 29 十一月 2025 22:10:41 +0800
Subject: [PATCH] feat: 更新车辆里程统计

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java |  489 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 476 insertions(+), 13 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java
index 7e28b71..ccff010 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java
@@ -2,10 +2,7 @@
 
 import java.io.*;
 import java.math.BigDecimal;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
-import java.util.ArrayList;
+import java.util.*;
 import java.util.stream.Collectors;
 import java.net.HttpURLConnection;
 import java.net.URL;
@@ -39,7 +36,9 @@
 import com.ruoyi.system.domain.VehicleInfo;
 import com.ruoyi.system.service.ISysTaskService;
 import com.ruoyi.system.service.ILegacySystemSyncService;
+import com.ruoyi.system.service.ISysTaskEmergencyService;
 import com.ruoyi.system.service.ITaskAttachmentSyncService;
+import com.ruoyi.system.service.IMapService;
 import com.ruoyi.system.event.TaskCreatedEvent;
 import com.ruoyi.system.event.TaskAssignedEvent;
 import com.ruoyi.system.event.TaskStatusChangedEvent;
@@ -87,6 +86,9 @@
     private ILegacySystemSyncService legacySystemSyncService;
     
     @Autowired
+    private ISysTaskEmergencyService sysTaskEmergencyService;
+    
+    @Autowired
     private ApplicationEventPublisher eventPublisher;
     
     @Autowired
@@ -97,6 +99,9 @@
 
     @Autowired
     private SysUserMapper sysUserMapper;
+
+    @Autowired(required = false)
+    private IMapService mapService;
 
     /**
      * 鏌ヨ浠诲姟绠$悊
@@ -205,6 +210,44 @@
         // 璁剧疆绂忕杞﹀叕閲屾暟
         if (createVO.getDistance() != null) {
             task.setEstimatedDistance(createVO.getDistance());
+        }
+        
+        // 鑷姩鑾峰彇鍑哄彂鍦癎PS鍧愭爣锛堝鏋滅己澶憋級
+        if (task.getDepartureAddress() != null && 
+            (task.getDepartureLongitude() == null || task.getDepartureLatitude() == null) && 
+            mapService != null) {
+            try {
+                Map<String, Double> coords = mapService.geocoding(
+                    task.getDepartureAddress(), 
+                    extractCityFromAddress(task.getDepartureAddress())
+                );
+                if (coords != null) {
+                    task.setDepartureLongitude(BigDecimal.valueOf(coords.get("lng")));
+                    task.setDepartureLatitude(BigDecimal.valueOf(coords.get("lat")));
+                    log.info("鍑哄彂鍦癎PS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
+                }
+            } catch (Exception e) {
+                log.error("鑷姩鑾峰彇鍑哄彂鍦癎PS鍧愭爣澶辫触", e);
+            }
+        }
+        
+        // 鑷姩鑾峰彇鐩殑鍦癎PS鍧愭爣锛堝鏋滅己澶憋級
+        if (task.getDestinationAddress() != null && 
+            (task.getDestinationLongitude() == null || task.getDestinationLatitude() == null) && 
+            mapService != null) {
+            try {
+                Map<String, Double> coords = mapService.geocoding(
+                    task.getDestinationAddress(), 
+                    extractCityFromAddress(task.getDestinationAddress())
+                );
+                if (coords != null) {
+                    task.setDestinationLongitude(BigDecimal.valueOf(coords.get("lng")));
+                    task.setDestinationLatitude(BigDecimal.valueOf(coords.get("lat")));
+                    log.info("鐩殑鍦癎PS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
+                }
+            } catch (Exception e) {
+                log.error("鑷姩鑾峰彇鐩殑鍦癎PS鍧愭爣澶辫触", e);
+            }
         }
         
         int result = sysTaskMapper.insertSysTask(task);
@@ -382,6 +425,44 @@
             task.setEstimatedDistance(createVO.getDistance());
         }
         
+        // 鑷姩鑾峰彇鍑哄彂鍦癎PS鍧愭爣锛堝鏋滅己澶憋級
+        if (task.getDepartureAddress() != null && 
+            (task.getDepartureLongitude() == null || task.getDepartureLatitude() == null) && 
+            mapService != null) {
+            try {
+                Map<String, Double> coords = mapService.geocoding(
+                    task.getDepartureAddress(), 
+                    extractCityFromAddress(task.getDepartureAddress())
+                );
+                if (coords != null) {
+                    task.setDepartureLongitude(BigDecimal.valueOf(coords.get("lng")));
+                    task.setDepartureLatitude(BigDecimal.valueOf(coords.get("lat")));
+                    log.info("鍑哄彂鍦癎PS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
+                }
+            } catch (Exception e) {
+                log.error("鑷姩鑾峰彇鍑哄彂鍦癎PS鍧愭爣澶辫触", e);
+            }
+        }
+        
+        // 鑷姩鑾峰彇鐩殑鍦癎PS鍧愭爣锛堝鏋滅己澶憋級
+        if (task.getDestinationAddress() != null && 
+            (task.getDestinationLongitude() == null || task.getDestinationLatitude() == null) && 
+            mapService != null) {
+            try {
+                Map<String, Double> coords = mapService.geocoding(
+                    task.getDestinationAddress(), 
+                    extractCityFromAddress(task.getDestinationAddress())
+                );
+                if (coords != null) {
+                    task.setDestinationLongitude(BigDecimal.valueOf(coords.get("lng")));
+                    task.setDestinationLatitude(BigDecimal.valueOf(coords.get("lat")));
+                    log.info("鐩殑鍦癎PS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
+                }
+            } catch (Exception e) {
+                log.error("鑷姩鑾峰彇鐩殑鍦癎PS鍧愭爣澶辫触", e);
+            }
+        }
+        
         int result = sysTaskMapper.insertSysTask(task);
         
         // 淇濆瓨杞﹁締鍏宠仈淇℃伅
@@ -397,7 +478,7 @@
                 taskVehicle.setCreateTime(createTime);
                 taskVehicle.setUpdateBy(userName);
                 taskVehicle.setUpdateTime(updateTime);
-                
+
                 sysTaskVehicleMapper.insertSysTaskVehicle(taskVehicle);
             }
         }
@@ -490,10 +571,178 @@
         task.setUpdateTime(DateUtils.getNowDate());
         task.setRemark(updateVO.getRemark());
         
+        // 濡傛灉鏇存柊浜嗛儴闂↖D
+        if (updateVO.getDeptId() != null) {
+            task.setDeptId(updateVO.getDeptId());
+        }
+        
+        // 鑷姩鑾峰彇鍑哄彂鍦癎PS鍧愭爣锛堝鏋滄洿鏂颁簡鍦板潃浣嗙己澶卞潗鏍囷級
+        if (updateVO.getDepartureAddress() != null && 
+            (updateVO.getDepartureLongitude() == null || updateVO.getDepartureLatitude() == null) && 
+            mapService != null) {
+            try {
+                Map<String, Double> coords = mapService.geocoding(
+                    updateVO.getDepartureAddress(), 
+                    extractCityFromAddress(updateVO.getDepartureAddress())
+                );
+                if (coords != null) {
+                    task.setDepartureLongitude(BigDecimal.valueOf(coords.get("lng")));
+                    task.setDepartureLatitude(BigDecimal.valueOf(coords.get("lat")));
+                    log.info("鍑哄彂鍦癎PS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
+                }
+            } catch (Exception e) {
+                log.error("鑷姩鑾峰彇鍑哄彂鍦癎PS鍧愭爣澶辫触", e);
+            }
+        }
+        
+        // 鑷姩鑾峰彇鐩殑鍦癎PS鍧愭爣锛堝鏋滄洿鏂颁簡鍦板潃浣嗙己澶卞潗鏍囷級
+        if (updateVO.getDestinationAddress() != null && 
+            (updateVO.getDestinationLongitude() == null || updateVO.getDestinationLatitude() == null) && 
+            mapService != null) {
+            try {
+                Map<String, Double> coords = mapService.geocoding(
+                    updateVO.getDestinationAddress(), 
+                    extractCityFromAddress(updateVO.getDestinationAddress())
+                );
+                if (coords != null) {
+                    task.setDestinationLongitude(BigDecimal.valueOf(coords.get("lng")));
+                    task.setDestinationLatitude(BigDecimal.valueOf(coords.get("lat")));
+                    log.info("鐩殑鍦癎PS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
+                }
+            } catch (Exception e) {
+                log.error("鑷姩鑾峰彇鐩殑鍦癎PS鍧愭爣澶辫触", e);
+            }
+        }
+        
         // 閲嶆柊璁$畻棰勮鍏噷鏁�
         calculateEstimatedDistance(task);
         
         int result = sysTaskMapper.updateSysTask(task);
+        
+        // 鐢ㄤ簬璺熻釜鏄惁闇�瑕侀噸鏂板悓姝ワ紙杞﹁締銆佷汉鍛樸�佸湴鍧�銆佹垚浜や环鍙樻洿锛�
+        boolean needResync = false;
+        
+        // 鏇存柊杞﹁締鍏宠仈
+        if (result > 0 && updateVO.getVehicleIds() != null && !updateVO.getVehicleIds().isEmpty()) {
+            // 鏌ヨ鐜版湁鐨勮溅杈嗗叧鑱�
+            List<SysTaskVehicle> existingVehicles = sysTaskVehicleMapper.selectSysTaskVehicleByTaskId(updateVO.getTaskId());
+            List<Long> existingVehicleIds = existingVehicles.stream()
+                .map(SysTaskVehicle::getVehicleId)
+                .collect(Collectors.toList());
+            
+            // 姣旇緝鏂版棫杞﹁締ID鍒楄〃锛屽垽鏂槸鍚︽湁鍙樺寲
+            boolean vehiclesChanged = !new HashSet<>(existingVehicleIds).equals(new HashSet<>(updateVO.getVehicleIds()));
+            
+            // 鍙湁杞﹁締鍙戠敓鍙樺寲鏃舵墠鏇存柊
+            if (vehiclesChanged) {
+                // 鍒犻櫎鏃х殑杞﹁締鍏宠仈
+                sysTaskVehicleMapper.deleteSysTaskVehicleByTaskId(updateVO.getTaskId());
+                
+                // 娣诲姞鏂扮殑杞﹁締鍏宠仈
+                Date now = DateUtils.getNowDate();
+                String currentUser = SecurityUtils.getUsername();
+                for (Long vehicleId : updateVO.getVehicleIds()) {
+                    SysTaskVehicle taskVehicle = new SysTaskVehicle();
+                    taskVehicle.setTaskId(updateVO.getTaskId());
+                    taskVehicle.setVehicleId(vehicleId);
+                    taskVehicle.setAssignTime(now);
+                    taskVehicle.setAssignBy(currentUser);
+                    taskVehicle.setCreateTime(now);
+                    sysTaskVehicleMapper.insertSysTaskVehicle(taskVehicle);
+                }
+                
+                // 鏍囪闇�瑕侀噸鏂板悓姝ワ紙杞﹁締鍙樻洿锛�
+                needResync = true;
+            }
+        }
+        
+        // 鏇存柊鎵ц浜哄憳锛堟娴嬩汉鍛樺彉鏇达級
+        if (result > 0 && updateVO.getAssignees() != null) {
+            // 鏌ヨ鐜版湁鐨勬墽琛屼汉鍛�
+            List<SysTaskAssignee> existingAssignees = sysTaskAssigneeMapper.selectSysTaskAssigneeByTaskId(updateVO.getTaskId());
+            List<Long> existingAssigneeIds = existingAssignees.stream()
+                .map(SysTaskAssignee::getUserId)
+                .collect(Collectors.toList());
+            
+            List<Long> newAssigneeIds = updateVO.getAssignees().stream()
+                .map(TaskUpdateVO.AssigneeInfo::getUserId)
+                .collect(Collectors.toList());
+            
+            // 姣旇緝鏂版棫鎵ц浜哄憳ID鍒楄〃锛屽垽鏂槸鍚︽湁鍙樺寲
+            boolean assigneesChanged = !new HashSet<>(existingAssigneeIds).equals(new HashSet<>(newAssigneeIds));
+            
+            // 鍙湁鎵ц浜哄憳鍙戠敓鍙樺寲鏃舵墠鏇存柊
+            if (assigneesChanged) {
+                // 鍒犻櫎鏃х殑鎵ц浜哄憳鍏宠仈
+                sysTaskAssigneeMapper.deleteSysTaskAssigneeByTaskId(updateVO.getTaskId());
+                
+                // 娣诲姞鏂扮殑鎵ц浜哄憳鍏宠仈
+                if (!updateVO.getAssignees().isEmpty()) {
+                    // 灏� TaskUpdateVO.AssigneeInfo 杞崲涓� TaskCreateVO.AssigneeInfo
+                    List<TaskCreateVO.AssigneeInfo> createAssignees = updateVO.getAssignees().stream()
+                        .map(assignee -> {
+                            TaskCreateVO.AssigneeInfo createAssignee = new TaskCreateVO.AssigneeInfo();
+                            createAssignee.setUserId(assignee.getUserId());
+                            createAssignee.setUserName(assignee.getUserName());
+                            createAssignee.setUserType(assignee.getUserType());
+                            return createAssignee;
+                        })
+                        .collect(Collectors.toList());
+                    saveTaskAssignees(updateVO.getTaskId(), createAssignees, SecurityUtils.getUsername());
+                }
+                
+                // 鏍囪闇�瑕侀噸鏂板悓姝ワ紙浜哄憳鍙樻洿锛�
+                needResync = true;
+            }
+        }
+        
+        // 鏇存柊鎬ユ晳杞繍鎵╁睍淇℃伅锛堟娴嬪湴鍧�鍜屾垚浜や环鍙樻洿锛�
+        if (result > 0 && "EMERGENCY_TRANSFER".equals(oldTask.getTaskType()) && updateVO.getEmergencyInfo() != null) {
+            // 鑾峰彇鏃х殑鎬ユ晳杞繍淇℃伅
+            SysTaskEmergency oldEmergency = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(updateVO.getTaskId());
+            
+            // 妫�娴嬭浆鍑哄尰闄㈠湴鍧�鍙樻洿
+            boolean hospitalOutAddressChanged = false;
+            if (updateVO.getEmergencyInfo().getHospitalOutAddress() != null 
+                && oldEmergency != null 
+                && !updateVO.getEmergencyInfo().getHospitalOutAddress().equals(oldEmergency.getHospitalOutAddress())) {
+                hospitalOutAddressChanged = true;
+            }
+            
+            // 妫�娴嬭浆鍏ュ尰闄㈠湴鍧�鍙樻洿
+            boolean hospitalInAddressChanged = false;
+            if (updateVO.getEmergencyInfo().getHospitalInAddress() != null 
+                && oldEmergency != null 
+                && !updateVO.getEmergencyInfo().getHospitalInAddress().equals(oldEmergency.getHospitalInAddress())) {
+                hospitalInAddressChanged = true;
+            }
+            
+            // 妫�娴嬫垚浜や环鍙樻洿
+            boolean transferPriceChanged = false;
+            if (updateVO.getEmergencyInfo().getTransferPrice() != null 
+                && oldEmergency != null 
+                && oldEmergency.getTransferPrice() != null
+                && updateVO.getEmergencyInfo().getTransferPrice().compareTo(oldEmergency.getTransferPrice()) != 0) {
+                transferPriceChanged = true;
+            }
+            
+            // 鏇存柊鎬ユ晳杞繍淇℃伅
+            updateEmergencyInfo(updateVO.getTaskId(), updateVO);
+            
+            // 濡傛灉鍦板潃鎴栨垚浜や环鍙戠敓鍙樻洿锛屾爣璁伴渶瑕侀噸鏂板悓姝�
+            if (hospitalOutAddressChanged || hospitalInAddressChanged || transferPriceChanged) {
+                needResync = true;
+            }
+        }
+        
+        // 濡傛灉鏄�ユ晳杞繍浠诲姟涓旀湁鍙樻洿锛屾爣璁伴渶瑕侀噸鏂板悓姝�
+        if (result > 0 && "EMERGENCY_TRANSFER".equals(oldTask.getTaskType()) && needResync) {
+            try {
+                sysTaskEmergencyService.markNeedResync(updateVO.getTaskId());
+            } catch (Exception e) {
+                // 鏍囪澶辫触涓嶅奖鍝嶄富娴佺▼
+            }
+        }
         
         // 璁板綍鎿嶄綔鏃ュ織
         if (result > 0) {
@@ -703,7 +952,7 @@
                 recordTaskLog(taskId, "UPDATE", "涓婁紶闄勪欢", null, 
                              "涓婁紶鏂囦欢锛�" + file.getOriginalFilename() + "(鍒嗙被锛�" + categoryDesc + ")", 
                              SecurityUtils.getUserId(), SecurityUtils.getUsername());
-                
+
 
             }
             
@@ -763,7 +1012,7 @@
                 recordTaskLog(taskId, "UPDATE", "涓婁紶闄勪欢", null, 
                              "閫氳繃寰俊涓婁紶鏂囦欢锛�" + fileName + "(鍒嗙被锛�" + categoryDesc + ")", 
                              SecurityUtils.getUserId(), SecurityUtils.getUsername());
-                
+
 
             }
             
@@ -1112,6 +1361,8 @@
             task.setAttachments(attachments);
             // 鏌ヨ鎿嶄綔鏃ュ織
             task.setOperationLogs(sysTaskLogMapper.selectSysTaskLogByTaskId(taskId));
+            // 鏌ヨ鎵ц浜哄憳鍒楄〃
+            task.setAssignees(sysTaskAssigneeMapper.selectSysTaskAssigneeByTaskId(taskId));
             // 鍔犺浇鎬ユ晳杞繍鎵╁睍淇℃伅
             if ("EMERGENCY_TRANSFER".equals(task.getTaskType())) {
                 SysTaskEmergency emergencyInfo = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId);
@@ -1212,6 +1463,30 @@
      */
     private String generateTaskCode() {
         return taskCodeGenerator.generateTaskCode();
+    }
+    
+    /**
+     * 浠庡湴鍧�涓彁鍙栧煄甯傚悕绉帮紙鐢ㄤ簬鍦板浘鍦扮悊缂栫爜锛�
+     * 
+     * @param address 鍦板潃
+     * @return 鍩庡競鍚嶇О
+     */
+    private String extractCityFromAddress(String address) {
+        if (address == null || address.trim().isEmpty()) {
+            return null;
+        }
+        
+        // 甯歌鍩庡競鍚嶅垪琛�
+        String[] cities = {"骞垮窞", "娣卞湷", "涓滆帪", "浣涘北", "鐝犳捣", "鎯犲窞", "涓北", "姹熼棬", "婀涙睙", "鑲囧簡", "娓呰繙", "闊跺叧", "姊呭窞", "娌虫簮", "娼窞", "鎻槼", "姹曞ご", "姹曞熬", "浜戞诞", "闃虫睙","鍖椾含","涓婃捣","澶╂触"};
+        
+        
+        for (String city : cities) {
+            if (address.contains(city)) {
+                return city;
+            }
+        }
+        
+        return null;
     }
 
     /**
@@ -1396,8 +1671,27 @@
             emergencyInfo.setHospitalOutDepartmentId(createVO.getHospitalOut().getDepartmentId());
             emergencyInfo.setHospitalOutBedNumber(createVO.getHospitalOut().getBedNumber());
             emergencyInfo.setHospitalOutAddress(createVO.getHospitalOut().getAddress());
-            emergencyInfo.setHospitalOutLongitude(createVO.getHospitalOut().getLongitude());
-            emergencyInfo.setHospitalOutLatitude(createVO.getHospitalOut().getLatitude());
+            
+            // GPS鍧愭爣锛氫紭鍏堜娇鐢ㄥ墠绔紶鍏ョ殑锛屽惁鍒欏悗绔嚜鍔ㄨ幏鍙�
+            if (createVO.getHospitalOut().getLongitude() != null && createVO.getHospitalOut().getLatitude() != null) {
+                emergencyInfo.setHospitalOutLongitude(createVO.getHospitalOut().getLongitude());
+                emergencyInfo.setHospitalOutLatitude(createVO.getHospitalOut().getLatitude());
+            } else if (mapService != null && createVO.getHospitalOut().getAddress() != null) {
+                // 鍚庣鑷姩鑾峰彇GPS鍧愭爣
+                try {
+                    Map<String, Double> coords = mapService.geocoding(
+                        createVO.getHospitalOut().getAddress(), 
+                        extractCityFromAddress(createVO.getHospitalOut().getAddress())
+                    );
+                    if (coords != null) {
+                        emergencyInfo.setHospitalOutLongitude(BigDecimal.valueOf(coords.get("lng")));
+                        emergencyInfo.setHospitalOutLatitude(BigDecimal.valueOf(coords.get("lat")));
+                        log.info("杞嚭鍖婚櫌GPS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
+                    }
+                } catch (Exception e) {
+                    log.error("鑷姩鑾峰彇杞嚭鍖婚櫌GPS鍧愭爣澶辫触", e);
+                }
+            }
         }
         
         // 璁剧疆杞叆鍖婚櫌淇℃伅
@@ -1408,8 +1702,27 @@
             emergencyInfo.setHospitalInDepartmentId(createVO.getHospitalIn().getDepartmentId());
             emergencyInfo.setHospitalInBedNumber(createVO.getHospitalIn().getBedNumber());
             emergencyInfo.setHospitalInAddress(createVO.getHospitalIn().getAddress());
-            emergencyInfo.setHospitalInLongitude(createVO.getHospitalIn().getLongitude());
-            emergencyInfo.setHospitalInLatitude(createVO.getHospitalIn().getLatitude());
+            
+            // GPS鍧愭爣锛氫紭鍏堜娇鐢ㄥ墠绔紶鍏ョ殑锛屽惁鍒欏悗绔嚜鍔ㄨ幏鍙�
+            if (createVO.getHospitalIn().getLongitude() != null && createVO.getHospitalIn().getLatitude() != null) {
+                emergencyInfo.setHospitalInLongitude(createVO.getHospitalIn().getLongitude());
+                emergencyInfo.setHospitalInLatitude(createVO.getHospitalIn().getLatitude());
+            } else if (mapService != null && createVO.getHospitalIn().getAddress() != null) {
+                // 鍚庣鑷姩鑾峰彇GPS鍧愭爣
+                try {
+                    Map<String, Double> coords = mapService.geocoding(
+                        createVO.getHospitalIn().getAddress(), 
+                        extractCityFromAddress(createVO.getHospitalIn().getAddress())
+                    );
+                    if (coords != null) {
+                        emergencyInfo.setHospitalInLongitude(BigDecimal.valueOf(coords.get("lng")));
+                        emergencyInfo.setHospitalInLatitude(BigDecimal.valueOf(coords.get("lat")));
+                        log.info("杞叆鍖婚櫌GPS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
+                    }
+                } catch (Exception e) {
+                    log.error("鑷姩鑾峰彇杞叆鍖婚櫌GPS鍧愭爣澶辫触", e);
+                    }
+                }
         }
         
         // 璁剧疆璐圭敤淇℃伅
@@ -1441,8 +1754,6 @@
             emergencyInfo.setDispatchSyncStatus(2);
             emergencyInfo.setDispatchSyncTime(new Date());
             emergencyInfo.setDispatchSyncErrorMsg("鏃х郴缁熷悓姝ヨ繃鏉�");
-
-        // 绯荤粺瀛楁
         }
         // 绯荤粺瀛楁
         emergencyInfo.setCreateTime(DateUtils.getNowDate());
@@ -1454,6 +1765,158 @@
     }
 
     /**
+     * 鏇存柊鎬ユ晳杞繍浠诲姟鎵╁睍淇℃伅
+     * 
+     * @param taskId 浠诲姟ID
+     * @param updateVO 浠诲姟鏇存柊瀵硅薄
+     */
+    private void updateEmergencyInfo(Long taskId, TaskUpdateVO updateVO) {
+        // 鏌ヨ鐜版湁鐨勬墿灞曚俊鎭�
+        SysTaskEmergency existingInfo = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId);
+        if (existingInfo == null) {
+            // 濡傛灉涓嶅瓨鍦紝鍒欏垱寤烘柊鐨�
+            existingInfo = new SysTaskEmergency();
+            existingInfo.setTaskId(taskId);
+            existingInfo.setCreateTime(DateUtils.getNowDate());
+            existingInfo.setCreateBy(SecurityUtils.getUsername());
+        }
+        
+        TaskUpdateVO.EmergencyInfoVO emergencyInfo = updateVO.getEmergencyInfo();
+        
+        // 鏇存柊鎮h�呬俊鎭�
+        if (emergencyInfo.getPatientContact() != null) {
+            existingInfo.setPatientContact(emergencyInfo.getPatientContact());
+        }
+        if (emergencyInfo.getPatientPhone() != null) {
+            existingInfo.setPatientPhone(emergencyInfo.getPatientPhone());
+        }
+        if (emergencyInfo.getPatientName() != null) {
+            existingInfo.setPatientName(emergencyInfo.getPatientName());
+        }
+        if (emergencyInfo.getPatientGender() != null) {
+            existingInfo.setPatientGender(emergencyInfo.getPatientGender());
+        }
+        if (emergencyInfo.getPatientIdCard() != null) {
+            existingInfo.setPatientIdCard(emergencyInfo.getPatientIdCard());
+        }
+        if (emergencyInfo.getPatientCondition() != null) {
+            existingInfo.setPatientCondition(emergencyInfo.getPatientCondition());
+        }
+        
+        // 鏇存柊杞嚭鍖婚櫌淇℃伅
+        if (emergencyInfo.getHospitalOutId() != null) {
+            existingInfo.setHospitalOutId(emergencyInfo.getHospitalOutId());
+        }
+        if (emergencyInfo.getHospitalOutName() != null) {
+            existingInfo.setHospitalOutName(emergencyInfo.getHospitalOutName());
+        }
+        if (emergencyInfo.getHospitalOutDepartment() != null) {
+            existingInfo.setHospitalOutDepartment(emergencyInfo.getHospitalOutDepartment());
+        }
+        if (emergencyInfo.getHospitalOutDepartmentId() != null) {
+            existingInfo.setHospitalOutDepartmentId(emergencyInfo.getHospitalOutDepartmentId());
+        }
+        if (emergencyInfo.getHospitalOutBedNumber() != null) {
+            existingInfo.setHospitalOutBedNumber(emergencyInfo.getHospitalOutBedNumber());
+        }
+        if (emergencyInfo.getHospitalOutAddress() != null) {
+            existingInfo.setHospitalOutAddress(emergencyInfo.getHospitalOutAddress());
+            
+            // 濡傛灉鏇存柊浜嗗湴鍧�浣嗘病鏈塆PS鍧愭爣锛屽悗绔嚜鍔ㄨ幏鍙�
+            if (emergencyInfo.getHospitalOutLongitude() == null && emergencyInfo.getHospitalOutLatitude() == null && mapService != null) {
+                try {
+                    Map<String, Double> coords = mapService.geocoding(
+                        emergencyInfo.getHospitalOutAddress(), 
+                        extractCityFromAddress(emergencyInfo.getHospitalOutAddress())
+                    );
+                    if (coords != null) {
+                        existingInfo.setHospitalOutLongitude(BigDecimal.valueOf(coords.get("lng")));
+                        existingInfo.setHospitalOutLatitude(BigDecimal.valueOf(coords.get("lat")));
+                        log.info("杞嚭鍖婚櫌GPS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
+                    }
+                } catch (Exception e) {
+                    log.error("鑷姩鑾峰彇杞嚭鍖婚櫌GPS鍧愭爣澶辫触", e);
+                }
+            }
+        }
+        if (emergencyInfo.getHospitalOutLongitude() != null) {
+            existingInfo.setHospitalOutLongitude(emergencyInfo.getHospitalOutLongitude());
+        }
+        if (emergencyInfo.getHospitalOutLatitude() != null) {
+            existingInfo.setHospitalOutLatitude(emergencyInfo.getHospitalOutLatitude());
+        }
+        
+        // 鏇存柊杞叆鍖婚櫌淇℃伅
+        if (emergencyInfo.getHospitalInId() != null) {
+            existingInfo.setHospitalInId(emergencyInfo.getHospitalInId());
+        }
+        if (emergencyInfo.getHospitalInName() != null) {
+            existingInfo.setHospitalInName(emergencyInfo.getHospitalInName());
+        }
+        if (emergencyInfo.getHospitalInDepartment() != null) {
+            existingInfo.setHospitalInDepartment(emergencyInfo.getHospitalInDepartment());
+        }
+        if (emergencyInfo.getHospitalInDepartmentId() != null) {
+            existingInfo.setHospitalInDepartmentId(emergencyInfo.getHospitalInDepartmentId());
+        }
+        if (emergencyInfo.getHospitalInBedNumber() != null) {
+            existingInfo.setHospitalInBedNumber(emergencyInfo.getHospitalInBedNumber());
+        }
+        if (emergencyInfo.getHospitalInAddress() != null) {
+            existingInfo.setHospitalInAddress(emergencyInfo.getHospitalInAddress());
+            
+            // 濡傛灉鏇存柊浜嗗湴鍧�浣嗘病鏈塆PS鍧愭爣锛屽悗绔嚜鍔ㄨ幏鍙�
+            if (emergencyInfo.getHospitalInLongitude() == null && emergencyInfo.getHospitalInLatitude() == null && mapService != null) {
+                try {
+                    Map<String, Double> coords = mapService.geocoding(
+                        emergencyInfo.getHospitalInAddress(), 
+                        extractCityFromAddress(emergencyInfo.getHospitalInAddress())
+                    );
+                    if (coords != null) {
+                        existingInfo.setHospitalInLongitude(BigDecimal.valueOf(coords.get("lng")));
+                        existingInfo.setHospitalInLatitude(BigDecimal.valueOf(coords.get("lat")));
+                        log.info("杞叆鍖婚櫌GPS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
+                    }
+                } catch (Exception e) {
+                    log.error("鑷姩鑾峰彇杞叆鍖婚櫌GPS鍧愭爣澶辫触", e);
+                }
+            }
+        }
+        if (emergencyInfo.getHospitalInLongitude() != null) {
+            existingInfo.setHospitalInLongitude(emergencyInfo.getHospitalInLongitude());
+        }
+        if (emergencyInfo.getHospitalInLatitude() != null) {
+            existingInfo.setHospitalInLatitude(emergencyInfo.getHospitalInLatitude());
+        }
+        
+        // 鏇存柊璐圭敤淇℃伅
+        if (emergencyInfo.getTransferDistance() != null) {
+            existingInfo.setTransferDistance(emergencyInfo.getTransferDistance());
+        }
+        if (emergencyInfo.getTransferPrice() != null) {
+            existingInfo.setTransferPrice(emergencyInfo.getTransferPrice());
+        }
+        
+        // 鏇存柊鐥呮儏ID鍒楄〃
+        if (updateVO.getDiseaseIds() != null && !updateVO.getDiseaseIds().isEmpty()) {
+            String diseaseIdsStr = updateVO.getDiseaseIds().stream()
+                .map(String::valueOf)
+                .collect(Collectors.joining(","));
+            existingInfo.setDiseaseIds(diseaseIdsStr);
+        } else {
+            // 濡傛灉鐥呮儏ID鍒楄〃涓虹┖锛屾竻绌鸿瀛楁
+            existingInfo.setDiseaseIds(null);
+        }
+        
+        // 绯荤粺瀛楁
+        existingInfo.setUpdateTime(DateUtils.getNowDate());
+        existingInfo.setUpdateBy(SecurityUtils.getUsername());
+        
+        // 鎵ц鏇存柊
+        sysTaskEmergencyMapper.updateSysTaskEmergency(existingInfo);
+    }
+
+    /**
      * 淇濆瓨绂忕杞︿换鍔℃墿灞曚俊鎭�
      * 
      * @param taskId 浠诲姟ID

--
Gitblit v1.9.1