增加通过店铺id 以及 经纬计算与店铺的距离 返回店铺详情

This commit is contained in:
cb 2025-08-23 16:04:57 +08:00
parent 1a5b094388
commit 0a37c352fb
7 changed files with 238 additions and 6 deletions

View File

@ -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<String, Object> 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<String, Object> 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<String, Object> 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()) {

View File

@ -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());

View File

@ -256,4 +256,6 @@ public class OrderListResponse {
private Date confirmStartTime;
private Integer deliveryType;
private Long goodsId;
}

View File

@ -36,6 +36,11 @@
<version>3.5.9</version>
<scope>compile</scope>
</dependency>
<!-- 添加对ghy-common模块的依赖 -->
<dependency>
<groupId>com.ghy</groupId>
<artifactId>ghy-common</artifactId>
</dependency>
<!-- 其他依赖可根据需要添加 -->
</dependencies>

View File

@ -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;
}
}

View File

@ -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<Shop> getShopsByWorkerId(Long workerId);
/**
* 通过店铺ID查询详细信息并计算距离
* @param query 距离查询请求实体
* @return 包含距离信息的店铺详情
*/
Shop getShopWithDistance(ShopDistanceQuery query);
}

View File

@ -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<Shop> 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<String, Double> 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<String, Double> 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<String, Double> coordinates = new HashMap<>();
// coordinates.put("longitude", lng);
// coordinates.put("latitude", lat);
// return coordinates;
// }
// 临时返回示例坐标实际使用时请替换为真实的API调用
Map<String, Double> 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;
}
}
}