package com.hwrj.cloud.admin.util;

import com.hwrj.cloud.admin.bo.AdminUserDetails;
import com.hwrj.cloud.admin.model.UmsAdmin;
import com.hwrj.cloud.common.api.ResultCode;
import com.hwrj.cloud.common.exception.GlobalException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;

/**
 * @className:
 * @description: 当前登录用户
 * @author: lym
 * @date: 2020/9/6 16:16
 */
public class CurrUserUtil {

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static UmsAdmin getUmsAdmin() {
        Authentication authentication = getAuthentication();
        if (authentication == null) {
            return null;
        }
        AdminUserDetails adminUserDetails = getLoginUser(authentication);
        UmsAdmin umsAdmin = adminUserDetails.getUmsAdmin();
        if (StringUtils.isEmpty(umsAdmin)) {
            throw new GlobalException((int) ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage());
        }
        String username = umsAdmin.getUsername();
        if("admin".equals(username)){
            umsAdmin.setAdmin(true);
        }
        return umsAdmin;
    }

    /**
     * 获取用户
     */
    public static AdminUserDetails getLoginUser(Authentication authentication) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof AdminUserDetails) {
            return (AdminUserDetails) principal;
        }
        throw new GlobalException((int) ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage());
    }

    /**
     * 获取用户id
     */
    public static int getUserId() {
        UmsAdmin umsAdmin = getUmsAdmin();
        if (StringUtils.isEmpty(umsAdmin.getId())) {
            throw new GlobalException(1, "请登录!");
        }
        long userId = umsAdmin.getId();
        return (int) userId;
    }

    /**
     * 获取用户id
     */
    public static Long getLongUserId() {
        UmsAdmin umsAdmin = getUmsAdmin();
        if (StringUtils.isEmpty(umsAdmin.getId())) {
            throw new GlobalException(1, "请登录!");
        }
        long userId = umsAdmin.getId();
        return  userId;
    }

    /**
     * 获取用户绑定供应商id
     */
    public static long getCompId() {
        UmsAdmin umsAdmin = getUmsAdmin();
        if (StringUtils.isEmpty(umsAdmin.getCompId())) {
            throw new GlobalException(1, "登录用户未绑定供应商!");
        }
        long compId = umsAdmin.getCompId();
        return compId;
    }

    /**
     * 获取用户绑定供应商id
     */
    public static Long getCompIdInfo() {
        UmsAdmin umsAdmin = getUmsAdmin();
        Long compId = umsAdmin.getCompId();
        if(StringUtils.isEmpty(compId)){
            return null;
        }
        return compId;
    }

    /**
     * 获取用户绑定供应商名称
     */
    public static String getCompName() {
        UmsAdmin umsAdmin = getUmsAdmin();
        String compName = umsAdmin.getCompName();
        return compName;
    }
}

