package com.sundot.airport.common.utils;

import com.sundot.airport.common.core.domain.entity.SysDept;

/**
 * 部门工具类
 *
 * @author sundot
 */
public class DeptUtils {


    /**
     * 获取顶级站点ID - 通用版本
     *
     * @return 站点ID
     */
    public static Long getTopSiteId(SysDept sysDept) {
        if (sysDept == null) {
            return null;
        }

        // 向上查找父级直到站点
        String ancestors = sysDept.getAncestors();
        if (ancestors != null && !ancestors.isEmpty()) {
            String[] ancestorIds = ancestors.split(",");
            if (ancestorIds.length > 0) {
                try {
                    // 如果包含0排除
                    if ("0".equals(ancestorIds[0]) && ancestorIds.length > 1) {
                        return Long.parseLong(ancestorIds[1]);
                    }
                    if (ancestorIds.length == 1 && "0".equals(ancestorIds[0])) {
                        return sysDept.getDeptId();
                    }
                } catch (NumberFormatException e) {
                    return 0L;
                }
            }
        }

        return null;
    }
}