package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.microsvc.toolkit.common.tools.DownloadManager;
import com.microsvc.toolkit.middleware.oss.OssPluginContext;
import com.microsvc.toolkit.middleware.oss.impl.TencentOssPlugin;
import com.yonge.cooleshow.biz.dal.service.OssFileService;
import com.yonge.cooleshow.biz.dal.wrapper.OssFileWrapper;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.utils.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.MessageFormat;
import java.util.List;

/**
 * @author OSS 文件通用处理
 */
@Slf4j
@Service
public class OssFileServiceImpl implements OssFileService {

    @Autowired
    private OssPluginContext ossPluginContext;

    @Override
    public OssFileWrapper.ExportFile uploadFile(List<?> dataList, Class<?> clazz,
                                                String fileName, String sheetName) {
        if (CollectionUtils.isEmpty(dataList)) {
            throw new BizException("导出数据为空");
        }

        // OSS上传文件目录
        String uploadOssPath = MessageFormat.format("excel-download/{0}", DateTime.now().toString("yyyy-MM"));

        // 文件上传下载地址
        String uploadPath = MessageFormat.format("{0}/{1}-{2}/{3}.xlsx", uploadOssPath,
                String.valueOf(IdWorker.getId()),
                DateTime.now().toString("MMddHHmmss"),fileName);

        // 本地文件地址
        String localPath = DownloadManager.getInstance().path(uploadPath);
        log.debug("pageExport localPath={}", localPath);

        EasyExcel
                .write(localPath, clazz)
                //.withTemplate(resource.getStream()) // 利用模板的输出流
                .sheet(0, sheetName)
                .doWrite(dataList);

        // 上传本地文件到OSS服务器
        String downloadPath = ossPluginContext.getPluginService(TencentOssPlugin.PLUGIN_NAME)
                .uploadFile(uploadOssPath, new File(localPath));
        log.debug("pageExport downloadPath={}", downloadPath);

        // 删除本地缓存文件
        DownloadManager.getInstance().deleteOnExit(localPath);

        // 学生统计下载
        return OssFileWrapper.ExportFile
                .builder()
                .fileName(MessageFormat.format("{0}.xlsx", fileName))
                .downloadPath(downloadPath)
                .build();
    }

    @Override
    public OssFileWrapper.ExportFile uploadFile(List<?> dataList, Class<?> clazz, String fileName, String sheetName,
                                                String tag) {
        if (CollectionUtils.isEmpty(dataList)) {
            throw new BizException("导出数据为空");
        }

        // OSS上传文件目录
        String uploadOssPath = MessageFormat.format("excel-download/{0}/{1}", DateTime.now().toString("yyyy-MM"), tag);

        // 文件上传下载地址
        String uploadPath = MessageFormat.format("{0}/{1}-{2}/{3}.xlsx", uploadOssPath,
                String.valueOf(IdWorker.getId()),
                DateTime.now().toString("MMddHHmmss"), fileName);

        // 本地文件地址
        String localPath = DownloadManager.getInstance().path(uploadPath);
        log.debug("pageExport localPath={}", localPath);

        EasyExcel
                .write(localPath, clazz)
                //.withTemplate(resource.getStream()) // 利用模板的输出流
                .sheet(0, sheetName)
                .doWrite(dataList);

        // 上传本地文件到OSS服务器
        String downloadPath = ossPluginContext.getPluginService(TencentOssPlugin.PLUGIN_NAME)
                .uploadFile(uploadOssPath, new File(localPath));
        log.debug("pageExport downloadPath={}", downloadPath);

        // 删除本地缓存文件
        DownloadManager.getInstance().deleteOnExit(localPath);

        // 学生统计下载
        return OssFileWrapper.ExportFile
                .builder()
                .fileName(MessageFormat.format("{0}.xlsx", fileName))
                .downloadPath(downloadPath)
                .build();
    }

    @Override
    public OssFileWrapper.ExportFile uploadFileWithShortUrl(List<?> dataList, Class<?> clazz,
                                                            String fileName, String sheetName) {
        if (CollectionUtils.isEmpty(dataList)) {
            throw new BizException("导出数据为空");
        }

        // OSS上传文件目录
        String uploadOssPath = MessageFormat.format("excel-download/{0}", DateTime.now().toString("yyyy-MM"));

        // 文件上传下载地址
        String uploadPath = MessageFormat.format("{0}/{1}-{2}.xlsx", uploadOssPath, String.valueOf(IdWorker.getId()),
                DateTime.now().toString("MMddHHmmss"));

        // 本地文件地址
        String localPath = DownloadManager.getInstance().path(uploadPath);
        log.debug("pageExport localPath={}", localPath);

        // 模板文件保存在springboot项目的resources/static下
        // Resource resource = new ClassPathResource("static/预报名模板导出.xlsx");

        EasyExcel
                .write(localPath, clazz)
                //.withTemplate(resource.getStream()) // 利用模板的输出流
                .sheet(0, sheetName)
                .doWrite(dataList);

        // 上传本地文件到OSS服务器
        String downloadPath = ossPluginContext.getPluginService(TencentOssPlugin.PLUGIN_NAME)
                .uploadFile(uploadOssPath, new File(localPath));
        log.debug("pageExport downloadPath={}", downloadPath);

        // 删除本地缓存文件
        DownloadManager.getInstance().deleteOnExit(localPath);

        // 学生统计下载
        return OssFileWrapper.ExportFile
                .builder()
                .fileName(MessageFormat.format("{0}.xlsx", fileName))
                .downloadPath(HttpUtil.getSortUrl(downloadPath))
                .build();
    }


}
