package com.lutao.cloud.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import com.lutao.cloud.admin.api.entity.SysMenu;
import com.lutao.cloud.admin.service.SysMenuService;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.log.annotation.SysLog;
import com.lutao.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**

 * @date 2017/10/31
 */
@Api(value = "/menu", tags ={"菜单管理"})
@RestController
@AllArgsConstructor
@RequestMapping("/menu")
@Slf4j
public class SysMenuController{

	private final SysMenuService sysMenuService;

	/**
	 * 返回当前用户的树形菜单集合
	 * @param type 类型
	 * @param parentId 父节点ID
	 * @return 当前用户的树形菜单
	 */
	@ApiOperation(value = "返回当前用户的树形菜单集合", notes = "返回当前用户的树形菜单集合", httpMethod = "GET")
	@GetMapping
	public MessageInfo<List<Tree<Integer>>> getUserMenu(String type, Integer parentId){
		// 获取符合条件的菜单
		Set<SysMenu> all = new HashSet<>();
		SecurityUtils.getRoles().forEach(roleId -> all.addAll(sysMenuService.findMenuByRoleId(roleId)));
		return MessageInfo.ok(sysMenuService.filterMenu(all, type, parentId));
	}

	/**
	 * 返回树形菜单集合
	 * @param lazy 是否是懒加载
	 * @param parentId 父节点ID
	 * @return 树形菜单
	 */
	@ApiOperation(value = "返回树形菜单集合", notes = "返回树形菜单集合", httpMethod = "GET")
	@GetMapping(value = "/tree")
	public MessageInfo<List<Tree<Integer>>> getTree(boolean lazy, Integer parentId){
		return MessageInfo.ok(sysMenuService.treeMenu(lazy, parentId));
	}

	/**
	 * 返回角色的菜单集合
	 * @param roleId 角色ID
	 * @return 属性集合
	 */
	@ApiOperation(value = "返回角色的菜单集合", notes = "返回角色的菜单集合", httpMethod = "GET")
	@GetMapping("/tree/{roleId}")
	public MessageInfo<List<Integer>> getRoleTree(@PathVariable Integer roleId){
		return MessageInfo.ok(
				sysMenuService.findMenuByRoleId(roleId).stream().map(SysMenu::getMenuId).collect(Collectors.toList()));
	}

	/**
	 * 通过ID查询菜单的详细信息
	 * @param id 菜单ID
	 * @return 菜单详细信息
	 */
	@ApiOperation(value = "通过ID查询菜单的详细信息", notes = "通过ID查询菜单的详细信息", httpMethod = "GET")
	@GetMapping("/{id}")
	public MessageInfo<SysMenu> getById(@PathVariable Integer id){
		return MessageInfo.ok(sysMenuService.getById(id));
	}

	/**
	 * 新增菜单
	 * @param sysMenu 菜单信息
	 * @return success/false
	 */
	@ApiOperation(value = "新增菜单", notes = "新增菜单", httpMethod = "POST")
	@SysLog("新增菜单")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_menu_add')")
	public MessageInfo<SysMenu> save(@Valid @RequestBody SysMenu sysMenu){
		sysMenuService.saveWithParent(sysMenu);
		return MessageInfo.ok(sysMenu);
	}

	/**
	 * 删除菜单
	 * @param id 菜单ID
	 * @return success/false
	 */
	@ApiOperation(value = "删除菜单", notes = "删除菜单", httpMethod = "DELETE")
	@SysLog("删除菜单")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_menu_del')")
	public MessageInfo<Boolean> removeById(@PathVariable Integer id){
		return sysMenuService.removeMenuById(id);
	}

	/**
	 * 更新菜单
	 * @param sysMenu
	 * @return
	 */
	@ApiOperation(value = "更新菜单", notes = "更新菜单", httpMethod = "PUT")
	@SysLog("更新菜单")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_menu_edit')")
	public MessageInfo<Boolean> update(@Valid @RequestBody SysMenu sysMenu){
		return MessageInfo.ok(sysMenuService.updateWithParent(sysMenu));
	}

}
