package com.judong.chuanyiserver.util;

import com.judong.chuanyiserver.dao.UserDao;
import com.judong.chuanyiserver.entity.User;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.exception.CustomException;
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.servlet.http.HttpServletRequest;
import java.util.List;

@Component
public class UserUtil {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserDao userDao;

    /**
     * 获取请求
     */
    public HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest();
    }

    /**
     * 获取请求中的token
     *
     * @return
     */
    public String getToken() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        if (Blank.isEmpty(token)) {
            throw new CustomException(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
        }
        return token;
    }

    /**
     * 获取当前登录人信息
     *
     * @return
     */
    public User getCurrentUser() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        if (Blank.isEmpty(token)) {
            throw new CustomException(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
        }
        String userId = (String) redisUtil.get(token);
        if (Blank.isEmpty(userId)) {
            throw new CustomException(ResultEnum.LOGIN_EXPIRED.getRespCode(), ResultEnum.LOGIN_EXPIRED.getRespMsg());
        }
        User user = userDao.getUserById(userId);
        if (Blank.isEmpty(user)) {
            throw new CustomException(ResultEnum.LOGIN_EXPIRED.getRespCode(), "未获取到此用户信息");
        }
        return user;
    }

    /**
     * 获取当前登录人Id
     *
     * @return
     */
    public String getCurrentUserId() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        if (Blank.isEmpty(token)) {
            throw new CustomException(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
        }
        String userId = (String) redisUtil.get(token);
        if (Blank.isEmpty(userId)) {
            throw new CustomException(ResultEnum.LOGIN_EXPIRED.getRespCode(), ResultEnum.LOGIN_EXPIRED.getRespMsg());
        }
        return userId;
    }

    /**
     * 获取请求的用户是否有管理员权限
     */
    public boolean haveAdminRights() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        if (Blank.isEmpty(token)) {
            throw new CustomException(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
        }
        String userId = (String) redisUtil.get(token);
        if (Blank.isEmpty(userId)) {
            throw new CustomException(ResultEnum.LOGIN_EXPIRED.getRespCode(), ResultEnum.LOGIN_EXPIRED.getRespMsg());
        }
        List<Integer> roleIdList = userDao.getRoleIdListByUserId(userId);
        for (Integer roleId : roleIdList) {
            if (roleId == ConstantStr.ROLE_ADMIN) {
                return true;
            }
        }
        throw new CustomException(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), ResultEnum.NO_OPERATION_AUTHORITY.getRespMsg());
    }
}
