puzhibing
2023-02-16 504fdd09f7ae6278308156ad416a35fc02ecb8b8
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
package com.supersavedriving.driver.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.supersavedriving.driver.core.util.ToolUtil;
import com.supersavedriving.driver.modular.system.model.DriverWork;
import com.supersavedriving.driver.modular.system.service.IDriverWorkService;
import com.supersavedriving.driver.modular.system.service.IOrderPositionService;
import com.supersavedriving.driver.modular.system.util.ResultUtil;
import com.supersavedriving.driver.modular.system.warpper.DriverPositionWarpper;
import com.supersavedriving.driver.modular.system.warpper.OrderPositionWarpper;
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.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 订单轨迹逻辑类
 */
@Service
public class OrderPositionServiceImpl implements IOrderPositionService {
 
    @Value("${filePath}")
    private String filePath;
 
 
 
 
 
    /**
     * 保存订单最新坐标
     * @param driverPositionWarpper
     * @throws Exception
     */
    @Override
    public void saveOrderPosition(DriverPositionWarpper driverPositionWarpper) throws Exception {
        Integer orderId = driverPositionWarpper.getOrderId();
        Integer orderType = driverPositionWarpper.getOrderType();
        File file = new File(filePath + orderId + "_" + orderType + ".json");
        if(!file.exists()){
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        List<OrderPositionWarpper> orderPositionWarppers = queryPosition(orderId, orderType);
        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(driverPositionWarpper));
        out.flush();
        out.close();
    }
 
 
    /**
     * 获取订单坐标数据
     * @param orderId
     * @param orderType
     * @return
     * @throws Exception
     */
    @Override
    public List<OrderPositionWarpper> queryPosition(Integer orderId, Integer orderType) throws Exception {
        //将数据存储到文件中
        File file = new File(filePath + orderId + "_" + orderType + ".txt");
        if(!file.exists()){
            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;
    }
 
 
}