package com.hs.admin.controller;

import com.hs.admin.common.Result;
import com.hs.admin.common.annotations.CurrentUser;
import com.hs.admin.common.annotations.Token;
import com.hs.admin.model.SysMenu;
import com.hs.admin.model.reqmodel.MenuReq;
import com.hs.admin.model.respmodel.SysMenuList;
import com.hs.admin.model.respmodel.SysRoleMenuRsList;
import com.hs.admin.model.respmodel.SysUserMenuRsList;
import com.hs.admin.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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "菜单API", produces = "produces", consumes = "consumes", protocols = "protocols")
@RequestMapping("/menu")
@RestController
public class MenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @PostMapping("parentall")
    @Token
    @ApiOperation("查询菜单列表及其子级列表")
    public Object getParentAll(@RequestBody MenuReq.GetMenuReq menu) throws Exception {
        List<SysMenuList> list = sysMenuService.getParentAll(menu);
        return Result.success(list);
    }

    @PostMapping("all")
    @Token
    @ApiOperation("查询菜单列表")
    public Object getAll() throws Exception {
        List<SysMenu> list = sysMenuService.getAll();
        return Result.success(list);
    }

    @PostMapping("info")
    @Token
    @ApiOperation("查询菜单详细信息")
    public Object getById(@RequestBody MenuReq.InfoMenuReq menu) throws Exception {
        SysMenu info = sysMenuService.info(menu);
        return Result.success(info);
    }

    @PostMapping("add")
    @Token
    @ApiOperation("添加菜单")
    public Object addUser(@ApiIgnore @CurrentUser Integer userId,@Valid @RequestBody MenuReq.AddMenuReq menu) throws Exception {
        boolean result = sysMenuService.addMenu(Long.valueOf(userId),menu);
        if(result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("delete")
    @Token
    @ApiOperation("删除菜单")
    public Object addUser(@RequestBody MenuReq.DeleteMenuReq menu) throws Exception {
        boolean result = sysMenuService.deleteMenu(menu);
        if(result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("update")
    @Token
    @ApiOperation("修改菜单")
    public Object addUser(@Valid @RequestBody MenuReq.UpdateMenuReq menu) throws Exception {
        boolean result = sysMenuService.updateMenu(menu);
        if(result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("usermenu")
    @Token
    @ApiOperation("查询用户菜单")
    public Object getUserMenu(@RequestBody MenuReq.GetUserMenuReq menu) throws Exception {
        List<SysUserMenuRsList> list = sysMenuService.getUserMenu(menu.getUserId());
        return Result.success(list);
    }

    @PostMapping("saveusermenu")
    @Token
    @ApiOperation("保存用户菜单")
    public Object saveUserMenu(@ApiIgnore @CurrentUser Integer userId,@RequestBody MenuReq.SaveUserMenuReq menu) throws Exception {
        boolean result = sysMenuService.saveUserMenu(Long.valueOf(userId),menu);
        if(result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("rolemenu")
    @Token
    @ApiOperation("查询角色菜单")
    public Object getRoleMenu(@RequestBody MenuReq.GetRoleMenuReq menu) throws Exception {
        List<SysRoleMenuRsList> list = sysMenuService.getRoleMenu(menu.getRoleId());
        return Result.success(list);
    }

    @PostMapping("saverolemenu")
    @Token
    @ApiOperation("保存角色菜单")
    public Object saveRoleMenu(@ApiIgnore @CurrentUser Integer userId,@RequestBody MenuReq.SaveRoleMenuReq menu) throws Exception {
        boolean result = sysMenuService.saveRoleMenu(Long.valueOf(userId),menu);
        if(result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("updateSort")
    @Token
    @ApiOperation("修改排序")
    public Object updateSort(@ApiIgnore @CurrentUser Integer userId,@RequestBody MenuReq.UpdateSortReq menu) throws Exception {
        boolean result = sysMenuService.updateSort(Long.valueOf(userId),menu);
        if(result)
            return Result.success(null);
        else
            return Result.error();
    }
}
