TaskCancelServiceImpl.java
7.4 KB
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
package com.huaheng.api.wcs.service.taskCancel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaheng.api.general.domain.ReorderDomain;
import com.huaheng.api.general.domain.TaskShipmentOrder;
import com.huaheng.api.wcs.domain.WcsTask;
import com.huaheng.common.constant.HttpConstant;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.restful.RestUtil;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.containerType.domain.ContainerType;
import com.huaheng.pc.config.containerType.service.ContainerTypeService;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.locationType.domain.LocationType;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
@Service
public class TaskCancelServiceImpl implements TaskCancelService {
@Autowired
private AddressService addressService;
@Resource
private TaskHeaderService taskHeaderService;
@Resource
private ContainerTypeService containerTypeService;
@Resource
private ContainerService containerService;
/**
* 取消任务
* 1、判断参数是否为空
* 2、转换实体
* 3、发送数据
*
* @param id 任务ID
* @return 取消结果
*/
@Override
@Transactional(rollbackFor = Exception.class)
public AjaxResult TaskCance(Integer id) {
if (id == null) {
throw new ServiceException("任务号为空");
}
TaskHeader taskHeader = taskHeaderService.getById(id);
if (taskHeader == null) {
throw new ServiceException("任务ID[" + id + "]不存在");
}
String area = null;
String warehouseCode = null;
String url = null;
// 查询容器信息(防null)
LambdaQueryWrapper<Container> containerWrapper = Wrappers.lambdaQuery();
containerWrapper.eq(Container::getCode, taskHeader.getContainerCode());
Container container = containerService.getOne(containerWrapper, false); // 无数据返回null,不抛异常
if (container == null) {
throw new ServiceException("任务关联容器[" + taskHeader.getContainerCode() + "]不存在");
}
// 查询容器类型信息(防null)
LambdaQueryWrapper<ContainerType> containerTypeWrapper = Wrappers.lambdaQuery();
containerTypeWrapper.eq(ContainerType::getCode, container.getContainerType());
ContainerType containerType = containerTypeService.getOne(containerTypeWrapper, false);
if (containerType == null) {
throw new ServiceException("容器类型[" + container.getContainerType() + "]不存在");
}
area = containerType.getArea();
warehouseCode = containerType.getWarehouseCode();
// 处理区域编码特殊逻辑
if (taskHeader.getZoneCode() == null) {
url = addressService.selectAddress(QuantityConstant.ADDRESS_WCS_TASK_CANCEL, warehouseCode, area);
} else {
if ("C".equals(taskHeader.getZoneCode())) {
area = "3";
warehouseCode = taskHeader.getWarehouseCode();
}
url = addressService.selectAddress(QuantityConstant.ADDRESS_WCS_TASK_CANCEL, warehouseCode, area);
}
// URL非空校验
if (StringUtils.isEmpty(url)) {
throw new ServiceException("未获取到WCS取消任务接口地址");
}
// 构建取消任务参数
ReorderDomain reorderDomain = new ReorderDomain();
// 防null:任务ID转字符串
reorderDomain.setTaskNo(Optional.ofNullable(taskHeader.getId()).map(String::valueOf).orElse(""));
// 整盘出库
if (taskHeader.getTaskType()==300) {
LambdaQueryWrapper<TaskHeader> taskWrapper = Wrappers.lambdaQuery();
taskWrapper
.eq(TaskHeader::getShipmentOrder, taskHeader.getShipmentOrder())
.eq(TaskHeader::getTaskType, 300);
List<TaskHeader> taskList = taskHeaderService.list(taskWrapper);
// 排序(升序)
taskList.sort(Comparator.comparing(TaskHeader::getSequence, Comparator.nullsLast(Integer::compareTo)));
// 移除当前任务
taskList.removeIf(task -> id.equals(task.getId()));
// 重新编排序号
int sequence = 1;
for (TaskHeader task : taskList) {
if (task.getStatus() != null && task.getStatus() != 1) {
task.setSequence(sequence++);
task.setSequenceNumber(taskList.size());
taskHeaderService.updateById(task);
}
}
// 构建批量取消参数
for (TaskHeader task : taskList) {
if (task.getStatus() != null && task.getStatus() != 1) {
TaskShipmentOrder shipmentOrder = new TaskShipmentOrder();
shipmentOrder.setTaskNo(Optional.ofNullable(task.getId()).map(String::valueOf).orElse(""));
shipmentOrder.setSequence(task.getSequence());
shipmentOrder.setSequenceNumber(task.getSequenceNumber());
shipmentOrder.setShipmentOrder(task.getShipmentOrder());
reorderDomain.getTaskShipmentOrders().add(shipmentOrder);
}
}
}
// 调用WCS接口取消任务
try {
String jsonParam = new ObjectMapper().writeValueAsString(reorderDomain);
ResponseEntity<JSONObject> result = RestUtil.request_post(url, warehouseCode, jsonParam);
if (result == null || result.getBody() == null) {
throw new ServiceException("接口地址错误或返回为空:" + url);
}
String code = result.getBody().getString("code");
String msg = result.getBody().getString("msg");
if (code == null || Integer.parseInt(code) != HttpConstant.OK) {
throw new ServiceException("WCS接口返回失败:" + msg);
}
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(result.getBody().toString(), AjaxResult.class);
} catch (Exception e) {
throw new ServiceException("调用WCS取消任务失败:" + e.getMessage());
}
}
}