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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesWorkOrderService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.tool.JsonUtilTool;
import cn.estsh.i3plus.pojo.model.wms.WmsJobParamModel;
import cn.estsh.impp.framework.boot.init.ApplicationProperties;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description : 排序报工job
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/28 11:17
 * @Modify:
 **/
@Slf4j
@DisallowConcurrentExecution
@Component
@ApiOperation("MES汇报报工job")
public class MesProductReportJob extends BaseMesScheduleJob {

    @Autowired
    private IMesWorkOrderService workOrderService;

    public MesProductReportJob() {
        super(MesProductReportJob.class, "MES汇报报工job");
    }

    @Override
    public void executeMesJob(JobExecutionContext context, ApplicationProperties applicationProperties) {
        List<WmsJobParamModel> wmsJobParamModelList = JsonUtilTool.toList(this.getJobParam(), WmsJobParamModel.class);
        if (!CollectionUtils.isEmpty(wmsJobParamModelList)) {
            for (WmsJobParamModel wmsJobParamModel : wmsJobParamModelList) {
                //排序汇报
                try {
                    log.info("工厂{}排序报工job --- START --- ", wmsJobParamModel.getOrganizeCode());
                    long startTime = System.currentTimeMillis();
                    workOrderService.doMesWorkOrderSortReport(wmsJobParamModel.getOrganizeCode(), wmsJobParamModel.getPageSize(), MesPcnExtConstWords.JOB);
                    long endTime = System.currentTimeMillis();
                    log.info("工厂{}排序报工job --- END --- 耗时: {} ms", wmsJobParamModel.getOrganizeCode(), endTime - startTime);
                } catch (Exception e) {
                    log.info("工厂{}排序报工job 执行失败{}", wmsJobParamModel.getOrganizeCode(), e);
                }
                //非排序汇报
                try {
                    log.info("工厂{}非排序报工JOB --- START --- ", wmsJobParamModel.getOrganizeCode());
                    long startTime = System.currentTimeMillis();
                    workOrderService.doMesWorkOrderNoSortReport(wmsJobParamModel.getOrganizeCode(), wmsJobParamModel.getPageSize(), MesPcnExtConstWords.JOB);
                    long endTime = System.currentTimeMillis();
                    log.info("工厂{}非排序报工JOB --- END --- 耗时: {} ms", wmsJobParamModel.getOrganizeCode(), endTime - startTime);
                } catch (Exception e) {
                    log.info("工厂{}非排序报工JOB 执行失败{}", wmsJobParamModel.getOrganizeCode(), e);
                }
            }
        }else{
            log.info("排序报工job 参数不能为空");
        }
    }
}
