package com.ruoyi.web.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.ImgUpload;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.MallGoodsClass;
import com.ruoyi.mall.domain.MallGoodsGoods;
import com.ruoyi.mall.domain.MallGoodsProduct;
import com.ruoyi.mall.domain.vo.GoodsProductVO;
import com.ruoyi.mall.domain.vo.IndexGoodsVO;
import com.ruoyi.mall.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ApiCommonController
 * @Description 工具方法接口
 * @Author changwj
 * @Date 2021/11/29 10:17 AM
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/cms")
@ResponseBody
public class ApiCommonController {

    /**
     * 单个上传
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/common/upload")
    @ResponseBody
    @CrossOrigin
    public ApiResult upload(MultipartFile file, HttpServletRequest request) {
        String fileName = null;
        try {
            BufferedImage image = ImageIO.read(file.getInputStream());
            if (image != null) {//如果image=null 表示上传的不是图片格式
//			    System.out.println(image.getWidth());//获取图片宽度，单位px
//			    System.out.println(image.getHeight());//获取图片高度，单位px
                fileName = "IMG_" + image.getWidth() + "_" + image.getHeight() + "@";
            }else {
                fileName = "";
            }
            String imagePath = new ImgUpload().upload(file.getBytes(),
                    fileName);
            if (StringUtils.isBlank(imagePath)) {
                return  ApiResult.error("上传失败");
            }
            return ApiResult.ok().put("data", imagePath);
        } catch (Exception e) {
            return ApiResult.error();
        }
    }

}
