package org.jeecg.modules.mdc.job;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import lombok.extern.slf4j.Slf4j;
|
import org.jeecg.common.api.dto.message.MessageDTO;
|
import org.jeecg.common.constant.CommonConstant;
|
import org.jeecg.common.system.api.ISysBaseAPI;
|
import org.jeecg.common.system.vo.DictModel;
|
import org.jeecg.modules.mdc.entity.MdcEquipment;
|
import org.jeecg.modules.mdc.service.IMdcEquipmentRunningSectionService;
|
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
|
import org.jeecg.modules.mdc.service.MdcEfficiencyReportService;
|
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.jeecg.modules.system.service.ISysDictService;
|
import org.quartz.*;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.time.LocalDate;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* @author Lius
|
* @Description: 利用率正常值,设备状态长期无变化报警
|
* @date 2024/1/17 14:10
|
*/
|
@PersistJobDataAfterExecution
|
@DisallowConcurrentExecution
|
@Slf4j
|
public class RunningEquipmentStatusJob implements Job {
|
|
@Resource
|
private IQuartzJobService quartzJobService;
|
|
@Resource
|
private ISysQuartzLogService sysQuartzLogService;
|
|
@Resource
|
private ISysAnnouncementService sysAnnouncementService;
|
|
@Resource
|
private IMdcEquipmentService mdcEquipmentService;
|
|
@Resource
|
private ISysDictService sysDictService;
|
|
@Resource
|
private MdcEfficiencyReportService mdcEfficiencyReportService;
|
|
@Resource
|
private ISysBaseAPI sysBaseApi;
|
|
@Resource
|
private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService;
|
|
@Override
|
public void execute(JobExecutionContext jobExecutionContext) 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());
|
}
|
log.info("设备状态长期无变化报警任务 RunningEquipmentStatusJob start! 时间:" + DateUtils.getNow());
|
long startTime = System.currentTimeMillis();
|
try {
|
List<MdcEquipment> equipmentList = mdcEquipmentService.list(new LambdaQueryWrapper<>());
|
//List<MdcEquipment> equipmentList = mdcEquipmentService.list(new LambdaQueryWrapper<MdcEquipment>().eq(MdcEquipment::getEquipmentId, "2140198"));
|
// 获取利用率判定天数
|
List<DictModel> dictModelList1 = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_EQUIPMENT_RATE_JUDGE);
|
Integer equipmentRateJudge = 5;
|
if (dictModelList1 != null && !dictModelList1.isEmpty()) {
|
equipmentRateJudge = Integer.valueOf(dictModelList1.get(0).getValue());
|
}
|
// 获取利用率正常值
|
List<DictModel> dictModelList2 = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_EQUIPMENT_SPEED_JUDGE);
|
Integer equipmentRateNormal = 20;
|
if (dictModelList2 != null && !dictModelList2.isEmpty()) {
|
equipmentRateNormal = Integer.valueOf(dictModelList2.get(0).getValue());
|
}
|
// 获取设备状态天数
|
List<DictModel> dictModelList3 = sysDictService.queryEnableDictItemsByCode(CommonConstant.DICT_EQUIPMENT_RATE_NORMAL);
|
Integer equipmentStatusJudge = 5;
|
if (dictModelList3 != null && !dictModelList3.isEmpty()) {
|
equipmentStatusJudge = Integer.valueOf(dictModelList3.get(0).getValue());
|
}
|
for (MdcEquipment mdcEquipment : equipmentList) {
|
// 判断利用率
|
String date = DateUtils.format(DateUtils.toDate(LocalDate.now().plusDays(-equipmentRateJudge).toString(), DateUtils.STR_DATE), DateUtils.STRDATE);
|
List<BigDecimal> efficiencyRateList = mdcEfficiencyReportService.getEfficiencyRate(mdcEquipment.getEquipmentId(), date);
|
if (efficiencyRateList != null && !efficiencyRateList.isEmpty()) {
|
boolean flag = true;
|
for (BigDecimal processLong : efficiencyRateList) {
|
BigDecimal efficiencyRate = processLong.divide(new BigDecimal("86400"), 4, RoundingMode.HALF_UP);
|
if (efficiencyRate.compareTo(new BigDecimal(equipmentRateNormal)) > -1) {
|
flag = false;
|
}
|
}
|
if (flag) {
|
// 上报
|
MessageDTO messageDTO = new MessageDTO();
|
messageDTO.setCategory("预警消息");
|
messageDTO.setFromUser("admin");
|
messageDTO.setToUser("admin");
|
messageDTO.setContent("设备编号为 [" + mdcEquipment.getEquipmentId() + "] 的设备利用率低于正常值报警!");
|
sysBaseApi.sendSysAnnouncement(messageDTO);
|
}
|
}
|
// 判断设备状态
|
Date date1 = DateUtils.toDate(LocalDate.now().plusDays(-equipmentStatusJudge).toString(), DateUtils.STR_DATE);
|
List<Integer> sectionList = mdcEquipmentRunningSectionService.getDataList(mdcEquipment.getEquipmentId(), date1);
|
if (sectionList != null && !sectionList.isEmpty() && sectionList.size() > 1) {
|
boolean flag = true;
|
Integer integer = sectionList.get(0);
|
for (Integer integer1 : sectionList) {
|
if (!integer.equals(integer1)) {
|
flag = false;
|
}
|
}
|
if (flag) {
|
// 上报
|
MessageDTO messageDTO = new MessageDTO();
|
messageDTO.setCategory("预警消息");
|
messageDTO.setFromUser("admin");
|
messageDTO.setToUser("admin");
|
messageDTO.setContent("设备编号为 [" + mdcEquipment.getEquipmentId() + "] 的设备状态长期无变化报警!");
|
sysBaseApi.sendSysAnnouncement(messageDTO);
|
}
|
}
|
}
|
quartzLog.setIsSuccess(0);
|
} catch (Exception e) {
|
quartzLog.setIsSuccess(-1);
|
quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(e));
|
// 发送消息通知
|
sysAnnouncementService.jobSendMessage("设备状态长期无变化报警任务失败!", quartzLog.getExceptionDetail());
|
}
|
long endTime = System.currentTimeMillis();
|
quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime)));
|
sysQuartzLogService.save(quartzLog);
|
}
|
}
|