package com.docus.server.archivefile.process;

import com.docus.infrastructure.redis.service.IdService;
import com.docus.log.context.TrackContext;
import com.docus.log.processor.AbstractProcessor;
import com.docus.server.archivefile.convert.TaskConfigRetryLogConvert;
import com.docus.server.archivefile.convert.TaskMessageRetryLogConvert;
import com.docus.server.archivefile.infrastructure.dao.ITaskConfigDao;
import com.docus.server.archivefile.infrastructure.dao.ITaskConfigRetryLogDao;
import com.docus.server.archivefile.infrastructure.dao.ITaskMessageDao;
import com.docus.server.archivefile.infrastructure.dao.ITaskMessageRetryLogDao;
import com.docus.server.entity.TaskConfig;
import com.docus.server.entity.TaskConfigRetryLog;
import com.docus.server.entity.TaskMessage;
import com.docus.server.entity.TaskMessageRetryLog;
import com.docus.server.enums.StateEnum;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 重试处理器
 *
 * @author linruifeng
 */
@Slf4j
public class RetryProcessor extends AbstractProcessor {
    public static final String EMPTY_STRING = "";
    @Resource
    private ITaskMessageRetryLogDao taskMessageRetryLogDao;
    @Resource
    private ITaskConfigRetryLogDao taskConfigRetryLogDao;
    @Resource
    private ITaskMessageDao taskMessageDao;
    @Resource
    private ITaskConfigDao taskConfigDao;
    @Resource
    private IdService idService;

    @Override
    public Object doProcess(TrackContext context) {

        String group = context.getGroup();

        if ("doRetryTaskMessage".equalsIgnoreCase(group)) {

            doRetryTaskMessageLog(context);

        } else if ("doRetryTaskConfig".equalsIgnoreCase(group)) {

            doRetryTaskConfigLog(context);
        }
        return null;
    }

    private void doRetryTaskConfigLog(TrackContext context) {
        boolean error = context.isError();
        TaskConfig taskConfig = (TaskConfig) context.getArgs()[0];
        TaskConfig taskConfigDO = taskConfigDao.findById(taskConfig.getId());
        TaskConfigRetryLog taskConfigRetryLog = TaskConfigRetryLogConvert.INSTANCE.toConvertRetryLog(taskConfig, idService.getDateSeq());
        if (error) {
            taskConfigRetryLog.setLastErrorMsg(context.getExMessageResult());
            taskConfigRetryLog.setState(StateEnum.FAIL);
            taskConfigRetryLog.setCreateTime(new Date());
            taskConfigRetryLog.setUpdateTime(new Date());
            taskConfigDO.setState(StateEnum.FAIL);
            taskConfigDO.setCreateTime(new Date());
            taskConfigDO.setUpdateTime(new Date());
        } else {
            taskConfigRetryLog.setLastErrorMsg(EMPTY_STRING);
            taskConfigRetryLog.setState(StateEnum.OK);
            taskConfigRetryLog.setCreateTime(new Date());
            taskConfigRetryLog.setUpdateTime(new Date());
            taskConfigDO.setState(StateEnum.OK);
            taskConfigDO.setCreateTime(new Date());
            taskConfigDO.setUpdateTime(new Date());
        }
        taskConfigRetryLogDao.save(taskConfigRetryLog);
        taskConfigDao.updateById(taskConfigDO);
    }

    private void doRetryTaskMessageLog(TrackContext context) {
        boolean error = context.isError();
        TaskMessage taskMessage = (TaskMessage) context.getArgs()[0];
        TaskMessage taskMessageDO = taskMessageDao.findById(taskMessage.getId());
        TaskMessageRetryLog taskMessageRetryLog = TaskMessageRetryLogConvert.INSTANCE.toConvertRetryLog(taskMessage, idService.getDateSeq());
        if (error) {
            taskMessageRetryLog.setErrorMsg(context.getExMessageResult());
            taskMessageRetryLog.setState(StateEnum.FAIL);
            taskMessageRetryLog.setCreateTime(new Date());
            taskMessageRetryLog.setUpdateTime(new Date());
            taskMessageDO.setState(StateEnum.FAIL);
            taskMessageDO.setCreateTime(new Date());
            taskMessageDO.setUpdateTime(new Date());
        } else {
            taskMessageRetryLog.setErrorMsg(EMPTY_STRING);
            taskMessageRetryLog.setState(StateEnum.OK);
            taskMessageRetryLog.setCreateTime(new Date());
            taskMessageRetryLog.setUpdateTime(new Date());
            taskMessageDO.setState(StateEnum.OK);
            taskMessageDO.setCreateTime(new Date());
            taskMessageDO.setUpdateTime(new Date());
        }
        taskMessageRetryLogDao.save(taskMessageRetryLog);
        taskMessageDao.updateById(taskMessageDO);
    }
}
