DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/api/OrderController.java
@@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; /** * 订单相关控制器 @@ -368,6 +369,40 @@ } } /** * 司机流程中创建猎鹰轨迹 * @param orderId * @param orderType * @param request * @return */ @ResponseBody @PostMapping("/api/order/getTrackId") @ApiOperation(value = "司机流程中创建猎鹰轨迹-接收轨迹id", tags = {"司机端-服务中"}, notes = "") @ApiImplicitParams({ @ApiImplicitParam(value = "订单id", name = "orderId", required = true, dataType = "int"), @ApiImplicitParam(value = "订单类型(1=专车,2=快车,3=城际,4=小件物流-同城,5=小件物流-跨城)", name = "orderType", required = true, dataType = "int"), @ApiImplicitParam(value = "轨迹id", name = "trackId", required = true, dataType = "int"), @ApiImplicitParam(name = "Authorization", value = "Bearer +token", required = true, dataType = "String", paramType = "header", defaultValue = "Bearer eyJhbGciOiJIUzUxMiJ9.....") }) public ResultUtil getTrackId(Integer orderId, Integer orderType, Integer trackId, HttpServletRequest request){ try { Integer uid = driverService.getUserIdFormRedis(request); if(null == uid){ return ResultUtil.tokenErr(); } // 设置订单轨迹id if(Objects.isNull(trackId)){ return ResultUtil.error("创建轨迹失败!"); } orderService.getTrackId(orderId, orderType, trackId); return ResultUtil.success(); }catch (Exception e){ e.printStackTrace(); return ResultUtil.runErr(); } } /** DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/listener/UploadTrajectoryEventListener.java
New file @@ -0,0 +1,32 @@ package com.stylefeng.guns.modular.listener; import com.stylefeng.guns.modular.listener.event.UploadTrajectoryEvent; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.event.EventListener; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import java.io.IOException; /** * @author xiaochen * @ClassName DeductPointsEventListener * @Description 存储定位事件异步监听器 * @date 2024-01-15 10:44 */ @Component public class UploadTrajectoryEventListener { @Value("${filePath}") private String filePath; /** * 打开事件监听器的异步处理 * @param event */ @Async @EventListener(UploadTrajectoryEvent.class) public void PublishTopicVipInfoEventListener(UploadTrajectoryEvent event) throws IOException { // 事件处理 String source = (String)event.getSource(); } } DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/listener/event/UploadTrajectoryEvent.java
New file @@ -0,0 +1,17 @@ package com.stylefeng.guns.modular.listener.event; import org.springframework.context.ApplicationEvent; /** * @author xiaochen * @ClassName DeductPointsEvent * @Description 上传轨迹的事件 * @date 2024-01-15 10:44 */ public class UploadTrajectoryEvent extends ApplicationEvent { public UploadTrajectoryEvent(String source) { super(source); } } DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/IOrderPrivateCarService.java
@@ -124,4 +124,13 @@ List<OrderPrivateCar> taskMidAxbUnBindSend(); List<OrderPrivateCar> getSmsOrderList(); /** * 设置订单轨迹id * @param orderId * @param trackId * @return */ void getTrackId(Integer orderId, Integer trackId); } DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/specialTrain/server/impl/OrderPrivateCarServiceImpl.java
@@ -1,6 +1,7 @@ package com.stylefeng.guns.modular.specialTrain.server.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.service.impl.ServiceImpl; @@ -15,9 +16,14 @@ import com.stylefeng.guns.modular.system.util.*; import com.stylefeng.guns.modular.taxi.model.OrderTaxi; import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService; import org.gavaghan.geodesy.Ellipsoid; import org.gavaghan.geodesy.GeodeticCalculator; import org.gavaghan.geodesy.GeodeticCurve; import org.gavaghan.geodesy.GlobalCoordinates; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import javax.annotation.Resource; import java.io.File; @@ -179,9 +185,10 @@ orderPrivateCar.setSnatchOrderTime(new Date()); //调用高德创建轨迹 String s = gdFalconUtil.selectTerminal(driver.getPhone()); String track = gdFalconUtil.createTrack(s); orderPrivateCar.setTrackId(track); // String s = gdFalconUtil.selectTerminal(driver.getPhone()); // String track = gdFalconUtil.createTrack(s); // JSONObject jsonObject = JSONObject.parseObject(track); // orderPrivateCar.setTrackId(String.valueOf(jsonObject.getInteger("trid"))); //调用移动的小号接口 /*Map<String, String> geocode = gdMapGeocodingUtil.geocode(orderPrivateCar.getStartLon().toString(), orderPrivateCar.getStartLat().toString()); @@ -306,29 +313,29 @@ } } }).start(); OrderPosition orderPosition = new OrderPosition(); orderPosition.setOrderId(orderId); orderPosition.setOrderType(1); orderPosition.setDriverId(orderPrivateCar.getDriverId()); orderPosition.setLat(lat.toString()); orderPosition.setLon(lon.toString()); orderPosition.setAltitude("0"); orderPosition.setDirectionAngle("0"); //先计算里程,在存储最新位置 orderPosition.setInsertTime(new Date()); //将数据存储到文件中 List<OrderPosition> orderPositions = orderPositionService.queryPosition(orderPosition.getOrderId(), orderPosition.getOrderType()); File file = new File(filePath + orderPosition.getOrderId() + "_" + orderPosition.getOrderType() + ".txt"); if(!file.exists()){ file.getParentFile().mkdirs(); file.createNewFile(); } //写入相应的文件 PrintWriter out = new PrintWriter(new FileWriter(file)); orderPositions.add(orderPosition); out.write(JSON.toJSONString(orderPositions)); out.flush(); out.close(); // OrderPosition orderPosition = new OrderPosition(); // orderPosition.setOrderId(orderId); // orderPosition.setOrderType(1); // orderPosition.setDriverId(orderPrivateCar.getDriverId()); // orderPosition.setLat(lat.toString()); // orderPosition.setLon(lon.toString()); // orderPosition.setAltitude("0"); // orderPosition.setDirectionAngle("0"); // //先计算里程,在存储最新位置 // orderPosition.setInsertTime(new Date()); // //将数据存储到文件中 // List<OrderPosition> orderPositions = orderPositionService.queryPosition(orderPosition.getOrderId(), orderPosition.getOrderType()); // File file = new File(filePath + orderPosition.getOrderId() + "_" + orderPosition.getOrderType() + ".txt"); // if(!file.exists()){ // file.getParentFile().mkdirs(); // file.createNewFile(); // } // //写入相应的文件 // PrintWriter out = new PrintWriter(new FileWriter(file)); // orderPositions.add(orderPosition); // out.write(JSON.toJSONString(orderPositions)); // out.flush(); // out.close(); break; case 6://结束服务(专车可以返回继续服务)不修改状态 orderPrivateCar.setGetoffLon(lon); @@ -336,6 +343,89 @@ orderPrivateCar.setGetoffAddress(address); orderPrivateCar.setGetoffTime(new Date()); orderPrivateCar.setEndServiceTime(new Date()); // 结束服务时查询轨迹,计算距离 Driver driver = driverService.selectById(orderPrivateCar.getDriverId()); String tid = gdFalconUtil.selectTerminal(driver.getPhone()); String result = gdFalconUtil.selectTrack(tid, orderPrivateCar.getTrackId(),1); JSONObject jsonObject = JSONObject.parseObject(result); JSONArray tracks = jsonObject.getJSONArray("tracks"); Integer counts = jsonObject.getInteger("counts"); JSONArray points = new JSONArray(); // 如果一单轨迹点数大于999,需进行分页查询,将所有的点查询出 if(counts > 999){ int i = counts%999; for (int j = 1; j < i + 1; j++) { String res = gdFalconUtil.selectTrack(tid, orderPrivateCar.getTrackId(),1); JSONObject jsonObject1 = JSONObject.parseObject(res); JSONArray tracks1 = jsonObject1.getJSONArray("tracks"); Object ob = tracks1.get(0); String obs = JSONObject.toJSONString(ob); JSONObject object = JSONObject.parseObject(obs); JSONArray point = object.getJSONArray("points"); points.addAll(point); } } // 轨迹点数小于999,一页查询出即可 if(counts > 0 && counts < 999){ Object ob = tracks.get(0); String obs = JSONObject.toJSONString(ob); JSONObject object = JSONObject.parseObject(obs); JSONArray point = object.getJSONArray("points"); points.addAll(point); } System.out.println("轨迹坐标点=====:"+points); String toLonLat = ""; double sum = 0; List<OrderPosition> orderPositions = new ArrayList<>(); for (Object o : points) { String s = JSONObject.toJSONString(o); JSONObject obj = JSONObject.parseObject(s); String fromLonLat = obj.getString("location"); String[] split = fromLonLat.split(","); Long locatetime = obj.getLong("locatetime"); Date date = new Date(locatetime); if(date.after(orderPrivateCar.getStartServiceTime()) && date.before(orderPrivateCar.getEndServiceTime())){ // 封装定位文件里的单个定位对象 OrderPosition orderPosition = new OrderPosition(); orderPosition.setOrderId(orderId); orderPosition.setOrderType(1); orderPosition.setDriverId(orderPrivateCar.getDriverId()); orderPosition.setLat(split[1]); orderPosition.setLon(split[0]); orderPosition.setAltitude("0"); orderPosition.setDirectionAngle("0"); //先计算里程,在存储最新位置 orderPosition.setInsertTime(new Date()); orderPositions.add(orderPosition); if(StringUtils.hasLength(fromLonLat) && StringUtils.hasLength(toLonLat)){ String[] from = fromLonLat.split(","); String[] to = toLonLat.split(","); GlobalCoordinates source = new GlobalCoordinates(Double.valueOf(from[1]), Double.valueOf(from[0])); GlobalCoordinates target = new GlobalCoordinates(Double.valueOf(to[1]), Double.valueOf(to[0])); double WGS84 = getDistanceMeter(source, target, Ellipsoid.WGS84); System.err.println("WGS84坐标系计算结果:"+WGS84 + "米"); sum += WGS84; } toLonLat = fromLonLat; } } System.err.println("======服务里程======"+sum); orderPrivateCar.setMileage(sum); // 将数据存储到文件中 File file = new File(filePath + orderId + "_" + 1 + ".txt"); if(!file.exists()){ file.getParentFile().mkdirs(); file.createNewFile(); } //写入相应的文件 PrintWriter out = new PrintWriter(new FileWriter(file)); out.write(JSON.toJSONString(orderPositions)); out.flush(); out.close(); break; } this.updateById(orderPrivateCar); @@ -352,6 +442,11 @@ return ResultUtil.success(); } private double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){ //创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离 GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo); return geoCurve.getEllipsoidalDistance(); } /** @@ -758,4 +853,11 @@ public List<OrderPrivateCar> getSmsOrderList() { return this.baseMapper.getSmsOrderList(); } @Override public void getTrackId(Integer orderId, Integer trackId) { OrderPrivateCar orderPrivateCar = this.selectById(orderId); orderPrivateCar.setTrackId(String.valueOf(trackId)); this.updateById(orderPrivateCar); } } DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/IOrderService.java
@@ -230,4 +230,13 @@ * @throws Exception */ Map<String, Object> queryMoneyInfo(Integer orderId, Integer orderType) throws Exception; /** * 设置订单轨迹id * @param orderId * @param orderType * @param trackId * @return */ void getTrackId(Integer orderId, Integer orderType, Integer trackId); } DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/DriverServiceImpl.java
@@ -503,9 +503,9 @@ loginWarpper.setAppid(UUIDUtil.getRandomCode()); //创建高德猎鹰的终端数据 // String tid = gdFalconUtil.createTerminal(phone); // loginWarpper.setServerId(gdFalconUtil.getServerId()); // loginWarpper.setTerminalId(tid); String tid = gdFalconUtil.createTerminal(phone); loginWarpper.setServerId(gdFalconUtil.getServerId()); loginWarpper.setTerminalId(tid); if(driver.getCompanyId()==null){ loginWarpper.setJumpCode("100000"); }else{ DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/OrderPositionServiceImpl.java
@@ -51,25 +51,25 @@ @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()); File file = new File(filePath + orderPosition.getOrderId() + "_" + orderPosition.getOrderType() + ".txt"); if(!file.exists()){ file.getParentFile().mkdirs(); file.createNewFile(); } //写入相应的文件 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(); } // boolean b = orderService.calculateMileage(orderPosition); // 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(); // } // //写入相应的文件 // 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(); // } new Thread(new Runnable() { @Override DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/OrderServiceImpl.java
@@ -1010,6 +1010,17 @@ return null; } @Override public void getTrackId(Integer orderId, Integer orderType, Integer trackId) { switch (orderType){ case 1: orderPrivateCarService.getTrackId(orderId,trackId); break; case 2: break; } } @Autowired private ISystemNoticeService systemNoticeService; DriverZYTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDFalconUtil.java
@@ -3,6 +3,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aliyun.oss.ServiceException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; @@ -146,7 +147,7 @@ * @return */ public String selectTerminal(String name){ String url = "https://tsapi.amap.com/v1/track/terminal/list?key=" + key + "&name=" + name; String url = "https://tsapi.amap.com/v1/track/terminal/list?key=" + key + "&sid=" + serverId + "&name=" + name; String forObject = restTemplate.getForObject(url, String.class); JSONObject jsonObject = JSON.parseObject(forObject); if(jsonObject.getIntValue("errcode") == 10000){ @@ -218,6 +219,27 @@ } } /** * 查询轨迹 * @param tid 终端id */ public String selectTrack(String tid, String trid,Integer page){ String url = "https://tsapi.amap.com/v1/track/terminal/trsearch?key=" + key + "&sid=" + serverId + "&tid=" + tid + "&trid=" + trid + "&page=" + page + "&pagesize=" + 999; // String url = "https://tsapi.amap.com/v1/track/terminal/trsearch?key=" + key + "&sid=" + serverId + "&tid=" + tid + "&starttime=1708410190000" + "&endtime=1708419641315"; //将请求头部和参数合成一个请求 String s = restTemplate.getForObject(url , String.class); JSONObject jsonObject = JSON.parseObject(s); if(jsonObject.getIntValue("errcode") == 10000){ return jsonObject.getString("data"); } System.err.println(jsonObject.getString("errmsg")); if(jsonObject.getIntValue("errcode") != 10000){ throw new ServiceException(jsonObject.getString("errmsg")); } return null; } // // public Map<String, Object> queryTrsearch(){