package com.it.rattan.monisuo.context;

import lombok.Data;

/**
 * 用户上下文信息
 */
@Data
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.getUserId() : null;
    }

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

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