package cn.com.ty.lift.common.utils;

import cn.com.ty.lift.common.model.StatisticsKVModel;
import cn.hutool.core.util.StrUtil;

import java.io.*;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author huangyuan
 * @date 2019-12-02
 * @description 项目工具类
 */
public class ProjectUtils {

    /**
     * @param
     * @return
     * @description 通过id字符串，获取long型id列表
     * @date 2019/11/27 10:03 AM
     */
    public static List<Long> getLongIdFromIds(String ids) {
        List<Long> longIdList = new ArrayList<>();
        if (StrUtil.isNotBlank(ids)) {
            String[] idArray = ids.split(",");
            for (String id : idArray) {
                longIdList.add(Long.parseLong(id));
            }
        }
        return longIdList;
    }

    /**
     * @param ids id连接的字符串
     * @return long型id数组
     * @description 从id连接数组中获取id数组
     * @date 2020/4/26 1:10 下午
     */
    public static long[] getLongIdArrayFromIds(String ids) {
        long[] idArray = new long[0];
        if (StrUtil.isNotBlank(ids)) {
            String[] idStrArray = ids.split(",");
            idArray = new long[idStrArray.length];
            int i = 0;
            for (String id : idStrArray) {
                idArray[i++] = Long.parseLong(id);
            }
        }
        return idArray;
    }

    /**
     * @param array 数组
     * @return 通过","连接的字符串
     * @description 将数组值通过","连接
     * @date 2020/4/26 11:39 上午
     */
    public static String arrayToStrByComma(long[] array) {
        if (array != null && array.length > 0) {
            StringBuilder sb = new StringBuilder();
            for (Object o : array) {
                sb.append(o.toString()).append(",");
            }
            return sb.toString();
        }
        return null;
    }

    /**
     * 将对象集合转化为attr->List<Obj>的map形式，通过特定属性给对象归类
     *
     * @param objList
     * @param attrName:属性名
     * @param parameterTypes
     * @return Map
     * @throws
     * @author huangy
     * @date 2018年12月19日
     */
    public static Map attrToListMap(List objList, String attrName, Class<?>... parameterTypes) {
        Map map = new HashMap<>();
        try {
            if (objList != null && objList.size() > 0) {
                for (Object o : objList) {
                    Class<?> classObj = o.getClass();
                    //获取属性get方法
                    String methodName = PojoUtils.attrGetMethodName(attrName);
                    Method method = classObj.getMethod(methodName, parameterTypes);
                    Object attr = method.invoke(o, parameterTypes);
                    //在map中获取对象集合
                    List objInfoList = (List) map.get(attr);
                    if (objInfoList == null) {
                        objInfoList = new ArrayList();
                    }
                    objInfoList.add(o);
                    map.put(attr, objInfoList);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 将对象集合转化为attribute->Object的map
     *
     * @param objList
     * @param attrName:属性名称
     * @param parameterTypes
     * @return Map
     * @throws
     * @author huangy
     * @date 2018年12月19日
     */
    public static Map attrToObjMap(List objList, String attrName, Class<?>... parameterTypes) {

        Map<Object, Object> map = new HashMap<>();
        try {
            if (objList != null && objList.size() > 0) {
                for (Object o : objList) {
                    Class<?> classObj = o.getClass();
                    Method method = classObj.getMethod(PojoUtils.attrGetMethodName(attrName), parameterTypes);
                    Object attr = method.invoke(o, parameterTypes);
                    map.put(attr, o);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;

    }

    /**
     * 通过集合对象集合获取对象id集合
     *
     * @param objList
     * @param attrName
     * @param parameterTypes
     * @return List<? extends Object>
     * @throws
     * @author huangy
     */
    public static List getAttrList(List objList, String attrName, Class<?>... parameterTypes) {
        List idList = new ArrayList();
        if (objList != null && objList.size() > 0) {
            for (Object o : objList) {
                try {
                    Class<?> classObj = o.getClass();
                    Method method = classObj.getMethod(PojoUtils.attrGetMethodName(attrName), parameterTypes);
                    Object id = method.invoke(o, parameterTypes);
                    idList.add(id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return idList;
    }

    /**
     * 列表去重
     *
     * @param list void
     * @throws
     */
    public static void removeDuplicateWithOrder(List list) {
        Set set = new HashSet<>();
        List newList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (Iterator<Object> iter = list.iterator(); iter.hasNext(); ) {
                Object element = iter.next();
                if (set.add(element)) {
                    newList.add(element);
                }
            }
            list.clear();
            list.addAll(newList);
        }
    }

    /**
     * @param
     * @return
     * @description 将map值转化为 key -> key值 value -> value值
     * @date 2020/2/24 7:37 下午
     */
    public static List<StatisticsKVModel> transReturnMapToStatisticsKVModel(Map dataMap) {
        List<StatisticsKVModel> statisticsKVModelList = new ArrayList<>();
        if (dataMap != null && dataMap.size() > 0) {
            dataMap.forEach((key, value) -> {
                StatisticsKVModel outStatisticsKVModel = new StatisticsKVModel();
                //设置键
                outStatisticsKVModel.setKey(key);
                //判断值是否为也为map如果是就行封装 目前只涉及一层封装
                if (value != null && value instanceof Map) {
                    outStatisticsKVModel.setValue(transReturnMapToStatisticsKVModel((Map) value));
                } else {
                    outStatisticsKVModel.setValue(value);
                }
                statisticsKVModelList.add(outStatisticsKVModel);
            });
        }
        return statisticsKVModelList;
    }

    /**
     * @param list 要拷贝的list
     * @return 返回拷贝后的对象
     * @description 对集合进行拷贝
     * @date 2020/4/16 5:05 下午
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> deepCopy(List<T> list) {
        List<T> dest = new ArrayList<>();
        try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream()) {
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(list);
            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            dest = (List<T>) in.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return dest;
    }

}
