ApiService.java
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
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.ReceiptMessage;
import com.erry.iclear.dateInterface.api.response.TokenResult;
import com.erry.iclear.dateInterface.common.Constants;
import com.erry.iclear.dateInterface.entity.DeclearTask;
import com.erry.iclear.dateInterface.repository.ReceiptMessageRepository;
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;
/**
* 获取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(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(refreshTokenUrl)
.map(map)
.encoding("utf-8");
HttpClientUtil.post(config);
}
/**
* 传输海关回执数据
* @param token
* @throws Exception
*/
public void pushCustoms(String token){
Header[] headers = HttpHeader.custom()
.other("Content-Type", "application/json")
.other("Authorization", token)
.build();
//获取海关回执数据
List<ReceiptMessage> receiptMessageList = receiptMessageRepository.findReceiptMessageList();
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(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)){
//如果凭证失效,则刷新凭证
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("接口调用失败!");
declearTask.setResponseContent(ex.getMessage());
declearTaskList.add(declearTask);
ex.printStackTrace();
}
});
declearTaskList.forEach(declearTask -> {
//将已经推送完成的回执信息状态修改
receiptMessageRepository.updateDeclearTaskStatus(declearTask.getPushTime(),
declearTask.getResponseTime(),
declearTask.getResponseResults(),
declearTask.getResponseContent(),
declearTask.getChmsignmentId().longValue());
});
}
/**
* 传输完整报文数据
* @param token
* @throws Exception
*/
public void pushMawb(String token){
Header[] headers = HttpHeader.custom()
.other("Content-Type", "application/json")
.other("Authorization", token)
.build();
//获取海关回执数据
}
}