huang
2025-11-17 92a5d16f0474ead87744b4ca5cb04417247a0619
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.mes.controller;
 
import com.mes.entity.PlcTestTask;
import com.mes.service.PlcTestTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * PLC测试任务控制器
 * 专注于任务管理相关的API接口
 * 
 * @author huang
 * @date 2025/10/30
 */
@Slf4j
@RestController
@RequestMapping("tasks")
@Api(tags = "PLC测试任务管理")
public class PlcTestTaskController {
 
    @Autowired
    private PlcTestTaskService plcTestTaskService;
 
    /**
     * 获取任务列表
     * 根据项目ID获取任务列表
     * 注意:projectId为必填参数
     */
    @GetMapping("/list")
    @ApiOperation("获取任务列表")
    public Map<String, Object> getTasksByProjectId(@RequestParam(required = true) String projectId) {
        log.info("获取任务列表,projectId: {}", projectId);
        
        Map<String, Object> result = new HashMap<>();
        try {
            // 由于使用了required=true,Spring会自动进行非空校验
            // 额外的格式校验
            if (projectId.trim().isEmpty()) {
                result.put("code", 400);
                result.put("message", "项目ID不能为空字符串");
                return result;
            }
            
            // 调用服务获取任务列表
            java.util.List<PlcTestTask> tasks = plcTestTaskService.getTasksByProjectId(projectId);
            
            result.put("code", 200);
            result.put("message", "获取任务列表成功");
            result.put("data", tasks);
        } catch (Exception e) {
            log.error("获取任务列表异常,projectId: {}", projectId, e);
            result.put("code", 500);
            result.put("message", "获取任务列表异常: " + e.getMessage());
        }
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
 
    /**
     * 获取任务详情
     * 根据任务ID获取任务详情
     * 注意:id为必填参数
     */
    @GetMapping("/detail")
    @ApiOperation("获取任务详情")
    public Map<String, Object> getTaskDetail(@RequestParam(required = true) Long id) {
        log.info("获取任务详情,id: {}", id);
        
        Map<String, Object> result = new HashMap<>();
        try {
            // 调用服务获取任务详情
            PlcTestTask task = plcTestTaskService.getTaskById(id);
            
            if (task != null) {
                result.put("code", 200);
                result.put("message", "获取任务详情成功");
                result.put("data", task);
            } else {
                result.put("code", 404);
                result.put("message", "未找到任务记录");
            }
        } catch (Exception e) {
            log.error("获取任务详情异常,id: {}", id, e);
            result.put("code", 500);
            result.put("message", "获取任务详情异常: " + e.getMessage());
        }
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
 
    /**
     * 更新任务状态
     * 注意:id和status均为必填参数
     */
    @PostMapping("/update-status")
    @ApiOperation("更新任务状态")
    public Map<String, Object> updateTaskStatus( @RequestParam(required = true) Long id,
                                               @RequestParam(required = true) String status) {
        log.info("更新任务状态,id: {}, status: {}", id, status);
        
        Map<String, Object> result = new HashMap<>();
        try {
            // 额外的格式校验
            if (status.trim().isEmpty()) {
                result.put("code", 400);
                result.put("message", "状态不能为空字符串");
                return result;
            }
            
            // 调用服务更新任务状态
            plcTestTaskService.updateTaskStatus(id, status);
            
            result.put("code", 200);
            result.put("message", "任务状态更新成功");
        } catch (Exception e) {
            log.error("更新任务状态异常,id: {}", id, e);
            result.put("code", 500);
            result.put("message", "更新任务状态异常: " + e.getMessage());
        }
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
 
    /**
     * 获取运行中的任务
     * 注意:projectId为必填参数
     */
    @GetMapping("/running")
    @ApiOperation("获取运行中的任务")
    public Map<String, Object> getRunningTasks(@RequestParam(required = true) String projectId) {
        log.info("获取运行中的任务,projectId: {}", projectId);
        
        Map<String, Object> result = new HashMap<>();
        try {
            // 额外的格式校验
            if (projectId.trim().isEmpty()) {
                result.put("code", 400);
                result.put("message", "项目ID不能为空字符串");
                return result;
            }
            
            // 调用服务获取运行中的任务
            java.util.List<PlcTestTask> tasks = plcTestTaskService.getRunningTasks(projectId);
            
            result.put("code", 200);
            result.put("message", "获取运行中的任务成功");
            result.put("data", tasks);
        } catch (Exception e) {
            log.error("获取运行中的任务异常,projectId: {}", projectId, e);
            result.put("code", 500);
            result.put("message", "获取运行中的任务异常: " + e.getMessage());
        }
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
}