#
luxiaotao1123
2 天以前 3e6cd2231fc99a855129d1a293b8d65adf1683b5
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
package com.zy.acs.manager.core.integrate.wms;
 
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.R;
import com.zy.acs.manager.common.annotation.OperationLog;
import com.zy.acs.manager.common.constant.Constants;
import com.zy.acs.manager.common.domain.TaskBoolDto;
import com.zy.acs.manager.core.integrate.dto.OpenBusCancelParam;
import com.zy.acs.manager.core.integrate.dto.OpenBusCancelResult;
import com.zy.acs.manager.core.integrate.dto.OpenBusSubmitParam;
import com.zy.acs.manager.core.service.MainService;
import com.zy.acs.manager.manager.entity.Bus;
import com.zy.acs.manager.manager.entity.IntegrationRecord;
import com.zy.acs.manager.manager.entity.Task;
import com.zy.acs.manager.manager.enums.TaskStsType;
import com.zy.acs.manager.manager.service.BusService;
import com.zy.acs.manager.manager.service.TaskService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * Created by vincent on 2023/6/12
 */
@Slf4j
@Api(tags = "Open Api")
@RestController
@RequestMapping("/api/open")
public class OpenController {
 
    @Autowired
    private MainService mainService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private BusService busService;
 
    @PostMapping("/bus/submit")
    @OperationLog("generate task from open api")
    public R submit(@RequestBody OpenBusSubmitParam param) {
        IntegrationRecord integrationRecord = new IntegrationRecord(
                null,    // 编号
                null,    // 名称空间
                null,    // 接口地址
                null,    // 平台密钥
                null,    // 调用方标识
                null,    // 方向[非空]
                null,    // 时间戳
                null,    // 客户端IP
                null,    // 请求内容
                null,    // 响应内容
                null,    // 异常内容
                null,    // 结果
                null,    // 耗时
                null,    // 状态
                null,    // 添加时间[非空]
                null,    // 修改时间[非空]
                null    // 备注
        );
 
        mainService.generateBusAndTask(param, null);
        return R.ok("generate tasks success");
    }
 
    @PostMapping("/task/cancel")
    @OperationLog("cancel task from open api")
    public R cancel(@RequestBody OpenBusCancelParam param) {
        if (Cools.isEmpty(param.getBatchNo())) {
            return R.error("batchNo is empty");
        }
        if (Cools.isEmpty(param.getTasks())) {
            return R.error("tasks is empty");
        }
        Bus bus = busService.selectByBusNo(param.getBatchNo());
        if (null == bus) {
            return R.error("batchNo is not exist");
        }
        OpenBusCancelResult result = new OpenBusCancelResult();
        result.setBatchNo(param.getBatchNo());
        for (String taskNo : param.getTasks()) {
            Task task = taskService.selectBySeqNum(bus.getId(), taskNo);
            if (null == task) {
                result.getTasks().add(new TaskBoolDto(taskNo, Boolean.FALSE, "task " + taskNo + " is not exist"));
                continue;
            }
            if (!task.getTaskSts().equals(TaskStsType.INIT.val())) {
                result.getTasks().add(new TaskBoolDto(taskNo, Boolean.FALSE,  "task " + taskNo + " has already been assigned"));
                continue;
            }
            Boolean cancel = false;
            try {
                cancel = taskService.cancel(task.getId(), null, Constants.UPLINK);
            } catch (Exception e) {
                log.error("failed to cancel task {}", taskNo, e);
            }
            result.getTasks().add(new TaskBoolDto(taskNo, cancel, "failed to cancel task " + taskNo));
        }
        return R.ok("cancel tasks success").add(result);
    }
 
}