package com.qmth.sop.business.templete.execute;

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

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @Description: 软通智服用户同步定时任务
 * @Author: CaoZixuan
 * @Date: 2023-11-27
 */
@Service
public class AsyncRtzfUserPushService extends AsyncSyncDataTaskTemplate {
    private final static Logger log = LoggerFactory.getLogger(AsyncRtzfUserPushService.class);

    @Resource
    TaskLogicService taskLogicService;

    @Resource
    TBTaskService tbTaskService;

    public static final String OBJ_TITLE = "软通智服用户推送";

    @Override
    public Result syncData(Map<String, Object> map) throws Exception {
        TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
        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);
        tbTask.setSummary(stringJoinerSummary.toString());
        tbTaskService.updateById(tbTask);
        try {
            // 执行软通智服用户推送任务
            Map<String, Object> result = taskLogicService.executeSyncRtzfUserPush(map);
            int successSize = Integer.parseInt(String.valueOf(result.get(SystemConstant.SUCCESS)));
            int errorSize = Integer.parseInt(String.valueOf(result.get(SystemConstant.RESULT_ERROR)));
            stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}{4}{5}{6}{7}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), FINISH_TITLE, (successSize + errorSize), FINISH_TOTAL_SIZE, successSize, FINISH_SUCCESS_SIZE, errorSize, FINISH_ERROR_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, ((ApiException) e).getCode(), e.getMessage());
            } else {
                ResultUtil.error(e.getMessage());
            }
            tbTask.setResult(TaskResultEnum.ERROR);
        } finally {//生成txt文件
            tbTask.setSummary(stringJoinerSummary.toString());
            super.createTxt(tbTask);
        }
        return ResultUtil.ok(map);
    }
}
