Explorar o código

顺丰快递相关接口修改

huangwenjie %!s(int64=7) %!d(string=hai) anos
pai
achega
6de9ee2662

+ 3 - 2
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/entity/patient/prescription/PrescriptionLog.java

@ -22,7 +22,8 @@ public class PrescriptionLog extends IdEntity {
        reviewed("审核", 4),
        pay("支付", 5),
        expressage("配送", 6),
        finish("已完成", 7);
        finish("已完成", 7),
        sf("顺丰对接", 8);
        private String name;
        private int value;
@ -89,7 +90,7 @@ public class PrescriptionLog extends IdEntity {
    private String prescriptionCode;      //处方code 关联表wlyy_prescription code
    private Date createTime;               //处方创建时间
    private Integer status;                  //状态 (-1 审核不通过 , 0 审核中, 10 审核通过/待支付 ,20 配药中/支付成功  , 21支付失败  , 30 配药成功/等待领药 ,31 配送中 32 配送失败,100配送成功/已完成)
    private Integer type;                   //类型: 1智业对接 2易联众对接  3创建处方 4 审核  5付款 6 配送 7完成
    private Integer type;                   //类型: 1智业对接 2易联众对接  3创建处方 4 审核  5付款 6 配送 7完成 8顺丰对接
    private String userCode;                  //医生或者患者code
    private String userName;                  //医生或者患者name
    private Integer userType;                 //1 患者 2医生

+ 3 - 0
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/repository/prescription/PrescriptionExpressageDao.java

@ -12,4 +12,7 @@ import org.springframework.data.repository.PagingAndSortingRepository;
public interface PrescriptionExpressageDao extends PagingAndSortingRepository<PrescriptionExpressage, Long>, JpaSpecificationExecutor<PrescriptionExpressage> {
    @Query("from PrescriptionExpressage p where p.prescriptionCode=?1")
    PrescriptionExpressage findByPrescriptionPay(String prescriptionCode);
    @Query("from PrescriptionExpressage p where p.mailno=?1")
    PrescriptionExpressage findByPrescriptionExpressMailno(String mailno);
}

+ 25 - 0
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/repository/prescription/PrescriptionExpressageLogDao.java

@ -0,0 +1,25 @@
package com.yihu.wlyy.repository.prescription;
import com.yihu.wlyy.entity.patient.prescription.PrescriptionExpressageLog;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
/**
 * 处方物流配送详情
 * @author  huangwenjie 2017.08.03
 */
public interface PrescriptionExpressageLogDao extends
        PagingAndSortingRepository<PrescriptionExpressageLog, Long>,
        JpaSpecificationExecutor<PrescriptionExpressageLog> {
    /**
     * 根据处方号获取该处方的物流配送日志,按路由节点发生的时间倒序排序
     * @param prescriptionCode
     * @return
     */
    @Query("from PrescriptionExpressageLog p where p.prescriptionCode=?1 order by p.acceptTime desc")
    List<PrescriptionExpressageLog> findByPrescriptionCode(String prescriptionCode);
}

+ 0 - 1
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/repository/prescription/PrescriptionLogDao.java

@ -1,6 +1,5 @@
package com.yihu.wlyy.repository.prescription;
import com.yihu.wlyy.entity.patient.prescription.PrescriptionDispensaryCode;
import com.yihu.wlyy.entity.patient.prescription.PrescriptionLog;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

+ 317 - 42
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/service/app/express/SFExpressService.java

@ -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);
        }
    }
    }
}

+ 28 - 0
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/service/app/prescription/PrescriptionExpressageLogService.java

@ -0,0 +1,28 @@
package com.yihu.wlyy.service.app.prescription;
import com.yihu.wlyy.entity.patient.prescription.PrescriptionExpressageLog;
import com.yihu.wlyy.repository.prescription.PrescriptionExpressageLogDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * 处方物流配送详情
 * @author  huangwenjie 2017.08.03
 */
@Service
public class PrescriptionExpressageLogService {
    @Autowired
    private PrescriptionExpressageLogDao prescriptionExpressageLogDao;
    /**
     * 根据处方号获取该处方的物流配送日志,按路由节点发生的时间倒序排序
     * @param prescriptionCode
     * @return
     */
    public List<PrescriptionExpressageLog> findByPrescriptionCode(String prescriptionCode){
        return prescriptionExpressageLogDao.findByPrescriptionCode(prescriptionCode);
    }
}

+ 4 - 4
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/service/app/prescription/PrescriptionExpressageService.java

@ -106,7 +106,7 @@ public class PrescriptionExpressageService {
     * @param uid
     * @throws Exception
     */
    public void updatePrescriptionExpressage(PrescriptionExpressage prescriptionExpressage, String uid)throws Exception{
    public void updatePrescriptionExpressage(PrescriptionExpressage prescriptionExpressage)throws Exception{
        //保存处方物流记录
        prescriptionExpressageDao.save(prescriptionExpressage);
@ -114,12 +114,12 @@ public class PrescriptionExpressageService {
        PrescriptionLog prescriptionLog = new PrescriptionLog();
        prescriptionLog.setPrescriptionCode(prescriptionExpressage.getPrescriptionCode());
        prescriptionLog.setCode(UUID.randomUUID().toString());
        prescriptionLog.setType(PrescriptionLog.PrescriptionLogType.expressage.getValue());
        prescriptionLog.setType(PrescriptionLog.PrescriptionLogType.sf.getValue());
        prescriptionLog.setCreateTime(new Date());
        prescriptionLog.setFlag(1);
        prescriptionLog.setUserCode(uid);
        prescriptionLog.setUserType(1);
        prescriptionLog.setStatus(PrescriptionLog.PrescriptionLogStatus.expressageing.getValue());
        prescriptionLogDao.save(prescriptionLog);
    }
}

+ 29 - 15
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/util/SFUtils.java

@ -33,6 +33,26 @@ public class SFUtils {
    private PrescriptionInfoService prescriptionInfoService;
    /**
     * 生成接口校验码
     * 1.先把XML报文与checkword前后连接。
     * 2.把连接后的字符串做 MD5 编码。
     * 3.把MD5编码后的数据进行 Base64 编码,此时编码后的字符串即为校验码。
     * @param xml   xml报文
     * @param sf_check_word  接口check_word
     * @return
     */
    public String verifyCodeSFXmlStr(String xml,String sf_check_word){
        String verifyCode = xml+sf_check_word;
        verifyCode = MD5.GetMD5Code(verifyCode);
        verifyCode = new BASE64Encoder().encode(verifyCode.getBytes());
        return verifyCode;
    }
    /**
     * 查询派送地址是否属于顺丰的派送范围
     * @param d_address     派送地址
@ -165,22 +185,16 @@ public class SFUtils {
    }
    /**
     * 生成接口校验码
     * 1.先把XML报文与checkword前后连接。
     * 2.把连接后的字符串做 MD5 编码。
     * 3.把MD5编码后的数据进行 Base64 编码,此时编码后的字符串即为校验码。
     * @param xml   xml报文
     * @param sf_check_word  接口check_word
     * 顺丰路由查询
     * @param sfexpress_obj
     * @param sf_code
     * @return
     */
    public String verifyCodeSFXmlStr(String xml,String sf_check_word){
        String verifyCode = xml+sf_check_word;
        verifyCode = MD5.GetMD5Code(verifyCode);
        verifyCode = new BASE64Encoder().encode(verifyCode.getBytes());
        return verifyCode;
    public String getRoutInfos(PrescriptionExpressage sfexpress_obj, String sf_code) {
        //head 传入接口接入码
        StringBuilder xml = new StringBuilder("<Request service='RouteService' lang='zh-cn'> <Head>"+sf_code+"</Head><Body>");
        xml.append("<RouteRequest tracking_type='1' method_type='1' ").append("tracking_number='"+sfexpress_obj.getMailno()+"' />");
        xml.append("</Body></Request>");
        return xml.toString();
    }
}

+ 67 - 0
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/web/express/SFExpressApiController.java

@ -0,0 +1,67 @@
package com.yihu.wlyy.web.express;
import com.yihu.wlyy.aop.ObserverRequired;
import com.yihu.wlyy.service.app.express.SFExpressService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.net.URLDecoder;
/**
 * 提供给顺丰快递调用的相关接口
 * @author huangwenjie 2017.08.03
 */
@Controller
@RequestMapping(value = "/express/sf")
@Api(description = "顺丰速运相关接口")
public class SFExpressApiController extends BaseController {
    @Autowired
    private SFExpressService sfexpressService;
    private String successxml = "<Response service=”RoutePushService”><Head>OK</Head></Response>";
    private String errorxml = "<Response service=”RoutePushService”>\n" +
            "\n" +
            "<Head>ERR</Head>\n" +
            "\n" +
            "<ERROR code=\"4001\">系统发生数据错误或运行时异常</ERROR>\n" +
            "\n" +
            "</Response>";
    @RequestMapping(value="/routepushservice",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("向顺丰快递下订单")
    @ObserverRequired
    public String SFRoutePushService (
            @ApiParam(name="xml", value="入参报文") @RequestParam(value = "xml",required = true) String xml){
        try {
            xml = URLDecoder.decode(xml,"utf-8");
            sfexpressService.SFRoutePushService(xml);
            return successxml;
        }catch (Exception e){
            //日志文件中记录异常信息
            error(e);
            //返回接口异常信息处理结果
            return "<Response service=”RoutePushService”>\n" +
                    "\n" +
                    "<Head>ERR</Head>\n" +
                    "\n" +
                    "<ERROR code=\"-1\">系统发生数据错误或运行时异常</ERROR>\n" +
                    "\n" +
                    "</Response>";
        }
    }
}

+ 40 - 12
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/web/patient/express/SFExpressController.java

@ -1,9 +1,14 @@
package com.yihu.wlyy.web.patient.express;
import com.yihu.wlyy.aop.ObserverRequired;
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.service.app.express.SFExpressService;
import com.yihu.wlyy.service.app.prescription.PrescriptionExpressageLogService;
import com.yihu.wlyy.service.app.prescription.PrescriptionExpressageService;
import com.yihu.wlyy.service.app.prescription.PrescriptionService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
@ -17,6 +22,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.UUID;
/**
@ -35,7 +41,13 @@ public class SFExpressController extends BaseController {
    @Autowired
    private PrescriptionExpressageService prescriptionExpressageService;
    @RequestMapping(value = "/SFOrderFilterService", method = RequestMethod.GET)
    @Autowired
    private PrescriptionService prescriptionService;
    @Autowired
    private PrescriptionExpressageLogService prescriptionExpressageLogService;
    @RequestMapping(value = "/sforderfilterservice", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询派送地址是否属于顺丰的派送范围")
    public String SFOrderFilterService(
@ -59,9 +71,10 @@ public class SFExpressController extends BaseController {
        }
    }
    @RequestMapping(value="/SFOrderService",method = RequestMethod.POST)
    @RequestMapping(value="/sforderservice",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("向顺丰快递下订单")
    @ObserverRequired
    public String SFOrderService(
            @ApiParam(name="prescriptionCode", value="处方编号") @RequestParam(value = "prescriptionCode",required = true) String prescriptionCode
//            @ApiParam(name="j_company", value="寄件方公司名称") @RequestParam(value = "j_company") String j_company,
@ -93,7 +106,8 @@ public class SFExpressController extends BaseController {
                    return write(200, "顺丰快递下单成功!");
                }else{
                //如果该处方的快递单号未生成,则继续下单
                    //先判断地址是否可派送
                    //先判断地址是否可派送---由于下订单前已判断是否能否派送
                    boolean delivery = sfexpressService.getSFOrderFilterService(sfexpress_obj.getProvinceName()+sfexpress_obj.getCityName()+sfexpress_obj.getTownName()+sfexpress_obj.getAddress());
                    if(delivery){
@ -114,7 +128,7 @@ public class SFExpressController extends BaseController {
                        sfexpress_obj = sfexpressService.postSFOrderService(sfexpress_obj);
                        //保存快递单号和增加处方物流记录为配送
                        prescriptionExpressageService.updatePrescriptionExpressage(sfexpress_obj,getUID());
                        prescriptionExpressageService.updatePrescriptionExpressage(sfexpress_obj);
                        return write(200, "顺丰快递下单成功!");
                    }else{
@ -130,18 +144,32 @@ public class SFExpressController extends BaseController {
        }
    }
    @RequestMapping(value="/SFOrderService",method = RequestMethod.GET)
    @RequestMapping(value="/sfrouteservice",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("通过快递单号或者业务订单号查询包裹路由信息")
    @ApiOperation("通过处方编码查询顺丰物流派送记录")
    @ObserverRequired
    public String SFRouteService(
            @ApiParam(name="tracking_number", value="快递单号或者订单编号") @RequestParam(value = "tracking_number",required = true) String tracking_number,
            @ApiParam(name="tracking_type", value="查询号类别:1为快递单号,2为订单编号") @RequestParam(value = "tracking_type",required = true) Integer tracking_type){
            @ApiParam(name="prescriptionCode", value="处方编号") @RequestParam(value = "prescriptionCode",required = true) String prescriptionCode){
            //1.先从本地获取
        try {
            Prescription prescription = prescriptionService.findByCode(prescriptionCode);
            List<PrescriptionExpressageLog> sfexpresslogList = prescriptionExpressageLogService.findByPrescriptionCode(prescriptionCode);
            PrescriptionExpressage sfexpress_obj = prescriptionExpressageService.findByPrescriptionCode(prescriptionCode);
        return "";
            //先判断该处方是否已完成配送,如果配送完成,则直接取本地的物流记录
            if(100 == prescription.getStatus()){
                return write(200, "查询成功","data",sfexpresslogList);
            }else{
                //如果配送未完成,则调用顺丰接口获取物流路由日志,与本地匹配,进行增量修改
                sfexpresslogList = sfexpressService.getRoutInfos(prescription,sfexpress_obj,sfexpresslogList);
                return write(200, "查询成功","data",sfexpresslogList);
            }
        }catch (Exception e){
            //日志文件中记录异常信息
            error(e);
            //返回接口异常信息处理结果
            return error(-1, e.getMessage());
        }
    }
}