package com.sysu.admin.support.shiro;

import com.sysu.admin.support.system.organ.Organ;
import com.sysu.admin.support.system.organ.OrganRepository;
import com.sysu.admin.support.system.role.SRole.RoleEnum;
import com.sysu.admin.support.system.user.User;
import com.sysu.admin.support.system.user.UserRepository;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @title ShiroService
 * @description
 * @author XIE
 * @date 2020年4月24日下午10:09:55
 */
@Service
public class ShiroService {

    @Autowired
    UserRepository mUserRepository;

    @Autowired
    OrganRepository mOrganRepository;

    /**
     * 获取当前昵称
     */
    public String getCurrentNickName() {
        User user = getCurrentUser();
        if (ObjectUtils.isNotEmpty(user)) {
            return user.getNickname();
        }
        return null;
    }

    /**
     * 获取当前系统用户
     */
    @SneakyThrows
    public Organ getCurrentOrgan() {
        User user = getCurrentUser();
        return mOrganRepository.findOne(user.getOrganId());
    }

    /**
     * 获取当前用户本地用户
     */
    public ShiroPrincipal getCurrentPrincipal() {
        Subject localSubject = SecurityUtils.getSubject();
        if (localSubject != null) {
            ShiroPrincipal localPrincipal = (ShiroPrincipal) localSubject.getPrincipal();
            if (localPrincipal != null) {
                return localPrincipal;
            }
        }
        return null;
    }

    /**
     * 获取当前系统用户
     */
    @SneakyThrows
    public User getCurrentUser() {
        Subject subject = SecurityUtils.getSubject();
        if (ObjectUtils.isNotEmpty(subject)) {
            ShiroPrincipal principal = (ShiroPrincipal) subject.getPrincipal();
            if (ObjectUtils.isNotEmpty(principal)) {
                return mUserRepository.findOne(principal.getId());
            }
        }
        return null;
    }

    public Long getPrincipalId(){
        Subject subject = SecurityUtils.getSubject();
        if (ObjectUtils.isNotEmpty(subject)) {
            ShiroPrincipal principal = (ShiroPrincipal) subject.getPrincipal();
            if (ObjectUtils.isNotEmpty(principal)) {
                return principal.getId();
            }
        }
        return null;
    }

    /**
     * 获取当前编号
     */
    public Long getCurrentUserId() {
        User user = getCurrentUser();
        if (ObjectUtils.isNotEmpty(user)) {
            return user.getId();
        }
        return null;
    }

    /**
     * 获取当前帐号
     */
    public String getCurrentUserName() {
        User user = getCurrentUser();
        if (ObjectUtils.isNotEmpty(user)) {
            return user.getUsername();
        }
        return null;
    }

    /**
     * 用一个角色与当前用户角色比较
     */
    public boolean hasRole(String roleIdentifier) {
        Subject subject = SecurityUtils.getSubject();
        return subject.hasRole(roleIdentifier);
    }

    public boolean isAdmin(){
        return hasRole(RoleEnum.admin.name());
    }

    /**
     * 是否是县级执法
     * @return
     */
    public boolean isXjzf(){
        return hasRole(RoleEnum.xjzf.name());
    }

    /**
     * 是否是县级执法
     * @return
     */
    public boolean isZjxc(){
        return hasRole(RoleEnum.zjxc.name());
    }

    /**
     * 是否是市级执法
     * @return
     */
    public boolean isSjzf(){
        return hasRole(RoleEnum.sjzf.name());
    }

    /**
     * 是否通过认证
     */
    public boolean isAuthenticated() {
        Subject localSubject = SecurityUtils.getSubject();
        if (localSubject != null) {
            return localSubject.isAuthenticated();
        }
        return false;
    }
}
