package com.ssyf.plugs.pedigree;

import cn.hutool.core.util.StrUtil;
import com.ssyf.common.core.constants.SystemConstants;
import com.ssyf.common.core.util.ConvertUtil;

/**
 * 数据路径帮助类
 * @author huwei
 * @date 2022/11/14 16:23
 */
public class DataPathHelper {

    /**
     * 创建路径
     * @param parentPath 父级路径
     * @param childCount 孩子个数
     * @return
     */
    public static String createPath(String parentPath ,int childCount){
        StringBuilder stringBuilder = new StringBuilder();
        if(StrUtil.isNotBlank(parentPath)){
            stringBuilder.append(parentPath);
        }
        String current = ConvertUtil.toSystem62(childCount);
        stringBuilder.append(current).append(SystemConstants.DATA_SEPARATOR);
        return stringBuilder.toString();
    }

    /**
     * 解析路径最后节点的数量
     * @param path
     * @return
     */
    public static int parseLastNodeNumber(String path) {
        if(StrUtil.isBlank(path)){
            return 0;
        }
        path = path.endsWith(SystemConstants.DATA_SEPARATOR) ? path.substring(0,path.length() - 1) : path;
        path = path.lastIndexOf(SystemConstants.DATA_SEPARATOR) != -1 ? path.substring(path.lastIndexOf(SystemConstants.DATA_SEPARATOR) + 1) : path;
        return ConvertUtil.system62ToInt(path);
    }
}
