package com.zhiqiyun.open.core.service.impl;

import com.zhiqiyun.open.core.service.Ip2RegionService;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbSearcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
public class Ip2RegionServiceImpl implements Ip2RegionService {

    @Autowired
    private DbSearcher dbSearcher;

    @Override
    public String getRegion(String ipAddress) {
        String ipRegion = "未知地址";
        try {
            DataBlock dataBlock = dbSearcher.memorySearch(ipAddress);
            ipRegion = dataBlock.getRegion();
        } catch (IOException e) {
            log.warn("get IP Address", e);
        }
        return ipRegion;
    }
}
