TOPGP订阅快递及推送

This commit is contained in:
Bo 2021-08-10 15:53:45 +08:00
parent be45eadf5c
commit a22f446b28
4 changed files with 109 additions and 39 deletions

View File

@ -6,6 +6,7 @@ import com.ruoyi.bps.service.IExpSubsPushApiService;
import com.ruoyi.common.annotation.Log; import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
@ -18,6 +19,8 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
/** /**
* 接受快递推送信息的API接口Controller * 接受快递推送信息的API接口Controller
@ -58,7 +61,7 @@ public class ExpSubsPushApiController extends BaseController {
@PostMapping("api/express/topgpSubscribe") @PostMapping("api/express/topgpSubscribe")
public String topgpSubscribe(HttpServletRequest request, HttpServletResponse response) throws IOException { public String topgpSubscribe(HttpServletRequest request, HttpServletResponse response) throws IOException {
return expSubsPushApiService.ExpressSubscribeWithTopgp(request); return expSubsPushApiService.ExpressSubscribeFromTopgp(request);
} }

View File

@ -32,5 +32,5 @@ public interface IExpSubsPushApiService {
* @param request * @param request
* @return * @return
*/ */
public String ExpressSubscribeWithTopgp(HttpServletRequest request) throws IOException; public String ExpressSubscribeFromTopgp(HttpServletRequest request) throws IOException;
} }

View File

@ -16,12 +16,17 @@ import com.kuaidi100.sdk.response.SubscribeResp;
import com.kuaidi100.sdk.utils.SignUtils; import com.kuaidi100.sdk.utils.SignUtils;
import com.ruoyi.bps.domain.ExpSubsPushResp; import com.ruoyi.bps.domain.ExpSubsPushResp;
import com.ruoyi.bps.domain.ExpSubscribe; import com.ruoyi.bps.domain.ExpSubscribe;
import com.ruoyi.bps.domain.ExpTopgpLog;
import com.ruoyi.bps.service.IExpSubsPushApiService; import com.ruoyi.bps.service.IExpSubsPushApiService;
import com.ruoyi.bps.service.IExpSubsPushRespService; import com.ruoyi.bps.service.IExpSubsPushRespService;
import com.ruoyi.bps.service.IExpSubscribeService; import com.ruoyi.bps.service.IExpSubscribeService;
import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.TopgpXmlUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -42,9 +47,13 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
String tid = PropertiesReader.get("tid"); String tid = PropertiesReader.get("tid");
String secret_key = PropertiesReader.get("secret_key"); String secret_key = PropertiesReader.get("secret_key");
String secret_secret = PropertiesReader.get("secret_secret"); */ String secret_secret = PropertiesReader.get("secret_secret"); */
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
@Value("${express.key}") @Value("${express.key}")
private String key; private String key;
@Value("${topgp.webservice.toptest}")
private String webserviceUrl;
@Autowired @Autowired
private IExpSubsPushRespService expSubsPushRespService; private IExpSubsPushRespService expSubsPushRespService;
@ -58,10 +67,19 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
@Override @Override
public SubscribeResp ExpressSubscribe(ExpSubscribe expSubscribe) { public SubscribeResp ExpressSubscribe(ExpSubscribe expSubscribe) {
//如果订阅来源是topgp则来源为topgp,否则为local
String loginFrom= expSubscribe.getSalt(); String loginFrom= expSubscribe.getSalt();
String subscribeFrom= StringUtils.isNotEmpty(loginFrom)?loginFrom.equals("topgp")?"topgp":"localhost":"localhost"; String subscribeFrom= StringUtils.isNotEmpty(loginFrom)?loginFrom.equals("topgp")?"topgp":"local":"local";
SubscribeParameters subscribeParameters = new SubscribeParameters(); if( expSubscribe.getSid()==null || expSubscribe.getSid()==0) {
expSubscribe.setSid(System.currentTimeMillis()); //获取时间戳
}
if(StringUtils.isEmpty(expSubscribe.getSubscribeTime())){
expSubscribe.setSubscribeTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
}
//组合订阅参数
SubscribeParameters subscribeParameters = new SubscribeParameters();
SubscribeResp subscribeResp = new SubscribeResp(); SubscribeResp subscribeResp = new SubscribeResp();
subscribeParameters.setCallbackurl("http://report.bpsemi.cn:8081/it_war/anon/subscribeCallBackUrl"); subscribeParameters.setCallbackurl("http://report.bpsemi.cn:8081/it_war/anon/subscribeCallBackUrl");
subscribeParameters.setPhone(expSubscribe.getPhone()); subscribeParameters.setPhone(expSubscribe.getPhone());
@ -78,8 +96,8 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
IBaseClient subscribe = new Subscribe(); IBaseClient subscribe = new Subscribe();
try{ try{
//推送订阅并获得快递100响应结果
HttpResult httpResult= subscribe.execute(subscribeReq); HttpResult httpResult= subscribe.execute(subscribeReq);
//System.out.println(httpResult);
subscribeResp= new Gson().fromJson(httpResult.getBody(),SubscribeResp.class); subscribeResp= new Gson().fromJson(httpResult.getBody(),SubscribeResp.class);
}catch (Exception e) }catch (Exception e)
{ {
@ -91,13 +109,15 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
return subscribeResp; return subscribeResp;
} }
//订阅记录写入数据库 //订阅记录写入数据库
ExpSubscribe newExpSubscribe = new ExpSubscribe(); ExpSubscribe newExpSubscribe = new ExpSubscribe();
newExpSubscribe.setSid(expSubscribe.getSid()); //将时间戳设为Sid 210810 yangbo
newExpSubscribe.setCompany(expSubscribe.getCompany()); newExpSubscribe.setCompany(expSubscribe.getCompany());
newExpSubscribe.setNumber(expSubscribe.getNumber()); newExpSubscribe.setNumber(expSubscribe.getNumber());
newExpSubscribe.setPhone(expSubscribe.getPhone()); newExpSubscribe.setPhone(expSubscribe.getPhone());
newExpSubscribe.setSalt(subscribeFrom); //偷懒把请求来源记录到salt栏位不再增加exp_subscribe字段了以后找时间改吧 newExpSubscribe.setSalt(subscribeFrom); //偷懒把请求来源记录到salt栏位不再增加exp_subscribe字段了以后找时间改吧
newExpSubscribe.setSubscribeTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss")); newExpSubscribe.setSubscribeTime(expSubscribe.getSubscribeTime());
newExpSubscribe.setResult((subscribeResp.isResult())?"true":"false"); newExpSubscribe.setResult((subscribeResp.isResult())?"true":"false");
newExpSubscribe.setReturnCode(subscribeResp.getReturnCode()); newExpSubscribe.setReturnCode(subscribeResp.getReturnCode());
newExpSubscribe.setMessage(subscribeResp.getMessage()); newExpSubscribe.setMessage(subscribeResp.getMessage());
@ -177,8 +197,28 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
String state = subscribePushResult.getState(); String state = subscribePushResult.getState();
//if(state.equals("3")){ //if(state.equals("3")){
//处理签收逻辑 //处理签收逻辑
//todo
if(state.equals("3")) {
//如果该快递单号是由TOPGP订阅的话则推送给TOPGP
ExpSubscribe expSubscribe=new ExpSubscribe();
expSubscribe.setCompany(subscribePushResult.getCom());
expSubscribe.setNumber(subscribePushResult.getNu());
expSubscribe.setSalt("topgp");
expSubscribeService.selectExpSubscribeList(expSubscribe);
Map<String, Object> requestMap = new HashMap<>();
requestMap.put("expressNum", subscribePushResult.getNu());
requestMap.put("expressCom", subscribePushResult.getCom());
requestMap.put("expressState", subscribePushResult.getState());
requestMap.put("requestTime", DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
//调用topgp Webservice接口
JSONObject jsonObject= SendRequestToTopgp(webserviceUrl, "express_testRequest",requestMap);
log.info(jsonObject.toJSONString());
}
//将快递流转状态存入数据库 //将快递流转状态存入数据库
ExpSubsPushResp expSubsPushResp=new ExpSubsPushResp(); expSubsPushRespService.insertExpSubsPushResp(ToExpSubsPushResp(subscribePushParamResp)); //无论数据库中存在快递单号+快递公司编码都更新数据库 210809 yangbo 修正
/*ExpSubsPushResp expSubsPushResp=new ExpSubsPushResp();
expSubsPushResp.setLastResultNu(subscribePushResult.getNu()); expSubsPushResp.setLastResultNu(subscribePushResult.getNu());
expSubsPushResp.setLastResultCom(subscribePushResult.getCom()); expSubsPushResp.setLastResultCom(subscribePushResult.getCom());
List<ExpSubsPushResp> list=expSubsPushRespService.selectExpSubsPushRespList(expSubsPushResp); List<ExpSubsPushResp> list=expSubsPushRespService.selectExpSubsPushRespList(expSubsPushResp);
@ -193,7 +233,7 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
}else { }else {
//如果数据库中没有快递单号+快递公司编码则更插入新记录 //如果数据库中没有快递单号+快递公司编码则更插入新记录
expSubsPushRespService.insertExpSubsPushResp(ToExpSubsPushResp(subscribePushParamResp)); expSubsPushRespService.insertExpSubsPushResp(ToExpSubsPushResp(subscribePushParamResp));
} }*/
// } // }
return subscribeResp; return subscribeResp;
@ -272,50 +312,79 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
* @return * @return
*/ */
@Override @Override
public String ExpressSubscribeWithTopgp(HttpServletRequest request) throws IOException { public String ExpressSubscribeFromTopgp(HttpServletRequest request) throws IOException {
//定义Return变量
String retrunStr;
//获取httpServletRequest传过来的Json字符串并进行解析 //获取httpServletRequest传过来的Json字符串并进行解析
JSONObject contentJson= JSONObject.parseObject(ServletUtils.getRequestContent(request)); JSONObject contentJson= JSONObject.parseObject(ServletUtils.getRequestContent(request));
String deliveryNo= contentJson.getString("deliveryNo"); String requestId=contentJson.getString("requestID"); //TOPGP请求ID年月日时分稍毫秒
String expressNo = contentJson.getString("expressNo"); String deliveryNo= contentJson.getString("deliveryNo"); //TOPGP出货单号
String company = contentJson.getString("company"); String expressNo = contentJson.getString("expressNo"); //TOPGP快递单号
String phone = contentJson.getString("phone"); String company = contentJson.getString("company"); //TOPGP物流公司编号
//如果出货单号或者快递单号为空则返回错误信息并写入Logo String phone = contentJson.getString("phone"); //TOPGP出货单号
if(StringUtils.isEmpty(deliveryNo) || StringUtils.isEmpty(expressNo)){ Long timeStamp = System.currentTimeMillis(); //获取时间戳
String subscribeTime= DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"); //获取订阅时间
String responseCode; //返回给Topgp的返回码
//如果请求ID出货单号或者快递单号为空的处理
if(StringUtils.isEmpty(deliveryNo) || StringUtils.isEmpty(expressNo) || StringUtils.isEmpty(requestId)){
SubscribeResp subscribeResp=new SubscribeResp(); SubscribeResp subscribeResp=new SubscribeResp();
subscribeResp.setMessage("快递单号或出货单号为空"); subscribeResp.setMessage("快递单号或出货单号为空");
subscribeResp.setResult(false); subscribeResp.setResult(false);
subscribeResp.setReturnCode("700"); subscribeResp.setReturnCode("700");
responseCode="700";
Map<String,Object> map=new HashMap<>(); Map<String,Object> map=new HashMap<>();
map.put("subscribeResp",subscribeResp); map.put("subscribeResp",subscribeResp);
map.put("deliveryNo",deliveryNo); map.put("deliveryNo",deliveryNo);
map.put("expressNo",expressNo); map.put("expressNo",expressNo);
//写入Logo
//todo
//返回错误信息 //返回错误信息
return JSONObject.toJSONString(map); retrunStr= JSONObject.toJSONString(map);
}else {
} //组合向快递100推送订阅请求的参数
//向快递100推送订阅请求
ExpSubscribe expSubscribe=new ExpSubscribe(); ExpSubscribe expSubscribe=new ExpSubscribe();
expSubscribe.setSid(timeStamp); //时间戳
expSubscribe.setNumber(expressNo); expSubscribe.setNumber(expressNo);
expSubscribe.setCompany(company); expSubscribe.setCompany(company);
expSubscribe.setPhone(phone); expSubscribe.setPhone(phone);
expSubscribe.setSalt("topgp"); //偷懒把请求来源记录到salt栏位不再增加exp_subscribe字段了以后找时间改吧 expSubscribe.setSubscribeTime(subscribeTime); //订阅时间
expSubscribe.setSalt("topgp"); // 订阅来源 偷懒把请求来源记录到salt栏位不再增加exp_subscribe字段了以后找时间改吧
//向快递100推送订阅请求取得订阅返回结果
SubscribeResp subscribeResp= ExpressSubscribe(expSubscribe); SubscribeResp subscribeResp= ExpressSubscribe(expSubscribe);
responseCode =subscribeResp.getReturnCode();
/*Object object = JSONObject.toJSON(subscribeResp); //根据快递100的订阅返回结果组合返回Topgp的JSON字符串
Map map=JSONObject.parseObject(object.toString(), Map.class);*/
Map<String,Object> map= new HashMap<>(); Map<String,Object> map= new HashMap<>();
map.put("expSubscribe",subscribeResp); map.put("requestId",timeStamp); //时间戳
map.put("deliveryNo",deliveryNo); map.put("expSubscribe",subscribeResp); //快递100的返回结果报文
map.put("expressNo",expressNo); map.put("deliveryNo",deliveryNo); //出货单号
return JSONObject.toJSONString(map); map.put("expressNo",expressNo); //快递单号
map.put("responseCode",subscribeResp.getReturnCode()); //返回码
//返回Json字符串给TOPGP
retrunStr= JSONObject.toJSONString(map);
}
//记录本次TOPGP订阅请求的Log
ExpTopgpLog expTopgpLog=new ExpTopgpLog();
expTopgpLog.setRequestId(timeStamp);
expTopgpLog.setRequestType("fromTopgp");
expTopgpLog.setExpressNumber(expressNo);
expTopgpLog.setDeliveryNumber(deliveryNo);
expTopgpLog.setRequestStr(contentJson.toString());
expTopgpLog.setRequestTime(subscribeTime);
expTopgpLog.setResponseCode(responseCode);
//返回TOPGP json字符串
return retrunStr;
}
public JSONObject SendRequestToTopgp( String url,String tip,Map<String,Object> map) {
String param = TopgpXmlUtils.GetTopgpRequestXml(tip, map);
String returnXml = HttpUtils.sendXmlPost(url, param);
return TopgpXmlUtils.GetStatusFromTopgpResponse(returnXml);
} }
} }

View File

@ -1,7 +1,7 @@
package com.ruoyi.common.utils; package com.ruoyi.common.utils;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils; import com.ruoyi.common.utils.http.HttpUtils;
import org.json.JSONObject;
import org.json.XML; import org.json.XML;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -34,10 +34,9 @@ public class TopgpXmlUtils {
" &lt;/Access>\n" + " &lt;/Access>\n" +
" &lt;RequestContent>\n" + " &lt;RequestContent>\n" +
" &lt;Parameter>\n" + " &lt;Parameter>\n" +
" &lt;Record>\n" + " &lt;Record>\n");
" &lt;Field name=");
for(String key:mapInfo.keySet()){ for(String key:mapInfo.keySet()){
stringBuffer.append("'"+key+"' value='" +mapInfo.get(key).toString().replaceAll("&","&amp")+"' />\n"); stringBuffer.append("&lt;Field name='"+key+"' value='" +mapInfo.get(key).toString().replaceAll("&","&amp")+"' />\n");
} }
stringBuffer.append(" &lt;/Record>\n" + stringBuffer.append(" &lt;/Record>\n" +
" &lt;/Parameter>\n" + " &lt;/Parameter>\n" +
@ -54,12 +53,11 @@ public class TopgpXmlUtils {
/** /**
* 将TOPGP返回的XML转化为Json,并提出返回Status * 将TOPGP返回的XML转化为Json,并提出返回Status
* @param TopgpResonseXml 调用TOPGP的Webservice的方法名 express_testRequest * @param TopgpResponseXml 调用TOPGP的Webservice的方法名 express_testRequest
* @return Status JsonObject * @return Status JsonObject
*/ */
public static JSONObject GetStatusFromTopgpResponse(String TopgpResonseXml) { public static JSONObject GetStatusFromTopgpResponse(String TopgpResponseXml) {
JSONObject jsonObject = XML.toJSONObject(TopgpResonseXml); JSONObject jsonObject = JSONObject.parseObject(XML.toJSONObject(TopgpResponseXml).toString());
JSONObject envelope = jsonObject.getJSONObject("SOAP-ENV:Envelope"); JSONObject envelope = jsonObject.getJSONObject("SOAP-ENV:Envelope");
JSONObject body = envelope.getJSONObject("SOAP-ENV:Body"); JSONObject body = envelope.getJSONObject("SOAP-ENV:Body");
JSONObject express_testResponse = body.getJSONObject("fjs1:express_testResponse"); JSONObject express_testResponse = body.getJSONObject("fjs1:express_testResponse");