| | |
| | | package com.stylefeng.guns.modular.system.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.baomidou.mybatisplus.service.impl.ServiceImpl; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | 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.GDMapElectricFenceUtil; |
| | | import com.stylefeng.guns.modular.system.util.PushMinistryOfTransportUtil; |
| | | import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService; |
| | | 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.io.*; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | |
| | | @Service |
| | | public class OrderPositionServiceImpl extends ServiceImpl<OrderPositionMapper, OrderPosition> implements IOrderPositionService { |
| | | |
| | | |
| | | @Resource |
| | | private OrderPositionMapper orderPositionMapper; |
| | | |
| | | |
| | | @Autowired |
| | | private IOrderService orderService; |
| | | |
| | | |
| | | @Autowired |
| | | private PushMinistryOfTransportUtil pushMinistryOfTransportUtil; |
| | | |
| | | @Value("${filePath}") |
| | | private String filePath; |
| | | |
| | | |
| | | @Resource |
| | | private MongoTemplate mongoTemplate; |
| | | |
| | | @Value("${pushMinistryOfTransport}") |
| | | private boolean pushMinistryOfTransport; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 添加数据 |
| | | * |
| | | * @param orderPosition |
| | | * @throws Exception |
| | | */ |
| | |
| | | if(b){//如果计算了里程则将新的数据存储到数据库作为下个起点 |
| | | //先计算里程,在存储最新位置 |
| | | orderPosition.setInsertTime(new Date()); |
| | | //将数据存储到文件中 |
| | | List<OrderPosition> orderPositions = this.queryPosition(orderPosition.getOrderId(), orderPosition.getOrderType()); |
| | | File file = new File(filePath + orderPosition.getOrderId() + "_" + orderPosition.getOrderType() + ".txt"); |
| | | if(!file.exists()){ |
| | | file.getParentFile().mkdirs(); |
| | | file.createNewFile(); |
| | | file.setReadable(true, false);//设置读取权限 |
| | | file.setWritable(true, false);//设置写入权限 |
| | | |
| | | } |
| | | //写入相应的文件 |
| | | PrintWriter out = new PrintWriter(new FileWriter(file)); |
| | | orderPositions.add(orderPosition); |
| | | System.err.println("存储新数据:" + JSON.toJSONString(orderPositions)); |
| | | out.write(JSON.toJSONString(orderPositions)); |
| | | out.flush(); |
| | | out.close(); |
| | | mongoTemplate.save(orderPosition); |
| | | } |
| | | |
| | | new Thread(new Runnable() { |
| | |
| | | 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{ |
| | | //将数据存储到文件中 |
| | | 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<OrderPosition> list = new ArrayList<>(); |
| | | if(ToolUtil.isNotEmpty(sb.toString())){ |
| | | list = JSONArray.parseArray(sb.toString(), OrderPosition.class); |
| | | } |
| | | System.err.println("坐标:" + sb); |
| | | return list; |
| | | 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; |
| | | } |
| | | } |