From 45d90d1e7ba86286e998d1ac4d2cba8e98cd059b Mon Sep 17 00:00:00 2001
From: wlzboy <66905212@qq.com>
Date: 星期一, 12 一月 2026 20:52:30 +0800
Subject: [PATCH] feat: 优化内存

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java |  552 +++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 432 insertions(+), 120 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 d9c04d5..60aae88 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
@@ -8,8 +8,11 @@
 import java.net.URL;
 
 import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.core.domain.entity.SysDept;
 import com.ruoyi.common.core.domain.entity.SysUser;
+import com.ruoyi.common.utils.*;
 import com.ruoyi.system.domain.vo.*;
+import com.ruoyi.system.event.*;
 import com.ruoyi.system.mapper.*;
 import com.ruoyi.system.service.*;
 import com.ruoyi.system.utils.TaskCodeGenerator;
@@ -18,10 +21,6 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
-import com.ruoyi.common.utils.DateUtils;
-import com.ruoyi.common.utils.SecurityUtils;
-import com.ruoyi.common.utils.StringUtils;
-import com.ruoyi.common.utils.GpsDistanceUtils;
 import com.ruoyi.common.utils.file.FileUploadUtils;
 import com.ruoyi.common.utils.file.FileUtils;
 import com.ruoyi.system.domain.SysTask;
@@ -33,9 +32,6 @@
 import com.ruoyi.system.domain.SysTaskAssignee;
 import com.ruoyi.system.domain.enums.TaskStatus;
 import com.ruoyi.system.domain.VehicleInfo;
-import com.ruoyi.system.event.TaskCreatedEvent;
-import com.ruoyi.system.event.TaskAssignedEvent;
-import com.ruoyi.system.event.TaskStatusChangedEvent;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -82,8 +78,13 @@
     @Autowired
     private ISysTaskAttachmentService sysTaskAttachmentService;
 
+
+
     @Autowired
-    private SysUserMapper sysUserMapper;
+    private ISysDeptService deptService;
+
+    @Autowired
+    private ISysUserService userService;
 
     @Autowired(required = false)
     private IMapService mapService;
@@ -100,6 +101,23 @@
     @Autowired
     private ISysTaskVehicleService sysTaskVehicleService;
 
+    @Autowired
+    private ISysTaskAssigneeService assigneeService;
+
+
+    @Override
+    public Boolean dispatchSyncEvent(Long taskId) {
+        SysTask task= sysTaskMapper.selectSysTaskByTaskId(taskId);
+        SysUser user= userService.selectUserById(task.getCreatorId());
+        Integer oaUser=user.getOaUserId();
+        SysTaskEmergency emergency = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId);
+        eventPublisher.publishEvent(new TaskDispatchSyncEvent(this, taskId, task.getTaskCode(),emergency.getLegacyServiceOrdId(), emergency.getLegacyDispatchOrdId(),oaUser));
+        return true;
+    }
+
+    private Long getBranchCompanyId(Long userId) {
+       return userService.getBranchCompanyIdByUserId(userId);
+    }
     /**
      * 鏌ヨ浠诲姟绠$悊
      * 
@@ -110,6 +128,7 @@
     public SysTask selectSysTaskByTaskId(Long taskId) {
         SysTask task = sysTaskMapper.selectSysTaskByTaskId(taskId);
         if (task != null) {
+            bindTaskAssign(task);
             // 鍔犺浇鎬ユ晳杞繍鎵╁睍淇℃伅
             if ("EMERGENCY_TRANSFER".equals(task.getTaskType())) {
                 SysTaskEmergency emergencyInfo = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId);
@@ -124,6 +143,13 @@
         return task;
     }
 
+
+    private void bindTaskAssign(SysTask task){
+       List<SysTaskAssignee> assignees= assigneeService.getAssigneesByTaskId(task.getTaskId());
+       if(assignees!=null && !assignees.isEmpty()) {
+           task.setAssignees(assignees);
+       }
+    }
     /**
      * 鏌ヨ浠诲姟绠$悊鍒楄〃
      * 
@@ -132,7 +158,91 @@
      */
     @Override
     public List<SysTask> selectSysTaskList(TaskQueryVO queryVO) {
-        return sysTaskMapper.selectSysTaskList(queryVO);
+        List<SysTask> tasks= sysTaskMapper.selectSysTaskList(queryVO);
+        tasks.forEach(task -> {
+            bindTaskAssign( task);
+            if ("EMERGENCY_TRANSFER".equals(task.getTaskType())) {
+                SysTaskEmergency emergencyInfo = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(task.getTaskId());
+                task.setEmergencyInfo(emergencyInfo);
+            }
+        });
+        return tasks;
+    }
+
+    /**
+     * 璁剧疆浠诲姟鏄�婚儴鎺ㄩ�佺殑鏍囪
+     * @param taskCreatorId 浠诲姟鍒涘缓浜虹殑鐢ㄦ埛ID
+     * @param taskDeptId 浠诲姟涓殑褰掑睘鏈烘瀯ID
+     */
+    @Override
+    public Boolean isTaskHeaderPush(Long taskCreatorId,Long taskDeptId){
+        if(LongUtil.isEmpty(taskCreatorId))return false;
+        if(LongUtil.isEmpty(taskDeptId))return false ;
+        Long createrDeptId = getBranchCompanyId(taskCreatorId);
+
+        if(createrDeptId !=null && !taskDeptId.equals(createrDeptId) && createrDeptId.equals(DeptUtil.GUANGZHOU_DEPT_ID)){
+            //骞垮窞鎬婚儴鎺ㄩ�佺殑浠诲姟
+           return true;
+        }else{
+            return false;
+        }
+    }
+
+    /**
+     * 鏍规嵁浠诲姟缂栧彿銆佽皟搴﹀崟缂栧彿鎴栨湇鍔″崟缂栧彿鏌ヨ浠诲姟鍒楄〃
+     * 
+     * @param queryVO 浠诲姟鏌ヨ瀵硅薄
+     * @param taskCode 浠诲姟缂栧彿
+     * @return 浠诲姟绠$悊闆嗗悎
+     */
+    @Override
+    public List<SysTask> selectSysTaskListByMultiCode(TaskQueryVO queryVO, String taskCode) {
+        // Create a new query object without the taskCode filter
+        TaskQueryVO newQuery = new TaskQueryVO();
+        // Copy all properties except taskCode
+        try {
+            org.springframework.beans.BeanUtils.copyProperties(queryVO, newQuery, "taskCode");
+        } catch (Exception e) {
+            // If copy fails, manually copy the important fields
+            newQuery.setTaskType(queryVO.getTaskType());
+            newQuery.setTaskStatus(queryVO.getTaskStatus());
+            newQuery.setVehicleNo(queryVO.getVehicleNo());
+            newQuery.setCreatorId(queryVO.getCreatorId());
+            newQuery.setAssigneeId(queryVO.getAssigneeId());
+            newQuery.setDeptId(queryVO.getDeptId());
+            newQuery.setDeptIds(queryVO.getDeptIds());
+            newQuery.setPlannedStartTimeBegin(queryVO.getPlannedStartTimeBegin());
+            newQuery.setPlannedStartTimeEnd(queryVO.getPlannedStartTimeEnd());
+            newQuery.setPlannedEndTimeBegin(queryVO.getPlannedEndTimeBegin());
+            newQuery.setPlannedEndTimeEnd(queryVO.getPlannedEndTimeEnd());
+            newQuery.setOverdue(queryVO.getOverdue());
+        }
+
+        // Get all tasks matching the other criteria
+        List<SysTask> allTasks = sysTaskMapper.selectSysTaskList(newQuery);
+        allTasks.stream().forEach(task -> {
+
+            if ("EMERGENCY_TRANSFER".equals(task.getTaskType())) {
+                SysTaskEmergency emergencyInfo = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(task.getTaskId());
+                task.setEmergencyInfo(emergencyInfo);
+            }
+        });
+        return allTasks.stream().filter(task -> {
+            if (task.getTaskCode() != null && task.getTaskCode().contains(taskCode)) {
+                return true;
+            }
+            bindTaskAssign(task);
+
+            if ("EMERGENCY_TRANSFER".equals(task.getTaskType()) && task.getEmergencyInfo() != null) {
+                String dispatchCode = task.getEmergencyInfo().getDispatchCode();
+                String serviceCode = task.getEmergencyInfo().getServiceCode();
+                return (dispatchCode != null && dispatchCode.contains(taskCode)) ||
+                        (serviceCode != null && serviceCode.contains(taskCode));
+            }
+            return false;
+
+        }).collect(Collectors.toList());
+
     }
 
     /**
@@ -142,23 +252,29 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
     public int insertSysTask(TaskCreateVO createVO) {
+    // 鑾峰彇褰撳墠鐢ㄦ埛鍚嶅拰鐢ㄦ埛ID
         String username = SecurityUtils.getUsername();
         Long userId = SecurityUtils.getUserId();
+    // 鏍¢獙鐢ㄦ埛ID鏄惁涓虹┖鎴栦负0
         if(userId==null || userId==0){
             log.error("insertSysTask 鐢ㄦ埛ID涓虹┖ userName:{}",username);
             return 0;
         }
         SysTask task = new SysTask();
+    // 鍒涘缓鏂扮殑浠诲姟瀵硅薄
         task.setTaskCode(generateTaskCode());
-        task.setTaskType(createVO.getTaskType());
-        task.setTaskStatus(TaskStatus.PENDING.getCode());
-        task.setTaskDescription(createVO.getTaskDescription());
-        task.setPlannedStartTime(createVO.getPlannedStartTime());
-        task.setPlannedEndTime(createVO.getPlannedEndTime());
-        task.setAssigneeId(createVO.getAssigneeId());
+
+    // 璁剧疆浠诲姟鍩烘湰淇℃伅
+        task.setTaskType(createVO.getTaskType()); // 鐢熸垚浠诲姟缂栫爜
+        task.setTaskStatus(TaskStatus.PENDING.getCode()); // 璁剧疆浠诲姟绫诲瀷
+        task.setTaskDescription(createVO.getTaskDescription()); // 璁剧疆浠诲姟鐘舵�佷负寰呭鐞�
+        task.setPlannedStartTime(createVO.getPlannedStartTime()); // 璁剧疆浠诲姟鎻忚堪
+        task.setPlannedEndTime(createVO.getPlannedEndTime()); // 璁剧疆璁″垝寮�濮嬫椂闂�
+        task.setAssigneeId(createVO.getAssigneeId()); // 璁剧疆璁″垝缁撴潫鏃堕棿
+ // 璁剧疆鎸囨淳浜篒D
         task.setCreatorId(userId);
+    // 璁剧疆鍒涘缓浜轰俊鎭�
         // 浼樺厛浣跨敤鍓嶇浼犲叆鐨勯儴闂↖D锛屽鏋滄病鏈夊垯浣跨敤褰撳墠鐢ㄦ埛鐨勯儴闂↖D
         task.setDeptId(createVO.getDeptId() != null ? createVO.getDeptId() : SecurityUtils.getDeptId());
         task.setCreateBy(username);
@@ -167,6 +283,7 @@
         task.setUpdateTime(DateUtils.getNowDate());
         task.setRemark(createVO.getRemark());
         task.setDelFlag("0");
+
         
         // 璁剧疆鍦板潃鍜屽潗鏍囦俊鎭�
         setAddressAndCoordinatesFromVO(task, createVO);
@@ -215,10 +332,7 @@
             ));
         }
         
-        // 鍙戝竷浠诲姟鍒嗛厤浜嬩欢
-        if (result > 0 && createVO.getAssignees() != null && !createVO.getAssignees().isEmpty()) {
-            this.sendTaskAssigneeEvent(createVO,task,SecurityUtils.getUserId(),SecurityUtils.getUsername());
-        }
+
         
         // 寮傛鍚屾鎬ユ晳杞繍浠诲姟鍒版棫绯荤粺
         if (result > 0 && "EMERGENCY_TRANSFER".equals(createVO.getTaskType()) && legacySystemSyncService != null) {
@@ -247,8 +361,7 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
-    public int insertTask(TaskCreateVO createVO,String serviceOrderId,String dispatchOrderId, String serviceOrdNo, Long userId,String userName, Long deptId, Date createTime, Date updateTime) {
+    public int insertTask(TaskCreateVO createVO,Long serviceOrderId,Long dispatchOrderId, String serviceOrdNo, Long userId,String userName, Long deptId, Date createTime, Date updateTime) {
         SysTask task = new SysTask();
         if(createVO.getTaskCode()!=null){
             task.setTaskCode(createVO.getTaskCode());
@@ -273,8 +386,6 @@
         task.setUpdateBy(userName);
         task.setRemark(createVO.getRemark());
         task.setDelFlag("0");
-
-
         
         // 璁剧疆鍦板潃鍜屽潗鏍囦俊鎭�
         setAddressAndCoordinatesFromVO(task, createVO);
@@ -294,9 +405,7 @@
         if (createVO.getEndAddress() != null) {
             task.setDestinationAddress(createVO.getEndAddress());
         }
-        if (createVO.getDistance() != null) {
-            task.setEstimatedDistance(createVO.getDistance());
-        }
+
         // 鑷姩濉厖缂哄け鐨凣PS鍧愭爣
         autoFillMissingGpsCoordinates(task);
         
@@ -341,22 +450,20 @@
             ));
         }
         
-        // 鍙戝竷浠诲姟鍒嗛厤浜嬩欢
-        if (result > 0 && createVO.getAssignees() != null && !createVO.getAssignees().isEmpty()) {
-            this.sendTaskAssigneeEvent(createVO,task,userId,userName);
-        }
-        
 
+        if(result>0) {
+            this.sendEmeryTaskProcess(task, dispatchOrderId);
+        }
         
         return result;
     }
 
     private void sendTaskAssigneeEvent(TaskCreateVO createVO,SysTask task,Long userId,String userName){
         List<Long> assigneeIds = createVO.getAssignees().stream()
-                .map(assignee -> assignee.getUserId())
+                .map(TaskCreateVO.AssigneeInfo::getUserId)
                 .collect(Collectors.toList());
         List<String> assigneeNames = createVO.getAssignees().stream()
-                .map(assignee -> assignee.getUserName())
+                .map(TaskCreateVO.AssigneeInfo::getUserName)
                 .collect(Collectors.toList());
 
         eventPublisher.publishEvent(new TaskAssignedEvent(
@@ -377,16 +484,14 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
     public int updateSysTask(TaskUpdateVO updateVO, Boolean updateFromLegacy) {
-        SysTask oldTask = sysTaskMapper.selectSysTaskByTaskId(updateVO.getTaskId());
-        if (oldTask == null) {
+        SysTask task = sysTaskMapper.selectSysTaskByTaskId(updateVO.getTaskId());
+        if (task == null) {
             throw new RuntimeException("浠诲姟涓嶅瓨鍦�");
         }
         Long userId = SecurityUtils.getUserId();
         String userName = SecurityUtils.getUsername();
-        
-        SysTask task = new SysTask();
+
         task.setTaskId(updateVO.getTaskId());
         task.setTaskDescription(updateVO.getTaskDescription());
         task.setPlannedStartTime(updateVO.getPlannedStartTime());
@@ -395,41 +500,32 @@
         task.setUpdateBy(userName);
         task.setUpdateTime(updateVO.getUpdateTime() != null ? updateVO.getUpdateTime() : DateUtils.getNowDate());
         task.setRemark(updateVO.getRemark());
+
+
         
-        // 璁剧疆閫氱敤鍦板潃鍜屽潗鏍囦俊鎭�
-        task.setDepartureAddress(updateVO.getDepartureAddress());
-        task.setDestinationAddress(updateVO.getDestinationAddress());
-        task.setDepartureLongitude(updateVO.getDepartureLongitude());
-        task.setDepartureLatitude(updateVO.getDepartureLatitude());
-        task.setDestinationLongitude(updateVO.getDestinationLongitude());
-        task.setDestinationLatitude(updateVO.getDestinationLatitude());
+
         
         // 璁剧疆棰勮璺濈
-        if (updateVO.getEstimatedDistance() != null) {
-            task.setEstimatedDistance(updateVO.getEstimatedDistance());
-        } else if (updateVO.getTransferDistance() != null) {
-            // 鍏煎鎬ユ晳杞繍瀛楁
+        if (updateVO.getTransferDistance() != null) {
             task.setEstimatedDistance(updateVO.getTransferDistance());
-        } else if (updateVO.getDistance() != null) {
-            // 鍏煎绂忕杞﹀瓧娈�
-            task.setEstimatedDistance(updateVO.getDistance());
         }
         
         // 濡傛灉鏇存柊浜嗛儴闂↖D
         if (updateVO.getDeptId() != null) {
             task.setDeptId(updateVO.getDeptId());
         }
-        
         // 濡傛灉鏇存柊浜嗕换鍔$紪鍙�
         if (updateVO.getTaskCode() != null) {
             task.setTaskCode(updateVO.getTaskCode());
         }
-        
+        Boolean hasSetDepartureFlag=false;
+        //璁剧疆鎬婚儴鎺ㄩ��
+
         // 鑷姩鑾峰彇鍑哄彂鍦癎PS鍧愭爣锛堝鏋滄洿鏂颁簡鍦板潃浣嗙己澶卞潗鏍囷級
         if (updateVO.getDepartureAddress() != null && 
             (updateVO.getDepartureLongitude() == null || updateVO.getDepartureLatitude() == null) && 
             mapService != null) {
-            if (!updateVO.getDepartureAddress().equals(oldTask.getDepartureAddress())) {
+            if (!updateVO.getDepartureAddress().equals(task.getDepartureAddress())) {
                 try {
                     Map<String, Double> coords = mapService.geocoding(
                         updateVO.getDepartureAddress(),
@@ -438,6 +534,7 @@
                     if (coords != null) {
                         task.setDepartureLongitude(BigDecimal.valueOf(coords.get("lng")));
                         task.setDepartureLatitude(BigDecimal.valueOf(coords.get("lat")));
+                        hasSetDepartureFlag = true;
 //                        log.info("鍑哄彂鍦癎PS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
                     }
                 } catch (Exception e) {
@@ -445,12 +542,20 @@
                 }
             }
         }
-        
+        // 璁剧疆閫氱敤鍦板潃鍜屽潗鏍囦俊鎭�
+        task.setDepartureAddress(updateVO.getDepartureAddress());
+        if(!hasSetDepartureFlag) {
+
+            task.setDepartureLongitude(updateVO.getDepartureLongitude());
+            task.setDepartureLatitude(updateVO.getDepartureLatitude());
+        }
+
+        Boolean hasSetDestinationFlag=false;
         // 鑷姩鑾峰彇鐩殑鍦癎PS鍧愭爣锛堝鏋滄洿鏂颁簡鍦板潃浣嗙己澶卞潗鏍囷級
         if (updateVO.getDestinationAddress() != null && 
             (updateVO.getDestinationLongitude() == null || updateVO.getDestinationLatitude() == null) && 
             mapService != null) {
-            if (!updateVO.getDestinationAddress().equals(oldTask.getDestinationAddress())) {
+            if (!updateVO.getDestinationAddress().equals(task.getDestinationAddress())) {
                 try {
                     Map<String, Double> coords = mapService.geocoding(
                         updateVO.getDestinationAddress(),
@@ -459,6 +564,7 @@
                     if (coords != null) {
                         task.setDestinationLongitude(BigDecimal.valueOf(coords.get("lng")));
                         task.setDestinationLatitude(BigDecimal.valueOf(coords.get("lat")));
+                        hasSetDestinationFlag = true;
 //                        log.info("鐩殑鍦癎PS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
                     }
                 } catch (Exception e) {
@@ -466,8 +572,19 @@
                 }
             }
         }
+        task.setDestinationAddress(updateVO.getDestinationAddress());
+        if(!hasSetDestinationFlag) {
+            task.setDestinationLongitude(updateVO.getDestinationLongitude());
+            task.setDestinationLatitude(updateVO.getDestinationLatitude());
+        }
+
+        if(updateVO.getAssignees()!=null && !updateVO.getAssignees().isEmpty()){
+           TaskCreateVO.AssigneeInfo assigneeInfo= updateVO.getAssignees().get(0);
+            task.setAssigneeId(assigneeInfo.getUserId());
+            task.setAssigneeName(assigneeInfo.getUserName());
+        }
         // 鐢ㄤ簬璺熻釜鏄惁闇�瑕侀噸鏂板悓姝ワ紙杞﹁締銆佷汉鍛樸�佸湴鍧�銆佹垚浜や环鍙樻洿锛�
-        boolean needResync = false;
+        boolean needResync = true;
         int result = sysTaskMapper.updateSysTask(task);
         
 
@@ -481,7 +598,8 @@
                 needResync = true;
             }
         }
-        
+
+
         // 鏇存柊鎵ц浜哄憳锛堟娴嬩汉鍛樺彉鏇达級
         if (result > 0 && updateVO.getAssignees() != null) {
             boolean assigneesChanged = sysTaskAssigneeService.updateTaskAssignees(
@@ -491,41 +609,54 @@
                 needResync = true;
             }
         }
-        
+        Long dispatchOrderId=0L;
         // 鏇存柊鎬ユ晳杞繍鎵╁睍淇℃伅锛堟娴嬪湴鍧�鍜屾垚浜や环鍙樻洿锛�
-        if (result > 0 && "EMERGENCY_TRANSFER".equals(oldTask.getTaskType())) {
+        if (result > 0 && "EMERGENCY_TRANSFER".equals(task.getTaskType())) {
             SysTaskEmergency oldEmergency = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(updateVO.getTaskId());
             sysEmergencyTaskService.updateEmergencyInfoFromUpdateVO(oldEmergency, updateVO, userName);
-            sysEmergencyTaskService.markNeedResyncIfNecessary(updateVO.getTaskId(), oldTask, updateVO, updateFromLegacy);
+          dispatchOrderId=  oldEmergency.getLegacyDispatchOrdId();
+
+            markNeedSync(task,updateVO);
         }
         
         // 鏇存柊绂忕杞︽墿灞曚俊鎭�
-        if (result > 0 && "WELFARE".equals(oldTask.getTaskType())) {
+        if (result > 0 && "WELFARE".equals(task.getTaskType())) {
             if (updateVO.getPassenger() != null || updateVO.getStartAddress() != null || updateVO.getEndAddress() != null) {
                 sysWelfareTaskService.updateWelfareInfo(updateVO.getTaskId(), updateVO, userName);
             }
         }
         
-        // 濡傛灉鏄�ユ晳杞繍浠诲姟涓旀湁鍙樻洿锛屾爣璁伴渶瑕侀噸鏂板悓姝�
-        if (result > 0 && "EMERGENCY_TRANSFER".equals(oldTask.getTaskType()) && needResync && !updateFromLegacy) {
-            sysEmergencyTaskService.markNeedResyncIfNecessary(updateVO.getTaskId(), oldTask, updateVO, updateFromLegacy);
-        }
+
         
         // 璁板綍鎿嶄綔鏃ュ織
         if (result > 0) {
             recordTaskLog(updateVO.getTaskId(), "UPDATE", "鏇存柊浠诲姟",
-                buildTaskDescription(oldTask), buildTaskDescription(task),
+                buildTaskDescription(task), buildTaskDescription(task),
                 userId, userName);
         }
 
-        if(result > 0 && oldTask.getTaskStatus().equals(TaskStatus.PENDING.getCode()) && updateVO.getAssignees() != null && !updateVO.getAssignees().isEmpty()){
+        if(result > 0 && task.getTaskStatus().equals(TaskStatus.PENDING.getCode())
+                && updateVO.getAssignees() != null
+                && !updateVO.getAssignees().isEmpty()
+                && LongUtil.isNotEmpty(dispatchOrderId)){
 
-            this.sendTaskAssigneeEvent(updateVO,oldTask,userId,userName);
+            this.sendTaskAssigneeEvent(updateVO,task,userId,userName);
         }
 
         return result;
     }
 
+
+    private void markNeedSync(SysTask sysTask,TaskUpdateVO updateVO){
+        // 濡傛灉鏄�ユ晳杞繍浠诲姟涓旀湁鍙樻洿锛屾爣璁伴渶瑕侀噸鏂板悓姝�
+        if ( "EMERGENCY_TRANSFER".equals(sysTask.getTaskType()) ) {
+
+            sysEmergencyTaskService.markNeedResyncIfNecessary(updateVO.getTaskId(), sysTask, updateVO, true);
+
+            eventPublisher.publishEvent(new TaskUpdateEvent(this,sysTask.getTaskId(),
+                    sysTask.getTaskCode(),sysTask.getTaskType()));
+        }
+    }
     /**
      * 鏇存柊浠诲姟锛堢敤浜庢棫绯荤粺鍚屾锛�
      * 
@@ -541,15 +672,17 @@
      * @return 缁撴灉
      */
     @Override
-    public int updateTask(TaskUpdateVO updateVO, String serviceOrderId, String dispatchOrderId, String serviceOrdNo,
+    public int updateTask(TaskUpdateVO updateVO, Long serviceOrderId, Long dispatchOrderId, String serviceOrdNo,
                          Long userId, String userName, Long deptId, Date createTime, Date updateTime) {
 //        log.info("寮�濮嬫洿鏂颁换鍔� ServiceOrdID: {} , dispatchOrdId:{}", serviceOrderId,dispatchOrderId);
         // 閫氳繃鏃х郴缁熸湇鍔″崟ID鏌ユ壘浠诲姟
-        SysTaskEmergency taskEmergency = sysTaskEmergencyMapper.selectByLegacyServiceOrdId(Long.parseLong(serviceOrderId));
+        // 鑾峰彇鏃т换鍔′俊鎭紝鐢ㄤ簬鍒ゆ柇鍦板潃鏄惁鍙樻洿
+
+
+        SysTaskEmergency taskEmergency = sysTaskEmergencyMapper.selectByLegacyServiceOrdId(serviceOrderId);
         Long taskId = taskEmergency.getTaskId();
+        SysTask task = sysTaskMapper.selectSysTaskByTaskId(taskId);
         updateVO.setTaskId(taskId);
-        SysTask task = new SysTask();
-        task.setTaskId(taskId);
         if(updateVO.getTaskStatus()!=null){
             task.setTaskStatus(updateVO.getTaskStatus());
         }
@@ -562,20 +695,24 @@
         if(updateVO.getActualEndTime() != null) {
             task.setActualEndTime(updateVO.getActualEndTime());
         }
-        task.setAssigneeId(updateVO.getAssigneeId());
+        if(deptId!=null){
+            task.setDeptId(deptId);
+        }
+
+
+//        task.setAssigneeId(updateVO.getAssigneeId());
         task.setUpdateBy(userName);
         task.setUpdateTime(DateUtils.getNowDate());
         task.setRemark(updateVO.getRemark());
 
         
         // 璁剧疆鍦板潃鍜屽潗鏍囦俊鎭�
-        task.setDepartureAddress(updateVO.getDepartureAddress());
-        task.setDestinationAddress(updateVO.getDestinationAddress());
-        task.setDepartureLongitude(updateVO.getDepartureLongitude());
-        task.setDepartureLatitude(updateVO.getDepartureLatitude());
-        task.setDestinationLongitude(updateVO.getDestinationLongitude());
-        task.setDestinationLatitude(updateVO.getDestinationLatitude());
-        
+
+        if(updateVO.getAssignees()!=null && !updateVO.getAssignees().isEmpty()){
+            TaskCreateVO.AssigneeInfo assigneeInfo= updateVO.getAssignees().get(0);
+            task.setAssigneeId(assigneeInfo.getUserId());
+            task.setAssigneeName(assigneeInfo.getUserName());
+        }
         // 濡傛灉鏇存柊浜嗛儴闂↖D
         if (updateVO.getDeptId() != null) {
             task.setDeptId(updateVO.getDeptId());
@@ -585,13 +722,11 @@
         if (updateVO.getTaskCode() != null) {
             task.setTaskCode(updateVO.getTaskCode());
         }
-        
-        // 鑾峰彇鏃т换鍔′俊鎭紝鐢ㄤ簬鍒ゆ柇鍦板潃鏄惁鍙樻洿
-        SysTask oldTask = sysTaskMapper.selectSysTaskByTaskId(taskId);
-        
+
+        Boolean modifyOutLongLat = false;
         // 鑷姩鑾峰彇鍑哄彂鍦癎PS鍧愭爣锛堝鏋滃湴鍧�鍙樻洿涓旂己澶卞潗鏍囷級
-        if (oldTask != null && updateVO.getDepartureAddress() != null
-            && !updateVO.getDepartureAddress().equals(oldTask.getDepartureAddress())
+        if (task != null && updateVO.getDepartureAddress() != null
+            && !updateVO.getDepartureAddress().equals(task.getDepartureAddress())
             && (updateVO.getDepartureLongitude() == null || updateVO.getDepartureLatitude() == null)
             && mapService != null) {
             try {
@@ -602,16 +737,23 @@
                 if (coords != null) {
                     task.setDepartureLongitude(BigDecimal.valueOf(coords.get("lng")));
                     task.setDepartureLatitude(BigDecimal.valueOf(coords.get("lat")));
+                    modifyOutLongLat = true;
 //                    log.info("鍑哄彂鍦癎PS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
                 }
             } catch (Exception e) {
                 log.error("鑷姩鑾峰彇鍑哄彂鍦癎PS鍧愭爣澶辫触", e);
             }
         }
-        
+        task.setDepartureAddress(updateVO.getDepartureAddress());
+        if(!modifyOutLongLat){
+            task.setDepartureLongitude(updateVO.getDepartureLongitude());
+            task.setDepartureLatitude(updateVO.getDepartureLatitude());
+        }
+
+        Boolean modifyInLongLat = false;
         // 鑷姩鑾峰彇鐩殑鍦癎PS鍧愭爣锛堝鏋滃湴鍧�鍙樻洿涓旂己澶卞潗鏍囷級
-        if (oldTask != null && updateVO.getDestinationAddress() != null
-            && !updateVO.getDestinationAddress().equals(oldTask.getDestinationAddress())
+        if (task != null && updateVO.getDestinationAddress() != null
+            && !updateVO.getDestinationAddress().equals(task.getDestinationAddress())
             && (updateVO.getDestinationLongitude() == null || updateVO.getDestinationLatitude() == null)
             && mapService != null) {
             try {
@@ -622,11 +764,17 @@
                 if (coords != null) {
                     task.setDestinationLongitude(BigDecimal.valueOf(coords.get("lng")));
                     task.setDestinationLatitude(BigDecimal.valueOf(coords.get("lat")));
+                    modifyInLongLat = true;
 //                    log.info("鐩殑鍦癎PS鍧愭爣鑷姩鑾峰彇鎴愬姛: {}, {}", coords.get("lng"), coords.get("lat"));
                 }
             } catch (Exception e) {
                 log.error("鑷姩鑾峰彇鐩殑鍦癎PS鍧愭爣澶辫触", e);
             }
+        }
+        task.setDestinationAddress(updateVO.getDestinationAddress());
+        if(!modifyInLongLat){
+            task.setDestinationLongitude(updateVO.getDestinationLongitude());
+            task.setDestinationLatitude(updateVO.getDestinationLatitude());
         }
         
         int result = sysTaskMapper.updateSysTask(task);
@@ -646,16 +794,14 @@
 //            log.info("鏇存柊鎵ц浜哄憳 ServiceOrdID:{},dispatchOrderId:{}",serviceOrderId,dispatchOrderId);
             sysTaskAssigneeService.updateTaskAssignees(taskId, updateVO.getAssignees(), userName);
         }
-        
+
         // 鏇存柊鎬ユ晳杞繍鎵╁睍淇℃伅
         if (result > 0) {
             // 鏇存柊鏃х郴缁烮D
-            if (serviceOrderId != null) {
-                taskEmergency.setLegacyServiceOrdId(Long.parseLong(serviceOrderId));
-            }
-            if (dispatchOrderId != null) {
-                taskEmergency.setLegacyDispatchOrdId(Long.parseLong(dispatchOrderId));
-                taskEmergency.setLegacyDispatchOrdId(Long.parseLong(dispatchOrderId));
+            taskEmergency.setLegacyServiceOrdId(serviceOrderId);
+            if (LongUtil.isNotEmpty(dispatchOrderId)) {
+
+                taskEmergency.setLegacyDispatchOrdId(dispatchOrderId);
                 taskEmergency.setDispatchSyncStatus(2);
                 taskEmergency.setDispatchSyncTime(new Date());
                 taskEmergency.setDispatchSyncErrorMsg("鏃х郴缁熷悓姝ヨ繃鏉�");
@@ -665,23 +811,43 @@
             }
             taskEmergency.setUpdateTime(DateUtils.getNowDate());
 
-            Boolean hasEmergencyInfo = updateVO.getHospitalOut() != null || updateVO.getHospitalIn() != null || updateVO.getPatient() != null;
-//            log.info("鏇存柊杞繍浠诲姟淇℃伅 serviceOrdID:{},dispatchOrderId:{} hasEmergencyInfo:{}",serviceOrderId,
-//                    dispatchOrderId,hasEmergencyInfo);
+            Boolean hasEmergencyInfo = updateVO.getHospitalOut() != null || updateVO.getHospitalIn() != null || updateVO.getPatient() != null
+                    || updateVO.getPrice() != null || updateVO.getTransferDistance() != null;
+//            Boolean isHeadPush=isTaskHeaderPush(task.getCreatorId(),task.getDeptId());
+//            updateVO.setFromHQ2_is(isHeadPush?"1":"0");
 
             // 浣跨敤TaskCreateVO鐨勫瓧娈垫潵鏇存柊鎬ユ晳杞繍淇℃伅
             if (hasEmergencyInfo) {
                 sysEmergencyTaskService.updateEmergencyInfoFromCreateVO(taskEmergency, updateVO, userName);
             }
+           SysTaskEmergency emergency= sysEmergencyTaskService.selectSysTaskEmergencyByTaskId(taskId);
+            dispatchOrderId = emergency.getLegacyDispatchOrdId();
         }
 
-        if(updateVO.getTaskStatus()!=null && updateVO.getTaskStatus().equals(TaskStatus.PENDING.getCode()) && updateVO.getAssignees()!=null && !updateVO.getAssignees().isEmpty()){
+        if(updateVO.getTaskStatus()!=null
+                && updateVO.getTaskStatus().equals(TaskStatus.PENDING.getCode())
+                && updateVO.getAssignees()!=null && !updateVO.getAssignees().isEmpty()
+                && LongUtil.isNotEmpty(dispatchOrderId)){
             this.sendTaskAssigneeEvent(updateVO,task,userId,userName);
         }
-        
+
         return result;
     }
 
+    private void sendEmeryTaskProcess(SysTask task,Long dispatchOrderId){
+        Long taskId = task.getTaskId();
+        String taskCode = task.getShowTaskCode();
+        if(task.getTaskStatus()!=null && task.getTaskStatus().equals(TaskStatus.PENDING.getCode())){
+            //濡傛灉娌℃湁鍒嗛厤浜哄憳锛屼笖娌℃湁璋冨害鍗曪紝涓嶆槸骞垮窞鎬诲叕鍙哥殑灏遍渶瑕佸彂閫侀�氱煡璺熻繘
+            if(LongUtil.isEmpty(dispatchOrderId)){
+                if(!task.getDeptId().equals(DeptUtil.GUANGZHOU_DEPT_ID) && (task.getAssignees()==null  || task.getAssignees().isEmpty())){
+                    //鍙戦�侀�氱煡
+                    eventPublisher.publishEvent(new TaskOnlyServerOrderSyncEvent(this, taskId, taskCode));
+
+                }
+            }
+        }
+    }
     /**
      * 鎵归噺鍒犻櫎浠诲姟绠$悊
      * 
@@ -689,7 +855,7 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
+    
     public int deleteSysTaskByTaskIds(Long[] taskIds) {
         int result = 0;
         for (Long taskId : taskIds) {
@@ -710,7 +876,7 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
+    
     public int assignTask(Long taskId, Long assigneeId, String remark) {
         SysTask task = sysTaskMapper.selectSysTaskByTaskId(taskId);
         if (task == null) {
@@ -762,6 +928,67 @@
     @Override
     public int changeTaskStatus(Long taskId, TaskStatus newStatus, String remark) {
         return changeTaskStatusWithLocation(taskId, newStatus, remark, null);
+    }
+
+    /**
+     * 寮哄埗瀹屾垚浠诲姟锛堟寚瀹氬疄闄呭紑濮嬫椂闂村拰缁撴潫鏃堕棿锛�
+     * 
+     * @param task 浠诲姟淇℃伅
+     * @return 缁撴灉
+     */
+    @Override
+    public int forceCompleteTask(SysTask task) {
+        if (task == null || task.getTaskId() == null) {
+            throw new RuntimeException("浠诲姟淇℃伅涓嶈兘涓虹┖");
+        }
+        
+        SysTask oldTask = sysTaskMapper.selectSysTaskByTaskId(task.getTaskId());
+        if (oldTask == null) {
+            throw new RuntimeException("浠诲姟涓嶅瓨鍦�");
+        }
+        
+        // 鏍¢獙寮�濮嬫椂闂村拰缁撴潫鏃堕棿
+        if (task.getActualStartTime() == null || task.getActualEndTime() == null) {
+            throw new RuntimeException("瀹為檯寮�濮嬫椂闂村拰缁撴潫鏃堕棿涓嶈兘涓虹┖");
+        }
+        
+        if (task.getActualStartTime().after(task.getActualEndTime())) {
+            throw new RuntimeException("缁撴潫鏃堕棿蹇呴』澶т簬寮�濮嬫椂闂�");
+        }
+        
+        // 璁板綍鏃х姸鎬�
+        String oldStatus = oldTask.getTaskStatus();
+        TaskStatus oldTaskStatus = TaskStatus.getByCode(oldStatus);
+        
+        // 鏇存柊浠诲姟
+        int result = sysTaskMapper.updateTaskStatus(task);
+        
+        // 璁板綍鎿嶄綔鏃ュ織
+        if (result > 0) {
+            recordTaskLog(task.getTaskId(), "FORCE_COMPLETE", "寮哄埗瀹屾垚浠诲姟", 
+                         oldStatus, task.getTaskStatus(), 
+                         SecurityUtils.getUserId(), SecurityUtils.getUsername());
+            
+            // 鍙戝竷浠诲姟鐘舵�佸彉鏇翠簨浠�
+            TaskStatus newTaskStatus = TaskStatus.getByCode(task.getTaskStatus());
+            eventPublisher.publishEvent(new TaskStatusChangedEvent(
+                this,
+                task.getTaskId(),
+                oldTask.getTaskCode(),
+                oldStatus,
+                task.getTaskStatus(),
+                oldTaskStatus != null ? oldTaskStatus.getInfo() : "鏈煡",
+                newTaskStatus != null ? newTaskStatus.getInfo() : "鏈煡",
+                null, // assigneeIds
+                SecurityUtils.getUserId(),
+                SecurityUtils.getUserId(),
+                null, // longitude
+                null, // latitude
+                null  // address
+            ));
+        }
+        
+        return result;
     }
 
     /**
@@ -850,6 +1077,71 @@
     }
 
     /**
+     * 淇濆瓨浠诲姟鍙栨秷淇℃伅锛堜粎闄愯浆杩愪换鍔★級
+     * 
+     * @param taskId 浠诲姟ID
+     * @param cancelReason 鍙栨秷鍘熷洜锛堟暟鎹瓧鍏竧ask_cancel_reason鐨剉alue锛�
+     * @return 缁撴灉
+     */
+    @Override
+    public int saveCancelInfo(Long taskId, String cancelReason) {
+        // 鑾峰彇浠诲姟淇℃伅
+        SysTask task = sysTaskMapper.selectSysTaskByTaskId(taskId);
+        if (task == null) {
+            throw new RuntimeException("浠诲姟涓嶅瓨鍦�");
+        }
+        
+        // 鍙湁杞繍浠诲姟鎵嶄繚瀛樺彇娑堜俊鎭�
+        if (!"EMERGENCY_TRANSFER".equals(task.getTaskType())) {
+            return 0;
+        }
+        
+        // 鑾峰彇杞繍浠诲姟鎵╁睍淇℃伅
+        SysTaskEmergency emergency = sysEmergencyTaskService.selectSysTaskEmergencyByTaskId(taskId);
+        if (emergency == null) {
+            return 0;
+        }
+        
+        // 璁剧疆鍙栨秷淇℃伅
+        emergency.setCancelReason(cancelReason);
+        emergency.setCancelBy(SecurityUtils.getNickName());
+        emergency.setCancelTime(DateUtils.getNowDate());
+        emergency.setUpdateBy(SecurityUtils.getUsername());
+        emergency.setUpdateTime(DateUtils.getNowDate());
+        
+        // 鏇存柊鏁版嵁搴�
+        return sysTaskEmergencyMapper.updateSysTaskEmergency(emergency);
+    }
+
+    @Override
+    public int saveCancel(Long taskId, String cancelReason, String cancelBy,Date cancelTime) {
+        SysTask task = sysTaskMapper.selectSysTaskByTaskId(taskId);
+        if (task == null) {
+            throw new RuntimeException("浠诲姟涓嶅瓨鍦�");
+        }
+
+        // 鍙湁杞繍浠诲姟鎵嶄繚瀛樺彇娑堜俊鎭�
+        if (!"EMERGENCY_TRANSFER".equals(task.getTaskType())) {
+            return 0;
+        }
+
+        // 鑾峰彇杞繍浠诲姟鎵╁睍淇℃伅
+        SysTaskEmergency emergency = sysEmergencyTaskService.selectSysTaskEmergencyByTaskId(taskId);
+        if (emergency == null) {
+            return 0;
+        }
+
+        // 璁剧疆鍙栨秷淇℃伅
+        emergency.setCancelReason(cancelReason);
+        emergency.setCancelBy(cancelBy);
+        emergency.setCancelTime(cancelTime);
+        emergency.setUpdateTime(DateUtils.getNowDate());
+
+        // 鏇存柊鏁版嵁搴�
+        return sysTaskEmergencyMapper.updateSysTaskEmergency(emergency);
+    }
+
+    /**
      * 涓婁紶浠诲姟闄勪欢
      * 
      * @param taskId 浠诲姟ID
@@ -858,7 +1150,7 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
+    
     public Long uploadAttachment(Long taskId, MultipartFile file, String category) {
         return sysTaskAttachmentService.uploadAttachment(taskId, file, category);
     }
@@ -873,7 +1165,7 @@
      * @return 杩斿洖闄勪欢ID
      */
     @Override
-    @Transactional
+    
     public Long uploadAttachmentFromWechat(Long taskId, String accessToken, String mediaId, String category) {
         return sysTaskAttachmentService.uploadAttachmentFromWechat(taskId, accessToken, mediaId, category);
     }
@@ -886,7 +1178,7 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
+    
     public int deleteAttachment(Long attachmentId) {
         return sysTaskAttachmentService.deleteAttachment(attachmentId);
     }
@@ -916,7 +1208,7 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
+    
     public int assignVehicleToTask(Long taskId, Long vehicleId, String remark,Long userId,String userName) {
         int result = sysTaskVehicleService.assignVehicleToTask(taskId, vehicleId, remark, userId, userName);
         
@@ -938,7 +1230,7 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
+    
     public int unassignVehicleFromTask(Long taskId, Long vehicleId) {
         int result = sysTaskVehicleService.unassignVehicleFromTask(taskId, vehicleId);
         
@@ -961,7 +1253,7 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
+    
     public int assignMultipleVehiclesToTask(Long taskId, List<Long> vehicleIds, String remark,Long userId,String userName) {
         int result = sysTaskVehicleService.assignMultipleVehiclesToTask(taskId, vehicleIds, remark, userId, userName);
         
@@ -1026,7 +1318,14 @@
      */
     @Override
     public List<SysTask> selectMyTasks(Long userId) {
-        return sysTaskMapper.selectMyTasks(userId);
+        List<SysTask> list = sysTaskMapper.selectMyTasks(userId);
+        list.stream().forEach(task -> {
+            bindTaskAssign(task);
+            if(task.getTaskType().equals("EMERGENCY_TRANSFER")){
+                task.setEmergencyInfo(sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(task.getTaskId()));
+            }
+        });
+        return list;
     }
 
     /**
@@ -1247,8 +1546,8 @@
         if (createVO.getDestinationLatitude() != null) {
             task.setDestinationLatitude(createVO.getDestinationLatitude());
         }
-        if (createVO.getEstimatedDistance() != null) {
-            task.setEstimatedDistance(createVO.getEstimatedDistance());
+        if (createVO.getTransferDistance() != null) {
+            task.setEstimatedDistance(createVO.getTransferDistance());
         }
     }
 
@@ -1277,9 +1576,7 @@
         if (createVO.getEndAddress() != null) {
             task.setDestinationAddress(createVO.getEndAddress());
         }
-        if (createVO.getDistance() != null) {
-            task.setEstimatedDistance(createVO.getDistance());
-        }
+
     }
 
     /**
@@ -1464,7 +1761,7 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
+    
     public AjaxResult setAssigneeReady(Long taskId, Long userId) {
         return sysTaskAssigneeService.setAssigneeReady(taskId, userId);
     }
@@ -1477,9 +1774,24 @@
      * @return 缁撴灉
      */
     @Override
-    @Transactional
+    
     public AjaxResult cancelAssigneeReady(Long taskId, Long userId) {
         return sysTaskAssigneeService.cancelAssigneeReady(taskId, userId);
     }
+    
+    /**
+     * 妫�鏌ヤ换鍔℃槸鍚﹂噸澶嶏紙鏍规嵁鑱旂郴浜虹數璇濆拰鍒涘缓鏃ユ湡锛�
+     * 
+     * @param phone 鑱旂郴浜虹數璇�
+     * @param createDate 浠诲姟鍒涘缓鏃ユ湡锛堟牸寮忥細YYYY-MM-DD锛�
+     * @return true-瀛樺湪閲嶅锛宖alse-涓嶉噸澶�
+     */
+    @Override
+    public boolean checkTaskDuplicate(String phone, String createDate) {
+        int count = sysTaskMapper.countTaskByPhoneAndDate(phone, createDate);
+        return count > 0;
+    }
+
+
    
 }

--
Gitblit v1.9.1