package com.unis.framework.security.core;

import cn.hutool.core.map.MapUtil;
import com.unis.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 芋道源码
 */
@Data
public class LoginUser {

    public static final String INFO_KEY_NICKNAME = "nickname";
    public static final String INFO_KEY_DEPT_ID = "deptId";
    public static final String INFO_KEY_U_CODE = "areaCode";
    public static final String INFO_KEY_U_LEVEL = "areaLevel";
    public static final String INFO_KEY_SHENG_CODE = "shengCode";
    public static final String INFO_KEY_SHI_CODE = "shiCode";
    public static final String INFO_KEY_XIAN_CODE = "xianCode";
    public static final String INFO_KEY_ROLE_IDS = "roleIds";
    public static final String INFO_KEY_BANK_TYPE = "bankType";
    public static final String INFO_KEY_KQBM = "kqbm";

    /**
     * 用户编号
     */
    private Long id;
    /**
     * 用户类型
     *
     * 关联 {@link UserTypeEnum}
     */
    private Integer userType;
    /**
     * 额外的用户信息
     */
    private Map<String, String> info;
    /**
     * 租户编号
     */
    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);
    }

}
