package com.qmth.themis.business.templete;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.qmth.themis.business.constant.SpringContextHolder;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.entity.TBTaskHistory;
import com.qmth.themis.business.enums.TaskStatusEnum;
import com.qmth.themis.business.service.TBAttachmentService;
import com.qmth.themis.business.service.TBTaskHistoryService;
import com.qmth.themis.business.util.OssUtil;
import com.qmth.themis.common.contanst.Constants;
import com.qmth.themis.common.util.GsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 同步任务公用
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/7/19
 */
public class TaskSyncCommon {

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

    private Map<String, Object> map = null;

    private Long orgId = null;

    private Long createId = null;

    private TBTaskHistoryService tbTaskHistoryService = null;

    private OssUtil ossUtil = null;

    private Map<String, Object> ossEnv = null;

    private TBTaskHistory tbTaskHistory = null;

    private boolean exception = false;

    private TBAttachmentService tbAttachmentService = null;

    private String type = null;

    private List<String> txtList = null;

    public TaskSyncCommon() {

    }

    public TaskSyncCommon(Map<String, Object> map) {
        this.map = map;
    }

    /**
     * 初始化
     */
    public void init() {
        OssUtil ossUtil = SpringContextHolder.getBean(OssUtil.class);
        if (Objects.nonNull(this.map.get(SystemConstant.ORG_ID))) {
            this.orgId = Long.parseLong(String.valueOf(this.map.get(SystemConstant.ORG_ID)));
        }
        this.createId = Long.parseLong(String.valueOf(this.map.get(SystemConstant.CREATE_ID)));
        this.ossUtil = SpringContextHolder.getBean(OssUtil.class);
        this.ossEnv = ossUtil.getAliYunOssPrivateDomain().getMap();
        this.type = (boolean) this.ossEnv.get(SystemConstant.OSS) ? SystemConstant.OSS : SystemConstant.LOCAL;
        Map tbTaskHistoryMap = (Map) this.map.get("tbTaskHistory");
        this.tbTaskHistory = GsonUtil.fromJson(GsonUtil.toJson(tbTaskHistoryMap), TBTaskHistory.class);
        this.tbTaskHistoryService = SpringContextHolder.getBean(TBTaskHistoryService.class);
        this.tbAttachmentService = SpringContextHolder.getBean(TBAttachmentService.class);
        map.put("tbTaskHistory", tbTaskHistory);
    }

    /**
     * 写入txt文件
     *
     * @param path
     * @param txtStr
     * @throws IOException
     */
    public void writeSyncResultTxt(String path, String txtStr) {
        try {
            JSONObject json = SystemConstant.createTxt(path, this.type, txtStr);
            this.tbTaskHistory.setReportFilePath(json.toJSONString());
            tbTaskHistoryService.updateById(this.tbTaskHistory);
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
        }
    }

    /**
     * 异常处理
     *
     * @param e
     * @param message
     * @param txtList
     * @return
     */
    public List<String> exception(Exception e, String message, List<String> txtList) {
        this.exception = true;
        txtList.add(DateUtil.format(new Date(), Constants.DEFAULT_DATE_PATTERN) + "->" + message + "错误信息:[" + e.getMessage() + "]");
        this.tbTaskHistory.setSummary(txtList.toString());
        this.tbTaskHistory.setStatus(TaskStatusEnum.FINISH);
        return txtList;
    }

    public void setException(boolean exception) {
        this.exception = exception;
    }

    public boolean isException() {
        return exception;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public Long getOrgId() {
        return orgId;
    }

    public Long getCreateId() {
        return createId;
    }

    public TBTaskHistoryService getTbTaskHistoryService() {
        return tbTaskHistoryService;
    }

    public OssUtil getOssUtil() {
        return ossUtil;
    }

    public Map<String, Object> getOssEnv() {
        return ossEnv;
    }

    public TBTaskHistory getTbTaskHistory() {
        return tbTaskHistory;
    }

    public TBAttachmentService getTbAttachmentService() {
        return tbAttachmentService;
    }

    public List<String> getTxtList() {
        return txtList;
    }

    public void setTxtList(List<String> txtList) {
        this.txtList = txtList;
        map.put("txtList", txtList);
    }
}