增加通过商品id 去获取对应的服务店铺列表

This commit is contained in:
cb 2025-08-21 17:17:30 +08:00
parent 91cb050b47
commit 8c859e1866
1 changed files with 152 additions and 0 deletions

View File

@ -647,4 +647,156 @@ public class GoodsController extends BaseController {
return AjaxResult.error(ExceptionUtil.getExceptionMessage(e)); return AjaxResult.error(ExceptionUtil.getExceptionMessage(e));
} }
} }
/**
* 通过配件商品ID获取对应服务类目下的所有店铺信息
*
* @return 店铺信息列表包含距离信息
*/
@PostMapping("/getShopsByGoodsId")
@ResponseBody
public AjaxResult getShopsByGoodsId(@RequestBody Map<String, Object> params) {
try {
Long goodsId = Long.valueOf(params.get("goodsId").toString());
final Double latitude = params.get("latitude") != null ? Double.valueOf(params.get("latitude").toString()) : null;
final Double longitude = params.get("longitude") != null ? Double.valueOf(params.get("longitude").toString()) : null;
// 1. 通过配件商品ID获取商品信息
Goods goods = goodsService.selectById(goodsId);
if (goods == null) {
return AjaxResult.error("商品不存在");
}
// 2. 获取商品对应的服务类目ID
Long deptGoodsCategoryId = goods.getDeptGoodsCategoryId();
if (deptGoodsCategoryId == null) {
return AjaxResult.error("商品未关联服务类目");
}
// 3. 通过类目ID获取当前类目下的所有店铺信息
// 先获取类目信息找到所有使用该类目的商品
DeptGoodsCategory deptGoodsCategory = deptGoodsCategoryService.get(deptGoodsCategoryId);
if (deptGoodsCategory == null) {
return AjaxResult.error("服务类目不存在");
}
// 查询所有使用该服务类目的商品
Goods queryGoods = new Goods();
queryGoods.setDeptGoodsCategoryId(deptGoodsCategoryId);
queryGoods.setStatus(0); // 只查询上架的商品
List<Goods> goodsList = goodsService.selectGoodsList(queryGoods);
// 提取所有店铺ID去重
Set<Long> shopIds = goodsList.stream()
.filter(g -> g.getShopId() != null)
.map(Goods::getShopId)
.collect(Collectors.toSet());
if (shopIds.isEmpty()) {
return AjaxResult.success("该服务类目下暂无店铺信息", new ArrayList<>());
}
// 4. 获取所有店铺信息
List<Shop> shops = new ArrayList<>();
for (Long shopId : shopIds) {
Shop shop = shopService.getShop(shopId);
if (shop != null) {
// 5. 计算距离
if (LocationUtils.isValidCoordinate(latitude, longitude) &&
LocationUtils.isValidCoordinate(shop.getLatitude(), shop.getLongitude())) {
try {
double distanceInMeters = LocationUtils.getDistanceInMeters(
latitude, longitude,
shop.getLatitude(), shop.getLongitude()
);
// 将距离信息添加到店铺对象中可以创建一个新的DTO或者使用Map
Map<String, Object> shopWithDistance = new HashMap<>();
shopWithDistance.put("shop", shop);
shopWithDistance.put("distance", LocationUtils.formatDistance(distanceInMeters));
shopWithDistance.put("distanceInMeters", distanceInMeters);
shops.add(shop);
} catch (Exception e) {
logger.warn("计算店铺[{}]距离失败: {}", shop.getShopName(), e.getMessage());
shops.add(shop);
}
} else {
shops.add(shop);
}
}
}
// 6. 按距离排序如果提供了位置信息
if (LocationUtils.isValidCoordinate(latitude, longitude)) {
shops.sort((shop1, shop2) -> {
try {
if (LocationUtils.isValidCoordinate(shop1.getLatitude(), shop1.getLongitude()) &&
LocationUtils.isValidCoordinate(shop2.getLatitude(), shop2.getLongitude())) {
double dist1 = LocationUtils.getDistanceInMeters(
latitude, longitude,
shop1.getLatitude(), shop1.getLongitude()
);
double dist2 = LocationUtils.getDistanceInMeters(
latitude, longitude,
shop2.getLatitude(), shop2.getLongitude()
);
return Double.compare(dist1, dist2);
}
} catch (Exception e) {
logger.warn("排序时计算距离失败: {}", e.getMessage());
}
return 0;
});
}
// 7. 构建返回结果包含距离信息
List<Map<String, Object>> result = new ArrayList<>();
for (Shop shop : shops) {
Map<String, Object> shopInfo = new HashMap<>();
shopInfo.put("shopId", shop.getShopId());
shopInfo.put("shopName", shop.getShopName());
shopInfo.put("imageUrl", shop.getImageUrl());
shopInfo.put("workerId", shop.getWorkerId());
shopInfo.put("provinceId", shop.getProvinceId());
shopInfo.put("provinceName", shop.getProvinceName());
shopInfo.put("cityId", shop.getCityId());
shopInfo.put("cityName", shop.getCityName());
shopInfo.put("countryId", shop.getCountryId());
shopInfo.put("countryName", shop.getCountryName());
shopInfo.put("streetId", shop.getStreetId());
shopInfo.put("streetName", shop.getStreetName());
shopInfo.put("address", shop.getAddress());
shopInfo.put("phone", shop.getPhone());
shopInfo.put("latitude", shop.getLatitude());
shopInfo.put("longitude", shop.getLongitude());
// 计算并添加距离信息
if (LocationUtils.isValidCoordinate(latitude, longitude) &&
LocationUtils.isValidCoordinate(shop.getLatitude(), shop.getLongitude())) {
try {
double distanceInMeters = LocationUtils.getDistanceInMeters(
latitude, longitude,
shop.getLatitude(), shop.getLongitude()
);
shopInfo.put("distance", LocationUtils.formatDistance(distanceInMeters));
shopInfo.put("distanceInMeters", distanceInMeters);
} catch (Exception e) {
shopInfo.put("distance", null);
shopInfo.put("distanceInMeters", null);
}
} else {
shopInfo.put("distance", null);
shopInfo.put("distanceInMeters", null);
}
result.add(shopInfo);
}
return AjaxResult.success("获取成功", result);
} catch (Exception e) {
logger.error("获取店铺信息失败: {}", e.getMessage(), e);
return AjaxResult.error("获取店铺信息失败: " + e.getMessage());
}
}
} }