|
@@ -0,0 +1,135 @@
|
|
|
+package com.uas.cloud.pub.logictics.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.uas.cloud.commons.util.FastjsonUtils;
|
|
|
+import com.uas.cloud.commons.util.OkHttpUtils;
|
|
|
+import com.uas.cloud.pub.logictics.domain.LogisticsInfo;
|
|
|
+import com.uas.cloud.pub.logictics.domain.ReveiverInfo;
|
|
|
+import com.uas.cloud.pub.logictics.domain.SenderInfo;
|
|
|
+import com.uas.cloud.pub.logictics.repository.LogisticsInfoRepository;
|
|
|
+import com.uas.cloud.pub.logictics.service.LogisticsService;
|
|
|
+import com.uas.cloud.pub.logictics.util.LogisticsConfig;
|
|
|
+import okhttp3.Response;
|
|
|
+import org.apache.commons.collections.map.HashedMap;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import com.uas.cloud.pub.logictics.util.LogisticsUtils;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class LogisticsServiceImpl implements LogisticsService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private LogisticsInfoRepository logisticsInfoRepository;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String logisticsBooking(String orderid, String shipperCode, String goodsName, String receiverJson, String senderJson) throws Exception {
|
|
|
+ // 收件人信息
|
|
|
+ JSONObject reveiver = FastjsonUtils.parseObject(receiverJson);
|
|
|
+ ReveiverInfo receiverInfo = new ReveiverInfo();
|
|
|
+ receiverInfo.setName(reveiver.getString("name"));
|
|
|
+ receiverInfo.setTel(reveiver.getString("tel"));
|
|
|
+ receiverInfo.setProvinceName(reveiver.getString("provinceName"));
|
|
|
+ receiverInfo.setCityName(reveiver.getString("cityName"));
|
|
|
+ receiverInfo.setExpAreaName(reveiver.getString("expAreaName"));
|
|
|
+ receiverInfo.setAddress(reveiver.getString("address"));
|
|
|
+ // 发件人信息
|
|
|
+ JSONObject sender = FastjsonUtils.parseObject(senderJson);
|
|
|
+ SenderInfo senderInfo = new SenderInfo();
|
|
|
+ senderInfo.setName(sender.getString("name"));
|
|
|
+ senderInfo.setTel(sender.getString("tel"));
|
|
|
+ senderInfo.setProvinceName(sender.getString("provinceName"));
|
|
|
+ senderInfo.setCityName(sender.getString("cityName"));
|
|
|
+ senderInfo.setExpAreaName(sender.getString("expAreaName"));
|
|
|
+ senderInfo.setAddress(sender.getString("address"));
|
|
|
+ // 商品信息
|
|
|
+ Map<String, String> goodsMap = new HashMap<>();
|
|
|
+ goodsMap.put("GoodsName",goodsName);
|
|
|
+ List goodsList = new ArrayList();
|
|
|
+ goodsList.add(goodsMap);
|
|
|
+ // requestData请求参数
|
|
|
+ Map<String, Object> map = new HashedMap();
|
|
|
+ map.put("ShipperCode", shipperCode);
|
|
|
+ map.put("OrderCode", orderid);
|
|
|
+ map.put("PayType", LogisticsConfig.PAY_TYPE);
|
|
|
+ map.put("ExpType", LogisticsConfig.EXP_TYPE);
|
|
|
+ map.put("Receiver", receiverInfo);
|
|
|
+ map.put("Sender", senderInfo);
|
|
|
+ map.put("Commodity",goodsList);
|
|
|
+ String requestData = FastjsonUtils.toJson(map).toString();
|
|
|
+ // 系统级请求参数
|
|
|
+ Map<String, String> params = new HashMap();
|
|
|
+ params.put("EBusinessID", LogisticsConfig.E_BUSINESS_ID);
|
|
|
+ params.put("RequestType", LogisticsConfig.REQUEST_TYPE_BOOKING);
|
|
|
+ params.put("DataType", LogisticsConfig.DATA_TYPE);
|
|
|
+ String dataSign=LogisticsUtils.encrypt(requestData, LogisticsConfig.APP_KEY, "UTF-8");
|
|
|
+ params.put("DataSign", LogisticsUtils.urlEncoder(dataSign, "UTF-8"));
|
|
|
+ params.put("RequestData",LogisticsUtils.urlEncoder(requestData, "UTF-8"));
|
|
|
+ // 向快递鸟发请求
|
|
|
+ try {
|
|
|
+ Response response = OkHttpUtils.sendPost(LogisticsConfig.PROD_BOOKING_URL, params);
|
|
|
+ String reponseText = response.body().string();
|
|
|
+ System.out.println("reponseText==" + reponseText);
|
|
|
+ JSONObject reponse = FastjsonUtils.parseObject(reponseText);
|
|
|
+ String result = reponse.getString("Success");
|
|
|
+ if("true".equals(result)){
|
|
|
+ LogisticsInfo logisticsInfo = new LogisticsInfo(orderid, shipperCode, goodsName, receiverJson ,senderJson);
|
|
|
+ logisticsInfoRepository.save(logisticsInfo);
|
|
|
+ return "success";
|
|
|
+ }else{
|
|
|
+ return "fail";
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "fail";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, String> logisticsQuery(String shipperCode, String logisticsCode) throws Exception {
|
|
|
+ // requestData 请求参数
|
|
|
+ Map<String, Object> map = new HashedMap();
|
|
|
+ map.put("ShipperCode", shipperCode);
|
|
|
+ map.put("LogisticCode", logisticsCode);
|
|
|
+ String requestData = FastjsonUtils.toJson(map).toString();
|
|
|
+ // 系统级请求参数
|
|
|
+ Map<String, String> params = new HashMap();
|
|
|
+ params.put("EBusinessID", LogisticsConfig.E_BUSINESS_ID);
|
|
|
+ params.put("RequestType", LogisticsConfig.REQUEST_TYPE_QUERY);
|
|
|
+ params.put("DataType", LogisticsConfig.DATA_TYPE);
|
|
|
+ String dataSign=LogisticsUtils.encrypt(requestData, LogisticsConfig.APP_KEY, "UTF-8");
|
|
|
+ params.put("DataSign", LogisticsUtils.urlEncoder(dataSign, "UTF-8"));
|
|
|
+ params.put("RequestData",requestData);
|
|
|
+ System.out.println("param" + params.toString());
|
|
|
+ // 向快递鸟发请求
|
|
|
+ Map<String, String > info = new HashMap<>();
|
|
|
+ try {
|
|
|
+ Response response = OkHttpUtils.sendPost(LogisticsConfig.QUERY_URL, params);
|
|
|
+ String reponseText = response.body().string();
|
|
|
+ System.out.println("reponseText==" + reponseText);
|
|
|
+ JSONObject reponse = FastjsonUtils.parseObject(reponseText);
|
|
|
+ String result = reponse.getString("Success");
|
|
|
+ if("true".equals(result)){
|
|
|
+ String traces = reponse.getString("Traces");
|
|
|
+ info.put("traces", traces);
|
|
|
+ return info;
|
|
|
+ }else{
|
|
|
+ String reason = reponse.getString("Reason");
|
|
|
+ info.put("errorInfo", reason);
|
|
|
+ return info;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ info.put("errorInfo", "程序异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ return info;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|