From 21c50cd93a1de1e01e677962aa51e27704ac4b95 Mon Sep 17 00:00:00 2001 From: lqy <3431632001@qq,com> Date: Thu, 12 Dec 2024 12:04:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E4=BD=8D=E7=BD=AE=E5=8C=B9=E9=85=8D=E8=B7=AF?= =?UTF-8?q?=E7=BA=BF=E6=8E=A5=E5=8F=A3=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=BA=86?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B7=AF=E7=BA=BF=E6=97=B6=E4=B8=8D=E4=BC=9A?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E8=AE=BE=E7=BD=AE=E5=81=9C=E9=9D=A0=E7=82=B9?= =?UTF-8?q?=E7=9A=84stopOrder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../route/controller/RouteController.java | 14 +++ .../school/route/service/RoutesService.java | 2 + .../route/service/impl/RouteServiceImpl.java | 102 +++++++++++++++++- 3 files changed, 114 insertions(+), 4 deletions(-) diff --git a/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/controller/RouteController.java b/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/controller/RouteController.java index 6d114486..c94cfb04 100644 --- a/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/controller/RouteController.java +++ b/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/controller/RouteController.java @@ -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); + } } diff --git a/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/service/RoutesService.java b/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/service/RoutesService.java index bdd5e9bf..82752aa3 100644 --- a/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/service/RoutesService.java +++ b/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/service/RoutesService.java @@ -18,4 +18,6 @@ public interface RoutesService extends SkyeyeBusinessService { void queryRoutesByStartAndEnd(InputObject inputObject, OutputObject outputObject); void queryPageListBySchoolId(InputObject inputObject, OutputObject outputObject); + + void queryRoutesNavigationLists(InputObject inputObject, OutputObject outputObject); } diff --git a/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/service/impl/RouteServiceImpl.java b/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/service/impl/RouteServiceImpl.java index d0688677..683350e6 100644 --- a/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/service/impl/RouteServiceImpl.java +++ b/skyeye-school/school-pro/src/main/java/com/skyeye/school/route/service/impl/RouteServiceImpl.java @@ -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 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 bean = setBaseMation(queryWrapper); outputObject.setBeans(bean); @@ -129,6 +138,88 @@ public class RouteServiceImpl extends SkyeyeBusinessServiceImpl 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 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 routesList = list(queryWrapper); + if(CollectionUtil.isEmpty(routesList)){ + throw new CustomException("暂无去无改地点的路线"); + } + Map map = new HashMap<>(); + for (Routes route : routesList) { + QueryWrapper routeStopQueryWrapper = new QueryWrapper<>(); + routeStopQueryWrapper.eq(MybatisPlusUtil.toColumns(RouteStop::getRouteId), route.getId()); + routeStopQueryWrapper.orderByAsc(MybatisPlusUtil.toColumns(RouteStop::getStopOrder)); + List 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 beans = new ArrayList<>(); + List> list = new ArrayList<>(map.entrySet()); + list.sort(Map.Entry.comparingByValue()); + for (Map.Entry 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 routeStops = entity.getRouteStopList(); QueryWrapper queryWrapper = new QueryWrapper<>(); @@ -151,6 +243,8 @@ public class RouteServiceImpl extends SkyeyeBusinessServiceImpl