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.RoleDataScopeEntity;
import com.auth.model.base.dto.RoleDataScopeDto;
import com.auth.model.base.vo.RoleDataScopeVo;
import com.auth.service.base.service.RoleDataScopeService;
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-data-scope")
@RequiredArgsConstructor
public class RoleDataScopeController {

    private final RoleDataScopeService roleDataScopeService;

    @PreAuthorize("auth.decide('roleDataScope:query')")
    @Operation(summary = "分页查询系统角色数据权限范围", description = "分页查询系统角色数据权限范围")
    @GetMapping()
    public Result<PageResult<RoleDataScopeVo>> getRoleDataScopePage(RoleDataScopeDto dto) {
        Page<RoleDataScopeEntity> pageParams = new Page<>(dto.getPageIndex(), dto.getPageSize());
        PageResult<RoleDataScopeVo> pageResult = roleDataScopeService.getRoleDataScopePage(pageParams, dto);
        return Result.success(pageResult);
    }

    @PreAuthorize("auth.decide('roleDataScope:query')")
    @Operation(summary = "根据id查询系统角色数据权限范围详情", description = "根据id查询系统角色数据权限范围详情")
    @GetMapping("{id}")
    public Result<RoleDataScopeVo> getById(@PathVariable("id") Long id) {
        RoleDataScopeVo roleDataScopeVo = roleDataScopeService.getById(id);

        return Result.success(roleDataScopeVo);
    }

    @PreAuthorize("auth.decide('roleDataScope:create')")
    @Operation(summary = "添加系统角色数据权限范围", description = "添加系统角色数据权限范围")
    @PostMapping()
    public Result<String> create(@Valid @RequestBody RoleDataScopeDto dto) {
        roleDataScopeService.create(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @PreAuthorize("auth.decide('roleDataScope:update')")
    @Operation(summary = "更新系统角色数据权限范围", description = "更新系统角色数据权限范围")
    @PutMapping()
    public Result<String> update(@Valid @RequestBody RoleDataScopeDto dto) {
        roleDataScopeService.update(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

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