From 0a37c352fb480cb99e278ca398571b3c21be7988 Mon Sep 17 00:00:00 2001 From: cb <275647614@qq.com> Date: Sat, 23 Aug 2025 16:04:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=9A=E8=BF=87=E5=BA=97?= =?UTF-8?q?=E9=93=BAid=20=E4=BB=A5=E5=8F=8A=20=E7=BB=8F=E7=BA=AC=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E4=B8=8E=E5=BA=97=E9=93=BA=E7=9A=84=E8=B7=9D=E7=A6=BB?= =?UTF-8?q?=20=E8=BF=94=E5=9B=9E=E5=BA=97=E9=93=BA=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ghy/web/controller/ShopController.java | 72 ++++++++++- .../web/controller/order/OrderController.java | 10 +- .../ghy/web/pojo/vo/OrderListResponse.java | 2 + ghy-shop/pom.xml | 5 + .../ghy/shop/domain/ShopDistanceQuery.java | 27 ++++ .../com/ghy/shop/service/ShopService.java | 8 ++ .../shop/service/impl/ShopServiceImpl.java | 120 ++++++++++++++++++ 7 files changed, 238 insertions(+), 6 deletions(-) create mode 100644 ghy-shop/src/main/java/com/ghy/shop/domain/ShopDistanceQuery.java diff --git a/ghy-admin/src/main/java/com/ghy/web/controller/ShopController.java b/ghy-admin/src/main/java/com/ghy/web/controller/ShopController.java index d2d99191..229a6efd 100644 --- a/ghy-admin/src/main/java/com/ghy/web/controller/ShopController.java +++ b/ghy-admin/src/main/java/com/ghy/web/controller/ShopController.java @@ -2,6 +2,7 @@ package com.ghy.web.controller; import com.ghy.common.core.controller.BaseController; import com.ghy.shop.domain.Shop; +import com.ghy.shop.domain.ShopDistanceQuery; import com.ghy.shop.service.ShopService; import com.ghy.common.core.domain.AjaxResult; import org.springframework.beans.factory.annotation.Autowired; @@ -436,6 +437,75 @@ public class ShopController extends BaseController { } } + /** + * 通过店铺ID查询详细信息并计算距离 + * @param query 距离查询请求实体 + * @return 包含距离信息的完整店铺详情 + */ + @PostMapping("/getShopDetailWithDistance") + public AjaxResult getShopDetailWithDistance(@RequestBody ShopDistanceQuery query) { + try { + // 验证参数 + if (query.getShopId() == null) { + return AjaxResult.error("店铺ID不能为空"); + } + + if (query.getLatitude() == null || query.getLongitude() == null) { + return AjaxResult.error("经纬度参数不能为空"); + } + + if (!LocationUtils.isValidCoordinate(query.getLatitude(), query.getLongitude())) { + return AjaxResult.error("无效的坐标参数"); + } + + // 调用服务层方法获取店铺详情和距离 + Shop shop = shopService.getShopWithDistance(query); + + if (shop == null) { + return AjaxResult.error("未找到店铺"); + } + + // 检查距离是否有效 + if (shop.getDistance() == null || shop.getDistance().equals("-1")) { + return AjaxResult.error("无法计算店铺距离,请检查店铺地址信息"); + } + + // 构建返回结果 + Map result = new HashMap<>(); + result.put("shopId", shop.getShopId()); + result.put("shopName", shop.getShopName()); + result.put("imageUrl", shop.getImageUrl()); + result.put("workerId", shop.getWorkerId()); + result.put("provinceId", shop.getProvinceId()); + result.put("provinceName", shop.getProvinceName()); + result.put("cityId", shop.getCityId()); + result.put("cityName", shop.getCityName()); + result.put("countryId", shop.getCountryId()); + result.put("countryName", shop.getCountryName()); + result.put("streetId", shop.getStreetId()); + result.put("streetName", shop.getStreetName()); + result.put("address", shop.getAddress()); + result.put("phone", shop.getPhone()); + result.put("latitude", shop.getLatitude()); + result.put("longitude", shop.getLongitude()); + result.put("distance", shop.getDistance()); + + // 添加额外的距离信息 + Map distanceInfo = new HashMap<>(); + distanceInfo.put("formattedDistance", shop.getDistance()); + distanceInfo.put("currentLatitude", query.getLatitude()); + distanceInfo.put("currentLongitude", query.getLongitude()); + + result.put("distanceInfo", distanceInfo); + + return AjaxResult.success("查询成功", result); + + } catch (Exception e) { + logger.error("查询店铺详情失败: {}", e.getMessage(), e); + return AjaxResult.error("查询失败: " + e.getMessage()); + } + } + // /** // * 获取店铺覆盖范围内的其他店铺 // */ @@ -459,7 +529,7 @@ public class ShopController extends BaseController { // Map nearbyParams = new HashMap<>(); // nearbyParams.put("latitude", centerShop.getLatitude()); // nearbyParams.put("longitude", centerShop.getLongitude()); -// nearbyParams.put("radiusKm", radiusKm); +// radiusKm); // // AjaxResult nearbyResult = getNearbyShops(nearbyParams); // if (nearbyResult.isSuccess()) { diff --git a/ghy-admin/src/main/java/com/ghy/web/controller/order/OrderController.java b/ghy-admin/src/main/java/com/ghy/web/controller/order/OrderController.java index 347b1999..046890d6 100644 --- a/ghy-admin/src/main/java/com/ghy/web/controller/order/OrderController.java +++ b/ghy-admin/src/main/java/com/ghy/web/controller/order/OrderController.java @@ -589,10 +589,10 @@ public class OrderController extends BaseController { OrderMaster orderMaster = new OrderMaster(); orderMaster.setOrderType(0); // 判断是否是配件商品 - // if (goods.getType() != null && goods.getType() == 2) { - // // 配件商品,需要获取对应的服务商品 - // logger.info("当前商品是配件商品,需要获取对应的服务商品"); - // orderMaster.setOrderType(1); + if (goods.getType() != null && goods.getType() == 2) { + // 配件商品,需要获取对应的服务商品 + orderMaster.setOrderType(1); + } // // 通过商品的类目ID获取类目信息 // DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.selectOneByGoodsCategoryId(goods.getDeptGoodsCategoryId()); @@ -1709,7 +1709,7 @@ public class OrderController extends BaseController { orderListResponse.setOrderImages(master.getOrderImages()); orderListResponse.setIsInvoiced(master.getIsInvoiced()); orderListResponse.setOriginalWorkerId(master.getOriginalWorkerId()); - + orderListResponse.setGoodsId(master.getGoodsId()); orderListResponse.setServerGoodsId(master.getServerGoodsId()); orderListResponse.setServiceShopId(master.getServiceShopId()); orderListResponse.setOrderType(master.getOrderType()); diff --git a/ghy-admin/src/main/java/com/ghy/web/pojo/vo/OrderListResponse.java b/ghy-admin/src/main/java/com/ghy/web/pojo/vo/OrderListResponse.java index e35cf667..0403d7bd 100644 --- a/ghy-admin/src/main/java/com/ghy/web/pojo/vo/OrderListResponse.java +++ b/ghy-admin/src/main/java/com/ghy/web/pojo/vo/OrderListResponse.java @@ -256,4 +256,6 @@ public class OrderListResponse { private Date confirmStartTime; private Integer deliveryType; + + private Long goodsId; } diff --git a/ghy-shop/pom.xml b/ghy-shop/pom.xml index e87b30bc..e389dce1 100644 --- a/ghy-shop/pom.xml +++ b/ghy-shop/pom.xml @@ -36,6 +36,11 @@ 3.5.9 compile + + + com.ghy + ghy-common + diff --git a/ghy-shop/src/main/java/com/ghy/shop/domain/ShopDistanceQuery.java b/ghy-shop/src/main/java/com/ghy/shop/domain/ShopDistanceQuery.java new file mode 100644 index 00000000..ac9897e3 --- /dev/null +++ b/ghy-shop/src/main/java/com/ghy/shop/domain/ShopDistanceQuery.java @@ -0,0 +1,27 @@ +package com.ghy.shop.domain; + +import lombok.Data; +import java.io.Serializable; + +/** + * 店铺距离查询请求实体类 + */ +@Data +public class ShopDistanceQuery implements Serializable { + + private Long shopId; // 店铺ID + private Double latitude; // 当前纬度 + private Double longitude; // 当前经度 + + // 可选参数,用于扩展 + private String unit; // 距离单位:km(公里)、m(米) + private Boolean updateShopLocation; // 是否更新店铺的经纬度信息 + + public ShopDistanceQuery() {} + + public ShopDistanceQuery(Long shopId, Double latitude, Double longitude) { + this.shopId = shopId; + this.latitude = latitude; + this.longitude = longitude; + } +} diff --git a/ghy-shop/src/main/java/com/ghy/shop/service/ShopService.java b/ghy-shop/src/main/java/com/ghy/shop/service/ShopService.java index ed365614..e4510ed4 100644 --- a/ghy-shop/src/main/java/com/ghy/shop/service/ShopService.java +++ b/ghy-shop/src/main/java/com/ghy/shop/service/ShopService.java @@ -1,6 +1,7 @@ package com.ghy.shop.service; import com.ghy.shop.domain.Shop; +import com.ghy.shop.domain.ShopDistanceQuery; import java.util.List; public interface ShopService { @@ -14,4 +15,11 @@ public interface ShopService { * 根据工作人员ID查询商铺列表 */ List getShopsByWorkerId(Long workerId); + + /** + * 通过店铺ID查询详细信息并计算距离 + * @param query 距离查询请求实体 + * @return 包含距离信息的店铺详情 + */ + Shop getShopWithDistance(ShopDistanceQuery query); } \ No newline at end of file diff --git a/ghy-shop/src/main/java/com/ghy/shop/service/impl/ShopServiceImpl.java b/ghy-shop/src/main/java/com/ghy/shop/service/impl/ShopServiceImpl.java index 8b6d6aa8..81b6cff7 100644 --- a/ghy-shop/src/main/java/com/ghy/shop/service/impl/ShopServiceImpl.java +++ b/ghy-shop/src/main/java/com/ghy/shop/service/impl/ShopServiceImpl.java @@ -1,12 +1,16 @@ package com.ghy.shop.service.impl; import com.ghy.shop.domain.Shop; +import com.ghy.shop.domain.ShopDistanceQuery; import com.ghy.shop.mapper.ShopMapper; import com.ghy.shop.service.ShopService; +import com.ghy.common.utils.LocationUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Map; +import java.util.HashMap; @Service public class ShopServiceImpl implements ShopService { @@ -42,4 +46,120 @@ public class ShopServiceImpl implements ShopService { public List getShopsByWorkerId(Long workerId) { return shopMapper.selectShopsByWorkerId(workerId); } + + @Override + public Shop getShopWithDistance(ShopDistanceQuery query) { + // 获取店铺基本信息 + Shop shop = shopMapper.select(query.getShopId()); + if (shop == null) { + return null; + } + + // 计算距离 + double distance = calculateDistance(shop, query.getLatitude(), query.getLongitude()); + + // 设置距离信息 + shop.setDistance(LocationUtils.formatDistance(distance * 1000)); // 转换为米并格式化 + + // 如果请求更新店铺位置信息,则更新店铺的经纬度 + if (query.getUpdateShopLocation() != null && query.getUpdateShopLocation() && + shop.getLatitude() != null && shop.getLongitude() != null) { + shopMapper.update(shop); + } + + return shop; + } + + /** + * 计算店铺与指定坐标的距离 + * 如果店铺有经纬度,直接计算;如果没有,则通过地址解析获取经纬度后计算 + */ + private double calculateDistance(Shop shop, Double latitude, Double longitude) { + // 如果店铺有经纬度,直接计算距离 + if (shop.getLatitude() != null && shop.getLongitude() != null) { + return LocationUtils.getDistanceInKilometers( + latitude, longitude, + shop.getLatitude(), shop.getLongitude() + ); + } + + // 如果店铺没有经纬度但有地址,尝试通过地址解析获取经纬度 + if (shop.getAddress() != null && !shop.getAddress().trim().isEmpty()) { + try { + // 构建完整地址 + StringBuilder fullAddress = new StringBuilder(); + if (shop.getProvinceName() != null) fullAddress.append(shop.getProvinceName()); + if (shop.getCityName() != null) fullAddress.append(shop.getCityName()); + if (shop.getCountryName() != null) fullAddress.append(shop.getCountryName()); + if (shop.getStreetName() != null) fullAddress.append(shop.getStreetName()); + if (shop.getAddress() != null) fullAddress.append(shop.getAddress()); + + String address = fullAddress.toString(); + if (!address.isEmpty()) { + // 这里应该调用地图API获取地址的经纬度 + // 暂时使用示例坐标(实际使用时请替换为真实的API调用) + Map coordinates = getCoordinatesByAddress(address); + if (coordinates != null) { + double shopLat = coordinates.get("latitude"); + double shopLng = coordinates.get("longitude"); + + // 计算距离 + double distance = LocationUtils.getDistanceInKilometers( + latitude, longitude, shopLat, shopLng + ); + + // 更新店铺的经纬度信息 + shop.setLatitude(shopLat); + shop.setLongitude(shopLng); + + return distance; + } + } + } catch (Exception e) { + // 地址解析失败,记录日志但不影响主流程 + System.err.println("地址解析失败: " + e.getMessage()); + } + } + + // 无法获取距离,返回-1表示无效 + return -1; + } + + /** + * 通过地址获取经纬度坐标 + * 这是一个示例实现,实际使用时请替换为真实的地图API调用 + */ + private Map getCoordinatesByAddress(String address) { + try { + // TODO: 这里应该调用真实的地图API + // 示例:百度地图正向地理编码API + // String url = "https://api.map.baidu.com/geocoding/v3/?address=" + + // java.net.URLEncoder.encode(address, "UTF-8") + + // "&output=json&ak=YOUR_BAIDU_API_KEY"; + // + // String result = restTemplate.getForObject(url, String.class); + // JSONObject jsonResult = JSONObject.parseObject(result); + // + // if ("0".equals(jsonResult.getString("status"))) { + // JSONObject location = jsonResult.getJSONObject("result").getJSONObject("location"); + // Double lng = location.getDouble("lng"); + // Double lat = location.getDouble("lat"); + // + // Map coordinates = new HashMap<>(); + // coordinates.put("longitude", lng); + // coordinates.put("latitude", lat); + // return coordinates; + // } + + // 临时返回示例坐标(实际使用时请替换为真实的API调用) + Map coordinates = new HashMap<>(); + coordinates.put("latitude", 39.915 + Math.random() * 0.1); // 示例纬度 + coordinates.put("longitude", 116.404 + Math.random() * 0.1); // 示例经度 + + return coordinates; + } catch (Exception e) { + System.err.println("地址解析失败: " + e.getMessage()); + return null; + } + } } \ No newline at end of file