package com.qiaoba.module.system.controller;

import com.qiaoba.api.system.entity.SysMenu;
import com.qiaoba.api.system.entity.param.SysMenuParam;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.module.system.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Size;
import java.util.List;

/**
 * 菜单管理 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/5 10:06
 */
@Validated
@RestController
@RequestMapping("/system/menu")
@RequiredArgsConstructor
@Tag(name = "菜单管理")
public class SysMenuController {

    private final SysMenuService sysMenuService;

    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public AjaxResult list(@Validated SysMenuParam param) {
        List<SysMenu> menus = sysMenuService.selectList(param);
        return AjaxResult.success(menus);
    }

    @PreAuthorize("hasAuthority('system:menu:query')")
    @GetMapping(value = "/{menuId}")
    @Operation(summary = "获取详情")
    public AjaxResult getInfo(@Size(min = 19, max = 20, message = "ID不合法")@PathVariable String menuId) {
        return AjaxResult.success(sysMenuService.selectById(menuId));
    }

    @PreAuthorize("hasAuthority('system:menu:add')")
    @PostMapping
    @Operation(summary = "添加菜单")
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody SysMenu menu) {
        return AjaxResult.toAjax(sysMenuService.insert(menu));
    }

    @PreAuthorize("hasAuthority('system:menu:edit')")
    @PutMapping
    @Operation(summary = "修改菜单")
    public AjaxResult edit(@Validated(EditGroup.class) @RequestBody SysMenu menu) {
        return AjaxResult.toAjax(sysMenuService.updateById(menu));
    }

    @PreAuthorize("hasAuthority('system:menu:remove')")
    @DeleteMapping("/{menuId}")
    @Operation(summary = "删除菜单")
    public AjaxResult remove(@PathVariable("menuId") String menuId) {
        return AjaxResult.toAjax(sysMenuService.deleteById(menuId));
    }

    @GetMapping("/tree-select")
    @Operation(summary = "构建菜单树")
    public AjaxResult treeSelect(SysMenuParam param) {
        List<SysMenu> menus = sysMenuService.selectList(param);
        return AjaxResult.success(sysMenuService.buildMenuTree(menus));
    }

    @GetMapping(value = "/role-menu-tree-select/{roleId}")
    @Operation(summary = "角色菜单树")
    public AjaxResult roleMenuTreeSelect(@PathVariable("roleId") String roleId) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", sysMenuService.selectMenuIdsByRoleId(roleId));
        ajax.put("menus", sysMenuService.buildMenuTree(sysMenuService.selectList(new SysMenuParam())));
        return ajax;
    }
}
