Pu Zhibing
6 天以前 ae58e2ed4030730b772b30c91a8f129de63a94f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.stylefeng.guns.modular.api;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
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 com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.warpper.BaseWarpper;
import com.stylefeng.guns.modular.system.warpper.DriverInfoWarpper;
import com.stylefeng.guns.modular.system.warpper.DriverPosition;
import com.stylefeng.guns.modular.system.warpper.OrderEvaluateWarpper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
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.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
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 java.util.*;
import java.util.stream.Collectors;
 
/**
 * 司机控制器
 */
@Api
@RestController
@RequestMapping("")
public class DriverController {
 
    @Autowired
    private IDriverService driverService;
 
    @Autowired
    private IOrderEvaluateService orderEvaluateService;
    
    @Autowired
    private MongoTemplate mongoTemplate;
 
 
 
 
 
 
    /**
     * 获取5公里范围内空闲司机数量
     * @param type 业务类型(1=专车,2=出租车,3=城际,4=小件物流-同城,5=小件物流-跨城,6=包车)
     * @return
     */
    @ResponseBody
    @PostMapping("/base/driver/queryIdleDriver")
    @ApiOperation(value = "获取5公里范围内空闲司机数量", tags = {"用户端-首页"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "业务类型(1=专车,2=出租车,3=城际,4=小件物流-同城,5=小件物流-跨城,6=包车)", name = "type", required = true, dataType = "int"),
            @ApiImplicitParam(value = "乘客当前定位经度", name = "lon", required = true, dataType = "double"),
            @ApiImplicitParam(value = "乘客当前定位纬度", name = "lat", required = true, dataType = "double")
    })
    public ResultUtil<BaseWarpper> queryIdleDriver(Integer type, Double lon, Double lat){
        try {
            List<Driver> list = driverService.queryIdleDriver(type, lon, lat, 5D, null);
            BaseWarpper baseWarpper = new BaseWarpper();
            baseWarpper.setNumber(list.size());
            return ResultUtil.success(baseWarpper);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.runErr();
        }
    }
 
 
    /**
     * 获取司机详情
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/api/driver/queryDriverInfo")
    @ApiOperation(value = "获取司机详情", tags = {"用户端-订单相关"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "司机id", name = "id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResultUtil<DriverInfoWarpper> queryDriverInfo(Integer id){
        try {
            Map<String, Object> map = driverService.queryDriverInfo(id);
            String name = String.valueOf(map.get("name"));
            map.put("name", name.substring(0, 1) + "师傅");
            List<BaseWarpper> list = driverService.queryBusiness(id);
            map.put("list", list);
            return ResultUtil.success(DriverInfoWarpper.getDriverInfoWarpper(map));
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.runErr();
        }
    }
 
    /**
     * 获取司机的历史评价
     * @param id
     * @param pageNum
     * @param size
     * @return
     */
    @ResponseBody
    @PostMapping("/api/driver/queryOrderEvaluate")
    @ApiOperation(value = "获取司机的历史评价", tags = {"用户端-订单相关"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "司机id", name = "id", required = true, dataType = "int"),
            @ApiImplicitParam(value = "页码,首页1", name = "pageNum", required = true, dataType = "int"),
            @ApiImplicitParam(value = "页条数", name = "size", required = true, dataType = "int"),
            @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....")
    })
    public ResultUtil<List<OrderEvaluateWarpper>> queryOrderEvaluate(Integer id, Integer pageNum, Integer size){
        try {
            List<Map<String, Object>> list = orderEvaluateService.queryOrderEvaluate(id, pageNum, size);
            return ResultUtil.success(OrderEvaluateWarpper.getOrderEvaluateWarpper(list));
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.runErr();
        }
    }
    
    
    
    @ResponseBody
    @PostMapping("/base/driver/queryNearbyCar")
    @ApiOperation(value = "首页查询5公里范围内的车辆位置(黔云通)", tags = {"用户端-首页"}, notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "经度", name = "lon", required = true, dataType = "String"),
            @ApiImplicitParam(value = "纬度", name = "lat", required = true, dataType = "String"),
    })
    public ResultUtil<List<Map<String, Object>>> queryNearbyCar(String lon, String lat){
        //mongodb查询附近5公里范围内的坐标
        NearQuery query = NearQuery.near(new Point(Double.parseDouble(lon), Double.parseDouble(lat)));
        query.query(Query.query(Criteria.where("point")));
        query.maxDistance(5000);
        GeoResults<DriverPosition> results = mongoTemplate.geoNear(query, DriverPosition.class);
        List<Map<String, Object>> collect = new ArrayList<>();
        if(collect.size() > 0){
            List<GeoResult<DriverPosition>> content1 = results.getContent();
            List<DriverPosition> positionList = content1.stream().map(GeoResult::getContent).collect(Collectors.toList());
            List<Integer> driverIds = positionList.stream().map(DriverPosition::getDriverId).collect(Collectors.toList());
            List<Driver> drivers = driverService.selectList(new EntityWrapper<Driver>().in("driverId", driverIds)
                    .eq("authState", 2).eq("state", 2));
            collect = drivers.stream().map(driver -> {
                Optional<DriverPosition> driverPosition = positionList.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());
        }
        return ResultUtil.success(collect);
    }
}