
export const utilTools = {

    /**
     * 打乱数组元素
     * @param arr 
     */
    randomArr(arr: any): any {
        arr = JSON.parse(JSON.stringify(arr));
        let tmpArr = [];
        while (arr.length > 0) {
            let index = Math.floor(Math.random() * arr.length);
            tmpArr.push(arr[index]);
            arr.splice(index, 1);
        }
        return tmpArr;
    },

    /**
     * 返回给定范围内的随机数值
     * @param min 最小值
     * @param max 最大值
     * @returns {number}
     */
    getFloatValue(min: number, max: number) {
        var dv = max - min;
        if (dv < 0) {
            return 0;
        }
        var val = min + Math.random() * dv;
        return val;
    },

    // 跨天判断
    overDay(date: any, now?: any) {
        if (!date) {
            return true;
        }

        if (!now) {
            now = new Date();
        }
        let overDay = false;
        if (date.getDate() == now.getDate()) {
            if (now - date >= 24 * 60 * 60 * 1000) {
                overDay = true;
            }
        } else {
            overDay = true;
        }

        return overDay;
    },

    /**
     * 根据权重从数组中返回指定的个数元素
     * @param arr 数组
     * @param num 返回个数
     * @param canRepeat 是否可以重复取，默认不可以
     * @returns {Array} 返回结果数组
     */
    getRowsByWeightAndNum(arr: any, num: number, canRepeat?: boolean) {
        if (!num) {
            num = 1;
        }
        var ret = [];
        arr = utilTools.copyObj(arr);
        for (var i = 0; i < num; i++) {
            if (arr.length <= 0) {
                break;
            }
            var row = this.getRowByWeight(arr);
            ret.push(row);
            if (!canRepeat) {
                var index = arr.indexOf(row);
                arr.splice(index, 1);
            }
        }
        return ret;
    },

    /**
     * 根据权重从数组中返回一个元素
     * @param arr
     * @param totalWeight
     */
    getRowByWeight(arr: any, totalWeight?: number) {
        if (!totalWeight) {
            totalWeight = 0;
            for (var i in arr) {
                var weight = arr[i].weight;
                if (!weight) {
                    weight = 0;
                }
                totalWeight += weight;
            }
        }
        var random = Math.random() * totalWeight;
        var left = 0;
        var row = null;
        for (var i in arr) {
            var tmpRow = arr[i];
            var weight = tmpRow.weight;
            if (!weight) {
                weight = 0;
            }
            if (random >= left && random < left + weight) {
                row = tmpRow;
                break;
            }
            left += weight;
        }

        return row;
    },

    /**
     * 从给定的数组中，随机取出一个元素
     * @param arr 数组
     * @return {null|*} 返回随机到的元素
     */
    getRandomItemByArr(arr: any) {
        if (!arr || arr.length <= 0) {
            return null;
        }
        let index = Math.floor(Math.random() * arr.length);

        return arr[index];
    },

    /**
     * 深度拷贝对象
     * @param obj 要拷贝的对象内容
     */
    copyObj(obj: object) {
        return JSON.parse(JSON.stringify(obj));
    },

}

