package com.qmth.paper.library.business.templete.importData;

import com.alibaba.fastjson.JSON;
import com.qmth.paper.library.business.bean.vo.FilePathVo;
import com.qmth.paper.library.business.service.FileUploadService;
import com.qmth.paper.library.common.contant.SpringContextHolder;
import com.qmth.paper.library.common.entity.TBTask;
import com.qmth.paper.library.common.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @Description: 异步导入模版
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/3/29
 */
public abstract class AsyncImportTaskTemplate {

    private final static Logger log = LoggerFactory.getLogger(AsyncImportTaskTemplate.class);

    public static final String BEGIN_TITLE = "->开始准备处理导入的";
    public static final String EXCEPTION_TITLE = "->数据处理发生异常!";
    public static final String EXCEPTION_DATA = "错误信息:";
    public static final String FINISH_TITLE = "->数据处理结束，共处理了";
    public static final String FINISH_SIZE = "条数据";
    public static final String TXT_PREFIX = ".txt";
    public static final String EXCEPTION_CREATE_TXT_TITLE = "->创建导出日志时发生异常!";

    /**
     * 异步导入任务
     *
     * @param map
     * @return
     * @throws IOException
     */
    @Async
    public abstract Result importTask(Map<String, Object> map) throws Exception;

    /**
     * 获取上传的文件
     *
     * @param tbTask
     * @return
     */
    public InputStream getUploadFileInputStream(TBTask tbTask) {
        FileUploadService fileUploadService = SpringContextHolder.getBean(FileUploadService.class);
        FilePathVo filePathVo = JSON.parseObject(tbTask.getImportFilePath(), FilePathVo.class);
        return fileUploadService.downloadInputStream(filePathVo.getPath(), filePathVo.getType());
    }

}
