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

import cn.hutool.core.date.DateUtil;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.paper.library.business.templete.importData.AsyncImportTaskTemplate;
import com.qmth.paper.library.business.templete.service.TaskLogicService;
import com.qmth.paper.library.common.contant.SpringContextHolder;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.entity.TBTask;
import com.qmth.paper.library.common.enums.TaskResultEnum;
import com.qmth.paper.library.common.enums.TaskStatusEnum;
import com.qmth.paper.library.common.service.TBTaskService;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @Description: 系统用户批量导入
 * @Author: CaoZixuan
 * @Date: 2021-08-12
 */
@Service
public class AsyncSysUserDataImportService extends AsyncImportTaskTemplate {
    private final static Logger log = LoggerFactory.getLogger(AsyncSysUserDataImportService.class);

    public static final String OBJ_TITLE = "用户基本信息";

    @Override
    public Result importTask(Map<String, Object> map) throws Exception {
        TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
        InputStream inputStream = super.getUploadFileInputStream(tbTask);
        map.put("inputStream", inputStream);

        StringJoiner stringJoinerSummary = new StringJoiner("\n")
                .add(MessageFormat.format("{0}{1}{2}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), BEGIN_TITLE, OBJ_TITLE));
        tbTask.setStatus(TaskStatusEnum.RUNNING);
        TBTaskService tbTaskService = SpringContextHolder.getBean(TBTaskService.class);
        tbTaskService.updateById(tbTask);
        try {
            TaskLogicService taskLogicService = SpringContextHolder.getBean(TaskLogicService.class);

            // 执行导入基础用户数据
            Map<String, Object> result = taskLogicService.executeImportSysUserLogic(map);
            stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), FINISH_TITLE, Long.valueOf(String.valueOf(result.get("dataCount"))), FINISH_SIZE));
            tbTask.setResult(TaskResultEnum.SUCCESS);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), EXCEPTION_TITLE, EXCEPTION_DATA, e.getMessage()));
            tbTask.setResult(TaskResultEnum.ERROR);
            if (e instanceof ApiException) {
                ResultUtil.error((ApiException) e, e.getMessage());
            } else {
                ResultUtil.error(e.getMessage());
            }
        } finally {
            tbTask.setSummary(stringJoinerSummary.toString());
            tbTask.setStatus(TaskStatusEnum.FINISH);
            tbTaskService.updateById(tbTask);
        }
        return ResultUtil.ok(map);
    }
}
