You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-wms-java/src/main/java/com/glxp/api/task/DevicePlanTask.java

123 lines
4.9 KiB
Java

package com.glxp.api.task;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glxp.api.dao.inv.DeviceInspectPlanDao;
import com.glxp.api.dao.schedule.ScheduledDao;
import com.glxp.api.entity.inv.DeviceInspectPlanEntity;
import com.glxp.api.entity.system.ScheduledEntity;
import com.glxp.api.req.system.ScheduledRequest;
import com.glxp.api.service.inv.DeviceInspectPlanService;
import com.glxp.api.util.IntUtil;
import com.glxp.api.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Slf4j
@Component
@EnableScheduling
public class DevicePlanTask implements SchedulingConfigurer {
@Resource
private ScheduledDao scheduledDao;
@Resource
RedisUtil redisUtil;
@Resource
private DeviceInspectPlanService deviceInspectPlanService;
@Resource
private DeviceInspectPlanDao deviceInspectPlanDao;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(() -> process(),
triggerContext -> {
ScheduledRequest scheduledRequest = new ScheduledRequest();
scheduledRequest.setCronName("devPlanTask");
ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest);
if (scheduledEntity == null)
return null;
String cron = scheduledEntity.getCron();
if (cron.isEmpty()) {
log.error("cron is null");
}
return new CronTrigger(cron).nextExecutionTime(triggerContext);
});
}
private void process() {
//查询已运行巡检计划
List<DeviceInspectPlanEntity> deviceInspectTaskEntities =
deviceInspectPlanDao.selectList(new QueryWrapper<DeviceInspectPlanEntity>().eq("planStatus", 2));
if (CollUtil.isNotEmpty(deviceInspectTaskEntities)) {
for (DeviceInspectPlanEntity deviceInspectPlanEntity : deviceInspectTaskEntities) {
//计划已结束
if (deviceInspectPlanEntity.getEndTime().compareTo(new Date()) < 0) {
deviceInspectPlanEntity.setPlanStatus(3);
deviceInspectPlanDao.updateById(deviceInspectPlanEntity);
continue;
}
if (deviceInspectPlanEntity.getLastTime() == null) {
genTask(deviceInspectPlanEntity);
} else if (StrUtil.isNotEmpty(deviceInspectPlanEntity.getFrequency())) {
long time = 0;
//按时间频率生成任务
switch (deviceInspectPlanEntity.getFrequencyUnit()) {
case 1://年
time = IntUtil.value(deviceInspectPlanEntity.getFrequency()) * 365 * 24 * 60 * 60 * 1000 * 1l;
break;
case 2://月
time = IntUtil.value(deviceInspectPlanEntity.getFrequency()) * 30 * 24 * 60 * 60 * 1000 * 1l;
break;
case 3://日
time = IntUtil.value(deviceInspectPlanEntity.getFrequency()) * 24 * 60 * 60 * 1000 * 1l;
break;
case 4://小时
time = IntUtil.value(deviceInspectPlanEntity.getFrequency()) * 60 * 60 * 1000 * 1l;
break;
case 5://分
time = IntUtil.value(deviceInspectPlanEntity.getFrequency()) * 60 * 1000 * 1l;
break;
case 6://秒
time = IntUtil.value(deviceInspectPlanEntity.getFrequency()) * 1000 * 1l;
break;
default:
break;
}
long interval = new Date().getTime() - deviceInspectPlanEntity.getLastTime().getTime();
if (interval > time) {
genTask(deviceInspectPlanEntity);
}
} else if (deviceInspectPlanEntity.getUseFrequency() != null) {
//按使用频率生成任务
}
}
}
}
/**
* 生成任务
*/
public void genTask(DeviceInspectPlanEntity deviceInspectPlanEntity) {
deviceInspectPlanService.genTaskDev(deviceInspectPlanEntity);
}
}