package com.ruoyi.web.controller.api;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.ImgUpload;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.commons.collections4.MapUtils;
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.Map;

/**
 * @ClassName ApiCommonController
 * @Description 图片上传接口
 * @Author LCL
 * @Date 2023/07/11
 * @Version 1.0
 **/

@RestController
@RequestMapping("/api/common")
@ResponseBody
public class ApiCommonController {

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 单个上传
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/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();
        }
    }

    /**
     * 单个上传普通文件
     * @param map
     * @param request
     * @return
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    @CrossOrigin
    public AjaxResult uploadFile(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        String fileName = null;
        try {
            String base64 = MapUtils.getString(map, "base64");
            if (StringUtils.isBlank(base64)){
                throw new ServiceException("请上传数据");
            }
            String key = IdUtils.fastSimpleUUID() + ".mp3";
            byte[] bytes = java.util.Base64.getDecoder().decode(base64);
            String imagePath = new ImgUpload().uploadMp3(bytes,
                    fileName, key);
            if (StringUtils.isBlank(imagePath)) {
                return  AjaxResult.error("上传失败");
            }
            return AjaxResult.success().put("data", imagePath);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    @PostMapping("/uploadVideo")
    @ResponseBody
    @CrossOrigin
    public AjaxResult uploadVideo(MultipartFile file) {
        String fileName = null;
        try {

            String key = IdUtils.fastSimpleUUID() + ".mp4";
            byte[] bytes = file.getBytes();
            String str = new ImgUpload().uploadMp3(bytes,
                    fileName, key);
            if (StringUtils.isBlank(str)) {
                return  AjaxResult.error("上传失败");
            }
            return AjaxResult.success().put("data", str);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }
    //
    //    /**
    // * base64上传
    // * @param base64 base64
    // * @return 结果
    // */
    //public String uploadBase64(String base64, String key) {
    //    String url = "";
    //    byte[] bytes = null;
    //    try {
    //        File file = new File(key);
    //        // 将Base64字符串解码为字节数组
    //        bytes = java.util.Base64.getDecoder().decode(base64);
    //        // 创建新文件并将字节数组写入其中
    //        FileOutputStream outputStream = new FileOutputStream(file);
    //        outputStream.write(bytes);
    //        outputStream.close();
    //        url = this.uploadByte(bytes, key);
    //    }catch (Exception e){
    //        throw new ServiceException("报告单保存失败");
    //    }
    //    return url;
    //}

    /**
     * 获取字典值
     */
    @GetMapping("/getDictType")
    public ApiResult getDictType(@RequestParam String dictType){
        try{
            return ApiResult.ok().put("dictData",dictTypeService.selectDictDataByType(dictType));
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.error("服务器异常");
        }
    }
}
