package com.kingdee.eas.custom.perfweb.utils;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.IAdminOrgUnit;
import org.apache.commons.lang3.StringUtils;

/**
 * 行政组织工具：中心维度解析及下级组织范围判断。
 */
public final class CenterOrgUtil {

    private CenterOrgUtil() {
    }

    /**
     * 判断员工所属组织是否落在「中心组织及其全部下级」范围内（含中心自身）。
     * <p>
     * 优先用行政组织长编码前缀匹配；长编码缺失时回退为向上归集到同一中心。
     *
     * @param ctx       上下文
     * @param empOrg    员工所属行政组织
     * @param centerOrg 中心组织（考核结果说明单据上的 adminOrg）
     */
    public static boolean isUnderCenterOrg(Context ctx, AdminOrgUnitInfo empOrg, AdminOrgUnitInfo centerOrg)
            throws BOSException {
        if (empOrg == null || centerOrg == null || centerOrg.getId() == null) {
            return false;
        }
        if (empOrg.getId() != null && centerOrg.getId().toString().equals(empOrg.getId().toString())) {
            return true;
        }
        AdminOrgUnitInfo center = loadOrg(ctx, centerOrg.getId().toString());
        AdminOrgUnitInfo emp = empOrg.getId() != null ? loadOrg(ctx, empOrg.getId().toString()) : empOrg;
        if (center == null || emp == null) {
            return false;
        }
        String centerLongNumber = center.getLongNumber();
        String empLongNumber = emp.getLongNumber();
        if (StringUtils.isNotBlank(centerLongNumber) && StringUtils.isNotBlank(empLongNumber)) {
            return empLongNumber.equals(centerLongNumber) || empLongNumber.startsWith(centerLongNumber + "!");
        }
        AdminOrgUnitInfo resolvedCenter = resolveCenterOrg(ctx, emp);
        return resolvedCenter != null && resolvedCenter.getId() != null
                && centerOrg.getId().toString().equals(resolvedCenter.getId().toString());
    }

    /**
     * 从任意部门组织向上查找一级业务中心（公司直属下级组织）
     */
    public static AdminOrgUnitInfo resolveCenterOrg(Context ctx, AdminOrgUnitInfo org) throws BOSException {
        if (org == null || org.getId() == null) {
            return null;
        }
        AdminOrgUnitInfo current = loadOrg(ctx, org.getId().toString());
        if (current == null) {
            return org;
        }
        while (current != null) {
            AdminOrgUnitInfo parent = current.getParent() != null && current.getParent().getId() != null ? loadOrg(ctx, current.getParent().getId().toString()) : null;
            if (parent == null) {
                return current;
            }
            AdminOrgUnitInfo grandParent = parent.getParent() != null && parent.getParent().getId() != null ? loadOrg(ctx, parent.getParent().getId().toString()) : null;
            if (grandParent == null) {
                return current;
            }
            current = parent;
        }
        return org;
    }

    public static AdminOrgUnitInfo loadOrg(Context ctx, String orgId) throws BOSException {
        if (StringUtils.isBlank(orgId)) {
            return null;
        }
        IAdminOrgUnit iAdminOrgUnit = AdminOrgUnitFactory.getLocalInstance(ctx);
        return iAdminOrgUnit.getAdminOrgUnitCollection("where id = '" + orgId + "'").get(0);
    }
}
