package com.ningdatech.pmapi.common.util;

import cn.hutool.core.util.StrUtil;
import com.ningdatech.basic.util.StrPool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.NumberUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * <p>
 * BizUtils
 * </p>
 *
 * @author WendyYang
 * @since 17:32 2023/1/29
 */
public class BizUtils {

    private BizUtils() {

    }

    public static <T extends Number> List<T> splitToNum(String str, Class<T> aClass) {
        if (StrUtil.isEmpty(str)) {
            return Collections.emptyList();
        }
        return Arrays.stream(str.split(StrPool.COMMA))
                .map(w -> NumberUtils.parseNumber(w, aClass))
                .collect(Collectors.toList());
    }

    public static List<Long> splitToLong(String str) {
        return splitToNum(str, Long.class);
    }


    public static <T> void notNull(T obj, Consumer<T> consumer) {
        if (obj != null) {
            consumer.accept(obj);
        }
    }

    public static void notBlank(String str,Consumer<String> consumer) {
        if (StrUtil.isNotBlank(str)) {
            consumer.accept(str);
        }
    }

    public static boolean getJSONType(String str) {
        boolean result = false;
        if (StringUtils.isNotBlank(str)) {
            str = str.trim();
            if (str.startsWith("{") && str.endsWith("}")) {
                result = true;
            } else if (str.startsWith("[") && str.endsWith("]")) {
                result = true;
            }
        }
        return result;
    }
}
