获取站点附近信息

首先是Controller中,使用 PathVaiable 获取了经度和纬度:

@Operation(summary = "获取附近站点信息列表")
@GetMapping("/nearbyStationList/{latitude}/{longitude}")
public AjaxResult nearbyStation(@PathVariable String latitude, @PathVariable String longitude) {
  return success(deviceService.nearbyStation(latitude, longitude, DeviceConstants.SEARCH_PC_RADIUS));
}

设计Service方法 nearStaion 来获取站点附近的信息,要点如下:

public List<StationVO> nearbyStation(String latitude, String longitude, Integer searchPcRadius) {
        // 坐标,确定中心点
        // GeoJsonPoint(double x, double y) x 表示经度, y 表示维度
        GeoJsonPoint geoJsonPoint = new GeoJsonPoint(Double.parseDouble(longitude), Double.parseDouble(latitude));
        // 圆的半径, 如50km范围
        Distance d = new Distance(searchPcRadius, Metrics.KILOMETERS);
        // 画一个圆
        Circle circle = new Circle(geoJsonPoint, d);
        // 查询当前范围内的站点信息
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        List<StationLocation> stationLocationList = this.mongoTemplate.find(query, StationLocation.class);
        if (CollectionUtils.isEmpty(stationLocationList)) return null;
        // 组装数据
        List<Long> stationIdList = stationLocationList.stream().map(StationLocation::getStationId).toList();
        // 获取站点列表
        List<Station> stationList = stationService.list(new LambdaQueryWrapper<Station>().in(Station::getId, stationIdList).isNotNull(Station::getCabinetId));
        // 获取柜机id列表
        List<Long> cabinetIdList = stationList.stream().map(Station::getCabinetId).toList();
        // 获取柜机id与柜机信息Map
        Map<Long, Cabinet> cabinetIdToCabinetMap = cabinetService.listByIds(cabinetIdList).stream().collect(Collectors.toMap(Cabinet::getId, Cabinet -> Cabinet));

        List<StationVO> stationVoList = new ArrayList<>();
        stationList.forEach(item -> {
            StationVO stationVO = new StationVO();
            BeanUtils.copyProperties(item, stationVO);

            // 获取柜机信息
            Cabinet cabinet = cabinetIdToCabinetMap.get(item.getCabinetId());
            // 判断是否可借用
            if (cabinet.getAvailableNum() > 0) stationVO.setIsUsable("1");
            else stationVO.setIsUsable("0");
            // 获取空闲插槽数量,判断是否可归还
            stationVO.setIsReturn(cabinet.getFreeSlots() > 0 ? "1" : "0");
            stationVoList.add(stationVO);
        });
        return stationVoList;
}

更新初始数据

由于,原始的站点数据只有在MySQL中存储,而新增mongodb存储时,已经有了大量的初始数据。

因此添加一个方法来更新初始数据(插入没有插入MongoDB的数据)

public void updateData() {
        List<Station> stationList = this.list();
        stationList.forEach(item -> {
            StationLocation stationLocation = stationLocationRepository.getByStationId(item.getId());
            if (stationLocation != null) return;
            stationLocation = new StationLocation();
            stationLocation.setId(ObjectId.get().toString());
            stationLocation.setStationId(item.getId());
            stationLocation.setLocation(new GeoJsonPoint(item.getLongitude().doubleValue(), item.getLatitude().doubleValue()));
            stationLocation.setCreateTime(new Date());
            stationLocationRepository.save(stationLocation);
        });
}