ShipmentAPIService.java
20.7 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package com.huaheng.api.general.service;
import com.google.common.collect.Lists;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import com.alibaba.fastjson.JSON;
import com.huaheng.api.general.domain.ShipmentItems;
import com.huaheng.api.general.domain.ShipmentObject;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.IAddressService;
import com.huaheng.pc.general.company.domain.Company;
import com.huaheng.pc.general.company.service.ICompanyService;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.general.warehouse.domain.Warehouse;
import com.huaheng.pc.general.warehouse.service.IWarehouseService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.IShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.IShipmentHeaderService;
import com.huaheng.pc.system.dict.service.IDictDataService;
import com.huaheng.pc.task.task.domain.Task;
import com.huaheng.pc.task.task.service.ITaskService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.ITaskDetailService;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;
@Service
public class ShipmentAPIService {
private static final Logger log = LoggerFactory.getLogger(ShipmentAPIService.class);
@Autowired
private IDictDataService dictDataService;
@Autowired
IShipmentDetailService shipmentDetailService;
@Autowired
ICompanyService companyService;
@Autowired
IShipmentHeaderService shipmentHeaderService;
@Autowired
private IMaterialService materialService;
@Autowired
private IWarehouseService warehouseService;
@Autowired
private ITaskService taskService;
@Autowired
private ITaskDetailService taskDetailService;
@Autowired
private IAddressService addressService;
//出库单下发
@Transactional(rollbackFor = Exception.class)
public AjaxResult insertShipment(ShipmentObject shipmentObject){
log.warn("出库单下发数据:"+ shipmentObject);
//主单校验
if(StringUtils.isEmpty(shipmentObject.getShipmentCode()) ||
StringUtils.isEmpty(shipmentObject.getWarehouseCode()) ||
StringUtils.isEmpty(shipmentObject.getCompanyCode()) ||
StringUtils.isEmpty(shipmentObject.getShipmentType())
){
return AjaxResult.error("出库信息主单数据必填项有误: shipmentCode, warehouseCode, companyCode, shipmentType");
}
/*shipmentCode重复校验*/
ShipmentHeader checkSh = new ShipmentHeader();
checkSh.setWarehouseCode(shipmentObject.getWarehouseCode());
checkSh.setSourceCode(shipmentObject.getShipmentCode());
checkSh = shipmentHeaderService.selectFirstEntity(checkSh);
if(!StringUtils.isNull(checkSh)){
return AjaxResult.error("请勿重复下发出库单据: " + shipmentObject.getShipmentCode());
}
//明细校验
if(shipmentObject.getShipmentItems() == null || shipmentObject.getShipmentItems().size() < 1){
return AjaxResult.error("明细子单没有数据!");
}
/*if(shipmentObject.getShipmentItems().size() > 200){
return AjaxResult.error("明细单不能超过200行");
}*/
List<ShipmentItems> shipmentItemsList = shipmentObject.getShipmentItems();
/*if(dictDataService.checkConfig("shipmentType", shipmentApiHeader.getShipmentType()) == false)
{
throw new ServiceException("没有对应的出库单类型");
}*/
//插入出库主单数据
ShipmentHeader shipmentHeader = new ShipmentHeader();
shipmentHeader.setCode(shipmentHeaderService.createCode(shipmentObject.getShipmentType()));
Company company=new Company();
Warehouse warehouse= new Warehouse();
company.setCode(shipmentObject.getCompanyCode());
company = companyService.selectFirstEntity(company);
if (company == null) {
return AjaxResult.error("系统没有该货主: " + shipmentObject.getCompanyCode());
} else {
shipmentHeader.setCompanyId(company.getId());
}
warehouse.setCode(shipmentObject.getWarehouseCode());
warehouse = warehouseService.selectFirstEntity(warehouse);
if (warehouse == null) {
return AjaxResult.error("系统没有该仓库:" + shipmentObject.getWarehouseCode());
} else {
shipmentHeader.setWarehouseId(warehouse.getId());
}
Date date = null;
if(StringUtils.isNotEmpty(shipmentObject.getcDatetime())){
try{
DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = fmt.parse(shipmentObject.getcDatetime());
}catch (Exception e){
throw new ServiceException(e.toString());
}
shipmentHeader.setCreated(date);
}else {
shipmentHeader.setCreated(new Date());
}
shipmentHeader.setWarehouseCode(warehouse.getCode());
shipmentHeader.setCompanyCode(company.getCode());
shipmentHeader.setSourceCode(shipmentObject.getShipmentCode()); //上游系统编码
shipmentHeader.setSourcePlatform("ERP");
shipmentHeader.setType(shipmentObject.getShipmentType());
shipmentHeader.setPriority(10);
shipmentHeader.setStation(("1"));
shipmentHeader.setTotalQty(new BigDecimal("0"));
shipmentHeader.setuWarehouseCode("");
shipmentHeader.setTotalLines(0);
shipmentHeader.setFirstStatus(0);
shipmentHeader.setLastStatus(0);
shipmentHeader.setUploadStatus(0);
//shipmentHeader.setCreated(new Date());
shipmentHeader.setCreatedBy("ERP");
shipmentHeader.setLastUpdated(new Date());
shipmentHeader.setLastUpdatedBy("ERP");
shipmentHeader.setUserDef1(shipmentObject.getShipmentId().toString());//上游系统单号
shipmentHeader.setUserDef2("");
shipmentHeader.setUserDef3("");
int j = shipmentHeaderService.insert(shipmentHeader);
if(j < 1){
throw new ServiceException("插入出库主单失败");
}
//添加细表到shipmentDetail
List<ShipmentDetail> shipmentDetailList = new ArrayList<>();
BigDecimal totalQty = BigDecimal.ZERO;
for(ShipmentItems item : shipmentItemsList) {
//先校验
if(StringUtils.isEmpty(item.getItemCode()) ||
item.getQty() == null ||
item.getQty() ==0 ){
throw new ServiceException("出库明细单必填项数据错误: " + item);
}
Material materialTmp = new Material();
materialTmp.setCode(item.getItemCode());
// materialTmp.setSpecification(item.getItemSpecification());
// materialTmp.setName(item.getItemName());
materialTmp.setWarehouseCode(shipmentHeader.getWarehouseCode());
Material material = materialService.selectFirstEntity(materialTmp);
if (StringUtils.isNull(material)) {
throw new ServiceException("物料未找到: " + item.getItemCode() + " " + item.getItemName());
}
ShipmentDetail shipmentDetail = new ShipmentDetail();
shipmentDetail.setWarehouseId(shipmentHeader.getWarehouseId());
shipmentDetail.setWarehouseCode(shipmentHeader.getWarehouseCode());
shipmentDetail.setCompanyId(shipmentHeader.getCompanyId());
shipmentDetail.setCompanyCode(shipmentHeader.getCompanyCode());
shipmentDetail.setSourceCode(shipmentObject.getShipmentCode());
shipmentDetail.setSourceLine(shipmentObject.getShipmentId().toString());
shipmentDetail.setShipmentId(shipmentHeader.getId());
shipmentDetail.setShipmentCode(shipmentHeader.getCode());
shipmentDetail.setMaterialId(material.getId());
shipmentDetail.setMaterialCode(material.getCode());
//shipmentDetail.setMaterialName(material.getName());
//shipmentDetail.setSpecification(material.getSpecification());
//出库单zoneCode可能是钱柜,屏蔽
// shipmentDetail.setZoneCode("LK");
if(StringUtils.isEmpty(material.getMasterUnit())){
shipmentDetail.setUnit("PCS");
}else{
shipmentDetail.setUnit(material.getMasterUnit());
}
shipmentDetail.setBatch(item.getItemBatch());
shipmentDetail.setLot("");
shipmentDetail.setProject(item.getItemProject());
shipmentDetail.setManufactureDate(null);
shipmentDetail.setExpirationDate(null);
shipmentDetail.setAgingDate(null);
if(StringUtils.isNotEmpty(item.getItemState())){
shipmentDetail.setInventoryStatus(item.getItemState());
}else{
shipmentDetail.setInventoryStatus("good");
}
shipmentDetail.setQtyCompleted(new BigDecimal("0"));
try {
shipmentDetail.setQty(BigDecimal.valueOf(item.getQty()));
}catch (Exception e){
throw new ServiceException(e.toString());
}
shipmentDetail.setPrice(new BigDecimal("0"));
shipmentDetail.setStatus((short)0);
shipmentDetail.setCreated(date);
shipmentDetail.setCreatedBy("ERP");
shipmentDetail.setLastUpdated(new Date());
shipmentDetail.setLastUpdatedBy("ERP");
shipmentDetail.setUserDef1("");
shipmentDetail.setUserDef2("");
shipmentDetail.setUserDef3("");
//shipmentDetailService.insert(shipmentDetail);
shipmentDetailList.add(shipmentDetail);
//更新表头的总行数和总数量统计
totalQty = totalQty.add(shipmentDetail.getQty());
}
//批量插入出库单
int k = shipmentDetailService.insertBatch(shipmentDetailList);
if(k != shipmentDetailList.size()){
throw new ServiceException("明细插入失败!");
}
//更新单据总行数与总数量
shipmentHeader.setTotalLines(shipmentDetailList.size());
shipmentHeader.setTotalQty(totalQty);
shipmentHeaderService.updateByModel(shipmentHeader );
return AjaxResult.success("出库单下发成功");
}
/**
* 出库单回传
* 任务回传
* */
// @Transactional(rollbackFor = Exception.class)
public AjaxResult confirmShipment(boolean isRetry) {
/*出库任务完成时回传*/
//查询已完成的出库任务
Task task = null;
if(isRetry){
task = taskService.selectShipmentConfirm4Retry();
}else{
task = taskService.selectShipmentConfirm();
}
if(StringUtils.isNull(task)){
return AjaxResult.success("没有需要回传的出库任务");
}
//任务明细
TaskDetail taskDetailt = new TaskDetail();
taskDetailt.setWarehouseCode(task.getWarehouseCode());
taskDetailt.setTaskId(task.getId());
List<TaskDetail> taskDetails = taskDetailService.selectListEntityByEqual(taskDetailt);
if(taskDetails == null || taskDetails.size() < 1){
return AjaxResult.success(task.getId() + "任务没有明细");
}
//set回传实体
String timeStr = null; //时间
try{
timeStr= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}catch (Exception e) {
throw new ServiceException("获取时间错误!");
}
ShipmentObject shipmentObject = new ShipmentObject();
shipmentObject.setShipmentId(task.getId());
shipmentObject.setWarehouseCode(task.getWarehouseCode());
shipmentObject.setCompanyCode(task.getCompanyCode());
shipmentObject.setcDatetime(timeStr);
shipmentObject.setContainerCode(task.getContainerCode());
shipmentObject.setLocationCode(task.getSourceLocation());
//set明细
List<ShipmentItems> shipmentItemsList = new ArrayList<>();
for(TaskDetail detail:taskDetails){
ShipmentItems shipmentItems = new ShipmentItems();
//物料
Material material = materialService.getMaterial(detail.getMaterialCode(), task.getWarehouseCode(), task.getCompanyCode());
if(StringUtils.isNull(material)){
return AjaxResult.success("物料错误");
}
//出库明细
ShipmentDetail shipmentDetail = shipmentDetailService.selectEntityById(detail.getBillDetailId());
if(StringUtils.isNull(shipmentDetail)){
throw new ServiceException("没有这条出库明细。");
}
ShipmentHeader shipmentHeader = new ShipmentHeader();
shipmentHeader.setCode(detail.getBillCode());
shipmentHeader.setWarehouseCode("TD0001");
shipmentHeader = shipmentHeaderService.selectFirstEntity(shipmentHeader);
if(StringUtils.isNull(shipmentHeader)){
throw new ServiceException("没有这条出库单");
}
shipmentItems.setShipmentCode(shipmentHeader.getSourceCode()); //上游单号
if(org.apache.commons.lang3.StringUtils.isEmpty(shipmentHeader.getSourceCode())){
task.confirmIgnore();
taskService.updateByModel(task);
return AjaxResult.error("没有找到上游单号");
}
shipmentItems.setItemProject(detail.getProject());
shipmentItems.setItemId(detail.getId());
shipmentItems.setItemCode(detail.getMaterialCode());
shipmentItems.setItemName(detail.getMaterialName());
shipmentItems.setItemSpecification(material.getSpecification());
shipmentItems.setItemBatch(detail.getBatch());
shipmentItems.setItemState(shipmentDetail.getInventoryStatus());
try{
shipmentItems.setQty(detail.getQty().floatValue());
}catch (Exception e){
throw new ServiceException(e.toString());
}
shipmentItems.setItemUnit("PCS");
shipmentItemsList.add(shipmentItems);
}
shipmentObject.setShipmentItems(shipmentItemsList);
//json
String jsonParam = JSON.toJSONString(shipmentObject);
//url
Address address = new Address();
address.setWarehouseCode("TD0001");
address.setParam("u8ConfirmShipment");
address = addressService.selectEntity(address);
if(address == null || StringUtils.isEmpty(address.getUrl())){
throw new ServiceException("回传URL为空!");
}
String url = address.getUrl();
String result = null;
try{
result = HttpUtils.bodypost(url, jsonParam);
}catch (Exception e){
task.confirmFailure();
taskService.updateByModel(task);
throw new ServiceException("出库单回传ERP,请求发送失败!" + e.getMessage());
}
AjaxResult ajaxResult = null;
try{
ajaxResult = JSON.parseObject(result, AjaxResult.class);
}catch(Exception ex){
//erp在响应包体写的是转义json字串,此处兼容处理
try {
result = JSON.parse(result).toString();
ajaxResult = JSON.parseObject(result, AjaxResult.class);
}catch (Exception e){
task.confirmFailure();
taskService.updateByModel(task);
log.error("解析erp响应json异常,回传失败!", e);
throw new ServiceException("解析erp响应json异常,回传失败!");
}
}
if(ajaxResult.getCode() != 200){
task.confirmFailure();
taskService.updateByModel(task);
throw new ServiceException("出库单回传ERP回复异常,回传失败!");
}
//修改任务的回传状态
task.setFirstStatus((short) 120);
task.setLastStatus((short) 120);
task.confirmSuccess();
int j = taskService.updateByModel(task);
if(j < 1){
throw new ServiceException(task.getId() + "任务回传状态修改失败");
}
log.warn("出库单回传数据:"+ shipmentObject);
return AjaxResult.success("出库任务" + task.getId() + "回传ERP成功");
}
/**
* 出库单回传
* 根据单据回传
* */
@Transactional(rollbackFor = Exception.class)
public AjaxResult confirmShipmentA(String url)
{
//查询符合回传条件的主单,再根据主单回传子单
ShipmentHeader header = new ShipmentHeader();
header.setLastStatus(300);
header.setWarehouseCode("TD0001");
ShipmentHeader shipmentHeader = shipmentHeaderService.selectFirstEntity(header);
if (StringUtils.isNull(shipmentHeader)) {
return AjaxResult.success("没有主单需要回传");
}
ShipmentDetail shipmentDetail = new ShipmentDetail();
shipmentDetail.setWarehouseCode(shipmentHeader.getWarehouseCode());
shipmentDetail.setShipmentCode(shipmentHeader.getCode());
List<ShipmentDetail> shipmentDetails = shipmentDetailService.selectListEntityByEqual(shipmentDetail);
if (shipmentDetails == null || shipmentDetails.size() < 1) {
return AjaxResult.error("出库回传子单不存在");
}
//
ShipmentObject shipmentObject = new ShipmentObject();
String dateTime = null;
try{
dateTime = new java.sql.Timestamp(System.currentTimeMillis()).toString();
}catch (Exception e){
throw new ServiceException(e.toString());
}
//主
shipmentObject.setcDatetime(dateTime);
shipmentObject.setCompanyCode(shipmentHeader.getCompanyCode());
shipmentObject.setShipmentCode(shipmentHeader.getCode());
shipmentObject.setShipmentId(shipmentHeader.getId());
shipmentObject.setShipmentType(shipmentHeader.getType());
shipmentObject.setWarehouseCode(shipmentHeader.getWarehouseCode());
//明细
List<ShipmentItems> shipmentItemsList = new ArrayList<>();
for(ShipmentDetail item:shipmentDetails){
ShipmentItems shipmentItems = new ShipmentItems();
Material materialTmp = new Material();
materialTmp.setCode(item.getMaterialCode());
materialTmp.setWarehouseCode(item.getWarehouseCode());
Material material = materialService.selectFirstEntity(materialTmp);
if (StringUtils.isNull(material) ) {
return AjaxResult.error("没有该物料");
}
shipmentItems.setItemBatch(item.getProject()); //project
shipmentItems.setItemCode(material.getCode());
shipmentItems.setItemId(item.getId());
shipmentItems.setItemName(material.getName());
shipmentItems.setItemSpecification(material.getSpecification());
shipmentItems.setItemState(item.getInventoryStatus());
shipmentItems.setItemUnit("PCS");
try{
shipmentItems.setQty(item.getQty().floatValue());
}catch (Exception e){
throw new ServiceException(e.toString());
}
shipmentItemsList.add(shipmentItems);
}
//set
shipmentObject.setShipmentItems(shipmentItemsList);
String JsonParam = JSON.toJSONString(shipmentObject);
String result = HttpUtils.bodypost(url, JsonParam);
AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
if(ajaxResult.getCode() != 200){
throw new ServiceException("出库单回传ERP回复异常,回传失败!");
}
shipmentHeader.setFirstStatus(900);
shipmentHeader.setLastStatus(900);
shipmentHeader.setUploadTime(new Date());
shipmentHeaderService.updateByModel(shipmentHeader);
return ajaxResult;
}
}