package com.mortals.xhx.utils;

import com.mortals.framework.util.DateUtils;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.util.ObjectUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

/**
 * @author karlhoo
 */
@CommonsLog
public class CommonUtils {
    public static Collection<Collection<Long>> groupOrderIds(Collection<Long> rawIds) {
        if (ObjectUtils.isEmpty(rawIds)) {
            return new ArrayList<>();
        }
        Map<String, Collection<Long>> groupedIds = new HashMap<>(16);
        for (Long id : rawIds) {
            String time = getTimeById(id, DateUtils.P_yyyyMM);
            if(groupedIds.containsKey(time)) {
                groupedIds.get(time).add(id);
            } else {
                groupedIds.put(time, new ArrayList<Long>(){{add(id);}});
            }
        }
        return groupedIds.values();
    }

    private static synchronized String getTimeById(long id, String dateFormat) {
        return DateUtils.convertTime2Str((id >>> 22L) + 1483200000000L, dateFormat);
    }

    public static String getRelativePath(String url)  {

        URL url1 = null;
        try {
            url1 = new URL(url);
            String path = url1.getPath();
            url = path.substring(1);
        } catch (MalformedURLException e) {
            log.error("获取路径异常",e);
        }

      /*  System.out.println(path);
        Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");
        Matcher m = p.matcher(url);
        if(m.find()){
            String group = m.group();
            System.out.println(group);
            if (url.startsWith("http://")) {
                url=url.replaceAll("http://","");
                int i = url.indexOf("/");
                url.substring(i,url.length());
                System.out.println(url);

               // url = url.substring(8+group.length());
            } else if (url.startsWith("https://")) {
                url = url.substring(9+group.length());
            } else if (url.startsWith("//")) {
                url = url.substring(3+group.length());
            }
        }*/
        return url;
    }

    public static void main(String[] args) throws MalformedURLException {
        String url="https://iot-test1.oss-cn-shanghai.aliyuncs.com/oss/2019/12/09/dest/4f2cff5e50564b7faf282204d97c1aca.pdf?12";

        System.out.println(CommonUtils.getRelativePath(url));
    }

}
