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

import cn.estsh.i3plus.ext.mes.api.busi.bu3.ISxProdDataToSoftAdapterService;
import cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.busi.bu3.SxProdDataToSoftAdapterService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxProdDataToSoftAdapterDataModel;
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.util.StopWatch;
import org.springframework.util.StringUtils;

import java.util.Calendar;

@DisallowConcurrentExecution
@ApiOperation("生产数据回传软适配JOB")
public class SxProdDataToSoftAdapterJob extends BaseImppScheduleJob {

    public SxProdDataToSoftAdapterJob() { super(SxProdDataToSoftAdapterJob.class, "MES工厂生产数据回传软适配JOB"); }

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

        String currentThreadName = Thread.currentThread().getName();

        SxProdDataToSoftAdapterDataModel param = new SxProdDataToSoftAdapterDataModel(MesExtConstWords.JOB, currentThreadName);

        jobParam(param, this.getJobParam());

        if (StringUtils.isEmpty(param.getOrganizeCode())) return;

        StopWatch stopWatch = new StopWatch();

        stopWatch.start();

        String date = getYesterday();

        LOGGER.info("MES工厂{}DATE{}执行生产数据回传软适配JOB --- EXEC START --- THREAD:[{}]", param.getOrganizeCode(), date, currentThreadName);

        try {

            ISxProdDataToSoftAdapterService prodDataToSoftAdapterService = new SxProdDataToSoftAdapterService();
            prodDataToSoftAdapterService.exec(param.date(date));

        } finally {

            stopWatch.stop();

            LOGGER.info("MES工厂{}DATE{}执行生产数据回传软适配JOB --- EXEC END --- JOB总耗时:[{}] --- THREAD:[{}]", param.getOrganizeCode(), date, stopWatch.getTotalTimeMillis(), currentThreadName);

        }

    }

    private String getYesterday() {
        return TimeTool.timeCalc(TimeTool.stringParseToDate(TimeTool.getToday(), MesExtConstWords.DATE_FORMAT), Calendar.DATE, -1, MesExtConstWords.DATE_FORMAT);
    }

    private void jobParam(SxProdDataToSoftAdapterDataModel param, String jobParam) {
        String organizeCode = null;
        String suitCaseCode = null;
        try {
            JSONObject jsonObject = StringUtils.isEmpty(jobParam) ? null : JSONObject.parseObject(jobParam);
            organizeCode = null == jsonObject ? null : (String) jsonObject.get(MesExtConstWords.ORGANIZE_CODE);
            suitCaseCode = null == jsonObject ? null : (String) jsonObject.get(MesExtConstWords.SUIT_CASE_CODE);
        } catch (JSONException e) {
            LOGGER.info("MES工厂执行生产数据回传软适配JOB --- EXEC ERROR --- 参数[{}]转换异常:[{}] --- THREAD:[{}]", jobParam, e.getMessage(), param.getCurrentThreadName());
        }
        if (StringUtils.isEmpty(organizeCode)) LOGGER.info("MES工厂执行生产数据回传软适配JOB --- EXEC ERROR --- 参数[{}]缺失[工厂代码] --- THREAD:[{}]", jobParam, param.getCurrentThreadName());
        param.organizeCode(organizeCode).suitCaseCodeList(suitCaseCode);
    }
}
