package org.springblade.file.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.file.pojo.dto.FileTaskCreateDTO;
import org.springblade.file.pojo.dto.FileTaskUpdateDTO;
import org.springblade.file.pojo.dto.IdsDTO;
import org.springblade.file.pojo.vo.FileTaskUpdateVO;
import org.springblade.file.service.IFileTaskService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: FileTaskController
 * @Author: zhaowei
 * @Date: 2024/8/28
 */
@Valid
@NonDS
@RestController
@AllArgsConstructor
@RequestMapping("/fileTask")
@Tag(name = "文件任务", description = "文件任务")
public class FileTaskController extends BladeController {

    private final IFileTaskService fileTaskService;

    @GetMapping("/queryFileTask")
    @Operation(summary = "根据文件的MD5值查询文件任务", description = "文件的MD5值检查分段，参数名：md5")
    public R<FileTaskUpdateVO> queryFileTask(@RequestParam(value = "md5") String md5) {
        FileTaskUpdateVO fileTask = fileTaskService.queryFileTask(null, md5);
        return R.data(fileTask);
    }

    @ApiLog("上传任务-创建文件任务")
    @PostMapping("/createFileTask")
    @Operation(summary = "创建文件任务", description = "创建文件任务")
    public R<FileTaskUpdateVO> createFileTask(@RequestBody FileTaskCreateDTO param) {
        Long id = fileTaskService.createFileTask(param);
        return R.data(fileTaskService.queryFileTask(id, null));
    }

    @ApiLog("上传任务-更新文件任务进度")
    @PostMapping("/updateFileTask")
    @Operation(summary = "更新文件任务进度", description = "更新文件任务进度")
    public R<FileTaskUpdateVO> updateFileTask(@RequestBody FileTaskUpdateDTO param) {
        return R.data(fileTaskService.updateFileTask(param));
    }

    @ApiLog("上传任务-修改文件任务状态为失败")
    @PostMapping("/updateFailed")
    @Operation(summary = "更新文件任务为失败", description = "更新文件任务为失败")
    public R<FileTaskUpdateVO> updateFailed(@NotNull(message = "文件任务id不能为空") Long id) {
        return R.data(fileTaskService.updateFailed(id));
    }

    @ApiLog("上传任务-暂停文件任务")
    @PostMapping("/updatePaused")
    @Operation(summary = "更新文件任务为暂停", description = "更新文件任务为暂停")
    public R<Boolean> updatePaused(@Validated @RequestBody IdsDTO param) {
        return R.status(fileTaskService.updatePaused(param.getIds()));
    }

    @GetMapping("/getUploadUrl")
    @Parameters({
            @Parameter(name = "id", description = "文件任务id", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
            @Parameter(name = "partNumber", description = "分段编号", in = ParameterIn.QUERY, schema = @Schema(type = "int"))
    })
    @Operation(summary = "根据文件任务id查询上传链接", description = "根据文件任务id查询上传链接")
    public R<String> getUploadUrl(Long id, Integer partNumber) {
        String uploadUrl = fileTaskService.getUploadUrl(id, partNumber);
        return R.data(uploadUrl);
    }
}
