| | |
| | | package com.oying.modules.pc.store.service.impl; |
| | | |
| | | import cn.hutool.core.collection.ListUtil; |
| | | import cn.hutool.core.util.NumberUtil; |
| | | import com.oying.modules.pc.store.domain.Store; |
| | | import com.oying.modules.pc.store.domain.dto.StoreCustomerDetailDto; |
| | | import com.oying.modules.pc.store.domain.dto.StoreCustomerQueryCriteria; |
| | | 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; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | 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; |
| | | private final StoreMapper storeMapper; |
| | | |
| | | @Override |
| | | public StoreCustomerDetailDto getCustomerStoreDetail(StoreCustomerQueryCriteria criteria) { |
| | | Store store = storeService.getById(criteria.getStoreId()); |
| | | StoreCustomerDetailDto storeDto = new StoreCustomerDetailDto(); |
| | | BeanUtils.copyProperties(store, storeDto); |
| | | storeDto.setName(store.getStoreName()); |
| | | storeDto.setLogoUrl(""); |
| | | storeDto.setBusinessHours(""); |
| | | storeDto.setDeliveryDuration(0); |
| | | storeDto.setMonthlySales(0); |
| | | storeDto.setScore(0); |
| | | return storeDto; |
| | | 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 = storeMapper.queryStoreIds(criteria); |
| | | for (Long id : ids) { |
| | | redisTemplate.opsForZSet().add(setKey, id, 1); |
| | | } |
| | | // 设置缓存过期时间 |
| | | redisTemplate.expire(setKey, 30, 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.ofNullable(storeService.listByIds(storeIds)).orElse(ListUtil.toList()); |
| | | |
| | | page.setContent(stores); |
| | | page.setTotalElements(total); |
| | | return page; |
| | | } |
| | | } |