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

import cn.estsh.i3plus.ext.mes.pcn.api.job.jx.IJxRestoreProductDataService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/2/2 16:44
 **/
@Slf4j
@DisallowConcurrentExecution
@ApiOperation("嘉兴补录检测数据定时任务")
public class JxRestoreProductDataJob implements Job {

    @Autowired
    private IJxRestoreProductDataService restoreProductDataService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {

        Object param = jobExecutionContext.getJobDetail().getJobDataMap().get(MesPcnExtConstWords.PARAM);
        if (StringUtils.isEmpty(param)) return;

        log.info("RESTORE PRODUCT DATA JOB --- EXEC --- PARAM:{}", param.toString());

        Map<String, Object> paramMap = JSONObject.parseObject(param.toString(), Map.class);

        if (!paramMap.containsKey(MesPcnExtConstWords.ORGANIZE_CODE)) return;

        log.info("工厂{} RESTORE PRODUCT DATA JOB --- EXEC -- START --- PARAM:{}", paramMap.get(MesPcnExtConstWords.ORGANIZE_CODE), param.toString());

        long startTime = System.currentTimeMillis();

        restoreProductDataService.doRestoreProductData(paramMap);

        long endTime = System.currentTimeMillis();

        log.info("工厂{} RESTORE PRODUCT DATA JOB --- EXEC --- END --- 耗时: {} ms", paramMap.get(MesPcnExtConstWords.ORGANIZE_CODE), endTime - startTime);

    }
}
