|
@ -1,7 +1,15 @@
|
|
|
package com.yihu.wlyy.service.app.express;
|
|
|
|
|
|
import com.yihu.wlyy.entity.patient.prescription.Prescription;
|
|
|
import com.yihu.wlyy.entity.patient.prescription.PrescriptionExpressage;
|
|
|
import com.yihu.wlyy.entity.patient.prescription.PrescriptionExpressageLog;
|
|
|
import com.yihu.wlyy.entity.patient.prescription.PrescriptionLog;
|
|
|
import com.yihu.wlyy.repository.prescription.PrescriptionDao;
|
|
|
import com.yihu.wlyy.repository.prescription.PrescriptionExpressageDao;
|
|
|
import com.yihu.wlyy.repository.prescription.PrescriptionExpressageLogDao;
|
|
|
import com.yihu.wlyy.repository.prescription.PrescriptionLogDao;
|
|
|
import com.yihu.wlyy.service.BaseService;
|
|
|
import com.yihu.wlyy.util.DateUtil;
|
|
|
import com.yihu.wlyy.util.HttpClientUtil;
|
|
|
import com.yihu.wlyy.util.SFUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@ -10,14 +18,18 @@ import org.apache.http.message.BasicNameValuePair;
|
|
|
import org.dom4j.Document;
|
|
|
import org.dom4j.DocumentHelper;
|
|
|
import org.dom4j.Element;
|
|
|
import org.dom4j.dom.DOMDocument;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.TransactionDefinition;
|
|
|
import org.springframework.transaction.TransactionStatus;
|
|
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
/**
|
|
@ -44,10 +56,76 @@ public class SFExpressService extends BaseService {
|
|
|
@Autowired
|
|
|
private HttpClientUtil HttpClientUtil;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private SFUtils SFUtils;
|
|
|
|
|
|
@Autowired
|
|
|
private PrescriptionLogDao prescriptionLogDao;
|
|
|
|
|
|
@Autowired
|
|
|
private PrescriptionDao prescriptionDao;
|
|
|
|
|
|
@Autowired
|
|
|
private JpaTransactionManager transactionManager;
|
|
|
|
|
|
@Autowired
|
|
|
private PrescriptionExpressageLogDao prescriptionExpressageLogDao;
|
|
|
|
|
|
@Autowired
|
|
|
private PrescriptionExpressageDao prescriptionExpressageDao;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 组装请求参数,发送请求
|
|
|
* @param xml
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
private String SFExpressPost(String xml)throws Exception{
|
|
|
String verifyCode = SFUtils.verifyCodeSFXmlStr(xml,sf_check_word);
|
|
|
List<NameValuePair> params = new ArrayList<>();
|
|
|
params.add(new BasicNameValuePair("xml", xml));
|
|
|
params.add(new BasicNameValuePair("verifyCode", verifyCode));
|
|
|
String re = HttpClientUtil.post(sf_url, params, "UTF-8");
|
|
|
return re;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 校验返回的xml报文
|
|
|
* @param responseXml
|
|
|
* @param title
|
|
|
*/
|
|
|
private void verificationResponXml(String responseXml,String title) throws Exception {
|
|
|
String error = "";
|
|
|
if (StringUtils.isBlank(responseXml)) {
|
|
|
// 如果返回的XML报文为空,请求也算失败
|
|
|
//保存http日志
|
|
|
error = title + ",返回的XML为空!";
|
|
|
logger.info(error);
|
|
|
throw new Exception(error);
|
|
|
}else{
|
|
|
// 报错的报文示例<Response service="ScopeService"><Head>ERR</Head><ERROR code="8014">校验码错误 </ERROR></Response>
|
|
|
Document doc = DocumentHelper.parseText(responseXml);
|
|
|
String headvalue = doc.selectSingleNode("/Response/Head").getText();
|
|
|
if(StringUtils.isNotBlank(headvalue) && "ERR".equals(headvalue)){
|
|
|
//错误代码
|
|
|
String errorCode = "";
|
|
|
//错误代对应的文字
|
|
|
String errorMessage = doc.selectSingleNode("/Response/ERROR").getText();
|
|
|
Document error_doc = doc.selectSingleNode("/Response/ERROR").getDocument();
|
|
|
Element root = error_doc.getRootElement();
|
|
|
List<?> child = root.elements();
|
|
|
for (Object o : child){
|
|
|
Element e = (Element) o;
|
|
|
errorCode = e.attributeValue("code");
|
|
|
}
|
|
|
error = title+","+errorCode+","+errorMessage;
|
|
|
logger.info(error);
|
|
|
throw new Exception(error);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 查询派送地址是否属于顺丰的派送范围
|
|
@ -97,8 +175,10 @@ public class SFExpressService extends BaseService {
|
|
|
public PrescriptionExpressage postSFOrderService(PrescriptionExpressage sfexpress_obj) throws Exception {
|
|
|
String xml = SFUtils.postSFOrderService(sfexpress_obj,sf_code);
|
|
|
String re = this.SFExpressPost(xml);
|
|
|
|
|
|
//xml验证
|
|
|
verificationResponXml(re,"向顺丰快递下订单失败!");
|
|
|
|
|
|
Document doc = DocumentHelper.parseText(re);
|
|
|
String headvalue = doc.selectSingleNode("/Response/Head").getText();
|
|
|
if(StringUtils.isNotBlank(headvalue) && "OK".equals(headvalue)) {
|
|
@ -120,62 +200,257 @@ public class SFExpressService extends BaseService {
|
|
|
logger.info("顺丰快递下订单失败:派送地址不可派送,处方编号:"+sfexpress_obj.getPrescriptionCode());
|
|
|
throw new Exception("顺丰快递下订单失败:派送地址不可派送");
|
|
|
}
|
|
|
|
|
|
if(StringUtils.isNotBlank(mailno)){
|
|
|
logger.info("顺丰快递下订单失败:未获取到快递单号!"+sfexpress_obj.getPrescriptionCode());
|
|
|
throw new Exception("顺丰快递下订单失败:未获取到快递单号!");
|
|
|
}
|
|
|
sfexpress_obj.setMailno(mailno);
|
|
|
}
|
|
|
return sfexpress_obj;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 组装请求参数,发送请求
|
|
|
* 调用顺丰接口获取物流路由日志,与本地匹配,进行增量修改,并返回完整日志列表
|
|
|
* @param prescription
|
|
|
* @param sfexpress_obj
|
|
|
* @param sfexpresslogList
|
|
|
* @return
|
|
|
*/
|
|
|
public List<PrescriptionExpressageLog> getRoutInfos(Prescription prescription, PrescriptionExpressage sfexpress_obj, List<PrescriptionExpressageLog> sfexpresslogList) throws Exception {
|
|
|
String xml = SFUtils.getRoutInfos(sfexpress_obj,sf_code);
|
|
|
String re = this.SFExpressPost(xml);
|
|
|
//xml验证
|
|
|
verificationResponXml(re,"查询顺丰快递路由信息失败!");
|
|
|
Document doc = DocumentHelper.parseText(re);
|
|
|
String headvalue = doc.selectSingleNode("/Response/Head").getText();
|
|
|
if(StringUtils.isNotBlank(headvalue) && "OK".equals(headvalue)) {
|
|
|
|
|
|
}
|
|
|
List<Document> redocs = doc.selectNodes("/Response/Body/RouteResponse");
|
|
|
|
|
|
String mailno = "";
|
|
|
for (Document routdoc : redocs) {
|
|
|
Element root = routdoc.getRootElement();
|
|
|
List<?> child = root.elements();
|
|
|
for (Object o : child) {
|
|
|
Element e = (Element) o;
|
|
|
mailno = e.attributeValue("mailno");
|
|
|
//判断快递单号不为空,且和我们本地的一致
|
|
|
if(StringUtils.isNotBlank(mailno) &&
|
|
|
sfexpress_obj.getMailno().equals(mailno)){
|
|
|
|
|
|
//解析报文,结合本地数据返回最终的日志结果
|
|
|
return this.xmltologlist(e,sfexpresslogList,sfexpress_obj);
|
|
|
}else{
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
private List<PrescriptionExpressageLog> xmltologlist(Element e, List<PrescriptionExpressageLog> sfexpresslogList, PrescriptionExpressage sfexpress_obj)throws Exception{
|
|
|
|
|
|
//获取本地所有路由信息的时间集
|
|
|
Set<String> routAcceptTimeSet = new HashSet<>();
|
|
|
for (PrescriptionExpressageLog log: sfexpresslogList) {
|
|
|
routAcceptTimeSet.add(DateUtil.dateToStrLong(log.getAcceptTime()));
|
|
|
}
|
|
|
|
|
|
List<?> child = e.elements();
|
|
|
|
|
|
List<PrescriptionExpressageLog> newroutinfolist = new ArrayList<>();
|
|
|
|
|
|
//用于判断新节点是否包含了"已收件"的节点
|
|
|
boolean isContainEndRoutInfo = false;
|
|
|
|
|
|
for (Object o : child) {
|
|
|
Element routinfoe = (Element) o;
|
|
|
String accept_time = routinfoe.attributeValue("accept_time");
|
|
|
String accept_address = routinfoe.attributeValue("accept_address");
|
|
|
String accept_remark = routinfoe.attributeValue("remark");
|
|
|
String opcode = routinfoe.attributeValue("opcode");
|
|
|
|
|
|
//和时间集作比对,过滤已存在的
|
|
|
if(!routAcceptTimeSet.contains(accept_time)){
|
|
|
PrescriptionExpressageLog newlogobj = new PrescriptionExpressageLog();
|
|
|
newlogobj.setAcceptTime(DateUtil.strToDate(accept_time));
|
|
|
newlogobj.setAcceptAddress(accept_address);
|
|
|
newlogobj.setAcceptRemark(accept_remark);
|
|
|
newlogobj.setOpCode(opcode);
|
|
|
newlogobj.setCode(UUID.randomUUID().toString().replace("-",""));
|
|
|
newlogobj.setPrescriptionCode(sfexpress_obj.getPrescriptionCode());
|
|
|
newlogobj.setExpressageCode(sfexpress_obj.getExpressageCode());
|
|
|
newlogobj.setCreateTime(new Date());
|
|
|
newroutinfolist.add(newlogobj);
|
|
|
sfexpresslogList.add(newlogobj);
|
|
|
//accept_remark为"已收件",代表派送结束
|
|
|
if("已收件".equals(accept_remark)){
|
|
|
isContainEndRoutInfo = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
|
|
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务
|
|
|
TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
|
|
|
try {
|
|
|
|
|
|
//如果有新增的路由信息,则添加到数据库
|
|
|
if(!newroutinfolist.isEmpty()){
|
|
|
prescriptionExpressageLogDao.save(newroutinfolist);
|
|
|
}
|
|
|
|
|
|
//如果路由信息节点包含了"已收件"节点,这修改处方派送状态为完成,增加物流派送日志为完成
|
|
|
if(isContainEndRoutInfo){
|
|
|
//修改处方状态为完成
|
|
|
prescriptionDao.updateStatus(sfexpress_obj.getPrescriptionCode(), PrescriptionLog.PrescriptionLogStatus.finish.getValue());
|
|
|
|
|
|
//保存配送成功的日志
|
|
|
PrescriptionLog prescriptionLog = new PrescriptionLog();
|
|
|
prescriptionLog.setPrescriptionCode(sfexpress_obj.getPrescriptionCode());
|
|
|
prescriptionLog.setCode(UUID.randomUUID().toString());
|
|
|
prescriptionLog.setType(PrescriptionLog.PrescriptionLogType.sf.getValue());
|
|
|
prescriptionLog.setCreateTime(new Date());
|
|
|
prescriptionLog.setFlag(1);
|
|
|
prescriptionLog.setStatus(PrescriptionLog.PrescriptionLogStatus.finish.getValue());
|
|
|
prescriptionLogDao.save(prescriptionLog);
|
|
|
}
|
|
|
|
|
|
//事务提交
|
|
|
transactionManager.commit(status);
|
|
|
} catch (Exception ex) {
|
|
|
//报错事务回滚
|
|
|
transactionManager.rollback(status);
|
|
|
}
|
|
|
|
|
|
|
|
|
return sfexpresslogList;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 解析顺丰推送过来的路由信息,和本地数据匹配并进行增量更新)
|
|
|
* @param xml
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
private String SFExpressPost(String xml)throws Exception{
|
|
|
String verifyCode = SFUtils.verifyCodeSFXmlStr(xml,sf_check_word);
|
|
|
List<NameValuePair> params = new ArrayList<>();
|
|
|
params.add(new BasicNameValuePair("xml", xml));
|
|
|
params.add(new BasicNameValuePair("verifyCode", verifyCode));
|
|
|
String re = HttpClientUtil.post(sf_url, params, "UTF-8");
|
|
|
return re;
|
|
|
public void SFRoutePushService(String xml) throws Exception{
|
|
|
Document doc = DocumentHelper.parseText(xml);
|
|
|
Document bodydoc = doc.selectSingleNode("/Request/Body").getDocument();
|
|
|
Element root = bodydoc.getRootElement();
|
|
|
if (root.element("WaybillRoute") != null) //包含WaybillRoute节点
|
|
|
{
|
|
|
root = root.element("WaybillRoute");
|
|
|
}
|
|
|
List<?> child = root.elements();
|
|
|
|
|
|
Map<String,List<PrescriptionExpressageLog>> wayroutlsit = new HashMap<>();
|
|
|
for (Object o : child) {
|
|
|
Element routinfoe = (Element) o;
|
|
|
PrescriptionExpressageLog sflog = new PrescriptionExpressageLog();
|
|
|
String accept_time = routinfoe.attributeValue("acceptTime");
|
|
|
String accept_address = routinfoe.attributeValue("acceptAddress");
|
|
|
String accept_remark = routinfoe.attributeValue("remark");
|
|
|
String opcode = routinfoe.attributeValue("opCode");
|
|
|
String mailno = routinfoe.attributeValue("mailno");
|
|
|
String orderid = routinfoe.attributeValue("orderid");
|
|
|
|
|
|
sflog.setAcceptTime(DateUtil.strToDate(accept_time));
|
|
|
sflog.setAcceptAddress(accept_address);
|
|
|
sflog.setAcceptRemark(accept_remark);
|
|
|
sflog.setOpCode(opcode);
|
|
|
sflog.setExpressageCode(orderid);
|
|
|
sflog.setCode(UUID.randomUUID().toString());
|
|
|
if(wayroutlsit.keySet().contains(mailno)){
|
|
|
wayroutlsit.get(mailno).add(sflog);
|
|
|
}else{
|
|
|
List<PrescriptionExpressageLog> newsflogs = new ArrayList<>();
|
|
|
newsflogs.add(sflog);
|
|
|
wayroutlsit.put(mailno,newsflogs);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if(!wayroutlsit.keySet().isEmpty()){
|
|
|
for (String mailno:wayroutlsit.keySet()) {
|
|
|
List<PrescriptionExpressageLog> pushSFLogs = wayroutlsit.get(mailno);
|
|
|
//同一个快递单号的执行一个事务操作
|
|
|
this.saveSFPushRoutInfos(mailno,pushSFLogs);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 校验返回的xml报文
|
|
|
* @param responseXml
|
|
|
* @param title
|
|
|
* 同一个快递单号的执行一个事务操作
|
|
|
* @param mailno
|
|
|
* @param pushSFLogs
|
|
|
*/
|
|
|
private void verificationResponXml(String responseXml,String title) throws Exception {
|
|
|
String error = "";
|
|
|
if (StringUtils.isBlank(responseXml)) {
|
|
|
// 如果返回的XML报文为空,请求也算失败
|
|
|
//保存http日志
|
|
|
error = title + ",返回的XML为空!";
|
|
|
logger.info(error);
|
|
|
// logService.saveHttpLog(isSuccess,url,content,method,null,msgBody,res,error,logService.archiveType);
|
|
|
throw new Exception(error);
|
|
|
}else{
|
|
|
// <Response service="ScopeService"><Head>ERR</Head><ERROR code="8014">校验码错误 </ERROR></Response>
|
|
|
Document doc = DocumentHelper.parseText(responseXml);
|
|
|
String headvalue = doc.selectSingleNode("/Response/Head").getText();
|
|
|
if(StringUtils.isNotBlank(headvalue) && "ERR".equals(headvalue)){
|
|
|
//错误代码
|
|
|
String errorCode = "";
|
|
|
//错误代对应的文字
|
|
|
String errorMessage = doc.selectSingleNode("/Response/ERROR").getText();
|
|
|
Document error_doc = doc.selectSingleNode("/Response/ERROR").getDocument();
|
|
|
Element root = error_doc.getRootElement();
|
|
|
List<?> child = root.elements();
|
|
|
for (Object o : child){
|
|
|
Element e = (Element) o;
|
|
|
errorCode = e.attributeValue("code");
|
|
|
public void saveSFPushRoutInfos(String mailno,List<PrescriptionExpressageLog> pushSFLogs){
|
|
|
|
|
|
//根据快递单号获取处方配送详细信息
|
|
|
PrescriptionExpressage sfexpress = prescriptionExpressageDao.findByPrescriptionExpressMailno(mailno);
|
|
|
|
|
|
List<PrescriptionExpressageLog> localroutinfos = prescriptionExpressageLogDao.findByPrescriptionCode(sfexpress.getPrescriptionCode());
|
|
|
|
|
|
List<PrescriptionExpressageLog> newroutinfolist = new ArrayList<>();
|
|
|
|
|
|
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
|
|
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务
|
|
|
TransactionStatus status = transactionManager.getTransaction(def); // 获得事务状态
|
|
|
try {
|
|
|
|
|
|
//用于判断新节点是否包含了"已收件"的节点
|
|
|
boolean isContainEndRoutInfo = false;
|
|
|
|
|
|
//获取本地所有路由信息的时间集
|
|
|
Set<String> routAcceptTimeSet = new HashSet<>();
|
|
|
for (PrescriptionExpressageLog log: localroutinfos) {
|
|
|
routAcceptTimeSet.add(DateUtil.dateToStrLong(log.getAcceptTime()));
|
|
|
}
|
|
|
|
|
|
for (PrescriptionExpressageLog pushlog: pushSFLogs) {
|
|
|
|
|
|
if("已收件".equals(pushlog.getAcceptRemark())){
|
|
|
isContainEndRoutInfo = true;
|
|
|
}
|
|
|
error = title+","+errorCode+","+errorMessage;
|
|
|
logger.info(error);
|
|
|
throw new Exception(error);
|
|
|
|
|
|
if(routAcceptTimeSet.contains(DateUtil.dateToStrLong(pushlog.getAcceptTime()))){
|
|
|
continue;
|
|
|
}else{
|
|
|
pushlog.setPrescriptionCode(sfexpress.getPrescriptionCode());
|
|
|
newroutinfolist.add(pushlog);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//如果有新增的路由信息,则添加到数据库
|
|
|
if(!newroutinfolist.isEmpty()){
|
|
|
prescriptionExpressageLogDao.save(newroutinfolist);
|
|
|
}
|
|
|
|
|
|
//如果路由信息节点包含了"已收件"节点,这修改处方派送状态为完成,增加物流派送日志为完成
|
|
|
if(isContainEndRoutInfo){
|
|
|
//修改处方状态为完成
|
|
|
prescriptionDao.updateStatus(sfexpress.getPrescriptionCode(), PrescriptionLog.PrescriptionLogStatus.finish.getValue());
|
|
|
|
|
|
//保存配送成功的日志
|
|
|
PrescriptionLog prescriptionLog = new PrescriptionLog();
|
|
|
prescriptionLog.setPrescriptionCode(sfexpress.getPrescriptionCode());
|
|
|
prescriptionLog.setCode(UUID.randomUUID().toString());
|
|
|
prescriptionLog.setType(PrescriptionLog.PrescriptionLogType.sf.getValue());
|
|
|
prescriptionLog.setCreateTime(new Date());
|
|
|
prescriptionLog.setFlag(1);
|
|
|
prescriptionLog.setStatus(PrescriptionLog.PrescriptionLogStatus.finish.getValue());
|
|
|
prescriptionLogDao.save(prescriptionLog);
|
|
|
}
|
|
|
//事务提交
|
|
|
transactionManager.commit(status);
|
|
|
} catch (Exception ex) {
|
|
|
//报错事务回滚
|
|
|
transactionManager.rollback(status);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|