package com.keyware.shandan.control.controller;

import com.keyware.shandan.common.enums.SystemTypes;
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.SysSetting;
import com.keyware.shandan.system.entity.SysUser;
import com.keyware.shandan.system.service.SysRoleService;
import com.keyware.shandan.system.utils.MenuUtil;
import com.keyware.shandan.system.utils.SysSettingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * 首页前端控制器
 *
 * @author Administrator
 */
@Controller
@RequestMapping
public class ControlIndexController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/index")
    public ModelAndView index(ModelAndView view) {
        view.setViewName("index");

        //登录用户
        SysUser user = SecurityUtil.getLoginSysUser();
        if (user != null) {
            user.setPassword(null);
            //登录用户系统菜单
            List<SysRole> roles = sysRoleService.getUserRoles(user.getUserId());
            Set<SysMenu> menuList = new HashSet<>();
            for (SysRole role : roles) {
                menuList.addAll(role.getMenuList());
            }
            view.addObject("menuList", MenuUtil.getChildBySysMenuVo("CONTROL", menuList));
        }

        // 数据分类编目服务地址
        SysSetting bianmuSetting = SysSettingUtil.getSysSetting(SystemTypes.BIANMU.name());
        view.addObject("bianmuAddress", bianmuSetting.getSysAddress());
        view.addObject("appName", SysSettingUtil.getCurrentSysSetting().getSysName());
        view.addObject("user", user);
        return view;
    }
}
