package com.sleep.demo;

import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class MyThread {
    private static final int THREAD_SIZE = Integer.parseInt(System.getProperty("getRealPriceInfo.THREAD_SIZE", "20"));
    private static final long DEFAULT_TIMEOUT = Integer.parseInt(System.getProperty("getRealPriceInfo.defaultTimeout", "10000"));

    private static final ThreadPoolExecutor REAL_PRICE_EXECUTOR = new ThreadPoolExecutor(
            THREAD_SIZE, THREAD_SIZE, 30, TimeUnit.MINUTES, new LinkedBlockingDeque<>(), new ThreadFactory() {
        private final AtomicInteger count = new AtomicInteger(0);

        @Override
        public Thread newThread( Runnable r) {
            Thread thread = new Thread(r);
            thread.setDaemon(true);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.setName("getRealPriceInfo-" + count.incrementAndGet());
            return thread;
        }
    }, new ThreadPoolExecutor.CallerRunsPolicy());
    public static <T> List<List<T>> splitListBycapacity(List<T> source, int capacity) {
        List<List<T>> result = new ArrayList<List<T>>();
        if (source != null) {
            int size = source.size();
            if (size > 0) {
                for (int i = 0; i < size; ) {
                    List<T> value = null;
                    int end = i + capacity;
                    if (end > size) {
                        end = size;
                    }
                    value = source.subList(i, end);
                    i = end;
                    result.add(value);
                }

            } else {
                result = null;
            }
        } else {
            result = null;
        }

        return result;
    }
    public static void main(String[] args) {
        String skuIds = "1,2,3,4,5,6,7,8,9,10";
        String address="ddd";
        Map<String, Map<String, String>> returnMap = new HashMap<>();


        List<Long> skuList = Arrays.asList(skuIds.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        List<List<Long>> lists = splitListBycapacity(skuList, 3);
        List<String> splitGroup = new ArrayList<>();
        for (List<Long> list : lists) {
            String group = StringUtils.join(list, ",");
            splitGroup.add(group);
        }

        List<CompletableFuture<Map<String, Map<String, String>>>> futures = splitGroup.stream()
                .map(skuIdString -> CompletableFuture.supplyAsync(() -> {
                    try {
                        return queryRealPriceInfo3333(skuIdString, address);
                    } catch (Exception e) {

                        return null;
                    }
                }, REAL_PRICE_EXECUTOR))
                .collect(Collectors.toList());

        try {
            CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
                    .get(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        } catch (Exception e) {
            throw new RuntimeException("thread pool execute timeout", e);
        }

        for (CompletableFuture<Map<String, Map<String, String>>> future : futures) {
            Map<String, Map<String, String>> now = future.getNow(null);

            returnMap.putAll(now);

        }
        System.out.println(returnMap);
    }

    private static Map<String, Map<String, String>> queryRealPriceInfo3333(String skuIds, String address) {
        List<Long> skuList = Arrays.asList(skuIds.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());


        Map<String, Map<String, String>> returnMap = new HashMap<>();



        for (Long sku : skuList) {
            Map<String, String> chiled1 = new HashMap<>();
            chiled1.put("a1","a1");
            chiled1.put("b1","b1");
            chiled1.put("c1","c1");
            returnMap.put(sku+"",chiled1);
        }

        System.out.println(returnMap);
        return  returnMap;
    }

}