package com.sunxung.factoring.component.util;

import java.util.HashMap;
import java.util.Map;

import com.sunxung.factoring.entity.sys.Org;
import com.sunxung.factoring.entity.sys.Session;
import com.sunxung.factoring.entity.sys.User;

/**
 * 线程辅助变量
 */
@SuppressWarnings("unchecked")
public class ThreadLocalUtil {

    private static String SESSION_ID = "sid";
    private static String USER_ID = "uid";
    private static String ORG_ID = "orgId";
    private static ThreadLocal<Object> threadLocal = new ThreadLocal<Object>();

    /** 清除线程辅助变量 */
    public static void clearThreadVariable() {
        threadLocal.remove();
    }

    /**
     * 设置当前Session
     * 
     * @return
     */
    public static void setSession(Session session) {
        Map<String, Session> map = (Map<String, Session>) threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Session>();
        }
        map.remove(SESSION_ID);
        map.put(SESSION_ID, session);
        threadLocal.set(map);
    }

    /**
     * 获取当前Session
     * 
     * @return
     */
    public static Session getSession() {
        Map<?, ?> map = (Map<?, ?>) threadLocal.get();
        if (map != null) {
            return (Session) map.get(SESSION_ID);
        }
        return null;
    }

    /**
     * 获取当前SessionId
     * 
     * @return
     */
    public static Long getSessionId() {
        Map<?, ?> map = (Map<?, ?>) threadLocal.get();
        if (map != null) {
            return ((Session) map.get(SESSION_ID)).getId();
        }
        return null;
    }

    /**
     * 设置当前用户
     * 
     * @return
     */
    public static void setUser(User user) {
        Map<String, User> map = (Map<String, User>) threadLocal.get();
        if (map == null) {
            map = new HashMap<String, User>();
        }
        map.remove(USER_ID);
        map.put(USER_ID, user);
        threadLocal.set(map);
    }

    /**
     * 获取当前用户
     * 
     * @return
     */
    public static User getUser() {
        Map<?, ?> map = (Map<?, ?>) threadLocal.get();
        if (map != null) {
            return (User) map.get(USER_ID);
        }
        return null;
    }

    /**
     * 获取当前用户id
     * 
     * @return
     */
    public static Long getUserId() {
        Map<?, ?> map = (Map<?, ?>) threadLocal.get();
        if (map != null) {
            return ((User) map.get(USER_ID)).getId();
        }
        return null;
    }

    /**
     * 设置当前组织机构
     * 
     * @return
     */
    public static void setOrg(Org org) {
        Map<String, Org> map = (Map<String, Org>) threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Org>();
        }
        map.remove(ORG_ID);
        map.put(ORG_ID, org);
        threadLocal.set(map);
    }

    /**
     * 获取当前组织机构
     * 
     * @return
     */
    public static Org getOrg() {
        Map<?, ?> map = (Map<?, ?>) threadLocal.get();
        if (map != null) {
            return (Org) map.get(ORG_ID);
        }
        return null;
    }

    /**
     * 获取当前组织机构id
     * 
     * @return
     */
    public static Long getOrgId() {
        Map<?, ?> map = (Map<?, ?>) threadLocal.get();
        if (map != null) {
            return ((Org) map.get(ORG_ID)).getId();
        }
        return null;
    }
}
