package com.yn.bftl.thirdparty.modules.business.service.impl;

import com.yn.bftl.common.common.exception.YnceError;
import com.yn.bftl.common.modules.base.vo.MetaFileVO;
import com.yn.bftl.common.modules.meta.entity.MetaFile;
import com.yn.bftl.thirdparty.common.exception.YnceErrorException;
import com.yn.bftl.thirdparty.modules.business.repository.MetaFileRepository;
import com.yn.bftl.thirdparty.modules.business.service.MetaFileService;
import com.yn.bftl.thirdparty.modules.qiniu.util.QiniuFileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * 文件服务实现
 *
 * @author huabiao
 * @create 2022/12/8  10:50
 **/
@Service
public class MetaFileServiceImpl implements MetaFileService {

    @Value("${file.basePath}")
    private String basePath;

    @Value("${file.urlPath}")
    private String urlPath;

    @Resource
    private MetaFileRepository metaFileRepository;

    @Resource
    private QiniuFileUtils qiniuFileUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MetaFile fileUpload(MultipartFile file) {
        try {
            String last = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
            if(last.equalsIgnoreCase("HEIC")){
                last = "jpg";
            }
            File localPathFile = new File(basePath);
            if (!localPathFile.exists()) {
                localPathFile.mkdirs();
            }
            String fileName = System.nanoTime() + "." + last;
            file.transferTo(new java.io.File(localPathFile, fileName));

            MetaFile metaFile = new MetaFile();
            metaFile.setFileName(fileName);
            metaFile.setFilePath(fileName);
            metaFile.setFileSize(file.getSize());
            metaFile.setSizeText(file.getSize() + " KB");
            Path path = FileSystems.getDefault().getPath(localPathFile.getPath() + "/" + fileName);
            metaFile.setFileType(Files.probeContentType(path));
            if (null == metaFile.getFileType() || "".equals(metaFile.getFileType())) {
                metaFile.setFileType(file.getContentType());
            }
            // 上传七牛云
            String outerChainPath;
            if (metaFile.getFileType().contains("video")) {
                outerChainPath = qiniuFileUtils.uploadVideo(file);
            } else {
                outerChainPath = qiniuFileUtils.uploadImage(file);
            }
            metaFile.setOuterChainPath(outerChainPath);
            metaFileRepository.save(metaFile);
            return metaFile;
        } catch (Exception e) {
            throw new YnceErrorException(YnceError.YNCE_000000);
        }
    }

    @Override
    public MetaFileVO getMetaFileVO(MetaFile metaFile) {
        if (metaFile == null) {
            return null;
        }
        MetaFileVO metaFileVO = new MetaFileVO();
        metaFileVO.setId(metaFile.getId());
        metaFileVO.setName(metaFile.getFileName());
        metaFileVO.setPath(metaFile.getOuterChainPath() != null ? metaFile.getOuterChainPath() : urlPath + metaFile.getFilePath());
        return metaFileVO;
    }
}
