skyouc
2024-12-21 c635d78b479510ebe2556a420948effcd30a0731
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.zy.asrs.wms.apis.wcs.schedule;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.apis.wcs.entity.domain.Constant;
import com.zy.asrs.wms.apis.wcs.entity.request.ConveyorStarParam;
import com.zy.asrs.wms.apis.wcs.entity.request.PublishTasksParam;
import com.zy.asrs.wms.apis.wcs.entity.request.TaskDescribe;
import com.zy.asrs.wms.apis.wcs.entity.request.TaskParam;
import com.zy.asrs.wms.apis.wcs.entity.response.CommonReponse;
import com.zy.asrs.wms.asrs.entity.Task;
import com.zy.asrs.wms.asrs.entity.enums.TaskStsType;
import com.zy.asrs.wms.asrs.service.TaskService;
import com.zy.asrs.wms.asrs.service.WorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
 
@Slf4j
@Component
public class ScheduleJobs {
 
    @Autowired
    private DataSourceTransactionManager transactionManager;
 
    @Autowired
    private TaskService taskService;
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Autowired
    private WorkService workService;
 
 
    /***
     * 通知ESS输送线流动
     */
    //    @Scheduled(cron = "0/3 * * * * ? ")
    public void conveyorStart() {
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                .eq(Task::getTaskSts, TaskStsType.WCS_CONTAINER_RECEIVE.id).eq(Task::getStatus, 0));
        tasks.forEach(task -> {
            try {
                ConveyorStarParam conveyorStarParam = new ConveyorStarParam();
                conveyorStarParam.setSlotCode(task.getOriginLoc())
                        .setContainerCode(task.getBarcode());
                //调用三方接口,将任务推送至ESS平台
                MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
                // 设置请求参数
                params.add("params", JSONObject.toJSONString(conveyorStarParam));
                log.info("请求地址:{},请求参数:{}", Constant.CONVEYOR_START, JSONObject.toJSONString(conveyorStarParam));
                HttpHeaders headers = new HttpHeaders();
                headers.add("Content-Type", "application/json");
                HttpEntity httpEntity = new HttpEntity<>(params, headers);
                // 请求
                ResponseEntity<String> exchange = restTemplate.exchange(Constant.ISSUE_TASK_OF_PUTAWAY, HttpMethod.POST, httpEntity, String.class);
                log.info("下发流动通知 返回结果:{}", exchange);
                if (exchange.getBody() == null) {
                    throw new CoolException("下发流动通知失败!!");
                } else {
                    CommonReponse commonReponse = JSON.toJavaObject(JSON.parseObject(exchange.getBody()), CommonReponse.class);
                    if (commonReponse.getCode() == 0) {
                        log.info(task.getTaskNo() + "下发流动通知"  + commonReponse.getMsg());
                    } else {
                        throw new CoolException("下发流动通知失败!!");
                    }
                }
            } catch (Exception ex) {
                log.error(ex.getMessage());
            } finally {
                //如果异常修改禁用状态
                taskService.update(new LambdaUpdateWrapper<Task>().set(Task::getStatus, 1).eq(Task::getId, task.getId()));
            }
        });
 
    }
 
 
    /***
     * 每隔3秒,获取库中状态为『任务完成』且状态『sucess』的订单
     * 并将任务变为历史档
     */
    //    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    public void completeTaskSchedule() {
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                        .eq(Task::getTaskType, 1)
                        .eq(Task::getTaskSts, TaskStsType.WCS_PUTAWAY_SUCESS)
                        .eq(Task::getExcudeStatus, "sucess"))
                .stream().sorted(Comparator.comparing(Task::getTaskSts))
                .collect(Collectors.toList());
 
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        // TODO 单个执行失败后, 加入失败列表,添加失败处理字段,避免重复查询调用
//        def.setName("SomeTxName");
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = transactionManager.getTransaction(def);
        try {
            tasks.forEach(task -> {
                workService.completeTask(task.getId());
            });
            // execute your business logic here
            //db operation
        } catch (Exception ex) {
            transactionManager.rollback(status);
            throw ex;
        }
 
    }
 
 
    /***
     * 每隔10秒,刷新当前通知档列表,下发待入库订单至ESS
     * 查询当前任务列表,
     */
//    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional(rollbackFor = Exception.class)
    public void waitPakinSchedule() {
        //获取当前任务档中,所有为待入库状态的任务档,按时间升序排列
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                        .eq(Task::getTaskType, 1)
                        .eq(Task::getTaskSts, TaskStsType.GENERATE_IN))
                .stream().sorted(Comparator.comparing(Task::getTaskSts))
                .collect(Collectors.toList());
 
        // 数据组装
        PublishTasksParam tasksParam = new PublishTasksParam();
        //TODO 确认是否需要单任务多容器码的需求,目前系统都是单容器码生成单任务,多任务明细(物料混装)
        tasks.forEach(task -> {
            List<TaskParam> params = new ArrayList<>();
            TaskParam param = new TaskParam();
            //设置容器编码
            param.setTaskCode(task.getTaskNo());
            List<TaskDescribe> taskDescribes = new ArrayList<>();
            TaskDescribe describe = new TaskDescribe();
            //设置目标库位,站点
            describe.setContainerCode(task.getBarcode())
                    .setToLocationCode(task.getTargetLoc())
                    .setToStationCode(task.getTargetSite());
            taskDescribes.add(describe);
            param.setTaskDescribe(taskDescribes);
            params.add(param);
            tasksParam.setTasks(params);
        });
 
        tasksParam.setTaskType("putaway");
 
        // TODO 多任务多订单,统一调度,是否会出现部分成功,部分失败的情况
        //调用三方接口,将任务推送至ESS平台
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        // 设置请求参数
        params.add("params", JSONObject.toJSONString(tasksParam));
        log.info("请求地址:{},请求参数:{}", Constant.ISSUE_TASK_OF_PUTAWAY, JSONObject.toJSONString(tasksParam));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        HttpEntity httpEntity = new HttpEntity<>(params, headers);
        // 请求
        ResponseEntity<String> exchange = restTemplate.exchange(Constant.ISSUE_TASK_OF_PUTAWAY, HttpMethod.POST, httpEntity, String.class);
        log.info("下发任务 返回结果:{}", exchange);
        if (exchange.getBody() == null) {
            throw new CoolException("下发任务失败!!");
        } else {
            CommonReponse reponse = (CommonReponse) JSON.parse(exchange.getBody());
            if (reponse.getCode() == 0) {
                //请求成功后,统一修改所有任务档状态为入库执行中。
                tasks.forEach(task -> {
                    taskService.update(new LambdaUpdateWrapper<Task>()
                            .set(Task::getTaskSts, TaskStsType.WCS_EXECUTE_IN.id)
                            .eq(Task::getBarcode, task.getBarcode()));
                });
            } else {
                // TODO 请求失败需确认是否存在部分成功的情况,部分成功需要单独刷新成功的任务档状态
                throw new CoolException(reponse.getMsg());
            }
        }
    }
 
    /***
     * 每隔10秒执行,刷新当前任务列表,下发任务到ESS
     */
    public void flowNotify() {
 
    }
}