Commit b6e902ad by chamberone

feat: 添加获取道路距离接口

1 parent b7a21942
package com.dituhui.pea.dispatch.controller;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.druid.util.StringUtils;
import com.dituhui.pea.common.BusinessException;
import com.dituhui.pea.common.Result;
import com.dituhui.pea.dispatch.IPath;
import com.dituhui.pea.dispatch.dao.OrderInfoRepository;
import com.dituhui.pea.dispatch.entity.OrderInfo;
import com.dituhui.pea.dispatch.pojo.Location;
import com.dituhui.pea.dispatch.utils.RoadDistanceUtils;
import com.dituhui.pea.dispatch.utils.RoadDistanceUtils.Distance;
import com.dituhui.pea.pojo.DistanceDTO;
/**
* 道路控制层
*/
@RestController
public class PathController implements IPath {
@Autowired
OrderInfoRepository orderInfoRepository;
@Override
public Result<DistanceDTO> getRoadDistance(String fromOrderId, String toOrderId, Integer vehicleType) {
try {
// 参数检查
if (StringUtils.isEmpty(fromOrderId) || StringUtils.isEmpty(toOrderId)) {
return Result.failure("missing fromOrderId or toOrderId");
}
if (null == vehicleType) {
vehicleType = 1;
}
List<OrderInfo> fromOrders = orderInfoRepository.findByOrderId(fromOrderId);
List<OrderInfo> toOrders = orderInfoRepository.findByOrderId(toOrderId);
if (CollectionUtils.isEmpty(fromOrders) || CollectionUtils.isEmpty(toOrders)) {
return Result.failure("fromOrderId or toOrderId not exists");
}
if (StringUtils.isEmpty(fromOrders.get(0).getX()) || StringUtils.isEmpty(fromOrders.get(0).getY())
|| StringUtils.isEmpty(toOrders.get(0).getX()) || StringUtils.isEmpty(toOrders.get(0).getY())) {
return Result.failure("missing coordinates");
}
// 获取距离
Distance distance = RoadDistanceUtils.getDistance(
new Location(0L, Float.parseFloat(fromOrders.get(0).getX()),
Float.parseFloat(fromOrders.get(0).getY())),
new Location(0L, Float.parseFloat(toOrders.get(0).getX()),
Float.parseFloat(toOrders.get(0).getY())),
vehicleType);
DistanceDTO dto = new DistanceDTO();
dto.setDis(distance.getDis());
dto.setTime(distance.getTime());
return Result.success(dto);
} catch (BusinessException e) {
return Result.failure(e.getMessage());
}
}
}
package com.dituhui.pea.dispatch;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.dituhui.pea.common.Result;
import com.dituhui.pea.pojo.DistanceDTO;
/**
* 道路相关接口
*
* @author
*/
@FeignClient(value = "project-dispatch", contextId = "dispatch")
public interface IPath {
/**
* 获取2个订单之间的道路距离
*
* @param fromOrderId 出发订单id
* @param toOrderId 到达订单id
* @param vehicleType 交通工具:1汽车;2电动车;3自行车;4步行 默认是汽车
* @return
*/
@RequestMapping(value = "/pea-dispatch/getRoadDistance", method = RequestMethod.POST)
public Result<DistanceDTO> getRoadDistance(@RequestParam("fromOrderId") String fromOrderId,
@RequestParam("toOrderId") String toOrderId,
@RequestParam(name = "vehicleType", required = false) Integer vehicleType);
}
package com.dituhui.pea.pojo;
import lombok.Data;
@Data
public class DistanceDTO {
/**
* 单位 公里
*/
float dis;
/**
* 单位 秒
*/
int time;
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!