package org.eco.system.service.impl;

import org.eco.common.core.annotation.Excel;
import org.eco.common.core.config.EcoConfig;
import org.eco.common.core.exception.BusinessException;
import org.eco.common.core.utils.StringUtils;
import org.eco.common.core.utils.file.FileUploadUtils;
import org.eco.system.config.ServerConfig;
import org.eco.system.domain.vo.SysOssUploadVo;
import org.eco.system.domain.vo.SysOssVo;
import org.eco.system.service.ICommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Service
public class CommonService implements ICommonService {

    private static final String FILE_DELIMETER = ",";
    @Autowired
    private ServerConfig serverConfig;

    @Override
    public Map<String, String> uploadFile(MultipartFile file) {
        try {

            // 上传文件路径
            String filePath = EcoConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            String originalFilename = file.getOriginalFilename();
            assert originalFilename != null;
            String suffix = StringUtils.substring(originalFilename, originalFilename.lastIndexOf("."), originalFilename.length());
            Map<String,String> map = new HashMap<>();
            map.put("url", url);
            map.put("originalFilename", originalFilename);
            map.put("suffix", suffix);
            map.put("fileName", fileName);
            return map;
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
    }
}
