package com.eitc.common.context;

import com.eitc.common.utils.HttpContextUtils;
import com.eitc.modules.security.user.UserDetail;
import com.eitc.modules.sys.enums.SuperAdminEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * 租户
 *
 * @author Mark eitc@163.com
 */
public class TenantContext {

    public static Long getTenantCode(UserDetail user){
        if(user.getTenantCode() == null){
            return null;
        }

        String tenantCode = HttpContextUtils.getTenantCode();
        //超级管理员，才可以切换租户
        if(user.getSuperAdmin() == SuperAdminEnum.YES.value()){
            if(StringUtils.isNotBlank(tenantCode)){
                return Long.parseLong(tenantCode);
            }
        }
        return user.getTenantCode();
    }
}
