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

import cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.bu3.SxWorkCenterNotReachReasonService;
import cn.estsh.i3plus.ext.mes.apiservice.util.SxDateRangeUtils;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.impp.framework.base.schedule.BaseImppScheduleJob;
import cn.estsh.impp.framework.boot.init.ApplicationProperties;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;

/**
 * 文件描述
 *
 * @author xiangming.liao
 * @date 2021年07月01日 16:00
 */
@Slf4j
@DisallowConcurrentExecution
@ApiOperation("自动汇总产线未达成原因-job")
public class SxStatisticsWorkCenterNotReachReasonJob extends BaseImppScheduleJob {

    @Autowired
    private SxWorkCenterNotReachReasonService workCenterNotReachReasonService;

    public SxStatisticsWorkCenterNotReachReasonJob() {
        super(SxStatisticsWorkCenterNotReachReasonJob.class, "自动汇总产线未达成原因-job");
    }

    @SneakyThrows
    @Override
    public void executeImppJob(JobExecutionContext context, ApplicationProperties applicationProperties) {
        String organizeCode = jobParam(this.getJobParam());

        if (StringUtils.isEmpty(organizeCode)) return;

        StopWatch stopWatch = new StopWatch();

        stopWatch.start();

        String dateByNowTime = SxDateRangeUtils.getDateByNowTime(1);

        LOGGER.info("MES工厂{}DATE{}执行统计产线未达成原因JOB --- EXEC START --- THREAD:[{}]", organizeCode, dateByNowTime, Thread.currentThread().getName());

        try {

            workCenterNotReachReasonService.doStatisticsWorkCenterNotReachReason(organizeCode, SxDateRangeUtils.getDateByNowTime(1));

            LOGGER.info("MES工厂{}DATE{}执行统计产线未达成原因JOB --- EXEC SUCCESS --- THREAD:[{}]", organizeCode, dateByNowTime, Thread.currentThread().getName());

        } finally {

            stopWatch.stop();

            LOGGER.info("MES工厂{}DATE{}执行统计产线未达成原因JOB --- EXEC END --- JOB总耗时:[{}] --- THREAD:[{}]", organizeCode, dateByNowTime, stopWatch.getTotalTimeMillis(), Thread.currentThread().getName());

        }
    }

    private String jobParam(String jobParam) {
        String organizeCode = null;
        try {
            JSONObject jsonObject = StringUtils.isEmpty(jobParam) ? null : JSONObject.parseObject(jobParam);
            organizeCode = null == jsonObject ? null : (String) jsonObject.get(MesExtConstWords.ORGANIZE_CODE);
        } catch (JSONException e) {
            LOGGER.info("MES工厂执行统计产线未达成原因JOB --- EXEC ERROR --- 参数[{}]转换异常:[{}]", jobParam, e.getMessage());
        }
        if (StringUtils.isEmpty(organizeCode)) LOGGER.info("MES工厂执行统计产线未达成原因JOB --- EXEC ERROR --- 参数[{}]缺失[工厂代码]", jobParam);
        return organizeCode;
    }
}
