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.RoleEntity;
import com.auth.model.base.dto.RoleDto;
import com.auth.model.base.vo.RoleVo;
import com.auth.service.base.service.RoleService;
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")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @PreAuthorize("auth.decide('role:query')")
    @Operation(summary = "分页查询系统角色表", description = "分页查询系统角色表")
    @GetMapping()
    public Result<PageResult<RoleVo>> getRolePage(RoleDto dto) {
        Page<RoleEntity> pageParams = new Page<>(dto.getPageIndex(), dto.getPageSize());
        PageResult<RoleVo> pageResult = roleService.getRolePage(pageParams, dto);
        return Result.success(pageResult);
    }

    @PreAuthorize("auth.decide('role:query')")
    @Operation(summary = "根据id查询系统角色表详情", description = "根据id查询系统角色表详情")
    @GetMapping("{id}")
    public Result<RoleVo> getById(@PathVariable("id") Long id) {
        RoleVo roleVo = roleService.getById(id);

        return Result.success(roleVo);
    }

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

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

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