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


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

import java.util.List;

/**
 * @Description : 生产开模统计job
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/6/26 18:22
 * @Modify:
 **/
@Slf4j
@DisallowConcurrentExecution
@Component
@ApiOperation("生产开模统计job")
public class MesProdMouldStatisticsJob extends BaseMesScheduleJob {

    @Autowired
    private IMesProdMouldStatisticsService mesProdMouldStatisticsService;

    public MesProdMouldStatisticsJob() {
        super(MesProdMouldStatisticsJob.class, "生产开模统计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 {
                    long startTime = System.currentTimeMillis();
                    mesProdMouldStatisticsService.doSumMesProdMouldStatistics(wmsJobParamModel.getOrganizeCode(), wmsJobParamModel.getPageSize(), "job");
                    long endTime = System.currentTimeMillis();
                    log.info("工厂{}生产开模统计job --- END --- 耗时: {} ms", wmsJobParamModel.getOrganizeCode(), endTime - startTime);
                } catch (Exception e) {
                    log.info("工厂{}生产开模统计job 执行失败{}", wmsJobParamModel.getOrganizeCode(), e);
                }
                //发送数据
                try {
                    long startTime = System.currentTimeMillis();
                    mesProdMouldStatisticsService.doSendMesProdMouldStatistics(wmsJobParamModel.getOrganizeCode(), "job");
                    long endTime = System.currentTimeMillis();
                    log.info("工厂{}生产开模统计发送数据 --- END --- 耗时: {} ms", wmsJobParamModel.getOrganizeCode(), endTime - startTime);
                } catch (Exception e) {
                    log.info("工厂{}生产开模统计发送数据 执行失败{}", wmsJobParamModel.getOrganizeCode(), e);

                }
            }
        }
    }

}
