package com.ytpm.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ytpm.attach.Attach;
import com.ytpm.attach.Image;
import com.ytpm.handle.CustomerException;
import com.ytpm.oss.OssProperties;
import com.ytpm.oss.OssUtil;
import com.ytpm.service.AttachService;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j(topic = "attach-service")
@Service
public class AttachServiceImpl implements AttachService {

    @Autowired
    OssProperties properties;

    @Override
    public Attach uploadFile(MultipartFile multipartFile) {

        String originFilename = multipartFile.getOriginalFilename();
        Attach attach = new Attach();
        String ossName = upload(multipartFile, properties, false);
        attach.setName(originFilename);
        attach.setOssName(ossName);
        attach.setUrl(OssUtil.getUrl(ossName, properties));
        attach.setFileSize(multipartFile.getSize());
        return attach;
    }

    @Override
    public Image uploadImage(MultipartFile multipartFile) {
        String ossName = upload(multipartFile, properties, true);

        Image image = new Image();
        image.setOssName(ossName);
        image.setUrl(OssUtil.getUrl(ossName, properties));
        return image;
    }

    /**
     * 文件上传
     *
     * @param multipartFile 文件
     * @param properties    存储配置
     * @param isImage       是否为图片
     * @return 文件ossName
     */
    private String upload(MultipartFile multipartFile, OssProperties properties, Boolean isImage) {
        String originFilename = multipartFile.getOriginalFilename();
        String suffix = originFilename.substring(originFilename.lastIndexOf("."));

        if (isImage) {
            suffix = suffix.toUpperCase();
            //图片
            if (!".JPEG,.JPG,.PNG".contains(suffix)||suffix.equals(".")||suffix.equals(",")) {
                throw new CustomerException("格式类型错误");
            }
        }

        String folder = System.getProperty("java.io.tmpdir");
        File file = new File(folder + File.separator + IdUtil.fastSimpleUUID() + suffix);

        try {
            multipartFile.transferTo(file);
            //压缩图片
            compressFile(file, suffix);
        } catch (IOException e) {
            log.error("文件上传失败：", e);
            throw new CustomerException(e.getMessage());
        }

        return OssUtil.upload(file, properties);
    }

    private void compressFile(File file, String suffix) {
        //2、大于2M的图片需要压缩大小
        long fileSize = file.length();
        if (".JPEG,.JPG,.PNG".contains(suffix) && fileSize > 1024 * 1024 * 2) {
            try {
                Thumbnails.of(file)
                        .scale(0.2)
//                        .outputQuality(0.2f)
                        .toFile(file);
            } catch (IOException e) {
                log.error("压缩报错了：", e);
            }
        }
    }
}
