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<String, String> 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<DriverStatus> matchDriver(double longitude, double latitude) {
Circle circle = new Circle(new Point(longitude, latitude), //
new Distance(500, RedisGeoCommands.DistanceUnit.METERS));
GeoResults<RedisGeoCommands.GeoLocation<String>> result =
redisTemplate.opsForGeo().geoRadius("Drivers", circle);
if (result.getContent().size() == 0) {
LOGGER.info("没找到匹配司机");
return new ArrayList<>();

} else {
List<String> 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());
}
}
}

redis geo操作的更多相关文章

  1. Redis数据类型:Hashes、Geo操作指令

    Redis数据类型:Hashes.Geo操作指令 Hashes常用操作指令 Redis Hashes是一个键值对的映射表,最对能存储2^32-1(约40亿)个键值对. HSET HGET HSET:将 ...

  2. Redis GEO 功能使用场景

    本文来源:https://www.dazhuanlan.com/2020/02/05/5e3a0a3110649/ 背景 前段时间自己在做附近直播相关业务,其中有一个核心的点就是检索用户附近的主播,也 ...

  3. Redis GEO 地理位置

    目录 GEO指令 GEOADD GEODIST GEOPOP GEOHASH GEORADIUS GEORADIUSBYMEMBER 指令补充 删除操作 避免单集合数量过多 存储原理 GEOADD存储 ...

  4. Redis Geo: Redis新增位置查询功能

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/144.html 移动互联网增进了人与人之间的联系,其中基于位置信息的服务( ...

  5. spring data redis RedisTemplate操作redis相关用法

    http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...

  6. Spring Framework 中启动 Redis 事务操作

    背景: 项目中遇到有一系列对Redis的操作,并需要保持事务处理. 环境: Spring version 4.1.8.RELEASE Redis Server 2.6.12 (64位) spring- ...

  7. php的redis 操作类,适用于单台或多台、多组redis服务器操作

    redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...

  8. 转:Redis Geo: Redis新增位置查询功能

    原文来自于:http://www.infoq.com/cn/news/2015/07/redis-geo 移动互联网增进了人与人之间的联系,其中基于位置信息的服务(Location Based Ser ...

  9. 【springboot】【redis】springboot结合redis,操作List集合实现时间轴功能

    springboot结合redis,操作List集合实现时间轴功能

随机推荐

  1. ssh出现公钥错误问题的解决方法

      问题:主机app1推送公钥时,公钥判定错误   原因:之前推过公钥,用的是ip而不是主机名(即hosts文件中的对应关系不对),导致app1的~/.ssh/known_hosts中的公钥对不上. ...

  2. axios对请求各种异常情况处理的封装

    前端网络请求封装 前端采用了axios来处理网络请求,为了避免在每次请求时都去判断各种各样的网络情况,比如连接超时.服务器内部错误.权限不足等等不一而足,我对axios进行了简单的封装,这里主要使用了 ...

  3. java中JDBC是什么?

    [学习笔记] JDBC是什么? JDBC即(java database connectivity数据连接).JDBC是Sun公司编的一堆类和方法,都封装在java.sql包中.你可以利用这堆类和方法来 ...

  4. 搭建 python 3.5+pycharm 2017.1.3+django 1.12.0 首次 将sqlite3 迁移到mysql

  5. Inversion 多校签到题

    存下值和下标后排序,每次从坐后面开始取就可以了. ac代码: #include <algorithm> #include <cstdio> #include <cstri ...

  6. 7-MySQL DBA笔记-研发规范

    第7章 研发规范 本章将为读者解读一份研发规范.为了更好地协同工作和确保所开发的应用尽可能的稳定.高效,建立一套数据库相关的研发规范是很有必要的,虽然研发规范的确立和推广是一项很耗时的工作,但所取得的 ...

  7. Attribute预定义特性

    转载自:http://blog.csdn.net/wangyy130/article/details/44241173 一.什么是Attribute Attribute 类将预定义的系统信息或用户定义 ...

  8. 关于SpringMVC拦截器和异常

    一.文件上传 1.文件上传 SpringMVC为文件上传提供了直接的支持,这种类型是通过即插即用的MultipartResolver技术的.Spring用Jakarta Commons FileUpl ...

  9. express相关操作

    1.   安装应用生成器npm install express-generator –g 2.   生成项目Express --view=ejs myapp 3.   进入项目安装依赖包 npm in ...

  10. python实现蓝牙通信

    安装和示例 linux下安装 -dev sudo pip install bluepy 官方示例 import btle class MyDelegate(btle.DefaultDelegate): ...