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

import cn.estsh.i3plus.ext.mes.api.base.bu3.ISxAssistOutAndEnterService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu3.SxAssistOutAndEnterModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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;

import java.util.Calendar;
import java.util.Objects;

@DisallowConcurrentExecution
@ApiOperation("MES援交信息自动确认JOB")
public class SxAssistOutAndEnterAutoConfirmJob extends BaseImppScheduleJob {

    public SxAssistOutAndEnterAutoConfirmJob() { super(SxAssistOutAndEnterAutoConfirmJob.class, "MES援交信息自动确认JOB"); }

    @Autowired
    private ISxAssistOutAndEnterService assistOutAndEnterService;

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

        SxAssistOutAndEnterModel model = jobParam(this.getJobParam());
        if (StringUtils.isEmpty(model)) return;

        String organizeCode = model.getOrganizeCode();

        StopWatch stopWatch = new StopWatch();

        stopWatch.start();

        String dateYm = TimeTool.getToday().substring(0, 7);

        LOGGER.info("MES工厂:{},DATE:{},LEVEL:{}执行援交信息自动确认JOB --- EXEC START --- THREAD:[{}]", organizeCode, dateYm, model.getLevel(),Thread.currentThread().getName());

        try {
            model.setInfluenceDateStart(getBeforeMonthFirstDay(dateYm));
            model.setInfluenceDateEnd(dateYm+MesExtConstWords.DATE_DD_01);
            if (Objects.equals(model.getLevel(), MesExtEnumUtil.ASSIST_OUT_AND_ENTER_CONFIRM_LEVEL.TAKE_ON.getValue()) ||
                    Objects.equals(model.getLevel(), MesExtEnumUtil.ASSIST_OUT_AND_ENTER_CONFIRM_LEVEL.PERSON_IN_CHARGE.getValue())){
                model.setConfirm(false);
            }else if (Objects.equals(model.getLevel(), MesExtEnumUtil.ASSIST_OUT_AND_ENTER_CONFIRM_LEVEL.RESPONSIBLE_MINISTERGE.getValue())){
                model.setResponsibleMinistergeConfirmBool(false);
            }else {
                LOGGER.info("MES工厂执行援交信息自动确认JOB --- EXEC ERROR --- 参数[{}]级别维护有误,应为[级别10/20/30]其中一项", model.getLevel());
            }
            Integer count = assistOutAndEnterService.doAssistOutAndEnterAutoConfirm(model);

            LOGGER.info("MES工厂:{},DATE:{},LEVEL:{}执行援交信息自动确认JOB --- EXEC SUCCESS --- COUNT:[{}] --- THREAD:[{}]", organizeCode, dateYm,model.getLevel(), count, Thread.currentThread().getName());

        } finally {

            stopWatch.stop();

            LOGGER.info("MES工厂:{},DATE:{},LEVEL:{}执行援交信息自动确认JOB --- EXEC END --- JOB总耗时:[{}] --- THREAD:[{}]", organizeCode, dateYm, model.getLevel(), stopWatch.getTotalTimeMillis(), Thread.currentThread().getName());

        }

    }

    private String getBeforeMonthFirstDay(String dateYm) {
        return TimeTool.timeCalc(TimeTool.stringParseToDate(dateYm, MesExtConstWords.DATE_FORMAT_YM), Calendar.MONTH, -1, MesExtConstWords.DATE_FORMAT_YM) + MesExtConstWords.DATE_DD_01;
    }

    private SxAssistOutAndEnterModel jobParam(String jobParam) {
        SxAssistOutAndEnterModel model = null;
        try {
            model = JSONObject.parseObject(jobParam, SxAssistOutAndEnterModel.class);
        } catch (JSONException e) {
            LOGGER.info("MES工厂执行援交信息自动确认JOB --- EXEC ERROR --- 参数[{}]转换异常:[{}]", jobParam, e.getMessage());
        }
        if (model == null){
            LOGGER.info("MES工厂执行援交信息自动确认JOB --- EXEC ERROR --- 参数[{}]缺失参数", jobParam);
        }else if (StringUtils.isEmpty(model.getOrganizeCode())){
            LOGGER.info("MES工厂执行援交信息自动确认JOB --- EXEC ERROR --- 参数[{}]缺失[工厂代码]", jobParam);
        }else if (StringUtils.isEmpty(model.getLevel())){
            LOGGER.info("MES工厂执行援交信息自动确认JOB --- EXEC ERROR --- 参数[{}]缺失[级别(level:10/20/30)]", jobParam);
        }
        return model;
    }
}
