package com.rongwei.bscommon.sys.utils;

import com.rongwei.commonservice.serial.service.SysSerialNumberService;
import com.rongwei.commonservice.service.RedisService;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;


@Component
public class CommonUtils {

    @Autowired
    private RedisService autoRedisService;
    @Autowired
    private SysSerialNumberService autoSysSerialNumberService;

    private static RedisService redisService;
    private static SysSerialNumberService sysSerialNumberService;

    @PostConstruct
    public void init() {
        redisService = autoRedisService;
        sysSerialNumberService = autoSysSerialNumberService;
    }


    /**
     * 获取当前的登陆人信息
     *
     * @return
     */
    public static SysUserVo getCurrentUser() {
        // 获取当前登录人信息
        SysUserVo currUser = null;
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            if (request != null) {
                String token = request.getHeader("token");
                if (com.rongwei.rwcommon.utils.StringUtils.isNotEmpty(token)) {
                    currUser = redisService.getLoginUser(token);
                }
            }
        }
        return currUser;
    }

}
