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

import cn.estsh.i3plus.ext.mes.api.busi.ISxDeleteTravelDataService;
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 org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION: 定时删除工单日志数据（因数据量太大影响MES和PCN工单日志数据同步）
 * @USER: xinwang.yi
 * @DATE: 2022-11-11 11:11
 */
@DisallowConcurrentExecution
@ApiOperation("删除履历数据定时任务")
public class SxDeleteTravelDataJob extends BaseImppScheduleJob {

    @Autowired
    private ISxDeleteTravelDataService deleteTravelDataService;

    public SxDeleteTravelDataJob() {
        super(SxDeleteTravelDataJob.class, "删除履历数据定时任务job");
    }

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

        String jobParam = this.getJobParam();
        if (StringUtils.isEmpty(jobParam)) {
            LOGGER.error("MES删除履历数据JOB,未配置工厂代码参数");
            return;
        }

        JSONObject jsonObject;
        try {
            jsonObject = JSONObject.parseObject(jobParam);
        } catch (JSONException e) {
            LOGGER.error("MES删除履历数据JOB,参数异常，参数不是json格式");
            return;
        }

        Object organizationCode = jsonObject.get("organizationCode");

        long startTime = System.currentTimeMillis();

        String[] organizeCodeArray = organizationCode.toString().split(MesExtConstWords.COMMA);

        for (String organizeCode : organizeCodeArray) {
            deleteTravelDataService.doDeleteTravelData(organizeCode);
        }

        long endTime = System.currentTimeMillis();
        LOGGER.info(this.getClass().getSimpleName() + " :ALL:耗时: " + (endTime - startTime) + "ms");
    }
}
