package com.lantone.common.util;

import cn.hutool.json.JSONUtil;
import com.lantone.common.constant.AuthConstant;
import com.lantone.common.domain.UserDto;
import com.nimbusds.jose.JWSObject;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 用户工具类
 * @author: rengb
 * @time: 2021/1/5 18:27
 */
@Slf4j
public class SysUserUtils {

    /**
     * 获取当前请求的token
     *
     * @return
     */
    public static String getToken() {
        return HttpUtils.getHeader(AuthConstant.JWT_TOKEN_HEADER);
    }

    /**
     * 从token中解析出用户信息
     *
     * @param token
     * @return
     */
    public static UserDto getUserByToken(String token) {
        if (StringUtil.isBlank(token)) {
            return null;
        }
        try {
            String realToken = token.replace(AuthConstant.JWT_TOKEN_PREFIX, "");
            JWSObject jwsObject = JWSObject.parse(realToken);
            return JSONUtil.toBean(jwsObject.getPayload().toString(), UserDto.class);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * 获取当前请求的用户
     *
     * @return
     */
    public static UserDto getCurrentUser() {
        return getUserByToken(getToken());
    }

    /**
     * 获取当前请求的用户的名称
     *
     * @return
     */
    public static String getCurrentPrinciple() {
        UserDto userDto = getCurrentUser();
        return userDto == null ? null : userDto.getUserName();
    }

    /**
     * 获取当前请求的用户的Id
     *
     * @return
     */
    public static Long getCurrentPrincipleId() {
        UserDto userDto = getCurrentUser();
        return userDto == null ? null : userDto.getId();
    }

    /**
     * 获取当前请求的用户的Id字符串
     *
     * @return
     */
    public static String getCurrentPrincipleIdStr() {
        UserDto userDto = getCurrentUser();
        return userDto == null ? null : userDto.getId().toString();
    }

    /**
     * 获取当前请求的用户的当前医院id
     *
     * @return
     */
    public static Long getCurrentHospitalId() {
        String currentHospitalIdStr = getCurrentHospitalIdStr();
        return StringUtil.isBlank(currentHospitalIdStr) ? null : Long.parseLong(currentHospitalIdStr);
    }

    /**
     * 获取当前请求的用户的当前医院id字符串
     *
     * @return
     */
    public static String getCurrentHospitalIdStr() {
        String currentHospitalIdStr = HttpUtils.getHeader(AuthConstant.HOSPITAL_ID_HEADER);
        return StringUtil.isNotBlank(currentHospitalIdStr) && currentHospitalIdStr.matches("[0-9]+") ? currentHospitalIdStr : null;
    }

    /**
     * 获取当前请求的用户的当前系统id
     *
     * @return
     */
    public static Long getCurrentSoftwareId() {
        String currentSoftwareIdStr = getCurrentSoftwareIdStr();
        return StringUtil.isBlank(currentSoftwareIdStr) ? null : Long.parseLong(currentSoftwareIdStr);
    }

    /**
     * 获取当前请求的用户的当前系统id字符串
     *
     * @return
     */
    public static String getCurrentSoftwareIdStr() {
        String currentSoftwareIdStr = HttpUtils.getHeader(AuthConstant.SOFTWARE_ID_HEADER);
        return StringUtil.isNotBlank(currentSoftwareIdStr) && currentSoftwareIdStr.matches("[0-9]+") ? currentSoftwareIdStr : null;
    }

}