package com.keyware.shandan.bianmu.controller;

import com.keyware.shandan.frame.config.security.SecurityUtil;
import com.keyware.shandan.system.entity.SysMenu;
import com.keyware.shandan.system.entity.SysRole;
import com.keyware.shandan.system.entity.SysUser;
import com.keyware.shandan.system.service.SysRoleService;
import com.keyware.shandan.system.service.SysUserService;
import com.keyware.shandan.system.utils.MenuUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * <p>
 * BianmuIndexController
 * </p>
 *
 * @author Administrator
 * @since 2021/6/17
 */
@Slf4j
@RestController
@RequestMapping("/")
public class BianmuIndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;


    @GetMapping("index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("index");

        //登录用户
        SysUser user = sysUserService.findByLoginName(SecurityUtil.getLoginUserByContext().getUsername());
        if (user != null) {
            //登录用户系统菜单
            List<SysRole> roles = sysRoleService.getUserRoles(user.getUserId());
            if (roles.isEmpty()) {
                modelAndView.addObject("ERROR", "没有配置角色");
            } else {
                Set<SysMenu> menuList = new HashSet<>();
                for (SysRole role : roles) {
                    menuList.addAll(role.getMenuList());
                }
                modelAndView.addObject("menuList", MenuUtil.getChildBySysMenuVo("BIANMU", menuList));
            }
        }
        return modelAndView;
    }
}
