feat(trajectory): 新增轨迹后台查询与实时位置接口

- 新增轨迹分页、时间线、统计摘要等查询 DTO\n- 提供轨迹后台控制器,支持工牌下拉、轨迹查询、实时位置查询\n- 接入 TrajectoryStateApi 的 Feign 配置,打通 Ops 对 IoT 实时位置状态的读取
This commit is contained in:
lzh
2026-03-31 22:56:49 +08:00
parent bf5aa21648
commit d3eecc63ef
8 changed files with 415 additions and 0 deletions

View File

@@ -0,0 +1,181 @@
package com.viewsh.module.ops.controller.admin.trajectory;
import com.viewsh.framework.common.pojo.CommonResult;
import com.viewsh.framework.common.pojo.PageResult;
import com.viewsh.module.iot.api.device.IotDeviceQueryApi;
import com.viewsh.module.iot.api.device.dto.IotDeviceSimpleRespDTO;
import com.viewsh.module.iot.api.trajectory.DeviceLocationDTO;
import com.viewsh.module.iot.api.trajectory.TrajectoryStateApi;
import com.viewsh.module.ops.dal.dataobject.area.OpsAreaDeviceRelationDO;
import com.viewsh.module.ops.dal.dataobject.vo.area.OpsBusAreaRespVO;
import com.viewsh.module.ops.environment.service.trajectory.DeviceTrajectoryService;
import com.viewsh.module.ops.service.area.AreaDeviceService;
import com.viewsh.module.ops.service.area.OpsBusAreaService;
import com.viewsh.module.ops.service.trajectory.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.viewsh.framework.common.pojo.CommonResult.success;
/**
* 管理后台 - 设备轨迹
*
* @author lzh
*/
@Tag(name = "管理后台 - 设备轨迹")
@Slf4j
@RestController
@RequestMapping("/ops/trajectory")
@Validated
public class TrajectoryController {
@Resource
private DeviceTrajectoryService trajectoryService;
@Resource
private TrajectoryStateApi trajectoryStateApi;
@Resource
private AreaDeviceService areaDeviceService;
@Resource
private OpsBusAreaService opsBusAreaService;
@Resource
private IotDeviceQueryApi iotDeviceQueryApi;
// ==================== 工牌设备下拉列表 ====================
@GetMapping("/badge-list")
@Operation(summary = "获取工牌设备下拉列表(轨迹页面用)")
@PreAuthorize("@ss.hasPermission('ops:trajectory:query')")
public CommonResult<List<BadgeSimpleRespDTO>> getBadgeSimpleList() {
// 查询所有 relationType=BADGE 的设备关联
List<OpsAreaDeviceRelationDO> relations = areaDeviceService.listAllByType("BADGE");
// 按 deviceId 去重(同一设备可能绑定多个区域)
Map<Long, OpsAreaDeviceRelationDO> uniqueDevices = relations.stream()
.collect(Collectors.toMap(
OpsAreaDeviceRelationDO::getDeviceId,
Function.identity(),
(a, b) -> a,
LinkedHashMap::new));
// 批量查询设备信息以获取 nickname
Set<Long> deviceIds = uniqueDevices.keySet();
Map<Long, IotDeviceSimpleRespDTO> deviceMap = Collections.emptyMap();
if (!deviceIds.isEmpty()) {
try {
CommonResult<List<IotDeviceSimpleRespDTO>> deviceResult =
iotDeviceQueryApi.batchGetDevices(deviceIds);
if (deviceResult != null && deviceResult.getData() != null) {
deviceMap = deviceResult.getData().stream()
.collect(Collectors.toMap(IotDeviceSimpleRespDTO::getId, Function.identity()));
}
} catch (Exception e) {
log.warn("[badge-list] 批量查询设备信息失败,降级返回无 nickname", e);
}
}
Map<Long, IotDeviceSimpleRespDTO> finalDeviceMap = deviceMap;
List<BadgeSimpleRespDTO> result = uniqueDevices.values().stream()
.map(r -> {
BadgeSimpleRespDTO dto = BadgeSimpleRespDTO.builder()
.deviceId(r.getDeviceId())
.deviceKey(r.getDeviceKey())
.build();
IotDeviceSimpleRespDTO device = finalDeviceMap.get(r.getDeviceId());
if (device != null) {
dto.setNickname(device.getNickname());
}
return dto;
})
.collect(Collectors.toList());
return success(result);
}
// ==================== 轨迹查询 ====================
@GetMapping("/page")
@Operation(summary = "获得设备轨迹分页")
@PreAuthorize("@ss.hasPermission('ops:trajectory:query')")
public CommonResult<PageResult<TrajectoryRespDTO>> getTrajectoryPage(
@Valid TrajectoryPageReqDTO pageReq) {
return success(trajectoryService.getTrajectoryPage(pageReq));
}
@GetMapping("/timeline")
@Operation(summary = "获得设备某天的轨迹时间线")
@PreAuthorize("@ss.hasPermission('ops:trajectory:query')")
public CommonResult<List<TrajectoryRespDTO>> getTimeline(@Valid TrajectoryTimelineReqDTO req) {
return success(trajectoryService.getTimeline(req.getDeviceId(), req.getDate()));
}
@GetMapping("/summary")
@Operation(summary = "获得设备某天的轨迹统计摘要")
@PreAuthorize("@ss.hasPermission('ops:trajectory:query')")
public CommonResult<TrajectorySummaryDTO> getSummary(@Valid TrajectoryTimelineReqDTO req) {
return success(trajectoryService.getSummary(req.getDeviceId(), req.getDate()));
}
// ==================== 实时位置 ====================
@GetMapping("/current-location")
@Operation(summary = "获得设备当前位置(实时)")
@PreAuthorize("@ss.hasPermission('ops:trajectory:query')")
public CommonResult<DeviceCurrentLocationDTO> getCurrentLocation(
@Parameter(description = "设备ID", required = true, example = "31")
@RequestParam("deviceId") Long deviceId) {
// 调用 IoT RPC 查询设备实时位置
CommonResult<DeviceLocationDTO> result;
try {
result = trajectoryStateApi.getCurrentLocation(deviceId);
} catch (Exception e) {
log.error("[current-location] IoT 服务调用失败deviceId={}", deviceId, e);
throw new RuntimeException("IoT 服务不可用,无法查询设备实时位置", e);
}
if (result == null || !result.isSuccess() || result.getData() == null
|| !Boolean.TRUE.equals(result.getData().getInArea())) {
return success(DeviceCurrentLocationDTO.builder()
.deviceId(deviceId)
.inArea(false)
.build());
}
DeviceLocationDTO location = result.getData();
// 通过 Service 查询区域名称(不直接注入 Mapper
String areaName = null;
if (location.getAreaId() != null) {
try {
OpsBusAreaRespVO area = opsBusAreaService.getArea(location.getAreaId());
areaName = area.getAreaName();
} catch (Exception e) {
log.warn("[current-location] 查询区域名称失败areaId={}", location.getAreaId(), e);
}
}
return success(DeviceCurrentLocationDTO.builder()
.deviceId(location.getDeviceId())
.areaId(location.getAreaId())
.areaName(areaName)
.enterTime(location.getEnterTime())
.beaconMac(location.getBeaconMac())
.inArea(true)
.build());
}
}

View File

@@ -4,6 +4,7 @@ import com.viewsh.module.infra.api.file.FileApi;
import com.viewsh.module.iot.api.device.IotDeviceControlApi;
import com.viewsh.module.iot.api.device.IotDeviceQueryApi;
import com.viewsh.module.iot.api.device.IotDeviceStatusQueryApi;
import com.viewsh.module.iot.api.trajectory.TrajectoryStateApi;
import com.viewsh.module.system.api.notify.NotifyMessageSendApi;
import com.viewsh.module.system.api.social.SocialUserApi;
import com.viewsh.module.system.api.user.AdminUserApi;
@@ -18,6 +19,7 @@ import org.springframework.context.annotation.Configuration;
IotDeviceControlApi.class,
IotDeviceQueryApi.class,
IotDeviceStatusQueryApi.class,
TrajectoryStateApi.class,
FileApi.class
})
public class RpcConfiguration {