puzhibing
2023-06-02 9b230057d5941c4ec0d29cda2c8163f9a6d7a1e9
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
167
168
169
170
171
172
173
174
175
package com.agentdriving.driver.modular.system.service.impl;
 
import com.agentdriving.driver.core.util.ToolUtil;
import com.agentdriving.driver.modular.system.model.Order;
import com.agentdriving.driver.modular.system.service.IOrderPositionService;
import com.agentdriving.driver.modular.system.service.IOrderService;
import com.agentdriving.driver.modular.system.util.GeodesyUtil;
import com.agentdriving.driver.modular.system.util.RedisUtil;
import com.agentdriving.driver.modular.system.warpper.DriverPositionWarpper;
import com.agentdriving.driver.modular.system.warpper.OrderPositionWarpper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.agentdriving.driver.modular.system.warpper.ProcessOperationsWarpper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.io.*;
import java.util.*;
 
/**
 * 订单轨迹逻辑类
 */
@Service
public class OrderPositionServiceImpl implements IOrderPositionService {
 
    @Value("${filePath}")
    private String filePath;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private IOrderService orderService;
 
    private Map<String, Integer> map = new HashMap<>();
 
 
 
 
 
    /**
     * 保存订单最新坐标
     * @param driverPositionWarpper
     * @throws Exception
     */
    @Override
    public void saveOrderPosition(DriverPositionWarpper driverPositionWarpper) throws Exception {
        Integer orderId = driverPositionWarpper.getOrderId();
        Order order = orderService.selectById(orderId);
        if(order.getState() != 105){
            return;
        }
        Integer orderType = driverPositionWarpper.getOrderType();
        File file = new File(filePath + orderId + ".json");
        if(!file.exists()){
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        List<OrderPositionWarpper> orderPositionWarppers = queryPosition(orderId, orderType);
        if(orderPositionWarppers.size() > 0){
            OrderPositionWarpper orderPositionWarpper = orderPositionWarppers.get(orderPositionWarppers.size() - 1);
            saveOrderMileage(driverPositionWarpper, orderPositionWarpper);
        }
        //大于100米才存储有效定位数据
        if(orderPositionWarppers.size() > 0){
            OrderPositionWarpper orderPositionWarpper1 = orderPositionWarppers.get(orderPositionWarppers.size() - 1);
            String fromLonLat = driverPositionWarpper.getLon() + "," + driverPositionWarpper.getLat();
            String toLonLat = orderPositionWarpper1.getLon() + "," + orderPositionWarpper1.getLat();
            Map<String, Double> distance = GeodesyUtil.getDistance(fromLonLat, toLonLat);
            Double wgs84 = distance.get("WGS84");
            if(wgs84 >= 100){
                OrderPositionWarpper orderPositionWarpper = new OrderPositionWarpper();
                BeanUtils.copyProperties(driverPositionWarpper, orderPositionWarpper);
                orderPositionWarpper.setInsertTime(new Date());
                orderPositionWarppers.add(orderPositionWarpper);
            }
        }else{
            OrderPositionWarpper orderPositionWarpper = new OrderPositionWarpper();
            BeanUtils.copyProperties(driverPositionWarpper, orderPositionWarpper);
            orderPositionWarpper.setInsertTime(new Date());
            orderPositionWarppers.add(orderPositionWarpper);
        }
 
        //写入相应的文件
        PrintWriter out = new PrintWriter(new FileWriter(file));
        out.write(JSON.toJSONString(orderPositionWarppers));
        out.flush();
        out.close();
    }
 
 
    /**
     * 保存行驶里程
     * @param driverPositionWarpper
     * @param orderPositionWarpper
     */
    public void saveOrderMileage(DriverPositionWarpper driverPositionWarpper, OrderPositionWarpper orderPositionWarpper){
        String fromLonLat = driverPositionWarpper.getLon() + "," + driverPositionWarpper.getLat();
        String toLonLat = orderPositionWarpper.getLon() + "," + orderPositionWarpper.getLat();
        Map<String, Double> distance = GeodesyUtil.getDistance(fromLonLat, toLonLat);
        Order order = orderService.selectById(driverPositionWarpper.getOrderId());
        Double wgs84 = distance.get("WGS84");
        Integer num = map.get(order.getId().toString());
        if(100 < wgs84){
            order.setActualMileage(order.getActualMileage() + wgs84.intValue());
            orderService.updateById(order);
            map.put(order.getId().toString(), 0);
 
            if(order.getState() == 401){//定位变动,自动开始服务
                ProcessOperationsWarpper processOperationsWarpper = new ProcessOperationsWarpper();
                processOperationsWarpper.setOrderId(order.getId());
                processOperationsWarpper.setState(105);
                try {
                    orderService.driverProcessOperations(order.getDriverId(), processOperationsWarpper);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if(100 >= wgs84 && 12 <= (null == num ? 0 : num)){//1分钟(5秒上传一次数据)
            Integer integer = map.get(order.getId().toString());
            map.put(order.getId().toString(), null == integer ? 0 : integer++);
            //进入等待状态
            ProcessOperationsWarpper processOperationsWarpper = new ProcessOperationsWarpper();
            processOperationsWarpper.setOrderId(order.getId());
            processOperationsWarpper.setState(401);
            try {
                orderService.driverProcessOperations(order.getDriverId(), processOperationsWarpper);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(100 >= wgs84 && 12 > (null == num ? 0 : num)){
            Integer integer = map.get(order.getId().toString());
            map.put(order.getId().toString(), null == integer ? 0 : integer++);
        }
    }
 
 
 
 
    /**
     * 获取订单坐标数据
     * @param orderId
     * @param orderType
     * @return
     * @throws Exception
     */
    @Override
    public List<OrderPositionWarpper> queryPosition(Integer orderId, Integer orderType) throws Exception {
        //将数据存储到文件中
        File file = new File(filePath + orderId + ".json");
        if(!file.exists()){
            System.err.println("不存在");
            return new ArrayList<>();
        }
        //读取文件(字符流)
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
        //循环取出数据
        String str = null;
        StringBuffer sb = new StringBuffer();
        while ((str = in.readLine()) != null) {
            sb.append(str);
        }
        List<OrderPositionWarpper> list = new ArrayList<>();
        if(ToolUtil.isNotEmpty(sb.toString())){
            list = JSONArray.parseArray(sb.toString(), OrderPositionWarpper.class);
        }
        return list;
    }
 
 
}