feat(dms): 添加润滑计划任务调度功能

- 新增润滑计划任务调度相关代码
- 实现润滑计划任务的创建、更新和状态管理
- 优化了任务调度相关的数据结构和接口
- 添加了任务调度的分布式事务管理
master
zch 2 months ago
parent c776ff3fce
commit 8c197350db

@ -99,8 +99,6 @@ public class DmsPlanLube extends TenantEntity {
private String cronExpression;
/**
*
*/

@ -35,7 +35,7 @@ public class DmsPlanLubeDetail extends TenantEntity {
/**
* IDprod_base_machine_infodevice_id
*/
private Long machineId;
private Long deviceId;
/**
* ,

@ -22,7 +22,7 @@ import java.util.List;
*/
@Data
@EqualsAndHashCode(callSuper = true)
@AutoMapper(target = DmsPlanLube.class, reverseConvertGenerate = false)
@AutoMapper(target = DmsPlanLube.class)
public class DmsPlanLubeBo extends BaseEntity {
/**

@ -81,14 +81,14 @@ public class DmsPlanLubeDetailServiceImpl implements IDmsPlanLubeDetailService {
.leftJoin(DmsBaseLubeStation.class,DmsBaseLubeStation::getLubeStationId,DmsPlanLubeDetail::getLubeStationId)
//关联查询设备
.select(DmsBaseMachineInfo::getMachineName)
.leftJoin(DmsBaseMachineInfo.class,DmsBaseMachineInfo::getMachineId,DmsPlanLubeDetail::getMachineId)
.leftJoin(DmsBaseMachineInfo.class,DmsBaseMachineInfo::getMachineId,DmsPlanLubeDetail::getDeviceId)
//关联查询润滑标准
.select(DmsBaseLubeStandard::getLubeStandardCode)
.leftJoin(DmsBaseLubeStandard.class,DmsBaseLubeStandard::getLubeStandardId,DmsPlanLubeDetail::getLubeStandardId)
.eq(bo.getPlanLubeDetailId() != null, DmsPlanLubeDetail::getPlanLubeDetailId, bo.getPlanLubeDetailId())
.eq(bo.getPlanLubeId() != null, DmsPlanLubeDetail::getPlanLubeId, bo.getPlanLubeId())
.eq(bo.getDeviceId() != null, DmsPlanLubeDetail::getMachineId, bo.getDeviceId())
.eq(bo.getDeviceId() != null, DmsPlanLubeDetail::getDeviceId, bo.getDeviceId())
.eq(bo.getLubeStationId() != null, DmsPlanLubeDetail::getLubeStationId, bo.getLubeStationId())
.eq(bo.getLubeStandardId() != null, DmsPlanLubeDetail::getLubeStandardId, bo.getLubeStandardId())
.eq(StringUtils.isNotBlank(bo.getOperationDescription()), DmsPlanLubeDetail::getOperationDescription, bo.getOperationDescription())
@ -160,14 +160,14 @@ public class DmsPlanLubeDetailServiceImpl implements IDmsPlanLubeDetailService {
MPJLambdaWrapper<DmsPlanLubeDetail> wrapper = JoinWrappers.lambda(DmsPlanLubeDetail.class)
.selectAs(DmsPlanLubeDetail::getPlanLubeDetailId, DmsPlanLubeDetail::getPlanLubeDetailId)
.selectAs(DmsPlanLubeDetail::getPlanLubeId, DmsPlanLubeDetail::getPlanLubeId)
.selectAs(DmsPlanLubeDetail::getMachineId, DmsPlanLubeDetail::getMachineId)
.selectAs(DmsPlanLubeDetail::getDeviceId, DmsPlanLubeDetail::getDeviceId)
.selectAs(DmsPlanLubeDetail::getLubeStationId, DmsPlanLubeDetail::getLubeStationId)
.selectAs(DmsPlanLubeDetail::getLubeStandardId, DmsPlanLubeDetail::getLubeStandardId)
.selectAs(DmsPlanLubeDetail::getOperationDescription, DmsPlanLubeDetail::getOperationDescription)
.selectAs(DmsBaseMachineInfo::getMachineName, DmsPlanLubeDetail::getMachineName)
.selectAs(DmsBaseLubeStation::getLubeStationName, DmsPlanLubeDetail::getLubeStationName)
.selectAs(DmsBaseLubeStandard::getLubeStandardCode, DmsPlanLubeDetail::getLubeStandardCode)
.leftJoin(DmsBaseMachineInfo.class, DmsBaseMachineInfo::getMachineId, DmsPlanLubeDetail::getMachineId)
.leftJoin(DmsBaseMachineInfo.class, DmsBaseMachineInfo::getMachineId, DmsPlanLubeDetail::getDeviceId)
.leftJoin(DmsBaseLubeStation.class, DmsBaseLubeStation::getDeviceTypeId, DmsBaseMachineInfo::getMachineId)
.leftJoin(DmsBaseLubeStandard.class, DmsBaseLubeStandard::getLubeStandardId, DmsPlanLubeDetail::getLubeStandardId)
.eq(DmsPlanLubeDetail::getPlanLubeId, planLubeId);

@ -3,7 +3,10 @@ package org.dromara.dms.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.uuid.Seq;
@ -21,12 +24,20 @@ import org.dromara.dms.utils.CronUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.aizuda.snailjob.client.job.core.enums.AllocationAlgorithmEnum;
import com.aizuda.snailjob.client.job.core.enums.TriggerTypeEnum;
import com.aizuda.snailjob.client.job.core.openapi.SnailJobOpenApi;
import com.aizuda.snailjob.common.core.enums.JobBlockStrategyEnum;
import com.aizuda.snailjob.client.job.core.dto.JobResponseVO;
import com.aizuda.snailjob.common.core.enums.StatusEnum;
import org.dromara.job.api.model.DmsJobEntity;
import org.dromara.job.api.RemoteJobService;
/**
* Service
*
@ -41,6 +52,9 @@ public class DmsPlanLubeServiceImpl implements IDmsPlanLubeService {
private final DmsBillsLubeInstanceMapper dmsBillsLubeInstanceMapper;
@DubboReference
private final RemoteJobService remoteJobService;
private static final Long SECONDS_PER_DAY = 24 * 60 * 60L;
private static final Long SECONDS_PER_HOUR = 60 * 60L;
@ -160,7 +174,7 @@ public class DmsPlanLubeServiceImpl implements IDmsPlanLubeService {
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
@GlobalTransactional(rollbackFor = Exception.class)
public Boolean insertByBo(DmsPlanLubeBo bo) {
// 生成新的润滑计划编号
String planLubeCode = Seq.getId(Seq.dmsPlanLubeSeqType, Seq.dmsPlanLubeCode);
@ -179,6 +193,23 @@ public class DmsPlanLubeServiceImpl implements IDmsPlanLubeService {
// 添加任务到远程服务并获取任务ID
R<Integer> jobIdR = remoteJobService.add(SecurityConstants.INNER, job);*/
if(bo.getActiveFlag().equals("1")){//激活标识1是 0否
DmsJobEntity dmsJobEntity = new DmsJobEntity();
dmsJobEntity.setRouteKey(AllocationAlgorithmEnum.ROUND)//路由策略:轮询
.setJobName("润滑计划编号" + planLubeCode + "工单任务")
.setExecutorInfo("stanceLube")//执行器名称
.setExecutorTimeout(30)//执行超时时间
.setBlockStrategy(JobBlockStrategyEnum.CONCURRENCY)//阻塞策略:并发
.setMaxRetryTimes(2)//最大重试次数
.setTriggerType(TriggerTypeEnum.CRON)//触发器类型cron
.setTriggerInterval(bo.getCronExpression())//间隔时长: cron表达式
.setRetryInterval(3)//重试间隔时长
.setArgsStr("planLubeCode")
.setExtAttrs(planLubeCode);
R<Long> longR = remoteJobService.addClusterJob(dmsJobEntity);
Long jobId = longR.getData();
// 通过cron表达式获取下一次执行时间
String cronExpression = bo.getCronExpression();
if (StringUtils.isNotEmpty(cronExpression)) {
@ -187,7 +218,9 @@ public class DmsPlanLubeServiceImpl implements IDmsPlanLubeService {
}
// 设置润滑计划的任务ID
/* bo.setJobId(jobIdR.getData().longValue());*/
bo.setJobId(jobId);
}
// 计算润滑计划的时间限制(天和小时)
Long timeLimitDays = bo.getTimeLimitDays() == null ? 0L : bo.getTimeLimitDays();
@ -214,7 +247,7 @@ public class DmsPlanLubeServiceImpl implements IDmsPlanLubeService {
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean updateByBo(DmsPlanLubeBo bo) {
/* Long jobId = bo.getJobId();*/
Long jobId = bo.getJobId();
String cronExpression = bo.getCronExpression();
if (StringUtils.isNotEmpty(cronExpression)) {
@ -222,9 +255,9 @@ public class DmsPlanLubeServiceImpl implements IDmsPlanLubeService {
Date nextExecution = CronUtils.getNextExecution(cronExpression);
bo.setLubeTime(nextExecution);
/* if (jobId == null) {
// 如果没有关联任务,创建新任务
String planLubeCode = bo.getPlanLubeCode();
if (bo.getActiveFlag().equals("1")){//激活标识1是 0否
if (jobId == null ) {// 如果没有关联任务,创建新任务
/* String planLubeCode = bo.getPlanLubeCode();
SysJob job = new SysJob();
job.setJobName("润滑计划编号" + planLubeCode + "工单任务");
job.setJobGroup("DEFAULT");
@ -234,15 +267,59 @@ public class DmsPlanLubeServiceImpl implements IDmsPlanLubeService {
job.setConcurrent("1");
job.setStatus("1");
R<Integer> jobIdR = remoteJobService.add(SecurityConstants.INNER, job);
bo.setJobId(jobIdR.getData().longValue());
} else {
// 更新现有任务
bo.setJobId(jobIdR.getData().longValue());*/
String planLubeCode = bo.getPlanLubeCode();
Long newJobId = SnailJobOpenApi.addClusterJob()
.setRouteKey(AllocationAlgorithmEnum.ROUND)//路由策略:轮询
.setJobName("润滑计划编号" + planLubeCode + "工单任务")
.setExecutorInfo("stanceLube")//执行器名称
.addArgsStr("planLubeCode", planLubeCode)//执行任务参数
.setExecutorTimeout(30)//执行超时时间
.setBlockStrategy(JobBlockStrategyEnum.CONCURRENCY)//阻塞策略:并发
.setMaxRetryTimes(2)//最大重试次数
.setTriggerType(TriggerTypeEnum.CRON)//触发器类型cron
.setTriggerInterval(bo.getCronExpression())//间隔时长: cron表达式
.setRetryInterval(3)//重试间隔时长
.execute();
bo.setJobId(newJobId);
} else {// 如果有关联任务,更新状态
/* // 更新现有任务
SysJob sysJob = remoteJobService.getJobInfo(jobId, SecurityConstants.INNER);
if (sysJob != null) {
sysJob.setCronExpression(bo.getCronExpression());
remoteJobService.update(SecurityConstants.INNER, sysJob);
}
}*/
String planLubeCode = bo.getPlanLubeCode();
JobResponseVO execute = SnailJobOpenApi.getJobDetail(jobId).execute();
if (execute != null) {
SnailJobOpenApi
.updateJobStatus(jobId)
.setStatus(StatusEnum.YES)
.execute();
SnailJobOpenApi.updateClusterJob(jobId)
.setRouteKey(AllocationAlgorithmEnum.ROUND)//路由策略:轮询
.setJobName("润滑计划编号" + planLubeCode + "工单任务")
.setExecutorInfo("stanceLube")//执行器名称
.addArgsStr("planLubeCode", planLubeCode)//执行任务参数
.setExecutorTimeout(30)//执行超时时间
.setBlockStrategy(JobBlockStrategyEnum.CONCURRENCY)//阻塞策略:并发
.setMaxRetryTimes(2)//最大重试次数
.setTriggerType(TriggerTypeEnum.CRON)//触发器类型cron
.setTriggerInterval(bo.getCronExpression())//间隔时长: cron表达式
.setRetryInterval(3)//重试间隔时长
.execute();
}
}
}else{//如果激活状态为01是 0否
if (jobId != null) {//如果关联了任务,则任务状态为关闭
SnailJobOpenApi
.updateJobStatus(jobId)
.setStatus(StatusEnum.NO)//任务状态0关闭1开启
.execute();
}
}
}
// 计算润滑计划的时间限制(天和小时)

@ -33,7 +33,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<resultMap type="DmsPlanLubeDetail" id="DmsPlanLubeDetailResult">
<result property="planLubeDetailId" column="sub_plan_lube_detail_id" />
<result property="planLubeId" column="sub_plan_lube_id" />
<result property="machineId" column="sub_machine_id" />
<result property="deviceId" column="sub_machine_id" />
<result property="lubeStationId" column="sub_lube_station_id" />
<result property="lubeStandardId" column="sub_lube_standard_id" />
<result property="operationDescription" column="sub_operation_description" />
@ -47,9 +47,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="selectDmsPlanLubeJoinByPlanLubeCode" parameterType="String" resultMap="DmsPlanLubeDmsPlanLubeDetailResult">
select a.plan_lube_id, a.plan_lube_code, a.lube_level, a.lube_group, a.lube_supervisor, a.lube_time, a.time_limit, a.job_id, a.cycle_period, a.create_method, a.active_flag, a.remark, a.create_by, a.create_time, a.update_by, a.update_time,
b.plan_lube_detail_id as sub_plan_lube_detail_id,
/* b.plan_lube_detail_id as sub_plan_lube_detail_id,
b.plan_lube_id as sub_plan_lube_id,
b.machine_id as sub_machine_id,
b.device_id as sub_machine_id,
b.lube_station_id as sub_lube_station_id,
b.lube_standard_id as sub_lube_standard_id,
b.operation_description as sub_operation_description,
@ -58,9 +58,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
b.create_by as sub_create_by,
b.create_time as sub_create_time,
b.update_by as sub_update_by,
b.update_time as sub_update_time
b.update_time as sub_update_time*/
from dms_plan_lube a
left join dms_plan_lube_detail b on b.plan_lube_id = a.plan_lube_id
/* left join dms_plan_lube_detail b on b.plan_lube_id = a.plan_lube_id*/
where a.plan_lube_code = #{planLubeCode}
</select>

Loading…
Cancel
Save