package cc.smtweb.framework.core.util;

import cc.smtweb.framework.core.db.impl.BaseBean;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.util.kryo.KryoTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Akmm at 2022/1/5 9:10
 * 通用工具类
 */
@Slf4j
public class CommUtil {
    /**
     * 找指定类的泛型类
     *
     * @param cls
     * @param <T>
     * @return
     */
    public static <T> Class<T> getParameterizedType(Class cls) {
        while (cls != null) { //找到泛型类
            Type pt = cls.getGenericSuperclass();
            if (pt instanceof ParameterizedType) {
                return (Class<T>) ((ParameterizedType) pt).getActualTypeArguments()[0];
            }
            cls = (Class) pt;
        }
        return null;
    }

    /**
     * 序列化对象
     *
     * @param obj
     * @param <T>
     * @return
     */
    public static <T extends Serializable> byte[] writeObject(T obj) {
        return KryoTool.getINST().writeObject(obj);
    }

    /**
     * 反序列化
     *
     * @param data
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T extends Serializable> T readObject(byte[] data, Class<T> clazz) {
        return KryoTool.getINST().readObject(data, clazz);
    }

    public static <T extends Serializable> T cloneObj(T obj, Class<T> cls) {
        byte[] data = writeObject(obj);
        return readObject(data, cls);
    }

    public static boolean isEmpty(Collection c) {
        return c == null || c.isEmpty();
    }

    public static boolean isEmpty(Object[] c) {
        return c == null || c.length == 0;
    }

    /**
     * 从DefaultEntity实体转成自己的对象类
     *
     * @param bean
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T extends BaseBean> T castEntity(DefaultEntity bean, Class<T> clazz) {
        try {
            T ret = clazz.newInstance();
            ret.getData().putAll(bean.getData());
            return ret;
        } catch (Exception e) {
            log.error("转换bean失败：", e);
            return null;
        }
    }

    /*public static String getSqlInIds(Collection<String> ids) {
        if (null == ids || ids.isEmpty()) return "";
        StringBuilder s = new StringBuilder(128);
        Set<String> set = new HashSet<>();
        for (String id : ids) {
            if (set.contains(id)) continue;
            set.add(id);
            if (StringUtils.isEmpty(id)) continue;
            s.append(id).append(",");
        }
        if (s.length() == 0) return "";
        return s.substring(0, s.length() - 1);
    }*/

    public static String getSqlInIds(Collection<Long> ids) {
        if (null == ids || ids.isEmpty()) return "";
        StringBuilder s = new StringBuilder(128);
        Set<Long> set = new HashSet<>();
        for (Long id : ids) {
            if (id <= 0L) continue;
            if (set.contains(id)) continue;
            set.add(id);
            s.append(id).append(",");
        }
        if (s.length() == 0) return "";
        return s.substring(0, s.length() - 1);
    }

    public static String getSqlInStr(Collection<String> ids) {
        if (null == ids || ids.isEmpty()) return "";
        StringBuilder s = new StringBuilder(128);
        Set<String> set = new HashSet<>();
        for (String id : ids) {
            if (StringUtils.isEmpty(id)) continue;
            if (set.contains(id)) continue;
            set.add(id);
            s.append("'").append(id).append("',");
        }
        if (s.length() == 0) return "";
        return s.substring(0, s.length() - 1);
    }

    public static String getSqlInStr(String[] ids) {
        if (null == ids || ids.length == 0) return "";
        StringBuilder s = new StringBuilder(128);
        Set<String> set = new HashSet<>();
        for (String id : ids) {
            if (StringUtils.isEmpty(id)) continue;
            if (set.contains(id)) continue;
            set.add(id);
            s.append("'").append(id).append("',");
        }
        if (s.length() == 0) return "";
        return s.substring(0, s.length() - 1);
    }

    public static String getOrigMsg(Throwable e) {
        String s = e.getMessage();
        Throwable t = e.getCause();
        while (t != null) {
            s = t.getMessage();
            t = t.getCause();
        }
        return s;
    }


    public static boolean isLinux() {
        return System.getProperty("os.name").toLowerCase().contains("linux");
    }

    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().contains("windows");
    }
}
