Pu Zhibing
1 天以前 989bf3f8a3e09a8dd46c4c5f79abc5f7421ebd14
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.dao.OrderPositionMapper;
import com.stylefeng.guns.modular.system.model.OrderPosition;
import com.stylefeng.guns.modular.system.service.IOrderPositionService;
import com.stylefeng.guns.modular.system.service.IOrderService;
import com.stylefeng.guns.modular.system.util.PushMinistryOfTransportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
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.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
 
@Service
public class OrderPositionServiceImpl extends ServiceImpl<OrderPositionMapper, OrderPosition> implements IOrderPositionService {
    
    @Resource
    private OrderPositionMapper orderPositionMapper;
    
    @Autowired
    private IOrderService orderService;
    
    @Autowired
    private PushMinistryOfTransportUtil pushMinistryOfTransportUtil;
    
    @Resource
    private MongoTemplate mongoTemplate;
    
    @Value("${pushMinistryOfTransport}")
    private boolean pushMinistryOfTransport;
    
    
    /**
     * 添加数据
     *
     * @param orderPosition
     * @throws Exception
     */
    @Override
    public void saveData(OrderPosition orderPosition) throws Exception {
        //计算实时里程
        boolean b = orderService.calculateMileage(orderPosition);
        if(b){//如果计算了里程则将新的数据存储到数据库作为下个起点
            //先计算里程,在存储最新位置
            orderPosition.setInsertTime(new Date());
            //将数据存储到文件中
            List<OrderPosition> orderPositions = this.queryPosition(orderPosition.getOrderId(), orderPosition.getOrderType());
            orderPositions.add(orderPosition);
            mongoTemplate.insert(orderPositions, OrderPosition.class);
        }
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(pushMinistryOfTransport && orderPosition.getOrderType() == 1){//上传数据
                    pushMinistryOfTransportUtil.positionDriver(orderPosition.getOrderId());
                    pushMinistryOfTransportUtil.positionVehicle(orderPosition.getOrderId());
                }
            }
        }).start();
 
    }
 
 
    /**
     * 获取最新的坐标位置
     * @param orderId
     * @param orderType
     * @return
     * @throws Exception
     */
    @Override
    public OrderPosition queryNew(Integer orderId, Integer orderType) throws Exception {
        List<OrderPosition> orderPositions = this.queryPosition(orderId, orderType);
        return orderPositions.size() > 0 ? orderPositions.get(orderPositions.size() - 1) : null;
    }
    
    
    /**
     * 获取坐标文件中的坐标数据
     *
     * @param orderId
     * @param orderType
     * @return
     * @throws Exception
     */
    @Override
    public List<OrderPosition> queryPosition(Integer orderId, Integer orderType) throws Exception {
        Query query = new Query()
                .addCriteria(Criteria.where("orderId").is(orderId).and("orderType").is(orderType))
                .with(new Sort(Sort.Direction.ASC, "insertTime"));
        List<OrderPosition> positions = mongoTemplate.find(query, OrderPosition.class);
        return positions;
    }
}