package com.zhangmeng.admin.manager.controller;

import com.zhangmeng.admin.manager.service.PermissionService;
import com.zhangmeng.admin.manager.utils.UserUtil;
import com.zhangmeng.model.base.baseController.BaseController;
import com.zhangmeng.model.dto.Menu;
import com.zhangmeng.model.entity.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.UUID;

/**
 * 页面请求
 *
 * @author zhangmeng
 * @version 1.0
 * @date 2021年11月4日14:18:48
 */
@Controller
public class RequestController extends BaseController {

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private PermissionService permissionService;

    //登录
    @RequestMapping("/login")
    public ModelAndView login() {
        return this.jumpPage("admin/login");
    }

    //首页
    @RequestMapping("/admin/index")
    public ModelAndView index(Model model) {
        String uuid = UUID.randomUUID().toString();
        model.addAttribute("uuid", uuid);
        model.addAttribute("loginUser", this.userUtil.currentUser());
        return this.jumpPage("admin/index");
    }

    @GetMapping("/user/menuList")
    public List<Menu> permissionList() {
        List<Permission> permissions = this.permissionService.findByUserId(this.userUtil.currentUser().getId());
        return this.permissionService.toUserMenus(permissions, 0L);
    }
}
