From 6315e15e90054a9de383eefb077f52b488459616 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Mon, 21 Oct 2024 18:31:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=90=8E=E5=8F=B0=E4=BB=BB=E5=8A=A1=E7=9A=84?= =?UTF-8?q?=E4=B8=8A=E6=AC=A1=E5=92=8C=E4=B8=8B=E6=AC=A1=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/taskhub/TaskHubScheduleDTO.java | 3 ++ .../system/mapper/ExtScheduleMapper.java | 3 +- .../system/mapper/ExtScheduleMapper.xml | 7 ++++ .../system/service/BaseTaskHubService.java | 32 +++++++++++++++++-- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/TaskHubScheduleDTO.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/TaskHubScheduleDTO.java index ffd1ce3fe2..aebf249994 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/TaskHubScheduleDTO.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/TaskHubScheduleDTO.java @@ -45,6 +45,9 @@ public class TaskHubScheduleDTO implements Serializable { @Schema(description = "运行规则(cron表达式)") private String value; + @Schema(description = "上次完成时间") + private Long lastTime; + @Schema(description = "下次执行时间") private Long nextTime; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java index e6c9c766c7..6079dadeaf 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.java @@ -42,10 +42,11 @@ public interface ExtScheduleMapper { List getSchedule(@Param("request") TaskCenterScheduleBatchRequest request, @Param("projectIds") List projectIds); - int countByProjectIds(@Param("ids") List ids); List selectScheduleList(@Param("request") BasePageRequest request, @Param("projectIds") List projectIds); List getSchedules(@Param("request") TableBatchProcessDTO request, @Param("projectIds") List projectIds); + + List getLastAndNextTime(@Param("resourceIds") List resourceIds); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml index c7247c8ea5..124a96bf1f 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtScheduleMapper.xml @@ -366,6 +366,13 @@ + diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java index 8857b61cf7..e153657561 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java @@ -51,13 +51,16 @@ import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionUtils; +import org.quartz.CronExpression; import org.quartz.JobKey; import org.quartz.TriggerKey; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; +import java.text.ParseException; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -233,10 +236,35 @@ public class BaseTaskHubService { .flatMap(item -> Stream.of(item.getCreateUserName())) .collect(Collectors.toSet()); Map userMap = userLoginService.getUserNameMap(new ArrayList<>(userSet)); - list.forEach(item -> { + + List resourceIds = list.stream() + .filter(item -> StringUtils.isNotBlank(item.getResourceId())) + .map(TaskHubScheduleDTO::getResourceId) + .toList(); + + Map trigerTimeMap = Map.of(); + if (CollectionUtils.isNotEmpty(resourceIds)) { + trigerTimeMap = extScheduleMapper.getLastAndNextTime(resourceIds) + .stream() + .collect(Collectors.toMap(TaskHubScheduleDTO::getResourceId, Function.identity())); + } + + for (TaskHubScheduleDTO item : list) { item.setCreateUserName(userMap.getOrDefault(item.getCreateUserName(), StringUtils.EMPTY)); item.setOrganizationName(orgMap.getOrDefault(item.getProjectId(), StringUtils.EMPTY)); - }); + if (trigerTimeMap.get(item.getResourceId()) != null) { + item.setNextTime(trigerTimeMap.get(item.getResourceId()).getNextTime()); + item.setLastTime(trigerTimeMap.get(item.getResourceId()).getLastTime()); + } else { + try { + CronExpression cron = new CronExpression(item.getValue()); + Date date = new Date(System.currentTimeMillis()); + item.setNextTime(cron.getNextValidTimeAfter(date).getTime()); + } catch (ParseException e) { + LogUtils.error(e); + } + } + } } }