修改签到和更新逻辑
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 8m53s
All checks were successful
构建并部署 Spring Boot 应用 / build-and-deploy (push) Successful in 8m53s
This commit is contained in:
@@ -68,18 +68,6 @@ public class DeliveryPersonController {
|
||||
response.setCurrentLocation(location);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新指定货运人员位置。
|
||||
* @param id 货运人员ID
|
||||
* @param locationRequest 包含经纬度
|
||||
* @return 操作结果
|
||||
*/
|
||||
@PutMapping("/{id}/location")
|
||||
public ResponseEntity<String> updateLocation(@PathVariable Long id, @RequestBody LocationRequest locationRequest) {
|
||||
deliveryPersonService.updateLocation(id, locationRequest.getLongitude(), locationRequest.getLatitude());
|
||||
return ResponseEntity.ok("位置更新成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取货运人员的当前订单。
|
||||
@@ -112,13 +100,4 @@ public class DeliveryPersonController {
|
||||
}).collect(Collectors.toList());
|
||||
return ResponseEntity.ok(orders);
|
||||
}
|
||||
|
||||
/**
|
||||
* 位置请求体
|
||||
*/
|
||||
@Data
|
||||
public static class LocationRequest {
|
||||
private Double longitude;
|
||||
private Double latitude;
|
||||
}
|
||||
}
|
@@ -1,91 +0,0 @@
|
||||
package com.light.delivery.controller;
|
||||
|
||||
import com.light.delivery.service.LocationWebSocketHandler;
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 位置同步控制器,提供已签到员工位置信息查询接口。
|
||||
* 用于获取所有已签到员工的实时位置信息。
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/location-sync")
|
||||
public class LocationSyncController {
|
||||
|
||||
/**
|
||||
* WebSocket位置处理器依赖注入
|
||||
*/
|
||||
@Autowired
|
||||
private LocationWebSocketHandler locationWebSocketHandler;
|
||||
|
||||
/**
|
||||
* 获取所有已签到员工的实时位置。
|
||||
* 仅返回有位置信息的员工(最近有更新位置的)。
|
||||
* @return 员工位置列表
|
||||
*/
|
||||
@GetMapping("/employees/locations")
|
||||
public ResponseEntity<List<EmployeeLocation>> getEmployeeLocations() {
|
||||
// 获取所有已签到的员工位置信息
|
||||
Map<Long, LocationWebSocketHandler.UserInfo> userInfos = locationWebSocketHandler.getAllSignedInUsers();
|
||||
|
||||
List<EmployeeLocation> locations = userInfos.entrySet().stream()
|
||||
.map(entry -> {
|
||||
Long userId = entry.getKey();
|
||||
LocationWebSocketHandler.UserInfo userInfo = entry.getValue();
|
||||
|
||||
EmployeeLocation location = new EmployeeLocation();
|
||||
location.setEmployeeId(userId);
|
||||
location.setLatitude(locationWebSocketHandler.getUserLatitude(userId));
|
||||
location.setLongitude(locationWebSocketHandler.getUserLongitude(userId));
|
||||
location.setStatus(locationWebSocketHandler.getUserStatus(userId));
|
||||
location.setRole(userInfo.getRole() != null ? userInfo.getRole() : "");
|
||||
location.setName(userInfo.getName() != null ? userInfo.getName() : "");
|
||||
return location;
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return ResponseEntity.ok(locations);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在线员工位置信息DTO,用于向前端返回员工位置数据。
|
||||
*/
|
||||
@Data
|
||||
public static class EmployeeLocation {
|
||||
/**
|
||||
* 员工ID
|
||||
*/
|
||||
private Long employeeId;
|
||||
|
||||
/**
|
||||
* 姓名
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 纬度
|
||||
*/
|
||||
private Double latitude;
|
||||
|
||||
/**
|
||||
* 经度
|
||||
*/
|
||||
private Double longitude;
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private String status;
|
||||
|
||||
/**
|
||||
* 角色
|
||||
*/
|
||||
private String role;
|
||||
}
|
||||
}
|
@@ -180,15 +180,26 @@ public class UserController {
|
||||
/**
|
||||
* 用户签到接口。
|
||||
* @param request HTTP请求对象,用于提取认证令牌
|
||||
* @param initialLocation 初始位置信息
|
||||
* @return 更新后的用户信息
|
||||
*/
|
||||
@PostMapping("/signin")
|
||||
public ResponseEntity<?> signIn(HttpServletRequest request) {
|
||||
public ResponseEntity<?> signIn(HttpServletRequest request, @RequestBody LocationWebSocketHandler.LocationData initialLocation) {
|
||||
String token = extractToken(request);
|
||||
if (token == null) {
|
||||
return ResponseEntity.badRequest().body("Authorization token is missing");
|
||||
}
|
||||
|
||||
// 检查位置信息是否为空
|
||||
if (initialLocation == null) {
|
||||
return ResponseEntity.badRequest().body("Initial location information is required");
|
||||
}
|
||||
|
||||
// 检查位置信息的必要字段是否为空
|
||||
if (initialLocation.getLatitude() == null || initialLocation.getLongitude() == null) {
|
||||
return ResponseEntity.badRequest().body("Latitude and longitude are required in location information");
|
||||
}
|
||||
|
||||
try {
|
||||
User user = userService.getUserInfo(token);
|
||||
User updatedUser = userService.signIn(user.getId());
|
||||
@@ -197,7 +208,7 @@ public class UserController {
|
||||
// 通知WebSocket处理器用户已签到并自动订阅
|
||||
try {
|
||||
LocationWebSocketHandler handler = applicationContext.getBean(LocationWebSocketHandler.class);
|
||||
handler.userSignedIn(user.getId());
|
||||
handler.userSignedIn(user.getId(), initialLocation);
|
||||
} catch (Exception e) {
|
||||
System.err.println("通知WebSocket签到状态时出错: " + e.getMessage());
|
||||
}
|
||||
|
@@ -20,13 +20,6 @@ public interface DeliveryPersonService {
|
||||
* @return 配送员对象
|
||||
*/
|
||||
DeliveryPerson getDeliveryPersonById(Long id);
|
||||
/**
|
||||
* 更新配送员当前位置。
|
||||
* @param id 配送员ID
|
||||
* @param longitude 经度
|
||||
* @param latitude 纬度
|
||||
*/
|
||||
void updateLocation(Long id, Double longitude, Double latitude);
|
||||
/**
|
||||
* 获取指定配送员的所有订单。
|
||||
* @param id 配送员ID
|
||||
|
@@ -5,14 +5,6 @@ package com.light.delivery.service;
|
||||
*/
|
||||
public interface LocationSyncService {
|
||||
|
||||
/**
|
||||
* 处理配送员位置更新
|
||||
* @param deliveryPersonId 配送员ID
|
||||
* @param longitude 经度
|
||||
* @param latitude 纬度
|
||||
*/
|
||||
void handleLocationUpdate(Long deliveryPersonId, Double longitude, Double latitude);
|
||||
|
||||
/**
|
||||
* 订阅位置更新
|
||||
* @param sessionId 会话ID
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -34,12 +34,6 @@ public class DeliveryPersonServiceImpl implements DeliveryPersonService {
|
||||
return person.orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateLocation(Long id, Double longitude, Double latitude) {
|
||||
// 使用 LocationSyncService 更新位置信息到服务器缓存,而不是直接更新数据库
|
||||
locationSyncService.handleLocationUpdate(id, longitude, latitude);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Order> getCurrentOrders(Long id) {
|
||||
return orderRepository.findByDeliveryPersonId(id);
|
||||
|
@@ -34,19 +34,7 @@ public class LocationSyncServiceImpl implements LocationSyncService {
|
||||
// 位置过期时间(分钟)
|
||||
private static final int LOCATION_EXPIRE_MINUTES = 5;
|
||||
|
||||
@Override
|
||||
public void handleLocationUpdate(Long deliveryPersonId, Double longitude, Double latitude) {
|
||||
try {
|
||||
// 更新内存缓存中的配送员位置,而不是数据库
|
||||
deliveryPersonLongitudeMap.put(deliveryPersonId, longitude);
|
||||
deliveryPersonLatitudeMap.put(deliveryPersonId, latitude);
|
||||
|
||||
// 更新最后更新时间
|
||||
lastUpdateTimes.put(deliveryPersonId, LocalDateTime.now());
|
||||
} catch (Exception e) {
|
||||
System.err.println("更新配送员位置时出错: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void subscribe(String sessionId, Long deliveryPersonId) {
|
||||
|
Reference in New Issue
Block a user