package com.uas.platform.b2b.manage.support;

import com.uas.platform.b2b.manage.model.KeyEntity;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 集合处理工具类
 *
 * @author hejq
 * @date 2018-07-19 11:03
 */
public class CollectionUtil {

    /**
     * 用继承自KeyEntity的类，重写的getKey方法来取值，封装成List一次性查询数据
     *
     * @param paramArray
     * @return
     */
    public static List<Long> getKeyCollection(Collection<? extends KeyEntity> paramArray) {
        List<Long> idList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(paramArray)) {
            for (KeyEntity param : paramArray) {
                Object val = param.getKey();
                if (val != null) {
                    idList.add(Long.valueOf(val.toString()));
                }
            }
        }return idList;
    }

    /**
     * 每次请求物料服务数据最多传输id 200个
     */
    public final static int PRODUCT_MAX_ID_SIZE = 200;

    /**
     * 集合大小拆分
     *
     * @param ary 原集合
     * @param subSize 拆分大小
     * @return 拆分过后的子集合的集合
     */
    public static <T> List<List<T>> splitAry(List<T> ary, int subSize) {
        int count = ary.size() % subSize == 0 ? ary.size() / subSize: ary.size() / subSize + 1;
        List<List<T>> subAryList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int index = i * subSize;
            List<T> list = new ArrayList<>();
            int j = 0;
            while (j < subSize && index < ary.size()) {
                list.add(ary.get(index++));
                j++;
            }
            subAryList.add(list);
        }
        return subAryList;
    }

}
