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.FileEntity;
import com.auth.model.base.dto.FileDto;
import com.auth.model.base.vo.FileVo;
import com.auth.service.base.service.FileService;
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/file")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    @PreAuthorize("auth.decide('file:query')")
    @Operation(summary = "分页查询系统文件存储", description = "分页查询系统文件存储")
    @GetMapping()
    public Result<PageResult<FileVo>> getFilePage(FileDto dto) {
        Page<FileEntity> pageParams = new Page<>(dto.getPageIndex(), dto.getPageSize());
        PageResult<FileVo> pageResult = fileService.getFilePage(pageParams, dto);
        return Result.success(pageResult);
    }

    @PreAuthorize("auth.decide('file:query')")
    @Operation(summary = "根据id查询系统文件存储详情", description = "根据id查询系统文件存储详情")
    @GetMapping("{id}")
    public Result<FileVo> getById(@PathVariable("id") Long id) {
        FileVo fileVo = fileService.getById(id);

        return Result.success(fileVo);
    }

    @PreAuthorize("auth.decide('file:create')")
    @Operation(summary = "添加系统文件存储", description = "添加系统文件存储")
    @PostMapping()
    public Result<String> create(@Valid @RequestBody FileDto dto) {
        fileService.create(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @PreAuthorize("auth.decide('file:update')")
    @Operation(summary = "更新系统文件存储", description = "更新系统文件存储")
    @PutMapping()
    public Result<String> update(@Valid @RequestBody FileDto dto) {
        fileService.update(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @PreAuthorize("auth.decide('file:delete')")
    @Operation(summary = "删除系统文件存储", description = "删除系统文件存储")
    @DeleteMapping()
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        fileService.batchDelete(ids);
        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}