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


import cn.estsh.i3plus.ext.mes.pcn.api.job.IMesWorkOrderCalcQtyRestoreService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.impp.framework.boot.init.ApplicationProperties;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author wangjie
 * @Description 生产工单计数补录JOB
 * @version 1.0
 * @date 2021/2/2 16:44
 **/
@Slf4j
@DisallowConcurrentExecution
@Component
@ApiOperation("补录生产工单计数JOB")
public class MesWorkOrderCalcQtyRestoreJob extends BaseMesScheduleJob {

    public MesWorkOrderCalcQtyRestoreJob() {
        super(MesWorkOrderCalcQtyRestoreJob.class, "补录生产工单计数JOB");
    }

    @Autowired
    private IMesWorkOrderCalcQtyRestoreService workOrderCalcQtyRestoreService;

    @Override
    public void executeMesJob(JobExecutionContext context, ApplicationProperties applicationProperties) {

        String jobParam = this.getJobParam();
        if (StringUtils.isEmpty(jobParam)) {
            log.info("--- 异常 --- 补录生产工单计数JOB --- 参数为空 ---");
            return;
        }

        Map<String, String> paramMap = JSONObject.parseObject(jobParam, Map.class);

        if (CollectionUtils.isEmpty(paramMap) || !paramMap.containsKey(MesPcnExtConstWords.ORGANIZE_CODE)) {
            log.info("--- 异常 --- 补录生产工单计数JOB --- 参数未配置:工厂代码[organizeCode] ---");
            return;
        }

        StopWatch stopWatch = new StopWatch();

        try {

            stopWatch.start();

            workOrderCalcQtyRestoreService.doRestoreWorkOrderCalcQty(paramMap);

        } catch (Exception e) {

            log.info("--- 异常 --- 补录生产工单计数JOB --- ERROR --- {} ---", e.toString());

        } finally {

            stopWatch.stop();

            log.info("补录生产工单计数JOB --- END --- 耗时: {} ms ---", stopWatch.getTotalTimeMillis());

        }

    }

}
