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

import cn.hutool.core.date.DateUtil;
import com.qmth.boot.api.exception.ApiException;
import com.qmth.distributed.print.business.entity.TBSyncTask;
import com.qmth.distributed.print.business.service.TBSyncTaskService;
import com.qmth.distributed.print.business.templete.push.AsyncPushTaskTemplate;
import com.qmth.distributed.print.business.templete.service.PushLogicService;
import com.qmth.distributed.print.business.templete.service.TaskLogicService;
import com.qmth.teachcloud.common.contant.SpringContextHolder;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.TBTask;
import com.qmth.teachcloud.common.enums.TaskResultEnum;
import com.qmth.teachcloud.common.enums.TaskStatusEnum;
import com.qmth.teachcloud.common.service.TBTaskService;
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.text.MessageFormat;
import java.util.Date;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @Description: 用户推送模板（异步任务）
 * @Author: CaoZixuan
 * @Date: 2021-10-31
 */
@Service
public class AsyncUserPushService extends AsyncPushTaskTemplate {

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

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

    @Override
    public Result pushTask(Map<String, Object> map) throws Exception {
        TBSyncTask tbSyncTask = (TBSyncTask) map.get(SystemConstant.TB_SYNC_TASK);


        StringJoiner stringJoinerSummary = new StringJoiner("\n")
                .add(MessageFormat.format("{0}{1}{2}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), BEGIN_TITLE, OBJ_TITLE));
        tbSyncTask.setStatus(TaskStatusEnum.RUNNING);
        TBSyncTaskService tbSyncTaskService = SpringContextHolder.getBean(TBSyncTaskService.class);
        tbSyncTaskService.updateById(tbSyncTask);
        try {
            PushLogicService pushLogicService = SpringContextHolder.getBean(PushLogicService.class);

            // 执行需要重新同步的用户同步数据
            Map<String, Object> result = pushLogicService.executeUserPushLogic(map);
            System.out.println(Integer.parseInt(String.valueOf(result.get("correct"))));
            stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}{4}{5}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), FINISH_TITLE, Integer.parseInt(String.valueOf(result.get("count"))),SUCCESS_TITLE,Integer.parseInt(String.valueOf(result.get("correct"))), FINISH_SIZE));
            tbSyncTask.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 {//生成txt文件
            tbSyncTask.setSummary(stringJoinerSummary.toString());
            super.createTxt(tbSyncTask);
        }
        return ResultUtil.ok(map);
    }
}
