package com.backendsys.modules.system.controller;

import com.backendsys.exception.CustException;
import com.backendsys.modules.common.aspect.SysLog;
import com.backendsys.modules.common.utils.Result;
import com.backendsys.modules.system.entity.SysUserRole;
import com.backendsys.modules.system.entity.SysUserRoleMenu;
import com.backendsys.modules.system.service.SysUserRoleMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@Validated
@RestController
@Tag(name = "系统用户角色菜单")
public class SysUserRoleMenuController {

    @Autowired
    private SysUserRoleMenuService sysUserRoleMenuService;

    @Operation(summary = "获取系统用户角色菜单列表")
    @PreAuthorize("@sr.hasPermission('4.3')")
    @GetMapping("/api/system/user/getUserRoleMenu")
    public Result getUserRoleMenu() {
        return Result.success().put("data", Map.of("list", sysUserRoleMenuService.selectUserRoleMenuList(null, null)));
    }

    @Operation(summary = "获取系统用户菜单详情")
    @PreAuthorize("@sr.hasPermission('4.3.1')")
    @GetMapping("/api/system/user/getUserRoleMenuDetail")
    public Result getUserRoleMenuDetail(@Validated(SysUserRoleMenu.Detail.class) SysUserRoleMenu sysUserRoleMenu) {
        return Result.success().put("data", sysUserRoleMenuService.selectUserRoleMenuDetail(sysUserRoleMenu));
    }

    @SysLog("创建系统用户角色菜单")
    @Operation(summary = "创建系统用户角色菜单")
    @PreAuthorize("@sr.hasPermission('4.3.2')")
    @PostMapping("/api/system/user/createUserRoleMenu")
    public Result createUserRoleMenu(@Validated(SysUserRoleMenu.Create.class) @RequestBody SysUserRoleMenu sysUserRoleMenu) {
        return Result.success().put("data", sysUserRoleMenuService.insertUserRoleMenu(sysUserRoleMenu));
    }

    @SysLog("更新系统用户角色菜单")
    @Operation(summary = "更新系统用户角色菜单")
    @PreAuthorize("@sr.hasPermission('4.3.3')")
    @PutMapping("/api/system/user/updateUserRoleMenu")
    public Result updateUserRoleMenu(@Validated(SysUserRoleMenu.Update.class) @RequestBody SysUserRoleMenu sysUserRoleMenu) {
        return Result.success().put("data", sysUserRoleMenuService.updateUserRoleMenu(sysUserRoleMenu));
    }

    @SysLog("删除系统用户角色菜单")
    @Operation(summary = "删除系统用户角色菜单")
    @PreAuthorize("@sr.hasPermission('4.3.4')")
    @DeleteMapping("/api/system/user/deleteUserRoleMenu")
    public Result deleteUserRoleMenu(@Validated(SysUserRoleMenu.Delete.class) @RequestBody SysUserRoleMenu sysUserRoleMenu) {
        return Result.success().put("data", sysUserRoleMenuService.deleteUserRoleMenu(sysUserRoleMenu));
    }

}
