package com.qmth.distributed.print.business.templete.execute;

import cn.hutool.core.date.DateUtil;
import com.qmth.distributed.print.business.entity.TBTaskPdf;
import com.qmth.distributed.print.business.service.TBTaskPdfService;
import com.qmth.distributed.print.business.templete.callback.CallbackCreatePdf;
import com.qmth.distributed.print.business.templete.create.AsyncCreateTaskTemplete;
import com.qmth.distributed.print.business.templete.service.PdfTaskLogicService;
import com.qmth.teachcloud.common.contant.SpringContextHolder;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.TaskResultEnum;
import com.qmth.teachcloud.common.enums.TaskStatusEnum;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.*;

/**
 * @Description: 创建pdf模版
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/4/9
 */
@Service
public class AsyncCreatePdfTemplateService extends AsyncCreateTaskTemplete {
    private final static Logger log = LoggerFactory.getLogger(AsyncCreatePdfTemplateService.class);

    static final String OBJ_TITLE = "pdf数据";

    /**
     * 创建pdf
     *
     * @param tbTaskPdf
     * @param callbackCreatePdf
     * @return
     * @throws IOException
     */
    @Override
    public Result createPdf(TBTaskPdf tbTaskPdf, CallbackCreatePdf callbackCreatePdf) {
        StringJoiner stringJoinerSummary = new StringJoiner("\n").add(MessageFormat.format("{0}{1}{2}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), BEGIN_TITLE, OBJ_TITLE));
        TBTaskPdfService tbTaskPdfService = SpringContextHolder.getBean(TBTaskPdfService.class);
        // 任务状态为进行中，直接结束
        if (TaskStatusEnum.RUNNING.equals(tbTaskPdf.getStatus())) {
            return ResultUtil.ok();
        }
        tbTaskPdf.setStatus(TaskStatusEnum.RUNNING);
        tbTaskPdfService.updateById(tbTaskPdf);
        try {
            PdfTaskLogicService pdfTaskLogicService = SpringContextHolder.getBean(PdfTaskLogicService.class);
            pdfTaskLogicService.executeCreatePdfLogic(tbTaskPdf, stringJoinerSummary);
            tbTaskPdf.setResult(TaskResultEnum.SUCCESS);
        } catch (Exception e) {
            stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), EXCEPTION_TITLE, EXCEPTION_DATA, e.getMessage()));
            tbTaskPdf.setResult(TaskResultEnum.ERROR);
        } finally {
            tbTaskPdf.setStatus(TaskStatusEnum.FINISH);
            tbTaskPdf.setSummary(stringJoinerSummary.toString());
            tbTaskPdfService.updateById(tbTaskPdf);
        }
        return ResultUtil.ok(tbTaskPdf);
    }

    @Override
    public Result importTask(Map<String, Object> map) throws IOException {
        return null;
    }
}
