ApiService.java
12.8 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
package com.erry.iclear.dateInterface.service;
import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.erry.iclear.dateInterface.api.request.Mawb;
import com.erry.iclear.dateInterface.api.request.MawbDetail;
import com.erry.iclear.dateInterface.api.request.ReceiptMessage;
import com.erry.iclear.dateInterface.api.response.TokenResult;
import com.erry.iclear.dateInterface.common.Constants;
import com.erry.iclear.dateInterface.entity.ChmConsignment;
import com.erry.iclear.dateInterface.entity.ChmConsignmentDetail;
import com.erry.iclear.dateInterface.entity.DeclearTask;
import com.erry.iclear.dateInterface.repository.ChmConsignmentRepository;
import com.erry.iclear.dateInterface.repository.DeclearTaskRepository;
import com.erry.iclear.dateInterface.repository.ReceiptMessageRepository;
import com.erry.iclear.dateInterface.util.DateUtils;
import com.erry.iclear.dateInterface.util.JsonToJava;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.*;
/**
* 调用API接口Service
* mt
* 2021年7月28日17:04:27
*/
@Service
@Slf4j
@Transactional(rollbackOn = Exception.class)
public class ApiService {
@Value("${api.token-info.username}")
String username;
@Value("${api.token-info.password}")
String password;
@Value("${api.address.token-url}")
String tokenUrl;
@Value("${api.address.refresh-token-url}")
String refreshTokenUrl;
@Value("${api.address.customs-url}")
String customsUrl;
@Value("${api.address.complete-msg-url}")
String completeMsgUrl;
@Autowired
ReceiptMessageRepository receiptMessageRepository;
@Autowired
DeclearTaskRepository declearTaskRepository;
@Autowired
ChmConsignmentRepository consignmentRepository;
@Autowired
ChmConsignmentService consignmentService;
@Autowired
ChmConsignmentDetailService consignmentDetailService;
/**
* 获取token信息
* @return
* @throws Exception
*/
public TokenResult getToken() throws Exception{
Map<String,Object> map = new HashMap<String,Object>();
map.put("username", this.username);
map.put("password", this.password);
HttpConfig config = HttpConfig.custom()
.url(this.tokenUrl)
.map(map)
.encoding("utf-8");
String tokenJson = HttpClientUtil.post(config);
TokenResult tokenResult = JsonToJava.parseJson(tokenJson,TokenResult.class);
return tokenResult;
}
/**
* 刷新token
* @param token
* @throws Exception
*/
public void refreshToken(String token) throws Exception{
Map<String,Object> map = new HashMap<String,Object>();
map.put("token", token);
HttpConfig config = HttpConfig.custom()
.url(this.refreshTokenUrl)
.map(map)
.encoding("utf-8");
HttpClientUtil.post(config);
}
/**
* 传输海关回执数据
* @param token
* @throws Exception
*/
public void pushCustoms(String token){
//获取海关回执数据
List<ReceiptMessage> receiptMessageList = this.receiptMessageRepository.findReceiptMessageList();
//去除微秒
for(int i = 0 ; i < receiptMessageList.size() ; i ++){
ReceiptMessage receiptMessage = receiptMessageList.get(i);
Date noticeDate = DateUtils.string2Date(receiptMessage.getNoticeDate(),null);
String nDate = DateUtils.formatDate(noticeDate,"yyyy-MM-dd HH:mm:ss");
receiptMessage.setNoticeDate(nDate);
}
this.sendCustoms(token,receiptMessageList);
}
/**
* 将海关回执数据发送至西门子
* @param token
* @param receiptMessageList
*/
public void sendCustoms(String token ,List<ReceiptMessage> receiptMessageList){
Header[] headers = HttpHeader.custom()
.other("Content-Type", "application/json")
.other("Authorization", token)
.build();
List<DeclearTask> declearTaskList = new ArrayList<DeclearTask>();
receiptMessageList.forEach(d->{
DeclearTask declearTask = new DeclearTask();
try {
//设置ID
declearTask.setChmsignmentId(BigDecimal.valueOf(d.getId()));
//推送时间
declearTask.setPushTime(new Date());
String objJson = JsonToJava.toJson(d);
//推送海关回执数据
HttpConfig config = HttpConfig.custom()
.headers(headers)
.url(this.customsUrl)
.encoding("utf-8")
.json(objJson);
String json = HttpClientUtil.post(config);
//接口响应时间
declearTask.setResponseTime(new Date());
TokenResult tokenResult = JsonToJava.parseJson(json,TokenResult.class);
if(tokenResult.getErrorcode().equals(Constants.ApiResponseKeys.SUCCESS)){
//推送成功,记录推送状态
declearTask.setResponseResults(tokenResult.getErrorcode());
declearTask.setResponseContent(tokenResult.getErrormessage());
declearTaskList.add(declearTask);
}else if(tokenResult.getErrorcode().equals(Constants.ApiResponseKeys.KEY_10004) || tokenResult.getErrorcode().equals(Constants.ApiResponseKeys.KEY_10013)){
//如果凭证失效,则刷新凭证
refreshToken(token);
}else{
//推送失败,记录推送状态
declearTask.setResponseResults(tokenResult.getErrorcode());
declearTask.setResponseContent(tokenResult.getErrormessage());
declearTaskList.add(declearTask);
log.info("海关回执推送失败,推送相应码为:"+tokenResult.getErrorcode() + ",推送ID为:" + d.getId());
}
}catch(Exception ex){
//接口响应时间
declearTask.setResponseTime(new Date());
declearTask.setResponseResults("99999");
declearTask.setResponseContent(ex.getMessage());
declearTaskList.add(declearTask);
ex.printStackTrace();
}
});
declearTaskList.forEach(declearTask -> {
//将已经推送完成的回执信息状态修改
this.receiptMessageRepository.updateDeclearTaskStatus(declearTask.getPushTime(),
declearTask.getResponseTime(),
declearTask.getResponseResults(),
declearTask.getResponseContent(),
"2",
declearTask.getChmsignmentId().longValue());
});
}
/**
* 传输完整报文数据
* @param token
* @throws Exception
*/
public void pushMawb(String token){
try{
/****************将Export对象转换成西门子交互实体对象****************/
//查询需要推送数据
List<Mawb> mawbList = new ArrayList<Mawb>();
List<DeclearTask> declearTaskList = this.declearTaskRepository.findDeclearTask();
declearTaskList.forEach(declearTask -> {
Long consignmentId = declearTask.getChmsignmentId().longValue();
//查询需要推送运单数据,并且转换
ChmConsignment consignment = this.consignmentRepository.findChmConsignmentById(consignmentId);
Mawb mawb = this.consignmentService.convertToChmConsignment(consignment);
//获取需要推送运单数据,并且转换
if(consignment.getChmConsignmentDetailList() != null){
List<MawbDetail> mawbDetailList = new ArrayList<MawbDetail>();
for(int i = 0 ; i < consignment.getChmConsignmentDetailList().size() ; i++){
ChmConsignmentDetail detail = consignment.getChmConsignmentDetailList().get(i);
MawbDetail mawbDetail = this.consignmentDetailService.convertToChmConsignmentDetail(detail, String.valueOf((i + 1)));
mawbDetailList.add(mawbDetail);
}
mawb.setMawbDetailList(mawbDetailList);
}
mawbList.add(mawb);
});
this.sendMawb(token,mawbList);
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* 将完整报文发送至西门子
* @param token
* @param mawbList
*/
public void sendMawb(String token ,List<Mawb> mawbList){
Header[] headers = HttpHeader.custom()
.other("Content-Type", "application/json")
.other("Authorization", token)
.build();
List<DeclearTask> declearTaskList = new ArrayList<DeclearTask>();
mawbList.forEach(d->{
DeclearTask declearTask = new DeclearTask();
try {
//设置ID
declearTask.setChmsignmentId(BigDecimal.valueOf(d.getId()));
//推送时间
declearTask.setPushTime(new Date());
String objJson = JsonToJava.toJson(d);
//推送海关回执数据
HttpConfig config = HttpConfig.custom()
.headers(headers)
.url(this.completeMsgUrl)
.encoding("utf-8")
.json(objJson);
String json = HttpClientUtil.post(config);
//接口响应时间
declearTask.setResponseTime(new Date());
TokenResult tokenResult = JsonToJava.parseJson(json,TokenResult.class);
if(tokenResult.getErrorcode().equals(Constants.ApiResponseKeys.SUCCESS)){
//推送成功,记录推送状态
declearTask.setResponseResults(tokenResult.getErrorcode());
declearTask.setResponseContent(tokenResult.getErrormessage());
declearTaskList.add(declearTask);
}else if(tokenResult.getErrorcode().equals(Constants.ApiResponseKeys.KEY_10004) || tokenResult.getErrorcode().equals(Constants.ApiResponseKeys.KEY_10013)){
//如果凭证失效,则刷新凭证
refreshToken(token);
}else{
//推送失败,记录推送状态
declearTask.setResponseResults(tokenResult.getErrorcode());
declearTask.setResponseContent(tokenResult.getErrormessage());
declearTaskList.add(declearTask);
log.info("完整报关单推送失败,推送相应码为:"+tokenResult.getErrorcode() + ",推送ID为:" + d.getId());
}
}catch(Exception ex){
//接口响应时间
declearTask.setResponseTime(new Date());
declearTask.setResponseResults("99999");
declearTask.setResponseContent(ex.getMessage());
declearTaskList.add(declearTask);
ex.printStackTrace();
}
});
declearTaskList.forEach(declearTask -> {
//将已经推送完成的报关单状态修改
this.receiptMessageRepository.updateDeclearTaskStatus(declearTask.getPushTime(),
declearTask.getResponseTime(),
declearTask.getResponseResults(),
declearTask.getResponseContent(),
"1",
declearTask.getChmsignmentId().longValue());
});
}
/**
* 传输推送失败回执数据
* @param token
* @throws Exception
*/
public void pushErrorReturnDate(String token){
//获取海关回执数据
List<ReceiptMessage> receiptMessageList = receiptMessageRepository.findReceiptMessageErrorReturnList();
if (receiptMessageList !=null && receiptMessageList.size() > 0){
this.sendCustoms(token,receiptMessageList);
}
}
/**
* 传输推送失败申报数据
* @param token
* @throws Exception
*/
public void pushErrorDeclareDate(String token){
//获取海关申报数据
List<ReceiptMessage> receiptMessageList = receiptMessageRepository.findReceiptMessageErrorDeclareList();
if (receiptMessageList !=null && receiptMessageList.size() > 0){
this.sendCustoms(token,receiptMessageList);
}
}
}