

package com.weather.modules.security.user;

import com.weather.modules.sys.enums.SuperAdminEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用户
 *
 * @author 123
 */
public class SecurityUser {

    private static final Logger log = LoggerFactory.getLogger(SecurityUser.class);

    public static Subject getSubject() {
        try {
            return SecurityUtils.getSubject();
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 获取用户信息
     */
    public static UserDetail getUser() {
        Subject subject = getSubject();
        if (subject != null) {
            UserDetail user = (UserDetail) subject.getPrincipal();
            if (user != null) {
                return user;
            }
        }
        UserDetail fallback = UserContextHolder.get();
        if (fallback != null) {
            return fallback;
        }
        log.warn("无法获取当前用户信息，Subject和ThreadLocal均为空，返回空UserDetail");
        UserDetail empty = new UserDetail();
        empty.setSuperAdmin(SuperAdminEnum.NO.value());
        return empty;
    }

    /**
     * 获取用户ID
     */
    public static Long getUserId() {
        return getUser().getId();
    }

    /**
     * 获取部门ID
     */
    public static Long getDeptId() {
        return getUser().getDeptId();
    }
}