package cn.meowrain.aioj.backend.fileservice.controller;

import cn.meowrain.aioj.backend.fileservice.dao.entity.AttachmentDO;
import cn.meowrain.aioj.backend.fileservice.dto.resp.HashCheckRespDTO;
import cn.meowrain.aioj.backend.fileservice.service.AttachmentService;
import cn.meowrain.aioj.backend.framework.core.web.Result;
import cn.meowrain.aioj.backend.framework.core.web.Results;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 通用附件表 前端控制器
 * </p>
 *
 * @author meowrain
 * @since 2026-01-10
 */
@RestController
@RequestMapping("/v1/file")
@RequiredArgsConstructor
public class AttachmentController {

    private final AttachmentService attachmentService;

    @Operation(summary = "通用文件上传组件")
    @PostMapping("/upload")
    public Result<AttachmentDO> uploadFile(@Parameter(description = "上传的文件",
                                                   content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE))
                                           @RequestParam("file") MultipartFile file,
                                           @Parameter(description = "文件哈希值")
                                           @RequestParam(value = "hash", required = false) String hash) {
        return Results.success(attachmentService.upload(file, hash));
    }

    @Operation(summary = "哈希是否存在")
    @GetMapping("/check")
    public Result<HashCheckRespDTO> checkHash(@Parameter(description = "文件哈希值") @RequestParam("hash") String hash) {
        return Results.success(attachmentService.checkHash(hash));
    }

    /**
     * 分页查询通用附件表列表
     */
    @Operation(summary = "分页查询通用附件表列表")
    @GetMapping("/page")
    public Result<Page<AttachmentDO>> page(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size) {
        Page<AttachmentDO> page = new Page<>(current, size);
        return Results.success(attachmentService.page(page));
    }

    /**
     * 查询所有通用附件表列表
     */
    @Operation(summary = "查询所有通用附件表列表")
    @GetMapping("/list")
    public Result<List<AttachmentDO>> list() {
        return Results.success(attachmentService.list());
    }

    /**
     * 根据ID查询通用附件表详情
     */
    @Operation(summary = "根据ID查询通用附件表详情")
    @GetMapping("/{id}")
    public Result<AttachmentDO> getById(
            @Parameter(description = "通用附件表ID") @PathVariable Long id) {
        return Results.success(attachmentService.getById(id));
    }

    /**
     * 新增通用附件表
     */
    @Operation(summary = "新增通用附件表")
    @PostMapping
    public Result<Boolean> save(@RequestBody AttachmentDO entity) {
        return Results.success(attachmentService.save(entity));
    }

    /**
     * 修改通用附件表
     */
    @Operation(summary = "修改通用附件表")
    @PutMapping
    public Result<Boolean> update(@RequestBody AttachmentDO entity) {
        return Results.success(attachmentService.updateById(entity));
    }

    /**
     * 删除通用附件表
     */
    @Operation(summary = "删除通用附件表")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(
            @Parameter(description = "通用附件表ID") @PathVariable Long id) {
        return Results.success(attachmentService.removeById(id));
    }

    /**
     * 批量删除通用附件表
     */
    @Operation(summary = "批量删除通用附件表")
    @DeleteMapping("/batch")
    public Result<Boolean> deleteBatch(@RequestBody List<Long> ids) {
        return Results.success(attachmentService.removeByIds(ids));
    }

}
