IcsKuaidiService.java
7.36 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
package com.huaheng.pc.u8.service;
import com.alibaba.fastjson.JSON;
import com.huaheng.common.constant.HttpConstant;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.framework.aspectj.ApiLogAspect;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
import com.huaheng.pc.shipment.kuaidiHeader.domain.KuaidiHeader;
import com.huaheng.pc.u8.domain.KuaidiU8Request;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @ClassName icsKuaidiService
* @Description TODO
* @Author Administrator
* @Date 2021/5/3116:16
*/
@Service
public class IcsKuaidiService {
private static String param = "kd";
private static String paramV1 = "kd-new";
private static String tokenUrl = "https://api.yonyouup.com/system/token?from_account=huaheng2019&app_key=opa7271824ba6ce3541&app_secret=8425b85f0fea43bca6c14f149fe18b21";
@Resource
private AddressService addressService;
//获取token
public String getToken() {
String result = HttpUtils.sendGet(tokenUrl);
if (StringUtils.isEmpty(result)) {
throw new ServiceException("U8系统接口地址错误或服务器连接不到或返回为空");
}
Map map = JSON.parseObject(result, HashMap.class);
String accessToken = map.get("token").toString();
Map hashMap = JSON.parseObject(accessToken, HashMap.class);
String token = hashMap.get("id").toString();
return token;
}
public String getUrl() {
//获取url
Address address = new Address();
address.setParam(param);
address.setWarehouseCode("KS0001");
address = addressService.selectEntity(address);
if (address == null) {
throw new ServiceException("查询不到U8接口地址");
}
return address.getUrl();
}
public String getUrlNew() {
//获取url
Address address = new Address();
address.setParam(paramV1);
address.setWarehouseCode("KS0001");
address = addressService.selectEntity(address);
if (address == null) {
throw new ServiceException("查询不到U8接口地址");
}
return address.getUrl();
}
//通过单号获取出库单信息
public List<Map<String, String>> getKDMeseeage(KuaidiHeader kuaidiHeader) {
//获取url和获取token
String url = this.getUrl();
String token = this.getToken();
if (!StringUtils.isEmpty(kuaidiHeader.getOrderBill()) && StringUtils.isEmpty(kuaidiHeader.getFromCode())) {
if (kuaidiHeader.getOrderBill().substring(0, 2).equals("AA")) {
url
= url + "token=" + token + "&ds_sequence=1&rows_per_page=1000&code_begin=" + kuaidiHeader.getOrderBill() + "&code_end=" + kuaidiHeader.getOrderBill();
} else {
url
= url + "token=" + token + "&ds_sequence=2&rows_per_page=1000&code_begin=" + kuaidiHeader.getOrderBill() + "&code_end=" + kuaidiHeader.getOrderBill();
}
}
if (!StringUtils.isEmpty(kuaidiHeader.getOrderBill()) && StringUtils.isNotEmpty(kuaidiHeader.getFromCode())) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = dateFormat.format(kuaidiHeader.getCreated());
if (kuaidiHeader.getOrderBill().substring(0, 2).equals("AA")) {
url
= url + "token=" + token + "&ds_sequence=1&rows_per_page=1000&auditdate=" + dateString;
} else {
url
= url + "token=" + token + "&ds_sequence=2&rows_per_page=1000&auditdate=" + dateString;
}
}
if (!StringUtils.isEmpty(kuaidiHeader.getSourceCode())) {
if (kuaidiHeader.getSourceCode().substring(0, 2).equals("AH")) {
url
= url + "token=" + token + "&ds_sequence=1&rows_per_page=1000&subconsignmentcode=" + kuaidiHeader.getSourceCode();
} else {
url = url + "token=" + token + "&ds_sequence=2&rows_per_page=1000&subconsignmentcode=" + kuaidiHeader.getSourceCode();
}
}
//获取信息
ApiLog log = null;
String result = null;
HttpHeaders headers = new HttpHeaders();
try {
log = ApiLogAspect.initApiLog("get", url,
null, null, QuantityConstant.WAREHOUSE_KS);
log.setWarehouseCode(QuantityConstant.WAREHOUSE_KS);
log.setApiName("快递获取u8销售出库单");
log.setRequestBody(url);
result = HttpUtils.sendGet(url);
log.setResponseBody(result);
} catch (Exception e) {
// e.printStackTrace();
ApiLogAspect.setApiLogException(log, e);
} finally {
ApiLogAspect.finishApiLog(log, headers, result);
}
if (StringUtils.isEmpty(result)) {
throw new ServiceException("U8系统接口地址错误或服务器连接不到或返回为空");
}
Map map = JSON.parseObject(result, HashMap.class);
String code = map.get("errcode").toString();
if ("0".equals(code)) {
List<Map<String, String>> lists = new ArrayList<>();
if (StringUtils.isNull(map.get("saleoutlistall"))) {
throw new ServiceException("获取的U8信息错误,为空");
}
String msg = map.get("saleoutlistall").toString();
lists = JSON.parseObject(msg, ArrayList.class);
return lists;
} else {
throw new ServiceException(map.get("errmsg").toString());
}
}
//通过单号获取出库单信息--新版
//type 888 昆山股份公司 807 长沙公司 801 徐州公司
public List<Map<String, String>> getKDMeseeageV1(KuaidiHeader kuaidiHeader, String type, String warehouseCode) {
//获取url和获取token
String url = this.getUrlNew();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = dateFormat.format(kuaidiHeader.getCreated());
KuaidiU8Request kuaidiU8Request = new KuaidiU8Request();
kuaidiU8Request.setType(type);
kuaidiU8Request.setVerifydate(dateString);
String jsonParam = JSON.toJSONString(kuaidiU8Request);
String result = OkHttpUtils.bodypost(url, jsonParam, warehouseCode);
if (StringUtils.isNull(result) || StringUtils.isEmpty(result)) {
throw new ServiceException("获取快递单:接口地址错误或返回为空");
}
AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
if (ajaxResult.getCode() != HttpConstant.OK) {
return null;
}
List<Map<String, String>> map = new ArrayList<>();
map = (List<Map<String, String>>) ajaxResult.getData();
return map;
}
}