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. Java基础篇---多线程

    内容导航: 1.多线程的实现方式 2.线程安全问题 3.线程间通信 4.生产者消费者模式 第一部分多线程的实现方式 在java中多线程实现方式有2种 一.自定义一个类A,继承Thread类 publi ...

  2. 使用pycharm开发web——django2.1.5(二)创建一个app并做一些配置

    这里我学习的呢是刘江老师的站,主要原因在于他这个版本新,还比较细节 网址先留一手,约等于在引用http://www.liujiangblog.com/ 开始正题: 1.在pycharm界面终端命令行里 ...

  3. Java源码 -- LinkedList

    1.1.LinkedList概述 LinkedList是一种可以在任何位置进行高效地插入和移除操作的有序序列,它是基于双向链表实现的. LinkedList 是一个继承于AbstractSequent ...

  4. java 复制指定目录中的所有文件和文件夹到另一个指定文件夹中

    package com.test; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream ...

  5. (八)springMvc 的参数绑定

    参数绑定 将客户端传来的 key/value 数据,绑定到 Controller 参数的过程 : 这一过程发生在调用 处理器适配器 的时候,spring 会去调用 参数绑定 组件,我使用的版本(4.5 ...

  6. C++ Primer练习题day1

    /* 练习1.1略 练习1.2.改写程序,让他返回-1. 练习1.3.编写程序,在标准的输出上打印Hello,World. */ #include<iostream> int main() ...

  7. PHP和js判断访问设备是否是微信浏览器实例

    PHP和js判断访问设备是否是微信浏览器实例,代码非常精简,适合新手学习. js判断是否是微信浏览器: 1 function is_weixin() { 2 var ua = window.navig ...

  8. LeetCode 328——奇偶链表(JAVA)

    给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝试使用原地算法完成.你的算法的空间复杂度应为 O(1),时 ...

  9. S02_CH15_ AXI_OLED 实验

    S02_CH15_ AXI_OLED 实验 在上一个例子中,主要是以软件功能为主,采用了软件模拟SPI时序进行控制OLED.这样做的好处是灵活,但是牺牲了效率.本章采用的方式是让SPI驱动由Veril ...

  10. Typeof() 和 GetType()区别

    1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...