| | |
| | | package com.stylefeng.guns.modular.api; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import com.stylefeng.guns.modular.system.model.Driver; |
| | | import com.stylefeng.guns.modular.system.service.IDriverService; |
| | | import com.stylefeng.guns.modular.system.service.IOrderEvaluateService; |
| | |
| | | import io.swagger.annotations.ApiImplicitParam; |
| | | import io.swagger.annotations.ApiImplicitParams; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.geo.Point; |
| | | import org.springframework.data.mongodb.core.MongoTemplate; |
| | | import org.springframework.data.mongodb.core.query.Criteria; |
| | | import org.springframework.data.mongodb.core.query.Query; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.ResponseBody; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | @Api |
| | | @RestController |
| | | @RequestMapping("") |
| | | @Slf4j |
| | | public class DriverController { |
| | | |
| | | @Autowired |
| | |
| | | |
| | | @Autowired |
| | | private MongoTemplate mongoTemplate; |
| | | |
| | | @Resource |
| | | private RedisTemplate<String, Object> redisTemplate; |
| | | |
| | | |
| | | |
| | |
| | | }) |
| | | public ResultUtil<BaseWarpper> queryIdleDriver(Integer type, Double lon, Double lat){ |
| | | try { |
| | | if(type==2){ |
| | | type=null; |
| | | } |
| | | List<Driver> list = driverService.queryIdleDriver(type, lon, lat, 5D, null); |
| | | BaseWarpper baseWarpper = new BaseWarpper(); |
| | | baseWarpper.setNumber(list.size()); |
| | |
| | | //mongodb查询附近5公里范围内的坐标 |
| | | Query query = Query.query(Criteria.where("point").nearSphere(new Point(Double.parseDouble(lon), Double.parseDouble(lat))).maxDistance(5000)); |
| | | List<DriverPosition> driverPositions = mongoTemplate.find(query, DriverPosition.class); |
| | | log.info("地理空间检索出的数据:{}", JSON.toJSONString(driverPositions)); |
| | | List<Map<String, Object>> collect = new ArrayList<>(); |
| | | if(driverPositions.size() > 0){ |
| | | List<Integer> driverIds = driverPositions.stream().map(DriverPosition::getDriverId).collect(Collectors.toList()); |
| | | List<Driver> drivers = driverService.selectList(new EntityWrapper<Driver>().in("id", driverIds) |
| | | .eq("authState", 2).eq("state", 2)); |
| | | collect = drivers.stream().map(driver -> { |
| | | Optional<DriverPosition> driverPosition = driverPositions.stream().filter(position -> position.getDriverId().equals(driver.getId())).findFirst(); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("driverId", driver.getId()); |
| | | map.put("name", driver.getName()); |
| | | map.put("lot", driverPosition.isPresent() ? driverPosition.get().getPoint().getX() : ""); |
| | | map.put("lat", driverPosition.isPresent() ? driverPosition.get().getPoint().getY() : ""); |
| | | return map; |
| | | }).collect(Collectors.toList()); |
| | | String value = (String) redisTemplate.opsForValue().get("dache:DRIVER" + String.valueOf(driver.getId())); |
| | | if(ToolUtil.isNotEmpty(value)){ |
| | | Optional<DriverPosition> driverPosition = driverPositions.stream().filter(position -> position.getDriverId().equals(driver.getId())).findFirst(); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("driverId", driver.getId()); |
| | | map.put("name", driver.getName()); |
| | | map.put("lot", driverPosition.isPresent() ? driverPosition.get().getPoint().getX() : ""); |
| | | map.put("lat", driverPosition.isPresent() ? driverPosition.get().getPoint().getY() : ""); |
| | | return map; |
| | | } |
| | | return null; |
| | | }).filter(Objects::nonNull).collect(Collectors.toList()); |
| | | } |
| | | return ResultUtil.success(collect); |
| | | } |