package com.zcloud.modules.sys.controller;

import com.zcloud.common.annotation.SysLog;
import com.zcloud.common.utils.R;
import com.zcloud.modules.sys.entity.SysMenuEntity;
import com.zcloud.modules.sys.service.ShiroService;
import com.zcloud.modules.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 系统菜单
 *
 *
 */
@RestController
@RequestMapping("/sys/menu")
@Api(tags="菜单管理")
public class SysMenuController extends AbstractController {
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private ShiroService shiroService;


	/**
	 * 导航菜单
	 */
	@PostMapping("/nav")
	@ApiOperation(value = "获取导航")
	public R nav(){
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(getUserId());
		Set<String> permissions = shiroService.getUserPermissions(getUserId());
		return R.ok().put("menuList", menuList).put("permissions", permissions);
	}

	/**
	 * 所有菜单列表
	 */
	@PostMapping("/list")
	//@RequiresPermission("sys:menu:list")
	@ApiOperation(value = "所有菜单列表")
	public R list(@RequestBody SysMenuEntity menu){
		List<SysMenuEntity> list = sysMenuService.getAllList(menu.getParentId());

		return R.ok().put("menuList", list);
	}

	/**
	 * 所有菜单列表(不包括按钮)
	 */
	@PostMapping("/listNotButton")
	//@RequiresPermission("sys:menu:select")
	@ApiOperation(value = "所有菜单列表(不包括按钮)")
	public R listNotButton(@RequestBody SysMenuEntity menu){
		List<SysMenuEntity> list = sysMenuService.getAllListNotButton(menu.getParentId());

		return R.ok().put("menuList", list);
	}

	/**
	 * 菜单信息
	 */
	@PostMapping("/info")
	//@RequiresPermission("sys:menu:info")
	@ApiOperation(value = "菜单信息")
	public R info(@RequestBody SysMenuEntity menu){
		menu = sysMenuService.getById(menu.getMenuId());
		if (menu.getParentId()!=0){
			menu.setParentName(sysMenuService.getById(menu.getParentId()).getName());
		}
		return R.ok().put("menu", menu);
	}

	/**
	 * 保存
	 */
	@SysLog("保存菜单")
	@PostMapping("/save")
	//@RequiresPermission("sys:menu:save")
	@ApiOperation(value = "保存菜单信息")
	public R save(@RequestBody SysMenuEntity menu){

		sysMenuService.save(menu);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("修改菜单")
	@PostMapping("/update")
	//@RequiresPermission("sys:menu:update")
	@ApiOperation(value = "修改菜单信息")
	public R update(@RequestBody SysMenuEntity menu){

		sysMenuService.updateById(menu);

		return R.ok();
	}

	/**
	 * 修改菜单图标
	 */
	@SysLog("修改菜单图标")
	@PostMapping("/icon")
	//@RequiresPermission("sys:menu:update")
	@ApiOperation(value = "修改菜单图标")
	public R icon(@RequestBody SysMenuEntity menu){
		sysMenuService.updateById(menu);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@SysLog("删除菜单")
	@PostMapping("/delete")
	//@RequiresPermission("sys:menu:delete")
	@ApiOperation(value = "删除菜单信息")
	public R delete(@RequestBody SysMenuEntity menu){
		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menu.getMenuId());
		if(menuList.size() > 0){
			return R.error(200,"请先删除子菜单或按钮");
		}

		sysMenuService.delete(menu.getMenuId());

		return R.ok();
	}

}
