package cn.estsh.i3plus.ext.mes.pcn.apiservice.schedulejob;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxThirdPartyPlcCollectDataJobService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/2/2 16:44
 **/
@Slf4j
@DisallowConcurrentExecution
@ApiOperation("第三方PLC数据采集定时任务")
public class SxThirdPartyPlcCollectDataJob implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {

        Object param = jobExecutionContext.getJobDetail().getJobDataMap().get(MesPcnExtConstWords.PARAM);

        if (StringUtils.isEmpty(param)) return;

        log.info("第三方PLC数据采集 --- JOB --- PARAM:{}", param.toString());

        Map<String, String> paramMap = JSONObject.parseObject(param.toString(), Map.class);
        if (!paramMap.containsKey(MesPcnExtConstWords.ORGANIZE_CODE) || !paramMap.containsKey(MesPcnExtConstWords.STRATEGY_CLASS)) return;

        log.info("工厂{}第三方PLC数据采集 --- JOB START --- PARAM:{}", paramMap.get(MesPcnExtConstWords.ORGANIZE_CODE), param.toString());

        long startTime = System.currentTimeMillis();

        ((ISxThirdPartyPlcCollectDataJobService) SpringContextsUtil.getBean(paramMap.get(MesPcnExtConstWords.STRATEGY_CLASS))).doThirdPartyPlcCollectData(paramMap);

        long endTime = System.currentTimeMillis();

        log.info("工厂{}第三方PLC数据采集 --- JOB END --- 耗时: {} ms", paramMap.get(MesPcnExtConstWords.ORGANIZE_CODE), endTime - startTime);

    }
}
