package shop.alien.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import shop.alien.entity.result.R;
import shop.alien.lawyer.util.FileUploadUtil;

import java.util.List;

/**
 * 二期-文件上传
 *
 * @author ssk
 * @version 1.0
 * @date 2024/12/6 15:30
 */
@Slf4j
@Api(tags = {"二期-文件上传"})
@ApiSort(6)
@CrossOrigin
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileUploadController {

    private final FileUploadUtil fileUpload;

    @ApiOperation("单个文件上传(图片或视频,返回路径)")
    @ApiOperationSupport(order = 1)
    @PostMapping("/upload")
    public R<String> upload(@RequestParam("file") MultipartFile file) {
        log.info("StoreFileUploadController.upload fileName：{}", file.getOriginalFilename());
        return R.data(fileUpload.uploadOneFile(file));
    }

    @ApiOperation("多个文件上传(图片或视频,视频会截取第一秒图片,返回路径)")
    @ApiOperationSupport(order = 2)
    @PostMapping("/uploadMore")
    public R<List<String>> uploadMore(MultipartRequest multipartRequest) {
        log.info("StoreFileUploadController.upload uploadMore：{}", multipartRequest);
        return R.data(fileUpload.uploadMoreFile(multipartRequest));
    }

    @ApiOperation(value = "图片上传(其实也可以传视频,不会截图), 返回图片id", notes = "文件格式: file_0,file_1, list格式: [{\"storeId\":1,\"imgType\":3,\"imgDescription\":\"菜品1\",\"imgSort\":1},{\"storeId\":1,\"imgType\":3,\"imgDescription\":\"菜品2\",\"imgSort\":2}]")
    @ApiOperationSupport(order = 3)
    @PostMapping("/uploadImg")
    public R<List<Integer>> uploadImg(MultipartRequest multipartRequest, @RequestParam("list") String list) {
        return R.data(fileUpload.uploadImg(multipartRequest, list));
    }

    @ApiOperation("上传app(格式包含apk,ipa,wgt,返回路径)")
    @ApiOperationSupport(order = 4)
    @PostMapping("/uploadApp")
    public R<String> uploadApp(@RequestParam("file") MultipartFile file) {
        log.info("StoreFileUploadController.uploadApp fileName：{}", file.getOriginalFilename());
        return R.data(fileUpload.uploadApp(file));
    }

}
