XnTransServices.java
8.46 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
package com.huaheng.mobile.receipt;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction;
import com.huaheng.pc.inventory.inventoryTransfer.service.InventoryTransferService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerView;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
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.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.ReceiptTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class XnTransServices {
public static final String RECEIPT_TYPE = "XNT";
@Autowired
private ReceiptHeaderService receiptHeaderService;
@Autowired
private ReceiptDetailService receiptDetailService;
@Autowired
private InventoryTransferService inventoryTransferService;
@Autowired
private InventoryDetailService inventoryDetailService;
@Autowired
TaskHeaderService taskHeaderService;
@Autowired
ReceiptTaskService receiptTaskService;
@Autowired
private ReceiptContainerHeaderService receiptContainerHeaderService;
@Transactional(rollbackFor = Exception.class)
public AjaxResult trans(XnTransBean bean) {
check(bean);
ReceiptHeader receiptHeader = createTransReceipt(bean);
List<ReceiptDetail> receiptDetailList = createTransReceiptDetail(receiptHeader, bean);
TaskHeader task = combine(bean, receiptHeader, receiptDetailList);
transOut(bean);
finishTask(task);
return AjaxResult.success(task.getId());
}
private void finishTask(TaskHeader task){
try {
if(task.getZoneCode().equalsIgnoreCase("PK") || task.getZoneCode().equalsIgnoreCase("AGV")){
String ids = task.getId().toString();
Integer[] idArray = Convert.toIntArray(ids);
taskHeaderService.completeTaskByWMS(idArray,null);
return;
}
if (task.getStatus() == 1) {
taskHeaderService.sendTaskToWcs(Convert.toIntArray(task.getId().toString()));
}
}catch (Exception e){
e.printStackTrace();
}
}
private boolean check(XnTransBean bean) {
if (StringUtils.isEmpty(bean.getContainerCode())) {
throw new RuntimeException("容器号不能为空");
}
if (bean.getInventoryList() == null || bean.getInventoryList().size() == 0) {
throw new RuntimeException("明细不能为空");
}
return true;
}
/**
* 创建转移入库单
**/
private ReceiptHeader createTransReceipt(XnTransBean bean) {
ReceiptHeader receiptHeader = new ReceiptHeader();
receiptHeader.setReceiptType(RECEIPT_TYPE);
receiptHeader.setCompanyCode(bean.getCompanyCode());
// receiptHeader.set(true);
AjaxResult result = receiptHeaderService.saveReceiptHeader(receiptHeader);
if (result.hasErr()) {
throw new ServiceException("创建转移单头出错:" + result.getMsg());
}
return receiptHeader;
}
private void transOut(XnTransBean bean){
for (InventoryItem item: bean.getInventoryList()) {
InventoryDetail inventory = inventoryDetailService.getById(item.getId());
//库存出交易
String types = "transferShipment";
InventoryTransaction transaction = inventoryTransferService.transferInventoryTransaction(inventory, types, item.getQty());
//虚拟库存的修改或删除
if (inventory.getQty().compareTo(item.getQty()) == 0) {
if (!inventoryDetailService.removeById(inventory.getId())) {
throw new ServiceException("删除虚拟库存失败");
}
}
if (inventory.getQty().compareTo(item.getQty()) == 1) {
inventory.setQty(inventory.getQty().subtract(item.getQty()));
if (!inventoryDetailService.updateById(inventory)) {
throw new ServiceException("修改虚拟库存失败");
}
}
}
}
/**
* 创建转移单明细
**/
private List<ReceiptDetail> createTransReceiptDetail(ReceiptHeader header, XnTransBean bean) {
List<ReceiptDetail> receiptDetailList = new ArrayList<>();
for (InventoryItem item : bean.getInventoryList()) {
ReceiptDetail detail = new ReceiptDetail();
detail.setReceiptId(header.getId());
detail.setReceiptCode(header.getCode());
detail.setCompanyCode(header.getCompanyCode());
InventoryDetail inventory = inventoryDetailService.getById(item.getId());
detail.setQty(item.getQty());
detail.setMoCode(inventory.getMoCode());
detail.setBatch(inventory.getBatch());
detail.setMaterialSpec(inventory.getMaterialSpec());
detail.setLot(inventory.getLot());
detail.setUWarehouseCode(inventory.getUWarehouseCode());
detail.setInventorySts("good");
detail.setMaterialCode(inventory.getMaterialCode());
AjaxResult result = receiptDetailService.saveReceiptDetaial(detail);
if (result.hasErr()) {
throw new ServiceException("创建转移明细单出错:" + result.getMsg());
}
receiptDetailList.add(detail);
}
return receiptDetailList;
}
private ReceiptContainerHeader getUnCompleteCombine(String containerCode) {
ReceiptContainerHeader query = new ReceiptContainerHeader();
query.setStatus( 0);
query.setContainerCode(containerCode);
ReceiptContainerHeader ret = receiptContainerHeaderService.selectFirstEntity(query);
if (ret == null) {
query.setStatus( 10);
ret = receiptContainerHeaderService.selectFirstEntity(query);
}
return ret;
}
private TaskHeader combine(XnTransBean bean, ReceiptHeader header, List<ReceiptDetail> detailList) throws RuntimeException {
ReceiptContainerHeader receiptContainerHeader = null;
for (int i = 0; i < detailList.size(); i++) {
ReceiptDetail detail = detailList.get(i);
ReceiptContainerView record = new ReceiptContainerView();
record.setReceiptCode(header.getCode());
record.setQty(detail.getQty());
record.setReceiptDetailId(detail.getId());
record.setContainerCode(bean.getContainerCode());
receiptContainerHeaderService.saveContainer(record);
receiptContainerHeader = getUnCompleteCombine(bean.getContainerCode());
}
ReceiptContainerHeader query = new ReceiptContainerHeader();
query.setStatus( 0);
List<ReceiptContainerHeader> receiptContainerHeaders = receiptContainerHeaderService.selectFirstEntityList(query);
for (ReceiptContainerHeader item : receiptContainerHeaders) {
String ids = item.getId().toString();
Integer[] idArray = Convert.toIntArray(ids);
try {
receiptTaskService.createReceiptTask(Arrays.asList(idArray.clone()));
// taskHeaderService.createReceiptTask(idArray);
} catch (Exception e) {
throw new RuntimeException("组盘生成任务时出错:" + e.getMessage());
}
}
TaskHeader task = new TaskHeader();
task.setAllocationHeadId(receiptContainerHeader.getId());
task = taskHeaderService.selectFirstEntity(task);
return task;
}
}