package com.aiotagro.cattletrade.business.controller;

import com.aiotagro.cattletrade.business.dto.FileDto;
import com.aiotagro.common.core.utils.TencentCloudUtil;
import com.aiotagro.common.core.web.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

@RestController
@RequestMapping("/common")
public class CommonControl {


    private static final Logger logger = LoggerFactory.getLogger(CommonControl.class);

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) {
        logger.info("文件上传...");
        try {
            String fileUrl = TencentCloudUtil.upload(file);
            FileDto fileDto = new FileDto();
            fileDto.setSrc(fileUrl);
            return AjaxResult.success("上传成功！", fileDto);
        } catch (Exception e) {
            logger.error("图片上传异常", e);
        }
        return AjaxResult.error("上传失败！");
    }


}
