package com.bomaos.common.core.web;

import com.bomaos.common.system.entity.User;
import com.bomaos.website.entity.Website;
import com.bomaos.website.service.WebsiteService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Controller基类
 * Created by Panyoujie on 2017-06-10 10:10
 */
public class BaseController {

    @Autowired
    private WebsiteService websiteService;

    /**
     * 获取当前登录的user
     */
    public User getLoginUser() {
        Subject subject = SecurityUtils.getSubject();
        if (subject == null) return null;
        Object object = subject.getPrincipal();
        if (object != null) return (User) object;
        return null;
    }

    /**
     * 获取当前登录的userId
     */
    public Integer getLoginUserId() {
        User loginUser = getLoginUser();
        return loginUser == null ? null : loginUser.getUserId();
    }

    public String getWebName() {
        Website website = websiteService.getById(1);
        return website.getWebsiteName();
    }

    /**
     * 获取当前登录用户的supplierId，管理员返回null
     */
    public Integer getLoginUserSupplierId() {
        User loginUser = getLoginUser();
        if (loginUser == null) return null;
        Integer supplierId = loginUser.getSupplierId();
        return (supplierId != null && supplierId > 0) ? supplierId : null;
    }

    /**
     * 判断当前用户是否为供应商
     */
    public boolean isSupplier() {
        return getLoginUserSupplierId() != null;
    }

}
