package com.qmth.themis.business.templete.impl;

import cn.hutool.core.date.DateUtil;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.enums.TaskStatusEnum;
import com.qmth.themis.business.templete.TaskSyncCommon;
import com.qmth.themis.business.templete.TaskSyncTemplete;
import com.qmth.themis.business.templete.service.TempleteLogicService;
import com.qmth.themis.common.contanst.Constants;
import com.qmth.themis.common.util.Result;
import com.qmth.themis.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @Description: 推送云阅卷数据
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/10/20
 */
@Service
public class TaskPushCloudMarkTemplete implements TaskSyncTemplete {

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

    @Resource
    TempleteLogicService templeteLogicService;

    /**
     * 同步模版
     *
     * @param map
     * @return
     */
    @Override
    public Result syncTask(Map<String, Object> map) throws IOException {
        log.info("开始推送云阅卷数据");
        long start = System.currentTimeMillis();
        TaskSyncCommon taskSyncCommon = new TaskSyncCommon(map);
        taskSyncCommon.init();
        taskSyncCommon.setTxtList(new ArrayList());
        StringJoiner stringJoiner = null;
        try {
            taskSyncCommon.getTxtList().add(DateUtil.format(new Date(), Constants.DEFAULT_DATE_PATTERN) + "->开始准备推送云阅卷数据");
            boolean oss = (boolean) taskSyncCommon.getOssEnv().get(SystemConstant.OSS);
            stringJoiner = new StringJoiner("");
            if (!oss) {
                stringJoiner.add(SystemConstant.FILES_DIR).add(File.separator);
            }
            stringJoiner = SystemConstant.getDirName(stringJoiner, false);
            taskSyncCommon.getTbTaskHistory().setStatus(TaskStatusEnum.RUNNING);
            taskSyncCommon.getTbTaskHistory().setSummary("正在推送数据");
            taskSyncCommon.getTxtList().add(DateUtil.format(new Date(), Constants.DEFAULT_DATE_PATTERN) + "->" + taskSyncCommon.getTbTaskHistory().getSummary());
            map = templeteLogicService.execPushCloudMarkDataLogic(map);
            long end = System.currentTimeMillis();
            log.info("推送云阅卷数据结束,============耗时============:{}秒", (end - start) / 1000);
        } catch (Exception e) {
            log.error("推送云阅卷数据报错", e);
            taskSyncCommon.exception(e, "推送数据时发生异常，请联系系统管理员处理！", taskSyncCommon.getTxtList());
        } finally {
            //这里写入txt文件
            taskSyncCommon.writeSyncResultTxt(stringJoiner.toString(), taskSyncCommon.getTxtList().toString());
            List<File> paperJsonList = null, recordJsonList = null;
            paperJsonList = (List<File>) map.get("paperJsonList");
            if (!CollectionUtils.isEmpty(paperJsonList)) {
                for (File f : paperJsonList) {
                    f.delete();
                }
            }
            recordJsonList = (List<File>) map.get("recordJsonList");
            if (!CollectionUtils.isEmpty(recordJsonList)) {
                for (File f : recordJsonList) {
                    f.delete();
                }
            }
        }
        return ResultUtil.ok(true);
    }
}