package com.css.simulation.resource.server.infra.util;

import com.css.simulation.resource.server.infra.oauth.MyUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 认证信息工具类
 */
@Slf4j
public class AuthUtil {

    /**
     * 获取当前登录人id
     */
    public static String getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        return userDetails.getId();
    }

    /**
     * 获取当前登录人roleCode
     */
    public static String getCurrentUserRoleCode() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        return userDetails.getRoleCode();
    }

    /**
     * 获取当前登录人useType
     */
    public static String getCurrentUseType() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        return userDetails.getUseType();
    }

    /**
     * 获取当前登录人username
     */
    public static String getCurrentUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        return userDetails.getUsername();
    }

    /**
     * 获取当前登录人createUserId
     */
    public static String getCreateUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        return userDetails.getCreateUserId();
    }

}
