export function patchZero(origin,num) {
    let originSize = String(origin)
    for(var i = 0;i<num-originSize.length;i++){
      origin = "0"+String(origin)
    }
    return origin
}

export function sliceStr(str,num=100) {
    if(str.length>100){
      str = str.substr(0,num) +"...";
    }
    return str;
}
//输入编号区间批量生成编号
export function expandNumberRangesSafe(input) {
    if (!input || String(input).trim() === '') {
        return '';
    }

    // 确保输入是字符串
    const inputStr = String(input);
    const allNumbers = [];
    const ranges = inputStr.split(',');

    for (const range of ranges) {
        const trimmedRange = String(range).trim();
        if (!trimmedRange) continue;

        if (trimmedRange.includes('-')) {
            const parts = trimmedRange.split('-');
            if (parts.length === 2) {
                try {
                    const prefix = String(parts[0]).trim();
                    const endSuffix = String(parts[1]).trim();

                    if (!prefix || !endSuffix) {
                        throw new Error('无效的范围格式');
                    }

                    // 确保都是数字后缀
                    if (!/^\d+$/.test(endSuffix)) {
                        throw new Error('结束后缀必须为数字');
                    }

                    const suffixLength = endSuffix.length;

                    // 检查前缀长度是否足够
                    if (prefix.length <= suffixLength) {
                        throw new Error('前缀长度不足');
                    }

                    // 获取基础前缀（去掉最后suffixLength位）
                    const base = prefix.substring(0, prefix.length - suffixLength);
                    // 获取起始后缀（最后suffixLength位）
                    const startSuffix = prefix.substring(prefix.length - suffixLength);

                    // 确保起始后缀也是数字
                    if (!/^\d+$/.test(startSuffix)) {
                        throw new Error('起始后缀必须为数字');
                    }

                    const start = parseInt(startSuffix, 10);
                    const end = parseInt(endSuffix, 10);

                    // 添加安全检查，防止范围过大
                    if (end - start > 1000) {
                        throw new Error('耳标号区间范围过大');
                    }

                    if (isNaN(start) || isNaN(end) || start < 0 || end < 0) {
                        throw new Error('无效的数字格式');
                    }

                    // 确保起始值不大于结束值
                    if (start > end) {
                        throw new Error('起始值不能大于结束值');
                    }

                    // 展开数字范围
                    for (let i = start; i <= end; i++) {
                        // 保持后缀位数一致，不足补零
                        const formattedSuffix = i.toString().padStart(suffixLength, '0');
                        allNumbers.push(base + formattedSuffix);
                    }
                } catch (e) {
                    throw new Error('无效的数字格式: ' + trimmedRange + ' - ' + e.message);
                }
            } else {
                throw new Error('无效的范围格式: ' + trimmedRange);
            }
        } else {
            allNumbers.push(trimmedRange);
        }
    }

    return allNumbers.join(',');
}
