FedexDataInterface.java
16.6 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
package com.erry.iclear.dateInterface.api;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import com.erry.iclear.dateInterface.enums.ErrorCodeEnum;
import com.erry.iclear.dateInterface.api.request.*;
import com.erry.iclear.dateInterface.api.response.FedexResultChmRS;
import com.erry.iclear.dateInterface.api.response.FedexResultRS;
import com.erry.iclear.dateInterface.api.response.ResultRS;
import com.erry.iclear.dateInterface.common.Constant;
import com.erry.iclear.dateInterface.entity.*;
import com.erry.iclear.dateInterface.repository.ChmConsignmentRepository;
import com.erry.iclear.dateInterface.repository.IClearApiCheckLogRepository;
import com.erry.iclear.dateInterface.service.*;
import com.erry.iclear.dateInterface.util.HttpUtils;
import com.erry.iclear.dateInterface.util.JsonToJava;
import com.erry.iclear.dateInterface.util.StringUtil;
import com.google.gson.Gson;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
@RestController
@RequestMapping("/fedex")
@Slf4j
public class FedexDataInterface {
private Gson gson = new Gson();
@Autowired
private FedexService fedexService;
@Autowired
private ChmConsignmentService chmConsignmentService;
@Autowired
private IClearApiLogService iClearApiLogService;
@Autowired
private DeclaredInfoService declaredInfoService;
@Autowired
private FedexConsignmentService fedexConsignmentService;
@Autowired
private DeclearTaskService declearTaskService;
@Autowired
private ChmConsignmentRepository chmConsignmentRepository;
@Autowired
private IClearApiCheckLogRepository iClearApiCheckLogRepository;
@Value("${api.address.iclear-submit-url}")
String iclearCSubmitUrl;
@ApiOperation(value = "C类运单数据校验接口", httpMethod = "POST", notes = "ex: http://192.168.1.75:8080/fedex/checkBASFMawbFedex")
@PostMapping("/checkMawbFedexC")
@ResponseBody
public ResultRS checkBASFMawbFedex(HttpServletRequest request
,@RequestBody Fedexc fedexc) throws HttpProcessException {
String appId=(String) request.getHeader("Appid");
String appKey=(String) request.getHeader("Appkey");
String timeStamp=(String) request.getHeader("TimeStamp");
String token=(String) request.getHeader("Token");
String addr = request.getRemoteAddr();
//获取APPINFO信息
AppInfo appInfo = Constant.appInfoCache.get(appId + appKey);
TIClearApiCheckLog tIclearApiCheckLog = new TIClearApiCheckLog();
tIclearApiCheckLog.setRequestAppid(appId);
tIclearApiCheckLog.setRequestAppkey(appKey);
tIclearApiCheckLog.setRequestIp(addr);
tIclearApiCheckLog.setRequestToken(token);
tIclearApiCheckLog.setConsignmentCode(fedexc.getConsignmentCode());
tIclearApiCheckLog.setCreateTime(new Date());
tIclearApiCheckLog.setRequestText(
new String("requestHeader[appId:"+appId+"," +
"appKey:"+appKey+"," +
"timeStamp:"+timeStamp+"," +
"token:"+token) + "],"
+ fedexc.toString()
);
TIClearApiCheckLog tiClearApiCheckLogSave = iClearApiCheckLogRepository.save(tIclearApiCheckLog);
log.info("checkBASFMawbFedex:"+fedexc.toString());
log.info("appId:{},appKey:{},timeStamp:{},token:{}",appId,appKey,timeStamp,token);
//非空校验
ResultRS nonEmptyCheck = fedexService.checkBasfInterfaceData(fedexc,appInfo);
StopWatch sw = new StopWatch("checkBASFMawbFedex");
sw.start("generate FedexMawb");
fedexc.setAppId(appId);
FedexMawb saveMawbFedexResult = fedexService.generateFedexMawb(fedexc);
sw.stop();
sw.start("checkFedexMawb");
ResultRS iclearCheckRs = fedexService.checkFedexMawb(saveMawbFedexResult,appInfo);
//返回结果
ResultRS result = new ResultRS(Boolean.TRUE,ErrorCodeEnum.SUCCESS.getCode(),null,null);
if(nonEmptyCheck.getSuccess() && iclearCheckRs.getSuccess()){
//验证通过
result.setSuccess(Boolean.TRUE);
result.setCode(ErrorCodeEnum.SUCCESS.getCode());
}else{
//验证不通过
if(nonEmptyCheck.getSuccess()){
//基本非空信息验证通过,并且iclear验证失败
result.setMsg(iclearCheckRs.getMsg());
result.setMsgUS(iclearCheckRs.getMsgUS());
result.setCode(iclearCheckRs.getCode());
result.setSuccess(Boolean.FALSE);
}else{
//基本非空信息验证失败,并且iclear验证失败
result.setMsg(nonEmptyCheck.getMsg()+";"+iclearCheckRs.getMsg());
//基本非空信息验证失败,并且iclear验证失败(英文)
result.setMsgUS(iclearCheckRs.getMsgUS());
//非空校验与iclear校验都失败,则优先显示非空失败编码
result.setCode(nonEmptyCheck.getCode());
result.setSuccess(Boolean.FALSE);
}
}
sw.stop();
log.info(sw.prettyPrint());
tiClearApiCheckLogSave.setResponseText(result.getMsg());
tiClearApiCheckLogSave.setCode(result.getCode());
tiClearApiCheckLogSave.setSuccess(result.getSuccess());
iClearApiCheckLogRepository.save(tiClearApiCheckLogSave);
log.info(result.toString());
return result;
}
@ApiOperation(value = "Fedex推送运单数据", httpMethod = "POST", notes = "ex: http://192.168.1.75:8080/fedex/sendMawbFedex")
@PostMapping("/sendMawbFedex")
@ResponseBody
public ResultRS sendMawbFedex(HttpServletRequest request
, @RequestBody Fedexc fedexc) throws HttpProcessException {
log.info("接收到请求数据consignmentCode:"+fedexc.getConsignmentCode()+" data:" + gson.toJson(fedexc));
ResultRS result = new ResultRS(Boolean.FALSE,null,null,null);
if(Objects.equals(fedexc,null)){
result.setCode(ErrorCodeEnum.ERROR_7.getCode());
result.setMsg(ErrorCodeEnum.ERROR_7.getName());
return result;
}
//1、保存请求信息,保存到iClear的 SQLServer
StopWatch sw = new StopWatch("receice mawbFedex");
sw.start("save log");
String appId=(String) request.getHeader("Appid");
String appKey=(String) request.getHeader("Appkey");
String timeStamp=(String) request.getHeader("TimeStamp");
IClearApiLog saveIClearApiLogResult = iClearApiLogService.saveIClearApiLog(iClearApiLogService.convertToIClearApiLogC(fedexc,appId,appKey,new Date(Long.valueOf(timeStamp))));
fedexc.setAppId(appId);
FedexMawb saveMawbFedexResult = null;
if(!Objects.equals(saveIClearApiLogResult,null)){
log.info("请求保存成功,msgId:"+saveIClearApiLogResult.getMsgId());
fedexc.setMsgId(saveIClearApiLogResult.getMsgId());
fedexc.getConsignmentList().forEach(d->{
d.setMsgId(saveIClearApiLogResult.getMsgId());
});
saveMawbFedexResult = fedexService.saveMawbFedex(fedexc);
if(!Objects.equals(saveMawbFedexResult,null)){
log.info("请求数据运单号"+fedexc.getConsignmentCode()+"保存成功,msgId:"+saveMawbFedexResult.getMsgId());
}else{
log.info("请求数据运单号"+fedexc.getConsignmentCode()+"保存失败,msgId:"+saveMawbFedexResult.getMsgId());
}
}else{
log.info("请求日志保存失败,consignmentCode:"+fedexc.getConsignmentCode());
}
sw.stop();
//2、TODO 校验主单
sw.start("checkFedexMawb");
result=fedexService.checkFedexMawb(saveMawbFedexResult,null);
if(!result.getSuccess()){
return result;
}
sw.stop();
// //3、TODO 校验子单
// sw.start("checkMawbDetail");
// result=mawbDetailServicel.checkMawbDetail(mawb);
// if(!result.getSuccess()){
// return result;
// }
// sw.stop();
//4、保存运单到iclear中
sw.start("processChmConsignment");
result = chmConsignmentService.processChmConsignmentFedex(saveMawbFedexResult, timeStamp,appId);
if(!result.getSuccess()){
return result;
}
sw.stop();
ChmConsignment chmGetId = chmConsignmentRepository.findChmConsignmentByConsignmentCode(saveIClearApiLogResult.getBillNo(), saveIClearApiLogResult.getCreateTime());
//调用IClear接口进行运单推送EXP002
String objJson = JsonToJava.toJson(chmGetId.getId());
// Header[] headers = HttpHeader.custom()
// .other("Content-Type", "application/json")
// .build();
// log.info("******************* : " + this.iclearCSubmitUrl);
// //推送海关回执数据
// HttpConfig config = HttpConfig.custom()
// .headers(headers)
// .url(this.iclearCSubmitUrl)
// .encoding("utf-8")
// .json(objJson);
// String json = HttpClientUtil.post(config);
Map<String,String> headerMap = new HashMap<>();
headerMap.put("Content-Type", "application/json");
String json = HttpUtils.doPost(this.iclearCSubmitUrl,objJson,headerMap);
log.info("<<<<<<<< checkfedexMawb start >>>>>>>>>> result :" +result.getSuccess() +" iclear check response : " + json + " status : " + result.getSuccess());
//5、更新日志
sw.start("updateIClearApiLog");
if(!Objects.equals(saveIClearApiLogResult,null)){
saveIClearApiLogResult.setReceiveResult(result.getSuccess().toString());
saveIClearApiLogResult.setErrorCode(result.getCode());
saveIClearApiLogResult.setResponseTime(new Date());
saveIClearApiLogResult.setSpendTime(sw.getTotalTimeMillis());
iClearApiLogService.updateIClearApiLog(saveIClearApiLogResult);
}
log.info("请求数据consignmentCode:"+fedexc.getConsignmentCode() +" 接收结束");
sw.stop();
log.info(sw.prettyPrint());
//6、return result
result.setSuccess(Boolean.TRUE);
result.setCode(ErrorCodeEnum.SUCCESS.getCode());
return result;
}
@ApiOperation(value = "Fedex获取申报信息", httpMethod = "POST", notes = "ex: http://192.168.1.75:8080/fedex/selectDecListFedex")
@PostMapping("/selectDecListFedex")
@ResponseBody
public FedexResultChmRS selectDecListFedex(HttpServletRequest request
, @RequestBody ConsignmentCode consignmentCode) throws Exception {
log.info("接收到请求数据consignmentCode:"+consignmentCode.getConsignmentCode()+" data:" + gson.toJson(consignmentCode));
FedexResultChmRS result = new FedexResultChmRS(Boolean.FALSE,"","",null);
//获取请求的运单号
String[] splitConsignment = StringUtil.split(consignmentCode.getConsignmentCode(),",");
if(splitConsignment.length > 20){
result.setCode(ErrorCodeEnum.ERROR_30001.getCode());
result.setMsg("请求信息不能大于20条运单");
return result;
}
if(splitConsignment.length<1){
result.setCode(ErrorCodeEnum.ERROR_7.getCode());
result.setMsg(ErrorCodeEnum.ERROR_7.getName());
return result;
}
StopWatch sw = new StopWatch("receice mawbFedex");
sw.start("save log");
String appId=(String) request.getHeader("Appid");
String appKey=(String) request.getHeader("Appkey");
String timeStamp=(String) request.getHeader("TimeStamp");
//记录请求的运单
IClearApiLog saveIClearApiLogResult = iClearApiLogService.saveIClearApiLog(iClearApiLogService.convertToIClearApiLogCselect(consignmentCode.getConsignmentCode(),appId,appKey,new Date(Long.valueOf(timeStamp))));
//查询请求需要得运单申报信息
List<DeclaredInfo> declaredInfos = declaredInfoService.selectDeclaredInfo(splitConsignment,appId);
//构造返回数据
List<FedexConsignment> fedexConsignments = fedexConsignmentService.fedexConsignmentList(declaredInfos);
//返回申报数据
if(fedexConsignments.size() > 0){
result.setSuccess(Boolean.TRUE);
result.setConsignmentList(fedexConsignments);
result.setMsg("操作成功!");
}else{
result.setSuccess(Boolean.TRUE);
result.setMsg("操作成功,无申报数据!");
}
//更新记录的请求信息
if(!Objects.equals(saveIClearApiLogResult,null)){
saveIClearApiLogResult.setReceiveResult(result.getSuccess().toString());
saveIClearApiLogResult.setErrorCode(result.getCode());
saveIClearApiLogResult.setResponseTime(new Date());
saveIClearApiLogResult.setSpendTime(sw.getTotalTimeMillis());
iClearApiLogService.updateIClearApiLog(saveIClearApiLogResult);
}
//记录返回的申报数据记录
if(result.getSuccess()){
declearTaskService.insertDeclareTask(fedexConsignments, saveIClearApiLogResult.getMsgId());
}
log.info("Fedex申报数据请求成功!");
//6、return result
result.setSuccess(Boolean.TRUE);
result.setCode(ErrorCodeEnum.SUCCESS.getCode());
return result;
}
@ApiOperation(value = "Fedex获取回执信息", httpMethod = "POST", notes = "ex: http://192.168.1.75:8080/fedex/selectChmListFedex")
@PostMapping("/selectChmListFedex")
@ResponseBody
public FedexResultRS selectChmListFedex(HttpServletRequest request
, @RequestBody ConsignmentCode consignmentCode) throws Exception {
log.info("接收到请求数据consignmentCode:"+consignmentCode.getConsignmentCode()+" data:" + gson.toJson(consignmentCode));
FedexResultRS result = new FedexResultRS(Boolean.FALSE,"","",null);
//获取请求的运单号
String[] splitConsignment = StringUtil.split(consignmentCode.getConsignmentCode(),",");
if(splitConsignment.length > 20){
result.setCode(ErrorCodeEnum.ERROR_30001.getCode());
result.setMsg("请求信息不能大于20条运单");
return result;
}
if(splitConsignment.length<1){
result.setCode(ErrorCodeEnum.ERROR_7.getCode());
result.setMsg(ErrorCodeEnum.ERROR_7.getName());
return result;
}
StopWatch sw = new StopWatch("receice mawbFedex");
sw.start("save log");
String appId=(String) request.getHeader("Appid");
String appKey=(String) request.getHeader("Appkey");
String timeStamp=(String) request.getHeader("TimeStamp");
//记录请求的运单
IClearApiLog saveIClearApiLogResult = iClearApiLogService.saveIClearApiLog(iClearApiLogService.convertToIClearApiLogCselect(consignmentCode.getConsignmentCode(),appId,appKey,new Date(Long.valueOf(timeStamp))));
//查询请求需要得运单申报信息
//List<DeclaredInfo> declaredInfos = declaredInfoService.selectDeclaredInfo(splitConsignment);
List<FedexReceipt> chmConsignments = chmConsignmentService.getConsignmentCode(splitConsignment,appId);
//构造返回数据
//返回申报数据
if(chmConsignments.size() > 0){
result.setSuccess(Boolean.TRUE);
result.setConsignmentList(chmConsignments);
result.setMsg("操作成功!");
}else{
result.setSuccess(Boolean.TRUE);
result.setMsg("操作成功,无回执数据!");
}
//更新记录的请求信息
if(!Objects.equals(saveIClearApiLogResult,null)){
saveIClearApiLogResult.setReceiveResult(result.getSuccess().toString());
saveIClearApiLogResult.setErrorCode(result.getCode());
saveIClearApiLogResult.setResponseTime(new Date());
saveIClearApiLogResult.setSpendTime(sw.getTotalTimeMillis());
iClearApiLogService.updateIClearApiLog(saveIClearApiLogResult);
}
//记录返回的申报数据记录
if(result.getSuccess()){
declearTaskService.insertChmTask(chmConsignments, saveIClearApiLogResult.getMsgId());
}
log.info("Fedex回执数据请求成功!");
//6、return result
result.setSuccess(Boolean.TRUE);
result.setCode(ErrorCodeEnum.SUCCESS.getCode());
return result;
}
}