package com.docus.server.common.process;

import com.docus.infrastructure.redis.service.IdService;
import com.docus.log.context.TrackContext;
import com.docus.log.processor.AbstractProcessor;
import com.docus.server.dto.scheduling.management.schcollector.UpdateSchCollectorDTO;
import com.docus.server.entity.scheduling.management.SchCollector;
import com.docus.server.entity.scheduling.management.SchCollectorVersion;
import com.docus.server.entity.scheduling.management.SchCollectorVersionLog;
import com.docus.server.entity.scheduling.management.SchSystemParams;
import com.docus.server.enums.StateEnum;
import com.docus.server.infrastructure.dao.ISchCollectorDao;
import com.docus.server.infrastructure.dao.ISchCollectorVersionDao;
import com.docus.server.infrastructure.dao.ISchCollectorVersionLogDao;
import com.docus.server.infrastructure.dao.ISchSystemParamsDao;

import javax.annotation.Resource;

/**
 * 日志管理
 */
public class CollectorVersionProcessor extends AbstractProcessor {
    @Resource
    private ISchCollectorVersionLogDao iSchCollectorVersionLogDao;
    @Resource
    private ISchCollectorVersionDao iSchCollectorVersionDao;
    @Resource
    private ISchSystemParamsDao iSchSystemParamsDao;
    @Resource
    private ISchCollectorDao iSchCollectorDao;
    @Resource
    private IdService idService;

    @Override
    public Object beforeProcess(TrackContext context) {
        super.beforeProcess(context);
        UpdateSchCollectorDTO updateDTO = (UpdateSchCollectorDTO) context.getArgs()[0];
        Long collectorId = updateDTO.getCollectorId();
        SchCollector schCollector = iSchCollectorDao.findOneBy("collectorId", collectorId);
        SchCollectorVersion preVersion = iSchCollectorVersionDao.findById(schCollector.getCollectorVersionId());
        return preVersion.getCollectVersion();
    }

    @Override
    protected Object doProcess(TrackContext context) {
        logProcess(context);
        return null;
    }

    private void logProcess(TrackContext context) {

        boolean error = context.isError();

        UpdateSchCollectorDTO updateDTO = (UpdateSchCollectorDTO) context.getArgs()[0];
        Long collectorId = updateDTO.getCollectorId();
        String preVersion = (String) context.getBeforeResult();
        SchCollectorVersion curVersion = iSchCollectorVersionDao.findById(updateDTO.getCollectorVersionId());
        SchSystemParams schSystemParams = iSchSystemParamsDao.findOneBy("paramValue", collectorId);

        SchCollectorVersionLog log = new SchCollectorVersionLog();
        log.setId(idService.getDateSeq());
        log.setCollectorId(collectorId);
        log.setOperationModule(context.getGroup());
        log.setOperationType(context.getAction());
        log.setOperationDesc(context.getDesc());
        log.setOperationContent(String.format("采集器：%s，上一个版本是：%s，更换成当前版本是：%s", schSystemParams.getParamName(), preVersion, curVersion.getCollectVersion()));
        log.setState(error ? StateEnum.FAIL : StateEnum.OK);

        iSchCollectorVersionLogDao.save(log);
    }

}
