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.AuthLogEntity;
import com.auth.model.base.dto.AuthLogDto;
import com.auth.model.base.vo.AuthLogVo;
import com.auth.service.base.service.AuthLogService;
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/auth-log")
@RequiredArgsConstructor
public class AuthLogController {

    private final AuthLogService authLogService;

    @PreAuthorize("auth.decide('authLog:query')")
    @Operation(summary = "分页查询系统授权日志表", description = "分页查询系统授权日志表")
    @GetMapping()
    public Result<PageResult<AuthLogVo>> getAuthLogPage(AuthLogDto dto) {
        Page<AuthLogEntity> pageParams = new Page<>(dto.getPageIndex(), dto.getPageSize());
        PageResult<AuthLogVo> pageResult = authLogService.getAuthLogPage(pageParams, dto);
        return Result.success(pageResult);
    }

    @PreAuthorize("auth.decide('authLog:query')")
    @Operation(summary = "根据id查询系统授权日志表详情", description = "根据id查询系统授权日志表详情")
    @GetMapping("{id}")
    public Result<AuthLogVo> getById(@PathVariable("id") Long id) {
        AuthLogVo authLogVo = authLogService.getById(id);

        return Result.success(authLogVo);
    }

    @PreAuthorize("auth.decide('authLog:create')")
    @Operation(summary = "添加系统授权日志表", description = "添加系统授权日志表")
    @PostMapping()
    public Result<String> create(@Valid @RequestBody AuthLogDto dto) {
        authLogService.create(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @PreAuthorize("auth.decide('authLog:update')")
    @Operation(summary = "更新系统授权日志表", description = "更新系统授权日志表")
    @PutMapping()
    public Result<String> update(@Valid @RequestBody AuthLogDto dto) {
        authLogService.update(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

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