peng
2025-11-06 c4938f6f4e839890b032c75c7a57333a6a9157a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.rongyichuang.review.service;
 
import com.rongyichuang.review.dto.response.ReviewExportJobStatus;
import com.rongyichuang.review.dto.response.ReviewExportResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.concurrent.*;
 
/**
 * 异步评审导出任务服务
 */
@Service
@Slf4j
public class ReviewExportJobService {
 
    private final ReviewExportService reviewExportService;
 
    private final ExecutorService executor = Executors.newFixedThreadPool(Math.max(2, Runtime.getRuntime().availableProcessors() / 2));
 
    private final ConcurrentHashMap<String, ReviewExportJobStatus> jobStatusMap = new ConcurrentHashMap<>();
 
    public ReviewExportJobService(ReviewExportService reviewExportService) {
        this.reviewExportService = reviewExportService;
    }
 
    /**
     * 启动导出任务
     */
    public String startJob(Long activityId, List<Long> stageIds) {
        String jobId = UUID.randomUUID().toString();
        ReviewExportJobStatus init = new ReviewExportJobStatus(jobId, ReviewExportJobStatus.Status.PENDING, null, null, 0);
        jobStatusMap.put(jobId, init);
 
        executor.submit(() -> {
            ReviewExportJobStatus running = new ReviewExportJobStatus(jobId, ReviewExportJobStatus.Status.RUNNING, null, null, 10);
            jobStatusMap.put(jobId, running);
            try {
                ReviewExportResponse res = reviewExportService.exportReviewZip(activityId, stageIds);
                if (res != null && res.isSuccess()) {
                    ReviewExportJobStatus done = new ReviewExportJobStatus(jobId, ReviewExportJobStatus.Status.SUCCEEDED, res.getUrl(), res.getMessage(), 100);
                    jobStatusMap.put(jobId, done);
                } else {
                    String msg = res != null ? res.getMessage() : "导出失败";
                    ReviewExportJobStatus failed = new ReviewExportJobStatus(jobId, ReviewExportJobStatus.Status.FAILED, null, msg, 100);
                    jobStatusMap.put(jobId, failed);
                }
            } catch (Exception e) {
                log.error("导出任务执行失败, jobId: {}", jobId, e);
                ReviewExportJobStatus failed = new ReviewExportJobStatus(jobId, ReviewExportJobStatus.Status.FAILED, null, e.getMessage(), 100);
                jobStatusMap.put(jobId, failed);
            }
        });
 
        return jobId;
    }
 
    /**
     * 查询任务状态
     */
    public ReviewExportJobStatus getStatus(String jobId) {
        return jobStatusMap.get(jobId);
    }
 
}