package com.rongwei.trainingcommon.sys.utils;

import com.rongwei.commonservice.serial.service.SysSerialNumberService;
import com.rongwei.commonservice.service.RedisService;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.util.Date;

/**
 * TrainingUtils class
 *
 * @author XH
 * @date 2023/12/12
 */
@Component
public class TrainingUtils {

    private static final Logger log = LoggerFactory.getLogger(TrainingUtils.class.getName());
    @Autowired
    private SysSerialNumberService autoSysSerialNumberService;

    private static SysSerialNumberService sysSerialNumberService;

    @Autowired
    private RedisService autoRedisService;

    private static RedisService redisService;

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



    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;
    }
}
