package com.apobates.forum.thrones.admin.controller.helper;

import com.apobates.forum.utils.Commons;
import com.apobates.forum.utils.DateTimeUtils;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * 工具类
 * @author xiaofanku
 * @since 20200603
 */
public final class AdminUtils {
    /**
     * 填充数据源中时间的空值,主要用于连续日期的统计
     *
     * @param start 开始日期
     * @param finish 结束日期
     * @param rawdata 数据源,要求Key=YYYY-MM-DD
     * @return
     */
    public static TreeMap<String, Long> fillEmptyResult(LocalDateTime start, LocalDateTime finish, TreeMap<String, Long> rawdata) {
        TreeMap<String, Long> data = new TreeMap<>();
        LocalDateTime loopDate = start;
        do {
            String loopKey = DateTimeUtils.getYMDByLine(loopDate.toLocalDate());
            try {
                Long size = rawdata.get(loopKey);
                if (null==size) {
                    size = 0L;
                }
                data.put(loopKey, size);
            } catch (NullPointerException e) {
                data.put(loopKey, 0L);
            }
            loopDate = DateTimeUtils.addDayForDate(loopDate, 1);
        } while (!DateTimeUtils.isSameDay(loopDate, finish));
        return data;
    }
    /**
     * 将逗号分隔的数字字符串转成Integer列表
     *
     * @param idArrayString
     * @return
     */
    public static List<Integer> toIntegerList(String idArrayString) {
        if (!Commons.isNotBlank(idArrayString)) {
            return Collections.emptyList();
        }
        return Arrays.asList(idArrayString.split(",")).stream().filter(s -> s.trim().length() > 0 && NumberUtils.isNumber(s.trim())).map(s -> Integer.parseInt(s.trim())).distinct().collect(Collectors.toList());
    }
    /**
     * 将数字字符串数组转成Integer列表
     *
     * @param idArray
     * @return
     */
    public static List<Integer> toIntegerList(String[] idArray) {
        return Arrays.asList(idArray).stream().filter(s -> s.trim().length() > 0 && NumberUtils.isNumber(s.trim())).map(s -> Integer.parseInt(s.trim())).distinct().collect(Collectors.toList());
    }
    public static Set<Integer> toIntegerSet(String idArrayString) {
        if (!Commons.isNotBlank(idArrayString)) {
            return Collections.emptySet();
        }
        return Arrays.asList(idArrayString.split(",")).stream().filter(s -> s.trim().length() > 0 && NumberUtils.isNumber(s.trim())).map(s -> Integer.parseInt(s.trim())).distinct().collect(Collectors.toSet());
    }
    /**
     * 字符数组是否是空的,只要参数等于null或数组长度等于0返回false
     *
     * @param strArray
     * @return
     */
    public static boolean isNotEmpty(String[] strArray) {
        return ArrayUtils.isNotEmpty(strArray);
    }
    /**
     * 判断参数是否是字符和数字组成
     * @param str
     * @return 是返回true,反之返回false
     */
    public static boolean isAlphaNumeric(String str){
        return null != str && str.matches("^[a-zA-Z0-9]*$");
    }
}