

package com.nantian.sc.cloud.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nantian.sc.cloud.admin.api.dto.MenuTree;
import com.nantian.sc.cloud.admin.api.entity.SysMenu;
import com.nantian.sc.cloud.admin.api.entity.SysUserRole;
import com.nantian.sc.cloud.admin.api.vo.MenuVO;
import com.nantian.sc.cloud.admin.api.vo.TreeUtil;
import com.nantian.sc.cloud.admin.service.SysMenuService;
import com.nantian.sc.cloud.common.core.constant.CommonConstants;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 * @date 2017/10/31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/menu")
@Api(value = "menu", tags = "菜单管理模块")
public class SysMenuController {
	private final SysMenuService sysMenuService;

	/**
	 * 返回当前用户的树形菜单集合
	 *
	 * @return 当前用户的树形菜单
	 */
	@GetMapping
	public R getUserMenu() {
		// 获取符合条件的菜单
		/*Set<MenuVO> all = new HashSet<>();
		SecurityUtils.getRoles()
				.forEach(roleId -> all.addAll(sysMenuService.findMenuByRoleId(roleId)));
		List<MenuTree> menuTreeList = all.stream()
				.filter(menuVo -> CommonConstants.MENU.equals(menuVo.getType()))
				.map(MenuTree::new)
				.sorted(Comparator.comparingInt(MenuTree::getSort))
				.collect(Collectors.toList());
		return R.ok(TreeUtil.build(menuTreeList, CommonConstants.MENU_TREE_ROOT_ID));*/
		//根据角色编码获取权限
		/*List<MenuTree> all = new ArrayList<>();
		SecurityUtils.getRoles().forEach(roleId -> {
			List<MenuTree> menuTreeList = sysMenuService.findMenuTreeByRoleId(roleId);
			all.addAll(menuTreeList);
		});*/
		//根据角色集合获取去重后的权限
		List<MenuTree> all = sysMenuService.findMenuTreeByRoles(SecurityUtils.getRoles());
		return R.ok(TreeUtil.build(all, CommonConstants.MENU_TREE_ROOT_ID));
	}

	/**
	 * 返回树形菜单集合
	 *
	 * @return 树形菜单
	 */
	@GetMapping(value = "/tree")
	public R getTree() {
		return R.ok(TreeUtil.buildTree(sysMenuService
				.list(Wrappers.<SysMenu>lambdaQuery()
						.eq(SysMenu::getDelFlag, 0)
						.orderByAsc(SysMenu::getSort)), CommonConstants.MENU_TREE_ROOT_ID));
	}

	/**
	 * 返回角色的菜单集合
	 *
	 * @param roleId 角色ID
	 * @return 属性集合
	 */
	@GetMapping("/tree/{roleId}")
	public R getRoleTree(@PathVariable Integer roleId) {
		return R.ok(sysMenuService.findMenuByRoleId(roleId)
				.stream()
				.map(MenuVO::getMenuId)
				.collect(Collectors.toList()));
	}

	/**
	 * 通过ID查询菜单的详细信息
	 *
	 * @param id 菜单ID
	 * @return 菜单详细信息
	 */
	@GetMapping("/{id}")
	public R getById(@PathVariable Integer id) {
		return R.ok(sysMenuService.getById(id));
	}

	/**
	 * 新增菜单
	 *
	 * @param sysMenu 菜单信息
	 * @return success/false
	 */
	@SysLog("新增菜单")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_menu_add')")
	public R save(@Valid @RequestBody SysMenu sysMenu) {
		//return R.ok(sysMenuService.save(sysMenu));
		SysMenu menu = sysMenuService.getById(sysMenu.getMenuId());
		if(menu!=null){
			return R.failed("序号已存在，新增失败！");
		}
		return R.ok(sysMenuService.saveMenu(sysMenu));
	}

	/**
	 * 删除菜单
	 *
	 * @param id 菜单ID
	 * @return success/false
	 */
	@SysLog("删除菜单")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_menu_del')")
	public R removeById(@PathVariable Integer id) {
		//return sysMenuService.removeMenuById(id);
		SysMenu menu = sysMenuService.getById(id);
		if (menu == null) {
			return R.failed("菜单不存在，删除失败！");
		}
		return R.ok(sysMenuService.deleteMenuById(id));
	}

	/**
	 * 更新菜单
	 *
	 * @param sysMenu
	 * @return
	 */
	@SysLog("更新菜单")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_menu_edit')")
	public R update(@Valid @RequestBody SysMenu sysMenu) {
		return R.ok(sysMenuService.updateMenuById(sysMenu));
	}

}
