package shop.alien.storeplatform.util;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 附近商家地理位置工具类
 *
 * @author ssk
 * @version 1.0.0
 * @since 2025-01-xx
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class NearMeUtil {

    private final StringRedisTemplate stringRedisTemplate;

    /**
     * Redis GEO 存储key - 店铺ID维度
     */
    private static final String GEO_STORE_PRIMARY = "geo:store:primary";

    /**
     * 添加地理位置信息
     *
     * @param point   经纬度坐标
     * @param content 店铺ID
     * @return 添加成功的数量
     */
    public Long addGeolocation(Point point, String content) {
        try {
            if (point != null && content != null) {
                return stringRedisTemplate.opsForGeo().add(GEO_STORE_PRIMARY, point, content);
            }
            return 0L;
        } catch (Exception e) {
            log.error("NearMeUtil.addGeolocation ERROR: point={}, content={}, error={}", 
                    point, content, e.getMessage());
            return 0L;
        }
    }

    /**
     * 删除地理位置信息
     *
     * @param content 店铺ID
     * @return 删除成功的数量
     */
    public Long removeGeolocation(String content) {
        try {
            return stringRedisTemplate.opsForGeo().remove(GEO_STORE_PRIMARY, content);
        } catch (Exception e) {
            log.error("NearMeUtil.removeGeolocation ERROR: content={}, error={}", 
                    content, e.getMessage());
            return 0L;
        }
    }
}

