feat:添加根据当前位置匹配路线接口,修改了更新路线时不会重新设置停靠点的stopOrder

This commit is contained in:
lqy 2024-12-12 12:04:44 +08:00
parent e8077e84cf
commit 21c50cd93a
3 changed files with 114 additions and 4 deletions

View file

@ -65,9 +65,23 @@ public class RouteController {
@ApiImplicitParam(id = "startId", name = "startId", value = "起点id", required = "required"),
@ApiImplicitParam(id = "endId", name = "endId", value = "终点id", required = "required"),
@ApiImplicitParam(id = "schoolId", name = "schoolId", value = "学校id", required = "required"),
@ApiImplicitParam(id = "typeId", name = "typeId", value = "路线类型")
})
@RequestMapping("/post/RouteController/queryRoutesByStartAndEnd")
public void queryRoutesByStartAndEnd(InputObject inputObject, OutputObject outputObject) {
routeService.queryRoutesByStartAndEnd(inputObject, outputObject);
}
@ApiOperation(id = "queryRoutesNavigationLists", value = "根据当前位置到终点的导航路线查询前3条路线<=3", method = "POST", allUse = "2")
@ApiImplicitParams({
@ApiImplicitParam(id = "latitude", name = "latitude", value = "纬度", required = "required"),
@ApiImplicitParam(id = "longitude", name = "longitude", value = "经度", required = "required"),
@ApiImplicitParam(id = "endId", name = "endId", value = "终点id", required = "required"),
@ApiImplicitParam(id = "schoolId", name = "schoolId", value = "学校id", required = "required"),
@ApiImplicitParam(id = "typeId", name = "typeId", value = "路线类型")
})
@RequestMapping("/post/RouteController/queryRoutesNavigationLists")
public void queryRoutesNavigationLists(InputObject inputObject, OutputObject outputObject) {
routeService.queryRoutesNavigationLists(inputObject, outputObject);
}
}

View file

@ -18,4 +18,6 @@ public interface RoutesService extends SkyeyeBusinessService<Routes> {
void queryRoutesByStartAndEnd(InputObject inputObject, OutputObject outputObject);
void queryPageListBySchoolId(InputObject inputObject, OutputObject outputObject);
void queryRoutesNavigationLists(InputObject inputObject, OutputObject outputObject);
}

View file

@ -1,5 +1,7 @@
package com.skyeye.school.route.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
@ -7,12 +9,14 @@ import com.skyeye.annotation.service.SkyeyeService;
import com.skyeye.base.business.service.impl.SkyeyeBusinessServiceImpl;
import com.skyeye.common.constans.CommonNumConstants;
import com.skyeye.common.entity.search.CommonPageInfo;
import com.skyeye.common.enumeration.EnableEnum;
import com.skyeye.common.object.InputObject;
import com.skyeye.common.object.OutputObject;
import com.skyeye.common.util.mybatisplus.MybatisPlusUtil;
import com.skyeye.eve.entity.School;
import com.skyeye.eve.service.IAuthUserService;
import com.skyeye.eve.service.SchoolService;
import com.skyeye.exception.CustomException;
import com.skyeye.rest.wall.user.service.IUserService;
import com.skyeye.school.building.entity.TeachBuilding;
import com.skyeye.school.building.service.TeachBuildingService;
@ -22,13 +26,12 @@ import com.skyeye.school.route.entity.RouteStop;
import com.skyeye.school.route.entity.Routes;
import com.skyeye.school.route.service.RouteStopService;
import com.skyeye.school.route.service.RoutesService;
import org.nutz.el.opt.custom.DoBase64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
@ -87,11 +90,17 @@ public class RouteServiceImpl extends SkyeyeBusinessServiceImpl<RoutesDao, Route
String startId = (String) params.get("startId");
String endId = (String) params.get("endId");
String schoolId = (String) params.get("schoolId");
String typeId = (String) params.get("typeId");
int routeType = CommonNumConstants.NUM_ONE;
if(StrUtil.isNotEmpty(typeId)){
routeType = Integer.parseInt(typeId);
}
QueryWrapper<Routes> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(MybatisPlusUtil.toColumns(Routes::getStartId), startId)
.eq(MybatisPlusUtil.toColumns(Routes::getEndId), endId)
.eq(MybatisPlusUtil.toColumns(Routes::getSchoolId), schoolId)
.eq(MybatisPlusUtil.toColumns(Routes::getEnabled),CommonNumConstants.NUM_ONE)
.eq(MybatisPlusUtil.toColumns(Routes::getEnabled),EnableEnum.ENABLE_USING.getKey())
.eq(MybatisPlusUtil.toColumns(Routes::getRouteType), routeType)
.orderByAsc(MybatisPlusUtil.toColumns(Routes::getRouteLength));
List<Routes> bean = setBaseMation(queryWrapper);
outputObject.setBeans(bean);
@ -129,6 +138,88 @@ public class RouteServiceImpl extends SkyeyeBusinessServiceImpl<RoutesDao, Route
outputObject.settotal(page.getTotal());
}
@Override
public void queryRoutesNavigationLists(InputObject inputObject, OutputObject outputObject) {
Map<String, Object> params = inputObject.getParams();
String schoolId = (String) params.get("schoolId");
String endId = (String) params.get("endId");
String typeId = (String) params.get("typeId");
int routeType = CommonNumConstants.NUM_ONE;
if(StrUtil.isNotEmpty(typeId)){
routeType = Integer.parseInt(typeId);
}
double latitude = Double.parseDouble(params.get("latitude").toString()) ;
double longitude = Double.parseDouble(params.get("longitude").toString()) ;
School schoolMation = schoolService.selectById(schoolId);
QueryWrapper<Routes> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(MybatisPlusUtil.toColumns(Routes::getSchoolId), schoolId)
.eq(MybatisPlusUtil.toColumns(Routes::getEndId), endId)
.eq(MybatisPlusUtil.toColumns(Routes::getRouteType), routeType)
.eq(MybatisPlusUtil.toColumns(Routes::getEnabled), EnableEnum.ENABLE_USING.getKey());
List<Routes> routesList = list(queryWrapper);
if(CollectionUtil.isEmpty(routesList)){
throw new CustomException("暂无去无改地点的路线");
}
Map<String,Double> map = new HashMap<>();
for (Routes route : routesList) {
QueryWrapper<RouteStop> routeStopQueryWrapper = new QueryWrapper<>();
routeStopQueryWrapper.eq(MybatisPlusUtil.toColumns(RouteStop::getRouteId), route.getId());
routeStopQueryWrapper.orderByAsc(MybatisPlusUtil.toColumns(RouteStop::getStopOrder));
List<RouteStop> routeStopList = routeStopService.list(routeStopQueryWrapper);
Double start = haversine(latitude, longitude,
Double.parseDouble(routeStopList.get(CommonNumConstants.NUM_ZERO).getLatitude()),
Double.parseDouble(routeStopList.get(CommonNumConstants.NUM_ZERO).getLongitude()));
Double end = haversine(latitude, longitude,
Double.parseDouble(routeStopList.get(routeStopList.size() - 1).getLatitude()),
Double.parseDouble(routeStopList.get(routeStopList.size() - 1).getLongitude()));
// 将当前位置作为停靠点的第一个点
RouteStop routeStop = new RouteStop();
routeStop.setLatitude(String.valueOf(latitude));
routeStop.setLongitude(String.valueOf(longitude));
routeStop.setStopOrder(CommonNumConstants.NUM_ZERO);
routeStopList.add(CommonNumConstants.NUM_ZERO, routeStop);
route.setRouteStopList(routeStopList);
route.setSchoolMation(schoolMation);
map.put(route.getId(), start + end);
}
if(map.size() <= CommonNumConstants.NUM_THREE){
outputObject.setBeans(routesList);
outputObject.settotal(routesList.size());
}else {
// 根据距离排序
List<Routes> beans = new ArrayList<>();
List<Map.Entry<String, Double>> list = new ArrayList<>(map.entrySet());
list.sort(Map.Entry.comparingByValue());
for (Map.Entry<String, Double> entry : list) {
for (Routes route : routesList) {
if (entry.getKey().equals(route.getId())) {
beans.add(route);
break;
}
}
if(beans.size()==CommonNumConstants.NUM_THREE) break;
}
outputObject.setBeans(beans);
outputObject.settotal(beans.size());
}
}
// Haversine公式计算两个经纬度点之间的距离
private static double haversine(double lat1, double lon1, double lat2, double lon2) {
final int R = 6371; // 地球半径单位为公里
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double distance = R * c;
return distance;
}
@Transactional
@Override
public void createPostpose(Routes entity, String userId) {
@ -144,6 +235,7 @@ public class RouteServiceImpl extends SkyeyeBusinessServiceImpl<RoutesDao, Route
@Override
public void updatePostpose(Routes entity, String userId) {
Integer stopOrder = CommonNumConstants.NUM_ONE;
String routeId = entity.getId();
List<RouteStop> routeStops = entity.getRouteStopList();
QueryWrapper<RouteStop> queryWrapper = new QueryWrapper<>();
@ -151,6 +243,8 @@ public class RouteServiceImpl extends SkyeyeBusinessServiceImpl<RoutesDao, Route
routeStopService.remove(queryWrapper);
for (RouteStop routeStop : routeStops) {
routeStop.setRouteId(routeId);
routeStop.setStopOrder(stopOrder);
stopOrder++;
}
routeStopService.createEntity(routeStops, userId);
}