package com.inkasso.factoring.generator;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

/**
 * @Description TODO
 * @Author Yafan Wang
 * @Date 2023/11/16 17:02
 */
@SpringBootTest
public class test {

//    public static void main(String[] args) {
////        int[] a = new int[]{2, 3, 5, 6, 4, 8, 6, 4, 7, 5, 10};
////        int b = 3;
////        for (int i = 0; i < a.length - 1; i++) {
////            int e = a[i + 1] - a[i + 1];
////            if (b == Math.abs(e)) {
////                System.out.println(a[i] + ":" + a[i + 1]);
////            }
////        }
//    }



    @Test
    public void test1() {
        String s = new String("42600,42601,42602,42603,46236");
        String[] fileIdStr = s.split(",");
        for (int i = 0; i < fileIdStr.length; i++) {
            System.out.println(i);
        }
    }

    @Test
    public void doTest() {
        Integer[] arr = {1, 2, 5, 6, 6, 9, 10, 17, 18, 2, 8, 20, 32};
        List<List<Integer>> lists = minGroup(arr, 30);
        for (List<Integer> list : lists) {
            System.out.println(list);
        }
    }

    public List<List<Integer>> minGroup(Integer[] arr, int k) {
        //获取升序的list
        List<Integer> list = Arrays.asList();
        Collections.sort(list);

        //子list
        List<List<Integer>> subList = new ArrayList<>();//-->List<Map<Integer,List> >
        List<List<Integer>> resultList = new ArrayList<>();//返回结果


        Integer temp = 0;
        List<Integer> tempList = new ArrayList<>();


        for (Integer integer : list) {

            if ((temp + integer) < k) {
                tempList.add(integer);
                temp += integer;
            } else if ((temp + integer) > k) {
                temp = integer;
                subList.add(tempList);
                tempList = new ArrayList<>();
                tempList.add(integer);
            } else {
                tempList.add(integer);
                temp = 0;
                subList.add(tempList);
                tempList = new ArrayList<>();
            }


        }
        subList.add(tempList);//加上最后一个数


        for (int m = 0; m < subList.size(); m++) {
            division(subList, resultList, k, m);
        }

        //处理sublist中为0的元素
        List<List<Integer>> handZeroEleList = handZeroEle(subList);

        return handZeroEleList;


    }

    private static void division(List<List<Integer>> subList, List<List<Integer>> resultList, int k, int m) {

        /*生成D[i]:List的map按D[i]升序(除了第一个分组)*/
        List<Map<Integer, List>> mapList = getSortedList(subList, k, m);

        //遍历第一个分组下的所有元素
        List<Integer> subList1 = subList.get(m);
        Collections.reverse(subList1);

        for (int n = 0; n < subList1.size(); n++) {
            Integer integer = subList1.get(n);
            for (int j = 0; j < mapList.size(); j++) {
                Map<Integer, List> listMap = mapList.get(j);
                Integer diTemp = getFirstKey(listMap);
                List<Integer> listTemp = getFirstValue(listMap);
                if (integer > diTemp) {//不能放进去，则比较下一个分组
                }
            }
        }
        resultList.add(subList1);

    }

    /**
     * 获取除去第一个分组外将所有其它分组按D[i]进行排序
     *
     * @param subList
     * @param k
     * @return
     */
    private static List<Map<Integer, List>> getSortedList(List<List<Integer>> subList, int k, int m) {
        List<Map<Integer, List>> mapList = new ArrayList<>();
        for (int i = m + 1; i < subList.size(); i++) {
            Map<Integer, List> tempMap = new HashMap<>();
            tempMap.put(k - listSum(subList.get(i)), subList.get(i));
            mapList.add(tempMap);
        }
        sortByCustom(mapList);
        return mapList;
    }


    /**
     * 排序
     *
     * @param mapList
     * @return
     */
    private static void sortByCustom(List<Map<Integer, List>> mapList) {
        Collections.sort(mapList, new Comparator<Map<Integer, List>>() {
            @Override
            public int compare(Map<Integer, List> o1, Map<Integer, List> o2) {
                if (getFirstKey(o1) > getFirstKey(o2)) {
                    return 1;
                }
                if (getFirstKey(o1) == getFirstKey(o2)) {
                    return 0;
                }
                return -1;
            }
        });
    }


    /**
     * 求数组和
     *
     * @param arr
     * @return
     */
    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int i : arr) {
            sum += i;
        }
        return sum;
    }

    /**
     * 求list<integer>和
     *
     * @param list
     * @return
     */
    public static Integer listSum(List<Integer> list) {
        Integer sum = 0;
        for (Integer integer : list) {
            sum += integer;
        }
        return sum;
    }


    /**
     * 获取map中第一个key值
     *
     * @param map 数据源
     * @return
     */
    private static Integer getFirstKey(Map<Integer, List> map) {
        Integer obj = null;
        for (Map.Entry<Integer, List> entry : map.entrySet()) {
            obj = entry.getKey();
            if (obj != null) {
                break;
            }
        }
        return obj;
    }


    /**
     * 获取map中第一个数据值
     *
     * @param map 数据源
     * @return
     */
    private static List getFirstValue(Map<Integer, List> map) {
        List obj = null;
        for (Map.Entry<Integer, List> entry : map.entrySet()) {
            obj = entry.getValue();
            if (obj != null) {
                break;
            }
        }
        return obj;
    }


    /**
     * 将[0, 6, 6, 5, 0, 0]
     * [0, 0, 17, 10]
     * [2, 1, 9, 18]中的0去除掉
     *
     * @param subList
     */
    private static List<List<Integer>> handZeroEle(List<List<Integer>> subList) {
        List<List<Integer>> resultList = new ArrayList<>();
        for (List<Integer> integers : subList) {
            List<Integer> list = new ArrayList<>();
            for (Integer integer : integers) {
                if (integer != 0) {
                    list.add(integer);

                }

            }
            if (list.size() > 0) {
                resultList.add(list);

            }


        }
        return resultList;

    }

    public static List<Integer[]> partitionArray(int[] inputArray, int targetSum) {
        List<Integer[]> result = new ArrayList<>();

        int currentSum = 0;
        List<Integer> currentPartition = new ArrayList<>();

        for (int value : inputArray) {
            if (currentSum + value <= targetSum) {
                currentSum += value;
                currentPartition.add(value);
            } else {
                // Start a new partition
                result.add(new Integer[0]);
                currentPartition.clear();
                currentSum = 0;
            }
        }

        // Add the last partition
        if (!currentPartition.isEmpty()) {
            currentPartition.toArray();
            Integer[] a = currentPartition.toArray(new Integer[currentPartition.size()]);
            result.add(a);
        }

        return result;
    }


    public static void main(String[] args) {
//        int[] inputArray = {30, 40, 20, 80, 10, 70, 90, 40};
//        int targetSum = 200;
//
//        List<Integer[]> partitions = partitionArray(inputArray, targetSum);
//
//        for (int i = 0; i < partitions.size(); i++) {
//            System.out.println("Partition " + (i + 1) + ": " + java.util.Arrays.toString(partitions.get(i)));
//        }

        String s = new String("46235,46238,46237");
        String[] fileIdStr = s.split(",");
        for (int i = 0; i < fileIdStr.length; i++) {
            System.out.println(Long.valueOf(fileIdStr[i]));
        }

    }


}


