package com.younike.common.utils;


import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.Objects;

/**
 * @description:阿里云oss操作
 * @author: dail
 * @Date:9:14 2022/3/8
 */
@Component
@Log4j2
public class OssUtil {

    private static OssUtil ossUtil;

    private static String filePath = "upload";

    @Resource
    private AliyunOSSUtil aliyunOSSUtil;

    @PostConstruct
    public void init() {
        log.warn(">>>>>>进入OssUtil init()");
        ossUtil = this;
        ossUtil.aliyunOSSUtil = this.aliyunOSSUtil;
        log.warn(">>>>>>进入OssUtil init()执行结束,AliyunOSSUtil is null = " + Objects.isNull(this.aliyunOSSUtil));
    }


    public static String uploadExcel(File file) {
        log.info("excel--上传");

        try {
            if (file != null) {
                String datePath = DateUtils.formatDateByStyle(new Date(), DatePattern.CN_DATE_BASIC_STYLE4.getDatePattern());
                String path = filePath + "excel/" + datePath + "/" + file.getName();

                // 上传到OSS
                String uploadUrl = ossUtil.aliyunOSSUtil.upLoad(file, path);

                if (uploadUrl != null) {
                    return uploadUrl;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

        }

        return null;
    }

}
