TaskBackQueue.java
9.64 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
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
206
207
208
209
210
211
package com.huaheng.pc.task.taskHeader.service;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.ReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.service.ShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.ShipmentHeaderService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskBackDomain;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class TaskBackQueue implements BackQueue{
@Resource
private ReceiptHeaderService receiptHeaderService;
@Resource
private ReceiptDetailService receiptDetailService;
@Resource
private ShipmentHeaderService shipmentHeaderService;
@Resource
private ShipmentDetailService shipmentDetailService;
@Resource
private TaskHeaderService taskHeaderService;
@Resource
private TaskDetailService taskDetailService;
@Override
public void receiveBack(int taskId, CallBack callBack) {
Map<String,String> backMsg = new HashMap<>();
Integer type = null;
TaskBackDomain domain = new TaskBackDomain().setIsSuc(false)
.setBackNum(QuantityConstant.ISRETURN_FAIL);
AjaxResult ajaxResult = AjaxResult.success();
if(StringUtils.isNull(taskId)){
callBack.back(domain.setMsg("任务id不能为空"));
return;
}
TaskHeader header = taskHeaderService.getById(taskId);
if(header == null){
callBack.back(domain.setMsg("找不到任务id为:"+taskId+"的任务"));
return;
}
switch (header.getTaskType()){
case 500 :
case 600 :
case 800 :
case 900 :
case 1000 :
case 1100 :
case 1200 :
domain.setBackNum(QuantityConstant.NOT_RETURN).setMsg("无需回传").setIsSuc(true);
callBack.back(domain);
return;
}
List<TaskDetail> taskDetails = taskDetailService.findByTaskId(taskId);
if(taskDetails==null || taskDetails.size()==0){
callBack.back(domain.setMsg("找不到任务id为:"+taskId+"的任务,没有任务明细"));
return;
}
List<String> billCodes = taskDetails.stream().map(TaskDetail::getBillCode).collect(Collectors.toList());
Map<Integer, BigDecimal> detailsQtyMap =new HashMap<>();
for (TaskDetail taskDetail : taskDetails) {
BigDecimal bigDecimal = detailsQtyMap.get(taskDetail.getBillDetailId());
if(bigDecimal!=null){
detailsQtyMap.put(taskDetail.getBillDetailId(),bigDecimal.add(taskDetail.getQty()));
}else{
detailsQtyMap.put(taskDetail.getBillDetailId(),taskDetail.getQty());
}
}
switch (header.getInternalTaskType()){
case 100 :
String receiptHeaderId = "";
LambdaQueryWrapper<ReceiptHeader> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.in(ReceiptHeader::getCode,billCodes)
.eq(ReceiptHeader::getWarehouseCode,header.getWarehouseCode());
//获取任务中的入库单
List<ReceiptHeader> list = receiptHeaderService.list(queryWrapper);
for (ReceiptHeader receiptHeader : list) {
if(StringUtils.isEmpty(receiptHeader.getReferCode())){
domain.setIsSuc(true).setMsg("无需回传").setBackNum(QuantityConstant.NOT_RETURN);
callBack.back(domain);
continue;
}
if(taskAllComplete(receiptHeader.getCode())){
continue;
}
receiptHeaderId = receiptHeader.getId().toString();
//获取未回传任务集
List<TaskDetail> unBackTask = getUnBackTask(receiptHeader.getCode());
if(unBackTask.size()==0){
continue;
}
Map<Integer, BigDecimal> taskFinishMaps = getTaskFinishMaps(unBackTask);
ajaxResult = receiptHeaderService.postBack(header,receiptHeaderId, taskFinishMaps);
switch (receiptHeader.getReceiptType()){
case "SI" :
receiptHeaderService.postBack2Mes(header,receiptHeaderId, taskFinishMaps);
break;
}
String msg = ajaxResult.getMsg();
List<Map> list1 = JSON.parseArray(msg, Map.class);
if (!ajaxResult.hasErr()) {
switch (receiptHeader.getReceiptType()){
case "PI" :
receiptHeaderService.postBack2SRM(receiptHeader,list1,taskFinishMaps);
break;
}
}
if(!ajaxResult.hasErr()){
for (TaskDetail taskDetail : unBackTask) {
taskDetail.setBack(1);
}
taskDetailService.updateBatchById(unBackTask);
}
backMsg.put(receiptHeader.getCode(),ajaxResult.getMsg());
type = 1;
}
break;
case 200 :
String shipmentHeaderId = "";
//需回传的任务
LambdaQueryWrapper<ShipmentHeader> queryWrapper1 = Wrappers.lambdaQuery();
queryWrapper1.in(ShipmentHeader::getCode,billCodes)
.eq(ShipmentHeader::getWarehouseCode,header.getWarehouseCode());
//获取任务中的出库单
List<ShipmentHeader> list1 = shipmentHeaderService.list(queryWrapper1);
//遍历回传每个出库单
for (ShipmentHeader shipmentHeader : list1) {
if(StringUtils.isEmpty(shipmentHeader.getReferCode())){
domain.setIsSuc(true).setMsg("无需回传").setBackNum(QuantityConstant.NOT_RETURN);
continue;
}
if(taskAllComplete(shipmentHeader.getCode())){
continue;
}
shipmentHeaderId = shipmentHeader.getId().toString();
//获取未回传任务集
List<TaskDetail> unBackTask = getUnBackTask(shipmentHeader.getCode());
if(unBackTask.size()==0){
continue;
}
Map<Integer, BigDecimal> taskFinishMaps = getTaskFinishMaps(unBackTask);
ajaxResult = shipmentHeaderService.postBack(header,shipmentHeaderId,taskFinishMaps);
if(!ajaxResult.hasErr()){
for (TaskDetail taskDetail : unBackTask) {
taskDetail.setBack(1);
}
taskDetailService.updateBatchById(unBackTask);
}
backMsg.put(shipmentHeader.getCode(),ajaxResult.getMsg());
type = 2;
}
break;
}
boolean flag = !ajaxResult.hasErr();
String message = ajaxResult.getMsg();
Integer result = flag ? QuantityConstant.ISRETURN_SUCC : QuantityConstant.ISRETURN_FAIL;
domain.setIsSuc(flag).setMsg(message).setBackNum(result).setType(type).setMsgMaps(backMsg);
callBack.back(domain);
}
private boolean taskAllComplete(String billCode){
LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(TaskDetail::getBillCode,billCode)
.lt(TaskDetail::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
List<TaskDetail> taskDetails = taskDetailService.list(queryWrapper);
return !taskDetails.isEmpty();
}
private Map<Integer, BigDecimal> getTaskFinishMaps(List<TaskDetail> taskDetails){
Map<Integer, BigDecimal> map = new HashMap<>();
for (TaskDetail taskDetail : taskDetails) {
if(StringUtils.isNull(map.get(taskDetail.getBillDetailId()))){
map.put(taskDetail.getBillDetailId(),taskDetail.getQty());
}else{
map.put(taskDetail.getBillDetailId(),map.get(taskDetail.getBillDetailId()).add(taskDetail.getQty()));
}
}
return map;
}
private List<TaskDetail> getUnBackTask(String billCode){
LambdaQueryWrapper<TaskDetail> queryWrapper = Wrappers.lambdaQuery();
queryWrapper.eq(TaskDetail::getBillCode,billCode)
.eq(TaskDetail::getStatus,QuantityConstant.TASK_STATUS_COMPLETED)
.eq(TaskDetail::getBack,0);
List<TaskDetail> taskDetails = taskDetailService.list(queryWrapper);
return taskDetails;
}
}