package com.auth.service.base.controller;

import com.auth.common.model.common.result.PageResult;
import com.auth.common.model.common.result.Result;
import com.auth.common.model.common.result.ResultCodeEnum;
import com.auth.dao.base.entity.RolePermissionEntity;
import com.auth.model.base.dto.RolePermissionDto;
import com.auth.model.base.vo.RolePermissionVo;
import com.auth.service.base.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统角色权限表 前端控制器
 * </p>
 *
 * @author AuthoritySystem
 * @since 2025-07-22 23:14:35
 */
@Tag(name = "系统角色权限表", description = "系统角色权限表相关接口")
@RestController
@RequestMapping("/api/v1/base/role-permission")
@RequiredArgsConstructor
public class RolePermissionController {

    private final RolePermissionService rolePermissionService;

    @PreAuthorize("auth.decide('rolePermission:query')")
    @Operation(summary = "分页查询系统角色权限表", description = "分页查询系统角色权限表")
    @GetMapping()
    public Result<PageResult<RolePermissionVo>> getRolePermissionPage(RolePermissionDto dto) {
        Page<RolePermissionEntity> pageParams = new Page<>(dto.getPageIndex(), dto.getPageSize());
        PageResult<RolePermissionVo> pageResult = rolePermissionService.getRolePermissionPage(pageParams, dto);
        return Result.success(pageResult);
    }

    @PreAuthorize("auth.decide('rolePermission:query')")
    @Operation(summary = "根据id查询系统角色权限表详情", description = "根据id查询系统角色权限表详情")
    @GetMapping("{id}")
    public Result<RolePermissionVo> getById(@PathVariable("id") Long id) {
        RolePermissionVo rolePermissionVo = rolePermissionService.getById(id);

        return Result.success(rolePermissionVo);
    }

    @PreAuthorize("auth.decide('rolePermission:create')")
    @Operation(summary = "添加系统角色权限表", description = "添加系统角色权限表")
    @PostMapping()
    public Result<String> create(@Valid @RequestBody RolePermissionDto dto) {
        rolePermissionService.create(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @PreAuthorize("auth.decide('rolePermission:update')")
    @Operation(summary = "更新系统角色权限表", description = "更新系统角色权限表")
    @PutMapping()
    public Result<String> update(@Valid @RequestBody RolePermissionDto dto) {
        rolePermissionService.update(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @PreAuthorize("auth.decide('rolePermission:delete')")
    @Operation(summary = "删除系统角色权限表", description = "删除系统角色权限表")
    @DeleteMapping()
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        rolePermissionService.batchDelete(ids);
        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}