package com.cico.nest.lcdp.boot.security.core;

import cn.hutool.core.map.MapUtil;
import com.cico.nest.lcdp.framework.common.enums.UserTypeEnum;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登录用户信息
 *
 * @author zws
 */
@Data
public class LoginUser {

    /**
     * 用户编号
     */
    private Long id;
    /**
     * 用户类型
     *
     * 关联 {@link UserTypeEnum}
     */
    private Integer userType;
    /**
     * 租户编号
     */
    private Long tenantId;
    /**
     * 授权范围
     */
    private List<String> scopes;

    // ========== 上下文 ==========
    /**
     * 上下文字段，不进行持久化
     * 1. 用于基于 LoginUser 维度的临时缓存
     */
    @JsonIgnore
    private Map<String, Object> context;

    public void setContext(String key, Object value) {
        if (context == null) {
            context = new HashMap<>();
        }
        context.put(key, value);
    }

    public <T> T getContext(String key, Class<T> type) {
        return MapUtil.get(context, key, type);
    }

    public LoginUser setId(Long id) {
        this.id = id;
        return this;
    }

    public LoginUser setUserType(Integer userType) {
        this.userType = userType;
        return this;
    }

    public LoginUser setTenantId(Long tenantId) {
        this.tenantId = tenantId;
        return this;
    }

    public LoginUser setScopes(List<String> scopes){
        this.scopes = scopes;
        return this;
    }

}
