wlzboy
2025-11-29 364adbc9a93a396b74e154f910c2a0a72bfb1a0f
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysTaskServiceImpl.java
@@ -36,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;
@@ -84,6 +86,9 @@
    private ILegacySystemSyncService legacySystemSyncService;
    
    @Autowired
    private ISysTaskEmergencyService sysTaskEmergencyService;
    @Autowired
    private ApplicationEventPublisher eventPublisher;
    
    @Autowired
@@ -94,6 +99,9 @@
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired(required = false)
    private IMapService mapService;
    /**
     * 查询任务管理
@@ -202,6 +210,44 @@
        // 设置福祉车公里数
        if (createVO.getDistance() != null) {
            task.setEstimatedDistance(createVO.getDistance());
        }
        // 自动获取出发地GPS坐标(如果缺失)
        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("出发地GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
                }
            } catch (Exception e) {
                log.error("自动获取出发地GPS坐标失败", e);
            }
        }
        // 自动获取目的地GPS坐标(如果缺失)
        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("目的地GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
                }
            } catch (Exception e) {
                log.error("自动获取目的地GPS坐标失败", e);
            }
        }
        
        int result = sysTaskMapper.insertSysTask(task);
@@ -379,6 +425,44 @@
            task.setEstimatedDistance(createVO.getDistance());
        }
        
        // 自动获取出发地GPS坐标(如果缺失)
        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("出发地GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
                }
            } catch (Exception e) {
                log.error("自动获取出发地GPS坐标失败", e);
            }
        }
        // 自动获取目的地GPS坐标(如果缺失)
        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("目的地GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
                }
            } catch (Exception e) {
                log.error("自动获取目的地GPS坐标失败", e);
            }
        }
        int result = sysTaskMapper.insertSysTask(task);
        
        // 保存车辆关联信息
@@ -394,7 +478,7 @@
                taskVehicle.setCreateTime(createTime);
                taskVehicle.setUpdateBy(userName);
                taskVehicle.setUpdateTime(updateTime);
                sysTaskVehicleMapper.insertSysTaskVehicle(taskVehicle);
            }
        }
@@ -492,10 +576,51 @@
            task.setDeptId(updateVO.getDeptId());
        }
        
        // 自动获取出发地GPS坐标(如果更新了地址但缺失坐标)
        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("出发地GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
                }
            } catch (Exception e) {
                log.error("自动获取出发地GPS坐标失败", e);
            }
        }
        // 自动获取目的地GPS坐标(如果更新了地址但缺失坐标)
        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("目的地GPS坐标自动获取成功: {}, {}", coords.get("lng"), coords.get("lat"));
                }
            } catch (Exception e) {
                log.error("自动获取目的地GPS坐标失败", e);
            }
        }
        // 重新计算预计公里数
        calculateEstimatedDistance(task);
        
        int result = sysTaskMapper.updateSysTask(task);
        // 用于跟踪是否需要重新同步(车辆、人员、地址、成交价变更)
        boolean needResync = false;
        
        // 更新车辆关联
        if (result > 0 && updateVO.getVehicleIds() != null && !updateVO.getVehicleIds().isEmpty()) {
@@ -525,12 +650,98 @@
                    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) {
                // 标记失败不影响主流程
            }
        }
        
        // 记录操作日志
@@ -741,7 +952,7 @@
                recordTaskLog(taskId, "UPDATE", "上传附件", null, 
                             "上传文件:" + file.getOriginalFilename() + "(分类:" + categoryDesc + ")", 
                             SecurityUtils.getUserId(), SecurityUtils.getUsername());
            }
            
@@ -801,7 +1012,7 @@
                recordTaskLog(taskId, "UPDATE", "上传附件", null, 
                             "通过微信上传文件:" + fileName + "(分类:" + categoryDesc + ")", 
                             SecurityUtils.getUserId(), SecurityUtils.getUsername());
            }
            
@@ -1150,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);
@@ -1250,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;
    }
    /**
@@ -1434,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);
                }
            }
        }
        
        // 设置转入医院信息
@@ -1446,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);
                    }
                }
        }
        
        // 设置费用信息
@@ -1479,8 +1754,6 @@
            emergencyInfo.setDispatchSyncStatus(2);
            emergencyInfo.setDispatchSyncTime(new Date());
            emergencyInfo.setDispatchSyncErrorMsg("旧系统同步过来");
        // 系统字段
        }
        // 系统字段
        emergencyInfo.setCreateTime(DateUtils.getNowDate());
@@ -1548,6 +1821,23 @@
        }
        if (emergencyInfo.getHospitalOutAddress() != null) {
            existingInfo.setHospitalOutAddress(emergencyInfo.getHospitalOutAddress());
            // 如果更新了地址但没有GPS坐标,后端自动获取
            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());
@@ -1574,6 +1864,23 @@
        }
        if (emergencyInfo.getHospitalInAddress() != null) {
            existingInfo.setHospitalInAddress(emergencyInfo.getHospitalInAddress());
            // 如果更新了地址但没有GPS坐标,后端自动获取
            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());