package com.cool.store.controller;

import com.cool.store.context.CurrentUserHolder;
import com.cool.store.dto.common.IdParamDTO;
import com.cool.store.dto.common.IdsParamDTO;
import com.cool.store.dto.menu.AddMenuDTO;
import com.cool.store.enums.MenuTypeEnum;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zhangchenbiao
 * @FileName: MenuController
 * @Description:
 * @date 2023-06-08 16:58
 */
@RestController
@Slf4j
@Api(tags = "菜单")
public class MenuController {

    @Resource
    private MenuService menuService;

    @ApiOperation("获取用户权限菜单")
    @GetMapping("/menu/getUserMenus")
    public ResponseResult getUserMenus(){
        return ResponseResult.success(menuService.getUserMenus(CurrentUserHolder.getUserId(), CurrentUserHolder.getRoleId()));
    }

    @ApiOperation("获取所有菜单")
    @GetMapping("/menu/getAllMenus")
    public ResponseResult getAllMenus(){
        return ResponseResult.success(menuService.getAllMenus());
    }

    @ApiOperation("新增菜单")
    @PostMapping(path = "/menu/add")
    public ResponseResult addMenu(@RequestBody AddMenuDTO param){
        return ResponseResult.success(menuService.addMenu(param, MenuTypeEnum.MENU));
    }

    @ApiOperation("新增权限")
    @PostMapping(path = "/menu/auth/add")
    public ResponseResult addMenuAuth(@RequestBody AddMenuDTO param){
        return ResponseResult.success(menuService.addMenu(param, MenuTypeEnum.AUTH));
    }

    @ApiOperation("删除菜单或权限")
    @PostMapping(path = "/menu/auth/delete")
    public ResponseResult deleteMenuAuth(@RequestBody IdParamDTO param){
        return ResponseResult.success(menuService.deleteMenuAuth(param.getId()));
    }

    @ApiOperation("排序")
    @PostMapping(path = "/menu/sort")
    public ResponseResult sortMenu(@RequestBody IdsParamDTO param){
        return ResponseResult.success(menuService.sortMenu(param.getIds()));
    }

}
