From 728466c4924c04dc162811f34086855c51ec0878 Mon Sep 17 00:00:00 2001
From: xin <1099200748@qq.com>
Date: Fri, 29 Aug 2025 14:30:23 +0800
Subject: [PATCH] Merge branch 'master' into xin
---
oying-system/src/main/java/com/oying/modules/pc/store/service/impl/StoreQueryServiceImpl.java | 47 ++++++++++++++++++++++++++++++++++-------------
1 files changed, 34 insertions(+), 13 deletions(-)
diff --git a/oying-system/src/main/java/com/oying/modules/pc/store/service/impl/StoreQueryServiceImpl.java b/oying-system/src/main/java/com/oying/modules/pc/store/service/impl/StoreQueryServiceImpl.java
index a50b69c..88e77e6 100644
--- a/oying-system/src/main/java/com/oying/modules/pc/store/service/impl/StoreQueryServiceImpl.java
+++ b/oying-system/src/main/java/com/oying/modules/pc/store/service/impl/StoreQueryServiceImpl.java
@@ -1,9 +1,9 @@
package com.oying.modules.pc.store.service.impl;
-import cn.hutool.core.util.NumberUtil;
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.collection.ListUtil;
import com.oying.modules.pc.store.domain.Store;
import com.oying.modules.pc.store.domain.dto.StoreQueryCriteria;
-import com.oying.modules.pc.store.mapper.StoreMapper;
import com.oying.modules.pc.store.service.StoreQueryService;
import com.oying.modules.pc.store.service.StoreService;
import com.oying.utils.PageResult;
@@ -26,7 +26,6 @@
private final RedisTemplate<Object, Object> redisTemplate;
private final StoreService storeService;
- private final StoreMapper storeMapper;
@Override
public PageResult<Store> findPagedStores(StoreQueryCriteria criteria) {
@@ -38,27 +37,49 @@
Long total = redisTemplate.opsForZSet().size(setKey);
if (total == null || total == 0) {
// 初始化缓存
- List<Long> ids = storeMapper.queryStoreIds(criteria);
+ List<Long> ids = storeService.queryStoreIds(criteria);
+ int index = 0;
for (Long id : ids) {
- redisTemplate.opsForZSet().add(setKey, 1, id);
+ redisTemplate.opsForZSet().add(setKey, id, index++);
}
// 设置缓存过期时间
- redisTemplate.expire(setKey, 30, TimeUnit.MINUTES);
+ redisTemplate.expire(setKey, 15, TimeUnit.MINUTES);
total = (long) ids.size();
}
// 2. 从ZSET获取分页ID
- Long offset = NumberUtil.mul(criteria.getPage(), criteria.getSize()).longValue();
- long end = NumberUtil.add(offset, criteria.getSize()).longValue();
- Set<Long> storeIds = Optional.ofNullable(redisTemplate.opsForZSet()
- .range(setKey, offset, end - 1)).orElse(Collections.emptySet())
- .stream().map(i-> (Long) i).collect(Collectors.toSet());
+ int current = (criteria.getPage() == null || criteria.getPage() < 1) ? 1 : criteria.getPage();
+ int offset = (current - 1) * criteria.getSize();
+ int end = offset + criteria.getSize() - 1;
+ Set<Long> storeIds = Optional.ofNullable(redisTemplate.opsForZSet().range(setKey, offset, end))
+ .orElse(Collections.emptySet())
+ .stream()
+ .map(i -> (Long) i)
+ .collect(Collectors.toSet());
- // 3. 获取详情数据
- List<Store> stores = storeService.listByIds(storeIds);
+ // 3. 获取详情数据]
+ List<Store> stores = Optional.of(storeIds)
+ .filter(CollectionUtil::isNotEmpty)
+ .map(storeService::getStoresByIds)
+ .orElse(ListUtil.toList());
+
+ stores.forEach(u -> u.setDeliveryDuration(distanceCalculation(criteria.getLatitude(), criteria.getLongitude(), u.getLatitude(), u.getLongitude())));
page.setContent(stores);
page.setTotalElements(total);
return page;
}
+
+ private Integer distanceCalculation(double lat1, double lon1, double lat2, double lon2) {
+ // 地球半径(千米)
+ final int R = 6371;
+ // 将纬度、经度从度转换为弧度
+ double latDistance = Math.toRadians(lat2 - lat1);
+ double lonDistance = Math.toRadians(lon2 - lon1);
+ double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
+ + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
+ * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
+ double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
+ return new Double(R * c * 1000).intValue();
+ }
}
--
Gitblit v1.9.3