package com.it.rattan.monisuo.context;

import lombok.Getter;
import lombok.Setter;

/**
 * 用户上下文信息
 */
@Getter
@Setter
public class UserContext {

    /** 用户ID */
    private Long userId;
    /** 用户名 */
    private String username;
    /** 类型: user/admin */
    private String type;

    private static final ThreadLocal<UserContext> CONTEXT = new ThreadLocal<>();

    public static void set(UserContext context) {
        CONTEXT.set(context);
    }

    public static UserContext get() {
        return CONTEXT.get();
    }

    public static void clear() {
        CONTEXT.remove();
    }

    public static Long getUserId() {
        UserContext context = get();
        return context != null ? context.userId : null;
    }

    public static String getUsername() {
        UserContext context = get();
        return context != null ? context.username : null;
    }

    public static boolean isAdmin() {
        UserContext context = get();
        return context != null && "admin".equals(context.type);
    }
}
