package com.ruoyi.hywz.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.hywz.utils.BaseFile;
import com.ruoyi.hywz.utils.OssUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author: jxh
 * @description:
 * @create: 2024/4/8
 */
@RestController
@RequestMapping("adsMatHyxtFileUploadD")
@Api(value = "adsMatHyxtFileUploadD", tags = "文件上传")
public class AdsMatHyxtFileUploadDController {

    @ApiOperation(value = "文件上传（支持多文件、多格式上传）", notes = "文件上传")
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        List<BaseFile> fileVoList = new ArrayList<>();
        try {
            MultipartFile file = files.get(0);
            String originalFilename = file.getOriginalFilename();
            String suffer = null;
            if (originalFilename != null) {
                suffer = originalFilename.substring(originalFilename.lastIndexOf("."));
            }
            Random random = new Random();
            int randomNumber = random.nextInt(89999) + 10000;
            String fileName = "cwzjgk"+ System.currentTimeMillis() + randomNumber + suffer;
            BaseFile baseFile = OssUploadService.ossUpload(file, fileName);
            fileVoList.add(baseFile);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("上传异常");
        }
        return AjaxResult.success(fileVoList);
    }

}
