malei05 1 tháng trước cách đây
mục cha
commit
2cd64a907a

+ 24 - 33
src/main/java/net/yyc/quartz/service/impl/JobBriefingServiceImpl.java

@@ -13,6 +13,7 @@ import org.springframework.stereotype.Service;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -27,6 +28,8 @@ public class JobBriefingServiceImpl implements JobBriefingService {
     private final JobBriefingMapper jobBriefingMapper;
     private final SysJobService sysJobService;
 
+    private static final DateTimeFormatter defaultDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+
     @Override
     public JobBriefing generateBriefing(LocalDate date) {
         LocalDateTime startTime = date.atStartOfDay();
@@ -48,12 +51,6 @@ public class JobBriefingServiceImpl implements JobBriefingService {
                 .map(SysJob::getJobId)
                 .collect(Collectors.toSet());
 
-        // 4. 获取任务ID到任务的映射
-        Map<Integer, SysJob> jobMap = new HashMap<>();
-        for (SysJob job : runningJobList) {
-            jobMap.put(job.getJobId(), job);
-        }
-
         // 5. 查询昨日执行统计
         List<Map<String, Object>> statSummary = jobBriefingMapper.selectJobStatSummary(startTime, endTime);
         Map<Integer, Map<String, Object>> statMap = new HashMap<>();
@@ -64,16 +61,14 @@ public class JobBriefingServiceImpl implements JobBriefingService {
 
         // 6. 构建任务统计列表
         List<JobBriefing.JobStatInfo> jobStats = new ArrayList<>();
-        for (Integer jobId : allJobIds) {
-            SysJob sysJob = jobMap.get(jobId);
-            Map<String, Object> stat = statMap.get(jobId);
+        for (SysJob sysJob : runningJobList) {
+            Map<String, Object> stat = statMap.get(sysJob.getJobId());
 
-            String jobName = sysJob != null ? sysJob.getJobName() : String.valueOf(stat != null ? stat.get("job_name") : "未知");
-            String jobGroup = sysJob != null ? sysJob.getJobGroup() : String.valueOf(stat != null ? stat.get("job_group") : "未知");
+            String jobName = sysJob.getJobName();
+            String jobGroup = sysJob.getJobGroup();
 
             int execCount = 0, successCount = 0, failCount = 0;
             long avgExecuteTime = 0;
-            String lastExecuteTime = null;
             String lastExecStatus = null;
             String exceptionInfo = null;
 
@@ -85,21 +80,17 @@ public class JobBriefingServiceImpl implements JobBriefingService {
                 if (avgObj != null) {
                     avgExecuteTime = ((Number) avgObj).longValue();
                 }
-                lastExecuteTime = stat.get("lastExecuteTime") != null
-                        ? String.valueOf(stat.get("lastExecuteTime")) : null;
             }
 
             // 查询最新执行记录
-            if (endTime != null) {
-                Map<String, Object> lastLog = jobBriefingMapper.selectLastExecLog(jobId, LocalDateTime.now());
-                if (lastLog != null) {
-                    lastExecStatus = "1".equals(String.valueOf(lastLog.get("job_log_status"))) ? "失败" : "成功";
-                    Object exInfo = lastLog.get("exception_info");
-                    if (exInfo != null && !"null".equals(String.valueOf(exInfo))) {
-                        exceptionInfo = String.valueOf(exInfo);
-                        if (exceptionInfo.length() > 200) {
-                            exceptionInfo = exceptionInfo.substring(0, 200) + "...";
-                        }
+            Map<String, Object> lastLog = jobBriefingMapper.selectLastExecLog(sysJob.getJobId(), LocalDateTime.now());
+            if (lastLog != null) {
+                lastExecStatus = "1".equals(String.valueOf(lastLog.get("job_log_status"))) ? "失败" : "成功";
+                Object exInfo = lastLog.get("exception_info");
+                if (exInfo != null && !"null".equals(String.valueOf(exInfo))) {
+                    exceptionInfo = String.valueOf(exInfo);
+                    if (exceptionInfo.length() > 200) {
+                        exceptionInfo = exceptionInfo.substring(0, 200) + "...";
                     }
                 }
             }
@@ -107,18 +98,18 @@ public class JobBriefingServiceImpl implements JobBriefingService {
             double successRate = execCount > 0 ? (double) successCount / execCount * 100 : 0;
 
             jobStats.add(JobBriefing.JobStatInfo.builder()
-                    .jobId(jobId)
+                    .jobId(sysJob.getJobId())
                     .jobName(jobName)
                     .jobGroup(jobGroup)
-                    .jobStatus(sysJob != null ? sysJob.getJobStatus() : null)
-                    .nextExecuteTime(sysJob != null && sysJob.getNextTime() != null
-                            ? sysJob.getNextTime().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")) : null)
+                    .jobStatus(sysJob.getJobStatus())
+                    .nextExecuteTime(sysJob.getNextTime() != null
+                            ? sysJob.getNextTime().format(defaultDateTimeFormatter) : null)
                     .execCount(execCount)
                     .successCount(successCount)
                     .failCount(failCount)
                     .successRate(Math.round(successRate * 100) / 100.0)
                     .avgExecuteTime(avgExecuteTime)
-                    .lastExecuteTime(lastExecuteTime)
+                    .lastExecuteTime(sysJob.getPreviousTime() != null ? sysJob.getPreviousTime().format(defaultDateTimeFormatter) : "")
                     .lastExecStatus(lastExecStatus)
                     .exceptionInfo(exceptionInfo)
                     .build());
@@ -148,10 +139,10 @@ public class JobBriefingServiceImpl implements JobBriefingService {
      * 条件:任务状态=RUNNING(2) 且 next_time已过 且 昨日无执行记录
      */
     private List<JobBriefing.MissedJobInfo> detectMissedJobs(LocalDate date,
-                                                              LocalDateTime startTime,
-                                                              LocalDateTime endTime,
-                                                              List<Integer> executedJobIds,
-                                                              List<SysJob> runningJobList) {
+                                                             LocalDateTime startTime,
+                                                             LocalDateTime endTime,
+                                                             List<Integer> executedJobIds,
+                                                             List<SysJob> runningJobList) {
         List<JobBriefing.MissedJobInfo> missed = new ArrayList<>();
         Set<Integer> executedSet = new HashSet<>(executedJobIds);
         LocalDateTime now = LocalDateTime.now();