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


import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesEquipmentLogService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.impp.framework.boot.init.ApplicationProperties;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
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.StringUtils;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/2/2 16:44
 **/
@Slf4j
@DisallowConcurrentExecution
@Component
@ApiOperation("保存工艺参数job")
public class MesSaveVariableJob extends BaseMesScheduleJob {

    @Autowired
    private IMesEquipmentLogService mesEquipmentLogService;

    //默认批量一次性最大写800条数据
    private static final Integer DEFAULT_QTY = 800;

    public MesSaveVariableJob() {
        super(MesSaveVariableJob.class, "保存工艺参数job");
    }

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

        log.info("保存工艺参数JOB --- START ");

        String jobParam = this.getJobParam();
        JSONObject params= JSONUtil.parseObj(jobParam);
        String organizeCode = (null == params || !params.containsKey(MesPcnExtConstWords.ORGANIZE_CODE)) ? null : params.get(MesPcnExtConstWords.ORGANIZE_CODE).toString();
        Integer qty = (null == params || !params.containsKey(MesPcnExtConstWords.QTY)) ? DEFAULT_QTY : Integer.valueOf(params.get(MesPcnExtConstWords.QTY).toString());
        qty = qty.compareTo(DEFAULT_QTY) > 0 ? DEFAULT_QTY : qty;

        if (StringUtils.isEmpty(organizeCode)){
            log.info("保存工艺参数JOB --- ERROR --- JOB未配置工厂参数");
            return;
        }

        long startTime = System.currentTimeMillis();

        mesEquipmentLogService.saveEquipmentLogDetails(organizeCode, MesPcnExtConstWords.JOB, qty);

        long endTime = System.currentTimeMillis();

        log.info("保存工艺参数JOB --- END --- 耗时: {} ms", endTime - startTime);

    }

}
