package io.renren.modules.admin.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.List;
import java.util.Set;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.admin.entity.AdminMenuEntity;
import io.renren.modules.admin.service.AdminMenuService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.service.ShiroService;

/**
 * 管理员菜单管理
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-28 19:20:56
 */
@RestController
@RequestMapping("admin/adminmenu")
public class AdminMenuController extends AbstractController {
    @Autowired
    private AdminMenuService adminMenuService;
    @Autowired
	private ShiroService shiroService;

    /**
     * 导航菜单
     */
    @GetMapping("/nav")
    public R nav(){
        List<AdminMenuEntity> menuList = adminMenuService.getUserMenuList(getUserId());
        Set<String> permissions = shiroService.getUserPermissions(getUserId());
        return R.ok().put("menuList", menuList).put("permissions", permissions);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("admin:adminmenu:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = adminMenuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{menuId}")
    @RequiresPermissions("admin:adminmenu:info")
    public R info(@PathVariable("menuId") Long menuId){
		AdminMenuEntity adminMenu = adminMenuService.getById(menuId);

        return R.ok().put("adminMenu", adminMenu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("admin:adminmenu:save")
    public R save(@RequestBody AdminMenuEntity adminMenu){
		adminMenuService.save(adminMenu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:adminmenu:update")
    public R update(@RequestBody AdminMenuEntity adminMenu){
		adminMenuService.updateById(adminMenu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("admin:adminmenu:delete")
    public R delete(@RequestBody Long[] menuIds){
		adminMenuService.removeByIds(Arrays.asList(menuIds));

        return R.ok();
    }

}
