package com.sunxung.factoring.component.util;

import java.util.Date;

import com.sunxung.factoring.entity.sys.Session;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.service.sys.OrgService;
import com.sunxung.factoring.service.sys.UserService;

/**
 * @Description:模拟admin用户线程变量
 * @Author:王庆之
 * @E-mail:122058102@qq.com
 * @Date:2017年4月12日 上午10:36:39
 */
public class MockAdminThreadLocalUtil {

    /** 模拟admin */
    public static void mock() {
        User user = getUserService().getByLoginName("admin");
        if (user == null) {
            user = new User();
        }
        Session session = new Session();
        Date date = new Date();
        session.setLaterTime(date);
        session.setLoginTime(date);
        session.setUserId(user.getId());
        // if (user.getOrg() != null) {
        // ThreadLocalUtil.setOrg(getOrgService().get(user.getOrg().getId()));
        // }
        ThreadLocalUtil.setSession(session);
        ThreadLocalUtil.setUser(user);
    }

    /** 销毁admin */
    public static void destroy() {
        ThreadLocalUtil.clearThreadVariable();
    }

    private static UserService userService;

    private static UserService getUserService() {
        if (userService == null) {
            userService = SpringUtil.getBeanByType(UserService.class);
        }
        return userService;
    }

    private static OrgService orgService;

    private static OrgService getOrgService() {
        if (orgService == null) {
            orgService = SpringUtil.getBeanByType(OrgService.class);
        }
        return orgService;
    }
}
