package org.lingniu.idp.controller;

import org.lingniu.idp.model.base.AjaxResult;
import org.lingniu.idp.model.entity.SysMenu;
import org.lingniu.idp.model.entity.SysUser;
import org.lingniu.idp.service.ISysMenuService;
import org.lingniu.idp.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 登录验证
 * 
 * @author portal
 */
@RestController
@RequestMapping("/idp")
public class SysLoginController
{

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysMenuService menuService;


    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters(@AuthenticationPrincipal Jwt jwt)
    {
        SysUser sysUser = sysUserService.selectUserByUserName(jwt.getSubject());
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(sysUser.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
