package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.collect;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxThirdPartyPlcCollectDataStepService;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import com.alibaba.fastjson.JSONObject;
import org.jboss.logging.Logger;
import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 第三方PLC数据采集定时任务---步骤执行调度
 */
public class SxThirdPartyPlcCollectStepFilterChain implements ISxThirdPartyPlcCollectDataStepService {

    private static final Logger LOGGER = Logger.getLogger(SxThirdPartyPlcCollectStepFilterChain.class);

    private List<ISxThirdPartyPlcCollectDataStepService> stepServiceList = new LinkedList<>();

    private String org;
    private String key;
    private String busiCode;
    private String[] clearField;
    private Long sleepTime2UnComplete;
    private Long sleepTime2Completed;

    public SxThirdPartyPlcCollectStepFilterChain(String org, String key, String busiCode) {
        this.org = org;
        this.key = key;
        this.busiCode = busiCode;
    }

    public SxThirdPartyPlcCollectStepFilterChain clearField(String... clearField) {
        this.clearField = clearField;
        return this;
    }

    public SxThirdPartyPlcCollectStepFilterChain sleepTime2UnComplete(Long sleepTime2UnComplete) {
        this.sleepTime2UnComplete = sleepTime2UnComplete;
        return this;
    }

    public SxThirdPartyPlcCollectStepFilterChain sleepTime2Completed(Long sleepTime2Completed) {
        this.sleepTime2Completed = sleepTime2Completed;
        return this;
    }

    public SxThirdPartyPlcCollectStepFilterChain add(ISxThirdPartyPlcCollectDataStepService stepService) {
        stepServiceList.add(stepService);
        return this;
    }

    @Override
    public StepResult exec(StepResult stepResult) {

        rollBackStepResult(stepResult);

        LOGGER.info(String.format("工厂%s第三方PLC数据采集 --- STRATEGY %s STEP EXEC --- key: %s START --- stepResult:%s ---", org, busiCode, key, JSONObject.toJSONString(stepResult)));

        try {
            for (ISxThirdPartyPlcCollectDataStepService stepService : stepServiceList) {
                if (null == stepService) continue;
                stepService.exec(stepResult);
                if (!stepResult.isCompleted()) break;
            }

            if (!stepResult.isCompleted()) TimeUnit.MILLISECONDS.sleep(!StringUtils.isEmpty(sleepTime2UnComplete) ? sleepTime2UnComplete : 1000L);
            else if (!StringUtils.isEmpty(sleepTime2Completed)) TimeUnit.MILLISECONDS.sleep(sleepTime2Completed);

        } catch (Exception e) {
            LOGGER.info(String.format("工厂%s第三方PLC数据采集 --- STRATEGY %s STEP ERROR --- key: %s --- %s --- stepResult:%s --- %s ---", org, busiCode, key, e.toString(), JSONObject.toJSONString(stepResult), JSONObject.toJSONString(e)));
        }

        LOGGER.info(String.format("工厂%s第三方PLC数据采集 --- STRATEGY %s STEP EXEC --- key: %s END --- stepResult:%s ---", org, busiCode, key, JSONObject.toJSONString(stepResult)));

        return stepResult;

    }

    private StepResult rollBackStepResult(StepResult stepResult) {
        stepResult.isCompleted(true).msg(null);
        if (null == clearField) return stepResult;
        for (String field : clearField) {
            if (StringUtils.isEmpty(field)) continue;
            ((Map<String, Object>) stepResult.getData()).remove(field);
        }
        return stepResult;
    }

}
