Pu Zhibing
6 天以前 2b769cbc555b57a006b2b3aac3ce92d74a5ba6f2
UserQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/api/DriverController.java
@@ -1,6 +1,8 @@
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;
@@ -13,11 +15,13 @@
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;
@@ -32,6 +36,7 @@
@Api
@RestController
@RequestMapping("")
@Slf4j
public class DriverController {
    @Autowired
@@ -42,6 +47,9 @@
    
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
@@ -142,20 +150,25 @@
        //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);
    }