package com.zd.system.service.impl;

import java.util.HashSet;
import java.util.Set;

import com.zd.common.core.constant.UserConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zd.system.api.domain.SysUser;
import com.zd.system.service.ISysMenuService;
import com.zd.system.service.ISysPermissionService;
import com.zd.system.service.ISysRoleService;

@Service
public class SysPermissionServiceImpl implements ISysPermissionService {
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;

    /**
     * 获取角色数据权限
     *
     * @param userId 用户Id
     * @return 角色权限信息
     */
    @Override
    public Set<String> getRolePermission(Long userId) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (SysUser.isAdmin(userId)) {
            roles.add("superAdmin");
        } else {
            roles.addAll(roleService.selectRolePermissionByUserId(userId));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     *
     * @param sysUser 用户Id
     * @return 菜单权限信息
     */
    @Override
    public Set<String> getMenuPermission(SysUser sysUser) {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (SysUser.isAdmin(sysUser.getUserId())) {
            perms.add("*:*:*");
        } else {
            if(UserConstants.USER_TYPE_STUDENT.equals(sysUser.getUserType())){
                // TODO 学生权限暂处理，项目拆分之后需要删除
                perms.addAll(menuService.selectMenuByStudent());
            }else {
                Set<String> data = menuService.selectNewMenuPermsByUserId(sysUser.getUserId());

                if(data.size() == 0 && UserConstants.USER_TYPE_TEACHER.equals(sysUser.getUserType())){
                    data = menuService.selectMenuByTeacher();
                }
                perms.addAll(data);
                //perms.addAll(menuService.selectMenuPermsByUserId(userId));

            }
        }
        return perms;
    }
}
