彭雪彬
2025-07-15 a7501803a3ca43310e57a5dd912e5047919c2e43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.oying.modules.pc.store.service.impl;
 
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.service.StoreQueryService;
import com.oying.modules.pc.store.service.StoreService;
import com.oying.utils.PageResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
@Slf4j
@Service
@RequiredArgsConstructor
public class StoreQueryServiceImpl implements StoreQueryService {
 
    private final RedisTemplate<Object, Object> redisTemplate;
    private final StoreService storeService;
 
    @Override
    public PageResult<Store> findPagedStores(StoreQueryCriteria criteria) {
 
        PageResult<Store> page = new PageResult<>();
 
        // 1. 检查ZSET是否存在
        String setKey = criteria.buildConditionCacheKey();
        Long total = redisTemplate.opsForZSet().size(setKey);
        if (total == null || total == 0) {
            // 初始化缓存
            List<Long> ids = storeService.queryStoreIds(criteria);
            int index = 0;
            for (Long id : ids) {
                redisTemplate.opsForZSet().add(setKey, id, index++);
            }
            // 设置缓存过期时间
            redisTemplate.expire(setKey, 15, TimeUnit.MINUTES);
            total = (long) ids.size();
        }
 
        // 2. 从ZSET获取分页ID
        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 = 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();
    }
}