package com.ssm.manage.utils;

import org.apache.commons.lang.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

public class SessionHelp {
    private static String USER_ID = "userId";
    private static String LOGIN_ACCOUNT = "loginAccount";
    private static String IS_SUPER_ADMIN = "isSuperAdmin";
    private static Integer IS_ADMIN = 0;
    private static String TOKEN = "token";
    private static String SUPER_ADMIN = "super";
    private static String AUTH = "auth";
    private static String STORE_ID = "storeId";
    private static String STORE_NUM = "storeNum";
    private static String STORE_NAME = "storeName";
    private static List<Long> STORE_IDS;




    public static Integer getUserId(HttpSession session) {
        return (Integer) session.getAttribute(USER_ID);
    }

    public static void setUserId(HttpSession session,Integer userId) {
        session.setAttribute(USER_ID,userId);
    }


    public static String getLoginAccount(HttpSession session) {
        return (String) session.getAttribute(LOGIN_ACCOUNT);
    }

    public static void setLoginAccount(HttpSession session,String loginAccount) {
        session.setAttribute(LOGIN_ACCOUNT,loginAccount);
    }

    public static int getIsSuperAdmin(HttpSession session) {
        return (int) session.getAttribute(IS_SUPER_ADMIN);
    }

    public static void setIsSuperAdmin(HttpSession session, int isSuperAdmin) {
        session.setAttribute(IS_SUPER_ADMIN,isSuperAdmin);
    }

    public static String getToken(HttpSession session) {
        return (String) session.getAttribute(TOKEN);
    }

    public static void setToken(HttpSession session,String token) {
        session.setAttribute(TOKEN,token);
    }

    public static Map getAuth(HttpSession session) {
        return (Map) session.getAttribute(AUTH);
    }

    public static void setAUTH(HttpSession session, Map authMap) {
        session.setAttribute(IS_SUPER_ADMIN,authMap);
    }

    public static Long getStoreId(HttpSession session) {
        return (Long) session.getAttribute(STORE_ID);
    }

    public static void setStoreId(HttpSession session, Long storeId){
        session.setAttribute(STORE_ID,storeId);
    }

    public static String getStoreNum(HttpSession session) {
        return (String) session.getAttribute(STORE_NUM);
    }

    public static void setStoreNum(HttpSession session, String storeNum){
        session.setAttribute(STORE_NUM,storeNum);
    }

    public static String getStoreName(HttpSession session) {
        return (String) session.getAttribute(STORE_NAME);
    }

    public static void setStoreName(HttpSession session, String storeName){
        session.setAttribute(STORE_NAME,storeName);
    }

    public static List<Long> getStoreIds() {
        return STORE_IDS;
    }

    public static void setStoreIds(List<Long> storeIds) {
        STORE_IDS = storeIds;
    }

    /**
     * 检查用户的权限
     * @param authId
     * @param request
     * @return
     */
    public  static boolean checkedUserAuth(String  authId, HttpSession session){
        if(isAdmin(null,session)){
            return true;
        }
        boolean flag=false;
        if(null != SessionHelp.getAuth(session)){
            Map authMap = SessionHelp.getAuth(session);
            if(null != authMap && !authMap.isEmpty()){
                String auth_id = ObjectUtils.toString(authMap.get(authId));
                if(!"".equals(auth_id) && auth_id.equals(authId)){
                    flag = true;
                }
            }
        }
        return flag;
    }


    /**
     * 是否是超级管理员
     * @param request
     * @return
     */
    public static  boolean   isAdmin(String loginAccount,HttpSession session){
        if(null == loginAccount || "".equals(loginAccount)) {
            loginAccount = SessionHelp.getLoginAccount(session);
        }

        String superAdmin = SUPER_ADMIN;
        if(loginAccount.equals(superAdmin)){
            return true;
        }

        return false;
    }

}
