java redis geo_redis geo操作

package club.newtech.qbike.trip.domain.service;

import club.newtech.qbike.trip.domain.core.Status;

import club.newtech.qbike.trip.domain.core.root.DriverStatus;

import club.newtech.qbike.trip.domain.core.vo.Driver;

import club.newtech.qbike.trip.domain.core.vo.Position;

import club.newtech.qbike.trip.domain.repository.DriverStatusRepo;

import club.newtech.qbike.trip.domain.repository.PositionRepository;

import club.newtech.qbike.trip.infrastructure.UserRibbonHystrixApi;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.geo.Circle;

import org.springframework.data.geo.Distance;

import org.springframework.data.geo.GeoResults;

import org.springframework.data.geo.Point;

import org.springframework.data.redis.connection.RedisGeoCommands;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Date;

import java.util.List;

import static java.util.stream.Collectors.toList;

@Service

@Transactional

public class PositionService {

private static final Logger LOGGER = LoggerFactory.getLogger(PositionService.class);

@Autowired

DriverStatusRepo driverStatusRepo;

@Autowired

UserRibbonHystrixApi userService;

@Autowired

RedisTemplate redisTemplate;

@Autowired

PositionRepository positionRepository;

/**

* 上传位置

*

* @param driverId

* @param longitude

* @param latitude

*/

public void updatePosition(Integer driverId, Double longitude, Double latitude) {

//先记录轨迹

Date current = new Date();

Position position = new Position();

position.setDriverId(String.valueOf(driverId));

position.setPositionLongitude(longitude);

position.setPositionLatitude(latitude);

//TODO 目前没有上传上下线状态

position.setStatus(Status.ONLINE);

position.setUploadTime(current);

//保存位置信息

positionRepository.save(position);

//更新状态表

DriverStatus driverStatus = driverStatusRepo.findByDriver_Id(driverId);

if (driverStatus != null) {

driverStatus.setCurrentLongitude(longitude);

driverStatus.setCurrentLatitude(latitude);

driverStatus.setUpdateTime(current);

driverStatusRepo.save(driverStatus);

} else {

Driver driver = userService.findById(driverId);

driverStatus = new DriverStatus();

driverStatus.setDriver(driver);

driverStatus.setCurrentLongitude(longitude);

driverStatus.setCurrentLatitude(latitude);

driverStatus.setUpdateTime(current);

driverStatus.setStatus(Status.ONLINE);

driverStatus.setDId(driverId);

driverStatusRepo.save(driverStatus);

}

//更新Redis中的坐标数据,GeoHash

redisTemplate.opsForGeo().geoAdd("Drivers", new Point(longitude, latitude), String.valueOf(driverId));

LOGGER.info("position update " + driverStatus);

}

/**

* 匹配司机

*

* @param longitude

* @param latitude

* @return

*/

public Collection matchDriver(double longitude, double latitude) {

Circle circle = new Circle(new Point(longitude, latitude), //

new Distance(500, RedisGeoCommands.DistanceUnit.METERS));

GeoResults> result =

redisTemplate.opsForGeo().geoRadius("Drivers", circle);

if (result.getContent().size() == 0) {

LOGGER.info("没找到匹配司机");

return new ArrayList<>();

} else {

List drivers = result.getContent()

.stream()

.map(x -> x.getContent().getName())

.collect(toList());

LOGGER.info("获取附近司机为{}", drivers);

return drivers.stream().map(Integer::parseInt)

.map(id -> driverStatusRepo.findByDriver_Id(id)).collect(toList());

}

}

}

原文:https://www.cnblogs.com/java-le/p/12085102.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值