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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToMonthlyStatisticsService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToAutoStatisticsModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
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 org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;

@DisallowConcurrentExecution
@ApiOperation("生产数据月度统计JOB")
public class SxProdDataToMonthlyStatisticsJob extends BaseImppScheduleJob {

    public SxProdDataToMonthlyStatisticsJob() { super(SxProdDataToMonthlyStatisticsJob.class, "MES工厂生产数据月度统计JOB"); }

    @Autowired
    private ISxProdDataToMonthlyStatisticsService prodDataToMonthlyStatisticsService;

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

        SxProdDataToAutoStatisticsModel model = jobParam(this.getJobParam());

        if (null == model) return;

        StopWatch stopWatch = new StopWatch();

        stopWatch.start();

        model.today(TimeTool.getToday());

        LOGGER.info("MES工厂{}TODAY{}执行生产数据月度统计JOB --- JOB START --- THREAD:[{}]", model.getOrganizeCode(), model.getToday(), Thread.currentThread().getName());

        try {

            prodDataToMonthlyStatisticsService.exec(model);

        } finally {

            stopWatch.stop();

            LOGGER.info("MES工厂{}TODAY{}执行生产数据月度统计JOB --- JOB END --- JOB总耗时:[{}] --- THREAD:[{}]", model.getOrganizeCode(), model.getToday(), stopWatch.getTotalTimeMillis(), Thread.currentThread().getName());

        }

    }

    private SxProdDataToAutoStatisticsModel jobParam(String jobParam) {
        String organizeCode = null;
        String flag = null;
        try {
            JSONObject jsonObject = StringUtils.isEmpty(jobParam) ? null : JSONObject.parseObject(jobParam);
            organizeCode = null == jsonObject ? null : (String) jsonObject.get(MesExtConstWords.ORGANIZE_CODE);
            flag = null == jsonObject ? null : (String) jsonObject.get(MesExtConstWords.FLAG);
        } catch (JSONException e) {
            LOGGER.info("MES工厂执行生产数据月度统计JOB --- EXEC ERROR --- 参数[{}]转换异常:[{}] --- THREAD:[{}]", jobParam, e.getMessage(), Thread.currentThread().getName());
        }
        if (!StringUtils.isEmpty(organizeCode) && !StringUtils.isEmpty(flag)) return new SxProdDataToAutoStatisticsModel(organizeCode, flag, MesExtConstWords.JOB);
        LOGGER.info("MES工厂执行生产数据月度统计JOB --- EXEC ERROR --- 参数[{}]缺失[工厂代码/执行标志] --- THREAD:[{}]", jobParam, Thread.currentThread().getName());
        return null;
    }
}
