package com.neusoft.smart.pos.utils;

import com.neusoft.smart.pos.framework.constants.ApplicationConstants;
import com.neusoft.smart.pos.framework.constants.RoleConstant;
import com.neusoft.smart.pos.redis.bean.UserInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Collections;
import java.util.List;

public class RequestHeaderUtil {

    public static String getUsername() {
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if (attributes != null && attributes.getRequest() != null) {
            String header = attributes.getRequest().getHeader("x-auth-username");
            return StringUtils.isEmpty(header) || header.equals("null") ? "" : header;
        } else {
            return "";
        }
    }

    public static String getRole() {
        final UserInfo userInfo = UserCacheUtils.getUserInfo(getUsername());
        return userInfo == null ? "" : userInfo.getRole();
    }

    public static Integer getUserId() {
        final UserInfo userInfo = UserCacheUtils.getUserInfo(getUsername());
        return userInfo == null ? null : userInfo.getId();
    }

    public static Integer getOrgId() {
        final UserInfo userInfo = UserCacheUtils.getUserInfo(getUsername());
        return userInfo == null ? null : userInfo.getOrgId();
    }

    public static List<Integer> getStoreIds() {
        final UserInfo userInfo = UserCacheUtils.getUserInfo(getUsername());
        return userInfo == null ? Collections.emptyList() : userInfo.getStoreIds();
    }

    /**
     * 判断门店ID是否在门店列表中
     *
     * @param storeId
     * @return true:存在  false:不存在
     */
    public static Boolean isStoreIdContains(Integer storeId) {
        List<Integer> shopIds = getStoreIds();
        if (null != shopIds && shopIds.size() > 0) {
            return shopIds.contains(storeId);
        } else {
            return false;
        }
    }

    /**
     * 判断门店ID是否在门店列表中
     *
     * @return true:存在  false:不存在
     */
    public static Boolean isStoreIdsContains(List<Integer> storeIds) {
        List<Integer> shopIds = getStoreIds();
        if (null == shopIds || shopIds.size() == 0) {
            return false;
        }
        if (null == storeIds || storeIds.size() == 0) {
            return false;
        }
        for(int i=0;i<storeIds.size();i++){
            if(!shopIds.contains(storeIds.get(i))){
                return false;
            }
        }
        return true;
    }

    /**
     * 根据角色获取集团ID
     *
     * @param orgId:集团ID
     */
    public static Integer getOrgIdByRole(Integer orgId) {
        switch (getRole()) {
            case RoleConstant.ROLE_SA:
                return orgId;
            case RoleConstant.ROLE_ADMIN:
                return getOrgId();
            case RoleConstant.ROLE_USER:
                return getOrgId();
            default:
                return null;
        }
    }

    /**
     * 根据角色获取门店ID
     *
     * @param storeId:门店ID
     */
    public static Integer getStoreIdByRole(Integer storeId) {
        switch (getRole()) {
            case RoleConstant.ROLE_SA:
                return storeId;
            case RoleConstant.ROLE_ADMIN:
                //如果为空或为0表示查询集团下所有门店
                if (null == storeId || 0 == storeId) {
                    return 0;
                }
                return isStoreIdContains(storeId) == ApplicationConstants.TRUE ? storeId : null;
            case RoleConstant.ROLE_USER:
                return isStoreIdContains(storeId) == ApplicationConstants.TRUE ? storeId : null;
            default:
                return null;
        }
    }
}
