package com.seer.teach.mall.sys.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.response.OssImageUploadResp;
import com.seer.teach.common.service.CommonFileService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;


@RequestMapping("/common")
@RestController
@Tag(name = "通用Controller")
@Validated
@RequiredArgsConstructor
public class AdminUploadController {

    private final CommonFileService commonFileService;

    @PostMapping("/upload")
    @Operation(summary = "文件上传")
    @LogPrint
    public ResultBean<OssImageUploadResp> uploadImg(@RequestParam("file") @NotNull(message = "文件不能为空") MultipartFile file) throws IOException {
        OssImageUploadResp result = commonFileService.uploadImage(StpUtil.getLoginIdAsInt(),file);
        return ResultBean.success(result);
    }

    @PostMapping("/multi-upload")
    @Operation(summary = "多文件上传")
    @LogPrint
    public ResultBean<List<OssImageUploadResp>> multiUploadImg(@RequestParam("file") @NotNull(message = "文件不能为空") MultipartFile[] files) throws FileUploadException {
        List<OssImageUploadResp> result = commonFileService.multiUploadImg(StpUtil.getLoginIdAsInt(),files);
        return ResultBean.success(result);
    }
}