package com.blueland.core.util;

import com.blueland.bo.TucStaffEntity;
import com.blueland.model.TUcStaff;
import com.blueland.service.ITucStaffService;
import com.blueland.service.impl.TucStaffServiceImpl;
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 javax.servlet.http.HttpServletResponse;
import java.util.Optional;

/**
 * @Description 用户工具类
 * @Date 2021/7/22 1:30 下午
 * @Created BigTian
 */
@Component
public class UserUtils {
    private static ITucStaffService tucStaffServiceImpl;

    @Autowired
    private ITucStaffService tucStaffServiceImpl2;

    public static String getEmpno() {
        return getUser().getStaffNum();
    }


    @PostConstruct
    public void init() {
        tucStaffServiceImpl = tucStaffServiceImpl2;
    }

    /**
     * 获取iv-userId
     * @return
     */
    public static String getIvUser() {
        HttpServletRequest request = getRequest();
        return Optional.ofNullable(request)
                .map(el -> el.getHeader("iv-user"))
                .orElse("luob");
        //.orElseThrow(() -> new BusinessException("请求头中没有iv-user"));
    }

    /**
     * 获取租户id
     * @return
     */
    // tenantid
    public static String getTenantId() {
        HttpServletRequest request = getRequest();
        return Optional.ofNullable(request)
                .map(el -> el.getHeader("tenantid"))
                .orElseThrow(() -> new RuntimeException("请求头中没有tenantid"));
    }

    /**
     * 获取租户id
     * @return
     */
    public static String getAppId() {
        HttpServletRequest request = getRequest();
        return Optional.ofNullable(request)
                .map(el -> el.getHeader("tenantid"))
                .orElse("");
    }

    /**
     * 获取岗位id
     * @return
     */
    // staffPostCode
    public static String getPostCode() {
        HttpServletRequest request = getRequest();
        return Optional.ofNullable(request)
                .map(el -> el.getHeader("staffPostCode"))
                .orElseThrow(() -> new RuntimeException("请求头中没有staffPostCode"));
    }

    /**
     * 获取当前登录人信息
     * @return
     */
    public static TUcStaff getUser() {
        TucStaffEntity model = new TucStaffEntity();
        model.setStaffNum(getIvUser());
        return tucStaffServiceImpl.get(model);
    }

    /**
     * 获取请求属性
     *
     * @return
     */
    public static ServletRequestAttributes getRequestAttr() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

    /**
     * 获取request
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = getRequestAttr();
        return Optional.ofNullable(attributes).
                map(ServletRequestAttributes::getRequest)
                .orElse(null);
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse() {
        ServletRequestAttributes attributes = getRequestAttr();
        return attributes.getResponse();
    }


}
