package com.user.service.web.controller;


import com.base.orbit.constant.UserConstants;
import com.base.orbit.domain.AjaxResult;
import com.base.orbit.domain.enity.SysMenu;
import com.base.orbit.utils.DateUtils;
import com.user.service.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单信息
 *
 * @author xj
 */
@RestController
@RequestMapping("/api/menu")
@Api(tags = "菜单管理")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 获取菜单列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取菜单列表", httpMethod = "GET", notes = "获取菜单列表")
    public AjaxResult list(SysMenu menu) {
        List<SysMenu> menus = sysMenuService.selectMenuList(menu, getUserId());
        return AjaxResult.success(sysMenuService.buildMenuTreeMenu(menus));
    }


    /**
     * 根据菜单编号获取详细信息
     */
    @GetMapping(value = "/{menuId}")
    @ApiOperation(value = "根据菜单编号获取详细信息", httpMethod = "GET", notes = "根据菜单编号获取详细信息")
    public AjaxResult getInfo(@PathVariable Long menuId) {
        return AjaxResult.success(sysMenuService.selectMenuById(menuId));
    }

    /**
     * 新增菜单
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增菜单", httpMethod = "POST", notes = "新增菜单")
    public AjaxResult add(@Validated @RequestBody SysMenu menu) {
        if (UserConstants.NOT_UNIQUE.equals(sysMenuService.checkMenuNameUnique(menu))) {
            return AjaxResult.error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        menu.setCreateBy(getUsername());
        menu.setCreateTime(DateUtils.getNowDate());
        return toAjax(sysMenuService.insertMenu(menu));
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    @ApiOperation(value = "删除菜单", httpMethod = "DELETE", notes = "删除菜单")
    public AjaxResult remove(@PathVariable("menuId") Long menuId) {
        if (sysMenuService.hasChildByMenuId(menuId)) {
            return AjaxResult.error("存在子菜单,不允许删除");
        }
        if (sysMenuService.checkMenuExistRole(menuId)) {
            return AjaxResult.error("菜单已分配,不允许删除");
        }
        return toAjax(sysMenuService.deleteMenuById(menuId));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    @ApiOperation(value = "获取菜单下拉树列表", httpMethod = "GET", notes = "获取菜单下拉树列表")
    public AjaxResult treeselect(SysMenu menu) {
        List<SysMenu> menus = sysMenuService.selectMenuList(menu, getUserId());
        return AjaxResult.success(sysMenuService.buildMenuTreeSelect(menus));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    @ApiOperation(value = "加载对应角色菜单列表树", httpMethod = "GET", notes = "加载对应角色菜单列表树")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        //  用户可见菜单
        List<SysMenu> menus = sysMenuService.selectMenuList(getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", sysMenuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", sysMenuService.buildMenuTreeSelect(menus));
        return ajax;
    }

    /**
     * 加载对应角色菜单编号数组
     */
    @GetMapping(value = "/roleMenuArr/{roleId}")
    @ApiOperation(value = "加载对应角色菜单编号数组", httpMethod = "GET", notes = "加载对应角色菜单编号数组")
    public AjaxResult roleMenuArr(@PathVariable("roleId") Long roleId) {
        List<Long> menuIds = sysMenuService.selMenuIdByRoleId(roleId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("menuIds", menuIds);
        return ajax;
    }
}
