Lius
2025-04-25 c9eea9f24aa52ea59dbb8dcf1cb6cf513ddc74a9
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/job/ScanDowntimeJob.java
@@ -1,110 +1,110 @@
package org.jeecg.modules.mdc.job;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.mdc.dto.MdcEquipmentWaitSectionDto;
import org.jeecg.modules.mdc.entity.MdcDowntime;
import org.jeecg.modules.mdc.entity.MdcEquipmentOvertime;
import org.jeecg.modules.mdc.entity.MdcEquipmentRunningSection;
import org.jeecg.modules.mdc.service.IMdcDowntimeService;
import org.jeecg.modules.mdc.service.IMdcEquipmentRunningSectionService;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.util.ThrowableUtil;
import org.jeecg.modules.quartz.entity.QuartzJob;
import org.jeecg.modules.quartz.entity.SysQuartzLog;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.quartz.service.ISysQuartzLogService;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.quartz.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author: Lius
 * @CreateTime: 2025-03-12
 * @Description: 扫描停机任务
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class ScanDowntimeJob implements Job {
    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改  时间: yyyyMMdd 例: 20230414
     */
    private String parameter;
    public void setParameter(String parameter) {
        this.parameter = parameter;
    }
    @Resource
    private IQuartzJobService quartzJobService;
    @Resource
    private ISysAnnouncementService sysAnnouncementService;
    @Resource
    private ISysQuartzLogService sysQuartzLogService;
    @Resource
    private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService;
    @Resource
    private IMdcDowntimeService mdcDowntimeService;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        SysQuartzLog quartzLog = new SysQuartzLog();
        quartzLog.setCreateTime(new Date());
        List<QuartzJob> byJobClassName = this.quartzJobService.findByJobClassName(this.getClass().getName());
        if (byJobClassName != null && !byJobClassName.isEmpty()) {
            quartzLog.setJobId(byJobClassName.get(0).getId());
        }
        quartzLog.setParams(this.parameter);
        log.info("定时扫描待机时长超20分钟设备任务 ScanDowntimeJob start!  时间:{}, 参数:{}", DateUtils.getNow(), this.parameter);
        long startTime = System.currentTimeMillis();
        try {
            String date = "";
            if (StringUtils.isNotBlank(this.parameter)) {
                date = DateUtils.format(DateUtils.toDate(this.parameter, DateUtils.STRDATE), DateUtils.STR_DATE);
            } else {
                date = DateUtils.format(DateUtils.getNow(), DateUtils.STR_DATE);
            }
            mdcDowntimeService.remove(new LambdaQueryWrapper<MdcDowntime>().eq(MdcDowntime::getTheDate, date).eq(MdcDowntime::getStatus, CommonConstant.DOWNTIME_STATUS_0));
            List<MdcEquipmentWaitSectionDto> mdcEquipmentRunningSections = mdcEquipmentRunningSectionService.findWaitList(date);
            if (mdcEquipmentRunningSections != null && !mdcEquipmentRunningSections.isEmpty()) {
                String finalDate = date;
                List<MdcDowntime> downtimeList = mdcEquipmentRunningSections.stream().map(mdcEquipmentWaitSectionDto -> {
                    MdcDowntime downtime = new MdcDowntime();
                    downtime.setEquipmentId(mdcEquipmentWaitSectionDto.getEquipmentId());
                    downtime.setEquipmentName(mdcEquipmentWaitSectionDto.getEquipmentName());
                    downtime.setTheDate(finalDate);
                    downtime.setStartDate(mdcEquipmentWaitSectionDto.getStartTime());
                    downtime.setEndDate(mdcEquipmentWaitSectionDto.getEndTime());
                    return downtime;
                }).collect(Collectors.toList());
                if (!downtimeList.isEmpty()) {
                    mdcDowntimeService.saveBatch(downtimeList);
                }
            }
            quartzLog.setIsSuccess(0);
        } catch (Exception e) {
            quartzLog.setIsSuccess(-1);
            quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(e));
            // 发送消息通知
            sysAnnouncementService.jobSendMessage("定时扫描待机时长超20分钟设备任务", quartzLog.getExceptionDetail());
        }
        long endTime = System.currentTimeMillis();
        quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime)));
        sysQuartzLogService.save(quartzLog);
    }
}
//package org.jeecg.modules.mdc.job;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.StringUtils;
//import org.jeecg.common.constant.CommonConstant;
//import org.jeecg.modules.mdc.dto.MdcEquipmentWaitSectionDto;
//import org.jeecg.modules.mdc.entity.MdcDowntime;
//import org.jeecg.modules.mdc.entity.MdcEquipmentOvertime;
//import org.jeecg.modules.mdc.entity.MdcEquipmentRunningSection;
//import org.jeecg.modules.mdc.service.IMdcDowntimeService;
//import org.jeecg.modules.mdc.service.IMdcEquipmentRunningSectionService;
//import org.jeecg.modules.mdc.util.DateUtils;
//import org.jeecg.modules.mdc.util.ThrowableUtil;
//import org.jeecg.modules.quartz.entity.QuartzJob;
//import org.jeecg.modules.quartz.entity.SysQuartzLog;
//import org.jeecg.modules.quartz.service.IQuartzJobService;
//import org.jeecg.modules.quartz.service.ISysQuartzLogService;
//import org.jeecg.modules.system.service.ISysAnnouncementService;
//import org.quartz.*;
//
//import javax.annotation.Resource;
//import java.util.Date;
//import java.util.List;
//import java.util.stream.Collectors;
//
///**
// * @Author: Lius
// * @CreateTime: 2025-03-12
// * @Description: 扫描停机任务
// */
//@PersistJobDataAfterExecution
//@DisallowConcurrentExecution
//@Slf4j
//public class ScanDowntimeJob implements Job {
//
//    /**
//     * 若参数变量名修改 QuartzJobController中也需对应修改  时间: yyyyMMdd 例: 20230414
//     */
//    private String parameter;
//
//    public void setParameter(String parameter) {
//        this.parameter = parameter;
//    }
//
//    @Resource
//    private IQuartzJobService quartzJobService;
//
//    @Resource
//    private ISysAnnouncementService sysAnnouncementService;
//
//    @Resource
//    private ISysQuartzLogService sysQuartzLogService;
//
//    @Resource
//    private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService;
//
//    @Resource
//    private IMdcDowntimeService mdcDowntimeService;
//
//    @Override
//    public void execute(JobExecutionContext context) throws JobExecutionException {
//        SysQuartzLog quartzLog = new SysQuartzLog();
//        quartzLog.setCreateTime(new Date());
//        List<QuartzJob> byJobClassName = this.quartzJobService.findByJobClassName(this.getClass().getName());
//        if (byJobClassName != null && !byJobClassName.isEmpty()) {
//            quartzLog.setJobId(byJobClassName.get(0).getId());
//        }
//        quartzLog.setParams(this.parameter);
//        log.info("定时扫描待机时长超20分钟设备任务 ScanDowntimeJob start!  时间:{}, 参数:{}", DateUtils.getNow(), this.parameter);
//        long startTime = System.currentTimeMillis();
//        try {
//            String date = "";
//            if (StringUtils.isNotBlank(this.parameter)) {
//                date = DateUtils.format(DateUtils.toDate(this.parameter, DateUtils.STRDATE), DateUtils.STR_DATE);
//
//            } else {
//                date = DateUtils.format(DateUtils.getNow(), DateUtils.STR_DATE);
//            }
//            mdcDowntimeService.remove(new LambdaQueryWrapper<MdcDowntime>().eq(MdcDowntime::getTheDate, date).eq(MdcDowntime::getStatus, CommonConstant.DOWNTIME_STATUS_0));
//
//            List<MdcEquipmentWaitSectionDto> mdcEquipmentRunningSections = mdcEquipmentRunningSectionService.findWaitList(date);
//
//            if (mdcEquipmentRunningSections != null && !mdcEquipmentRunningSections.isEmpty()) {
//                String finalDate = date;
//                List<MdcDowntime> downtimeList = mdcEquipmentRunningSections.stream().map(mdcEquipmentWaitSectionDto -> {
//                    MdcDowntime downtime = new MdcDowntime();
//                    downtime.setEquipmentId(mdcEquipmentWaitSectionDto.getEquipmentId());
//                    downtime.setEquipmentName(mdcEquipmentWaitSectionDto.getEquipmentName());
//                    downtime.setTheDate(finalDate);
//                    downtime.setStartDate(mdcEquipmentWaitSectionDto.getStartTime());
//                    downtime.setEndDate(mdcEquipmentWaitSectionDto.getEndTime());
//                    return downtime;
//                }).collect(Collectors.toList());
//                if (!downtimeList.isEmpty()) {
//                    mdcDowntimeService.saveBatch(downtimeList);
//                }
//            }
//            quartzLog.setIsSuccess(0);
//        } catch (Exception e) {
//            quartzLog.setIsSuccess(-1);
//            quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(e));
//            // 发送消息通知
//            sysAnnouncementService.jobSendMessage("定时扫描待机时长超20分钟设备任务", quartzLog.getExceptionDetail());
//        }
//        long endTime = System.currentTimeMillis();
//        quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime)));
//        sysQuartzLogService.save(quartzLog);
//    }
//}