Commit 8a764994dc05804fa795bf4f9b9b6cd204120b88
1 parent
3d8f71d6
新增轨迹创建逻辑,并基于运单消息触发轨迹生成
Showing
2 changed files
with
75 additions
and
0 deletions
sl-express-ms-track-service/src/main/java/com/sl/ms/track/mq/MQListener.java
| 1 | 1 | package com.sl.ms.track.mq; |
| 2 | 2 | |
| 3 | +import cn.hutool.json.JSONUtil; | |
| 3 | 4 | import com.sl.ms.track.service.TrackService; |
| 4 | 5 | import com.sl.transport.common.constant.Constants; |
| 6 | +import com.sl.transport.common.vo.TransportOrderMsg; | |
| 5 | 7 | import lombok.extern.slf4j.Slf4j; |
| 6 | 8 | import org.springframework.amqp.core.ExchangeTypes; |
| 7 | 9 | import org.springframework.amqp.rabbit.annotation.Exchange; |
| ... | ... | @@ -37,6 +39,8 @@ public class MQListener { |
| 37 | 39 | )) |
| 38 | 40 | public void listenTransportOrderCreatedMsg(String msg) { |
| 39 | 41 | log.info("接收到新增运单的消息 ({})-> {}", Constants.MQ.Queues.TRACK_TRANSPORT_ORDER_CREATED, msg); |
| 42 | + TransportOrderMsg transportOrderMsg = JSONUtil.toBean(msg, TransportOrderMsg.class); | |
| 43 | + trackService.create(transportOrderMsg.getId()); | |
| 40 | 44 | //TODO 实战完成 |
| 41 | 45 | } |
| 42 | 46 | ... | ... |
sl-express-ms-track-service/src/main/java/com/sl/ms/track/service/impl/TrackServiceImpl.java
| 1 | 1 | package com.sl.ms.track.service.impl; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.convert.Convert; | |
| 4 | +import cn.hutool.core.map.MapUtil; | |
| 5 | +import cn.hutool.core.util.StrUtil; | |
| 6 | +import cn.hutool.json.JSONArray; | |
| 7 | +import cn.hutool.json.JSONObject; | |
| 8 | +import cn.hutool.json.JSONUtil; | |
| 9 | +import com.itheima.em.sdk.EagleMapTemplate; | |
| 10 | +import com.itheima.em.sdk.enums.ProviderEnum; | |
| 11 | +import com.itheima.em.sdk.vo.Coordinate; | |
| 12 | +import com.sl.ms.oms.api.OrderFeign; | |
| 13 | +import com.sl.ms.oms.dto.OrderLocationDTO; | |
| 14 | +import com.sl.ms.track.domain.enums.TrackStatusEnum; | |
| 15 | +import com.sl.ms.track.domain.enums.TrackTypeEnum; | |
| 3 | 16 | import com.sl.ms.track.entity.TrackEntity; |
| 4 | 17 | import com.sl.ms.track.service.TrackService; |
| 18 | +import com.sl.ms.work.api.TransportOrderFeign; | |
| 19 | +import com.sl.ms.work.domain.dto.TransportOrderDTO; | |
| 20 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 21 | +import org.springframework.data.mongodb.core.MongoTemplate; | |
| 5 | 22 | import org.springframework.stereotype.Service; |
| 6 | 23 | |
| 7 | 24 | import java.util.List; |
| 25 | +import java.util.Map; | |
| 8 | 26 | |
| 9 | 27 | @Service |
| 10 | 28 | public class TrackServiceImpl implements TrackService { |
| 29 | + | |
| 30 | + @Autowired | |
| 31 | + private OrderFeign orderFeign; | |
| 32 | + | |
| 33 | + @Autowired | |
| 34 | + private TransportOrderFeign transportOrderFeign; | |
| 35 | + | |
| 36 | + @Autowired | |
| 37 | + private MongoTemplate mongoTemplate; | |
| 38 | + | |
| 39 | + @Autowired | |
| 40 | + private EagleMapTemplate eagleMapTemplate; | |
| 41 | + | |
| 11 | 42 | @Override |
| 12 | 43 | public boolean create(String transportOrderId) { |
| 44 | + TransportOrderDTO transportOrderDTO = transportOrderFeign.findById(transportOrderId); | |
| 45 | + | |
| 46 | + OrderLocationDTO orderLocationDTO = orderFeign.findOrderLocationByOrderId(transportOrderDTO.getOrderId()); | |
| 47 | + | |
| 48 | + TrackEntity trackEntity = new TrackEntity(); | |
| 49 | + trackEntity.setTransportOrderId(transportOrderId); | |
| 50 | + | |
| 51 | + String sendLocation = orderLocationDTO.getSendLocation(); | |
| 52 | + String receiveLocation = orderLocationDTO.getReceiveLocation(); | |
| 53 | + | |
| 54 | + double sendLnt = Double.parseDouble(sendLocation.split(",")[0]); | |
| 55 | + double sendLat = Double.parseDouble(sendLocation.split(",")[1]); | |
| 56 | + | |
| 57 | + | |
| 58 | + double receiveLnt = Double.parseDouble(receiveLocation.split(",")[0]); | |
| 59 | + double receiveLat = Double.parseDouble(receiveLocation.split(",")[1]); | |
| 60 | + | |
| 61 | + Coordinate origin = new Coordinate(sendLnt, sendLat); | |
| 62 | + Coordinate destination = new Coordinate(receiveLnt, receiveLat); | |
| 63 | + | |
| 64 | + //设置高德地图参数,默认是不返回预计耗时的,需要额外设置参数 | |
| 65 | + Map<String, Object> param = MapUtil.<String, Object>builder().put("show_fields", "polyline").build(); | |
| 66 | + String driving = this.eagleMapTemplate.opsForDirection().driving(ProviderEnum.AMAP, origin, destination, param); | |
| 67 | + if (StrUtil.isEmpty(driving)) { | |
| 68 | + return false; | |
| 69 | + } | |
| 70 | + JSONObject jsonObject = JSONUtil.parseObj(driving); | |
| 71 | + | |
| 72 | + //距离,单位:米 | |
| 73 | + Double distance = Convert.toDouble(jsonObject.getByPath("route.paths[0].distance"), -1d); | |
| 74 | + trackEntity.setDistance(distance); | |
| 75 | + trackEntity.setStatus(TrackStatusEnum.NEW); | |
| 76 | + trackEntity.setType(TrackTypeEnum.COURIER); | |
| 77 | + | |
| 78 | + JSONArray steps = jsonObject.getByPath("route.paths[0].steps", JSONArray.class); | |
| 79 | + for (int i = 0; i < steps.size(); i++) { | |
| 80 | + JSONObject entries = steps.getJSONObject(i); | |
| 81 | + String polyline = entries.getStr("polyline"); | |
| 82 | + } | |
| 83 | + | |
| 13 | 84 | return false; |
| 14 | 85 | } |
| 15 | 86 | ... | ... |