package com.sw.appcloudv1.service;

import com.sw.appcloudv1.Utils.ShortUrlUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by shiwn on 2022/2/24 10:11
 */
@Service
public class ShortUrlServiceImpl implements ShortUrlService {

    private static Map map = new HashMap();

    @Value("${app.url}")
    private String appUrl;

    @Override
    public String getLongUrl(String shortUrlKey) {
        return map.get(shortUrlKey).toString();
    }

    /**
     * @Description: 长链接转短链接保存到map中
     * @Return:
     */
    @PostConstruct
    public void createShortUrl() {
        String[] urls = appUrl.split(",");
        for (String longUrl : urls) {
            String[] shortUrls = ShortUrlUtils.shortUrl(longUrl);
            for (String shortUrl : shortUrls) {
                map.put(shortUrl, longUrl);
            }
        }
        System.out.println(map);
    }
}
