package com.imdroid.beidou.auth;

import com.imdroid.beidou.entity.Tenant;
import com.imdroid.beidou.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * session 工具类
 *
 * @author LiGang
 */
public class SessionUtils {

    public static final String SESSION_CURRENT_USER_ID = "user_id";
    public static final String SESSION_CURRENT_USER = "login_user";
    public static final String SESSION_TENANT_ID = "tenant_id";
    public static final String SESSION_TENANT_NAME = "tenant_name";
    public static final String SESSION_ROLE = "role";

    public static String getCurrentUser(HttpServletRequest request) {
        return (String) request.getSession().getAttribute(SESSION_CURRENT_USER);
    }

    public static void setCurrentUser(HttpServletRequest request, User user, Tenant tenant) {
        HttpSession session = request.getSession();
        session.setAttribute(SESSION_CURRENT_USER, user.getName());
        session.setAttribute(SESSION_TENANT_ID, user.getTenantid());
        session.setAttribute(SESSION_TENANT_NAME, tenant.getName());
        session.setAttribute(SESSION_ROLE, user.getRole());
        session.setAttribute(SESSION_CURRENT_USER_ID, user.getId());
    }

    public static Integer getTenantId(HttpServletRequest request) {
        return (Integer) request.getSession().getAttribute(SESSION_TENANT_ID);
    }

    public static String getRole(HttpServletRequest request) {
        return (String) request.getSession().getAttribute(SESSION_ROLE);
    }

    public static Long getUserId(HttpServletRequest request) {
        return (Long) request.getSession().getAttribute(SESSION_CURRENT_USER_ID);
    }
}
