liuyu
21 小时以前 6eef0b6730d940ef44e0f66dd8e7d178f1cd2d1f
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package jnpf.tmsController;
 
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.tmsEntity.task.TmsFromPlanResult;
import jnpf.tmsEntity.task.TmsTaskForm;
import jnpf.tmsEntity.task.TmsTaskQuery;
import jnpf.tmsService.TmsTaskService;
import jnpf.util.JsonUtil;
import lombok.RequiredArgsConstructor;
import jnpf.util.StringUtil;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 培训任务管理。网关 /api/tms/task/** (StripPrefix=2)→ /task/**
 */
@Tag(name = "TMS培训任务", description = "Task")
@RestController
@RequestMapping("/task")
@RequiredArgsConstructor
public class TmsTaskController {
 
    private final TmsTaskService taskService;
 
    @Operation(summary = "培训任务分页列表")
    @GetMapping("/list")
    public ActionResult<PageListVO<TmsTaskForm>> list(TmsTaskQuery query) {
        List<TmsTaskForm> list = taskService.getList(query);
        PageListVO<TmsTaskForm> vo = new PageListVO<>();
        vo.setList(list);
        vo.setPagination(JsonUtil.getJsonToBean(query, PaginationVO.class));
        return ActionResult.success(vo);
    }
 
    @Operation(summary = "培训任务详情")
    @GetMapping("/{id}")
    public ActionResult<TmsTaskForm> info(@PathVariable("id") String id) {
        return ActionResult.success(taskService.getInfo(id));
    }
 
    @Operation(summary = "新增培训任务")
    @PostMapping
    public ActionResult<String> create(@RequestBody TmsTaskForm form) {
        return ActionResult.success(taskService.create(form));
    }
 
    @Operation(summary = "更新培训任务")
    @PutMapping("/{id}")
    public ActionResult<String> update(@PathVariable("id") String id, @RequestBody TmsTaskForm form) {
        taskService.update(id, form);
        return ActionResult.success("更新成功");
    }
 
    @Operation(summary = "删除培训任务(仅草稿)")
    @DeleteMapping("/{id}")
    public ActionResult<String> delete(@PathVariable("id") String id) {
        taskService.delete(id);
        return ActionResult.success("删除成功");
    }
 
    @Operation(summary = "发布培训任务")
    @PutMapping("/{id}/publish")
    public ActionResult<String> publish(@PathVariable("id") String id) {
        taskService.publish(id);
        return ActionResult.success("发布成功");
    }
 
    @Operation(summary = "取消/终止培训任务")
    @PutMapping("/{id}/cancel")
    public ActionResult<String> cancel(@PathVariable("id") String id) {
        taskService.cancel(id);
        return ActionResult.success("取消成功");
    }
 
    @Operation(summary = "批量取消培训任务")
    @PutMapping("/cancelBatch")
    public ActionResult<String> cancelBatch(@RequestBody Map<String, List<String>> body) {
        taskService.cancelBatch(body == null ? null : body.get("ids"));
        return ActionResult.success("取消成功");
    }
 
    @Operation(summary = "恢复为草稿")
    @PutMapping("/{id}/restore")
    public ActionResult<String> restore(@PathVariable("id") String id) {
        taskService.restore(id);
        return ActionResult.success("已恢复为草稿");
    }
 
    @Operation(summary = "年计划审批通过:按子表生成未发布培训任务")
    @PostMapping("/from-annual/{planId}")
    public ActionResult<TmsFromPlanResult> fromAnnual(@PathVariable("planId") String planId) {
        return ActionResult.success(taskService.createFromAnnual(planId));
    }
 
    @Operation(summary = "年计划审批通过")
    @PostMapping("/from-annual")
    public ActionResult<TmsFromPlanResult> fromAnnualBody(
            @RequestBody(required = false) Map<String, Object> body,
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "planId", required = false) String planIdParam,
            @RequestParam(value = "f_id", required = false) String fId) {
        Map<String, Object> merged = new LinkedHashMap<>();
        if (body != null) {
            merged.putAll(body);
        }
        // 数据接口预览常把「接口参数」放 query,不进 JSON Body
        putIfAbsent(merged, "id", id);
        putIfAbsent(merged, "planId", planIdParam);
        putIfAbsent(merged, "f_id", fId);
        return ActionResult.success(taskService.createFromAnnual(pickPlanId(merged)));
    }
 
    @Operation(summary = "岗位培训计划审批通过:按子表生成未发布培训任务")
    @PostMapping("/from-post/{planId}")
    public ActionResult<TmsFromPlanResult> fromPost(@PathVariable("planId") String planId) {
        return ActionResult.success(taskService.createFromPost(planId));
    }
 
    @Operation(summary = "岗位培训计划审批通过")
    @PostMapping("/from-post")
    public ActionResult<TmsFromPlanResult> fromPostBody(
            @RequestBody(required = false) Map<String, Object> body,
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "planId", required = false) String planIdParam,
            @RequestParam(value = "f_id", required = false) String fId) {
        Map<String, Object> merged = new LinkedHashMap<>();
        if (body != null) {
            merged.putAll(body);
        }
        putIfAbsent(merged, "id", id);
        putIfAbsent(merged, "planId", planIdParam);
        putIfAbsent(merged, "f_id", fId);
        return ActionResult.success(taskService.createFromPost(pickPlanId(merged)));
    }
 
    private static void putIfAbsent(Map<String, Object> map, String key, String value) {
        if (StringUtil.isEmpty(value)) {
            return;
        }
        Object existed = map.get(key);
        if (existed == null || StringUtil.isEmpty(String.valueOf(existed))
                || "null".equalsIgnoreCase(String.valueOf(existed))
                || "value".equalsIgnoreCase(String.valueOf(existed).trim())) {
            map.put(key, value.trim());
        }
    }
 
    private static String pickPlanId(Map<String, Object> body) {
        if (body == null || body.isEmpty()) {
            return null;
        }
        for (String key : new String[]{"id", "planId", "f_id", "F_Id", "formId"}) {
            Object v = body.get(key);
            if (v == null) {
                continue;
            }
            String text = String.valueOf(v).trim();
            if (StringUtil.isEmpty(text) || "null".equalsIgnoreCase(text) || "value".equalsIgnoreCase(text)) {
                continue;
            }
            return text;
        }
        return null;
    }
}