ソースを参照

顺丰快递相关接口

huangwenjie 7 年 前
コミット
2d177cd9ab

+ 5 - 0
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/entity/patient/prescription/PrescriptionExpressage.java

@ -18,6 +18,7 @@ public class PrescriptionExpressage extends IdEntity{
    private String code;                    //业务code
    private String prescriptionCode;      //处方code  关联表wlyy_prescription code
    private String mailno;                  //快递单号
    private String name;                    //收货人名称
    private String mobile;                  //收货人手机号码
    private String phone;                   //收货人座机号
@ -180,4 +181,8 @@ public class PrescriptionExpressage extends IdEntity{
    public void setOneselfPickupFlg(Integer oneselfPickupFlg) {
        this.oneselfPickupFlg = oneselfPickupFlg;
    }
    public String getMailno() { return mailno; }
    public void setMailno(String mailno) { this.mailno = mailno; }
}

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

@ -0,0 +1,181 @@
package com.yihu.wlyy.service.app.express;
import com.yihu.wlyy.entity.patient.prescription.PrescriptionExpressage;
import com.yihu.wlyy.service.BaseService;
import com.yihu.wlyy.util.HttpClientUtil;
import com.yihu.wlyy.util.SFUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
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.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
 * 顺丰快递业务层方法
 * Created by huangwenjie  on 2017/8/2.
 */
@Service
public class SFExpressService extends BaseService {
    private static Logger logger = LoggerFactory.getLogger(SFExpressService.class);
    //顺丰快递接口请求地址
    @Value("${express.sf_url}")
    private String sf_url;
    //顺丰快递接口接入编码
    @Value("${express.sf_code}")
    private String sf_code;
    //顺丰快递接口checkword
    @Value("${express.sf_check_word}")
    private String sf_check_word;
    @Autowired
    private HttpClientUtil HttpClientUtil;
    @Autowired
    private SFUtils SFUtils;
    /**
     * 查询派送地址是否属于顺丰的派送范围
     * @param d_address 派送地址
     * @return
     */
    public Boolean getSFOrderFilterService(String d_address) throws Exception {
        boolean result = false;
        String xml = SFUtils.getSFOrderFilterXml(d_address,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)) {
            //是否能派送:1:人工确认;2:可收派;3:不可以收派
            String filter_result = "";
            //错误代对应的文字
            Document redoc = doc.selectSingleNode("/Response/Body/OrderFilterResponse").getDocument();
            Element root = redoc.getRootElement();
            List<?> child = root.elements();
            for (Object o : child) {
                Element e = (Element) o;
                filter_result = e.attributeValue("filter_result");
            }
            if(StringUtils.isNotBlank(filter_result) && "2".equals(filter_result)){
                result = true;
            }
        }
        return result;
    }
    /**
     * 向顺丰快递下订单
     * @param sfexpress_obj
     * @return
     * @throws Exception
     */
    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)) {
            //是否能派送:1:人工确认;2:可收派;3:不可以收派
            String filter_result = "";
            String orderid = "";//业务订单号
            String mailno = "";//顺丰运单号
            //错误代对应的文字
            Document redoc = doc.selectSingleNode("/Response/Body/Orderresponse").getDocument();
            Element root = redoc.getRootElement();
            List<?> child = root.elements();
            for (Object o : child) {
                Element e = (Element) o;
                filter_result = e.attributeValue("filter_result");
                orderid = e.attributeValue("orderid");
                mailno = e.attributeValue("mailno");
            }
            if(StringUtils.isNotBlank(filter_result) && "3".equals(filter_result)){
                logger.info("顺丰快递下订单失败:派送地址不可派送,处方编号:"+sfexpress_obj.getPrescriptionCode());
                throw new Exception("顺丰快递下订单失败:派送地址不可派送");
            }
            sfexpress_obj.setMailno(mailno);
        }
        return sfexpress_obj;
    }
    /**
     * 组装请求参数,发送请求
     * @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);
//            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");
                }
                error = title+","+errorCode+","+errorMessage;
                logger.info(error);
                throw new Exception(error);
            }
        }
    }
}

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

@ -15,7 +15,7 @@ import java.util.UUID;
/**
 * Created by chenweida on 2017/7/27.
 * 订单相关
 * 订单物流相关
 */
@Service
public class PrescriptionExpressageService {
@ -99,4 +99,27 @@ public class PrescriptionExpressageService {
        prescriptionDispensaryCode.setIsUse(1);
        return 1;
    }
    /**
     * 保存处方物流记录
     * @param prescriptionExpressage
     * @param uid
     * @throws Exception
     */
    public void addPrescriptionExpressage(PrescriptionExpressage prescriptionExpressage, String uid)throws Exception{
        //保存处方物流记录
        prescriptionExpressageDao.save(prescriptionExpressage);
        //保存配送日志
        PrescriptionLog prescriptionLog = new PrescriptionLog();
        prescriptionLog.setPrescriptionCode(prescriptionExpressage.getPrescriptionCode());
        prescriptionLog.setCode(UUID.randomUUID().toString());
        prescriptionLog.setType(PrescriptionLog.PrescriptionLogType.expressage.getValue());
        prescriptionLog.setCreateTime(new Date());
        prescriptionLog.setFlag(1);
        prescriptionLog.setUserCode(uid);
        prescriptionLog.setUserType(1);
        prescriptionLog.setStatus(PrescriptionLog.PrescriptionLogStatus.expressageing.getValue());
        prescriptionLogDao.save(prescriptionLog);
    }
}

+ 0 - 3
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/util/HttpClientUtil.java

@ -1,7 +1,6 @@
package com.yihu.wlyy.util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
@ -9,13 +8,11 @@ import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

+ 183 - 0
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/util/SFUtils.java

@ -0,0 +1,183 @@
package com.yihu.wlyy.util;
import com.yihu.wlyy.entity.patient.prescription.PrescriptionExpressage;
import com.yihu.wlyy.entity.patient.prescription.PrescriptionInfo;
import com.yihu.wlyy.service.app.prescription.PrescriptionInfoService;
import org.apache.commons.lang.StringUtils;
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.stereotype.Component;
import sun.misc.BASE64Encoder;
import java.util.List;
@Component
public class SFUtils {
    private static Logger logger = LoggerFactory.getLogger(SFUtils.class);
    private String j_company;       //寄件方公司名称
    private String j_contact;       //寄件方联系人
    private String j_tel;           //寄件方联系电话
    private String j_province;      //寄件方所在省份
    private String j_city;          //寄件方所在城市名称
    private String j_county;        //寄件人所在县/区
    private String j_address;       //寄件方详细地址,包括省市县/区
    @Autowired
    private PrescriptionInfoService prescriptionInfoService;
    /**
     * 查询派送地址是否属于顺丰的派送范围
     * @param d_address     派送地址
     * @param sf_code       顺丰接口接入码
     * @param orderid       订单ID
     * @param j_tel         寄方电话
     * @param j_address     寄方地址
     * @param d_tel         收方电话
     * @return
     */
    public String getSFOrderFilterXml(String d_address,String sf_code,String orderid,String j_tel,String j_address,String d_tel){
        //head 传入接口接入码
        StringBuilder xml = new StringBuilder("<Request service='OrderFilterService' lang='zh-cn'> <Head>"+sf_code+"</Head><Body>");
        xml.append("<OrderFilter filter_type='1' ");
        //订单号
        if(StringUtils.isNotBlank(orderid)){
            xml.append("orderid='"+orderid+"' ");
        }
        //派送地址,必填
        xml.append("d_address='"+d_address+"' >");
        //如果扩张字段有不为空的,则增加扩张字段参数
        if(StringUtils.isNotBlank(j_tel) ||
                StringUtils.isNotBlank(j_address)||
                    StringUtils.isNotBlank(d_tel)){
            xml.append("<OrderFilterOption ");
            //寄方电话
            if(StringUtils.isNotBlank(j_tel)){
                xml.append("j_tel='"+j_tel+"' ");
            }
            //寄方地址
            if(StringUtils.isNotBlank(j_address)){
                xml.append("j_address='"+j_address+"' ");
            }
            //收方电话
            if(StringUtils.isNotBlank(d_tel)){
                xml.append("d_tel='"+d_tel+"' ");
            }
            xml.append("/></OrderFilter>");
        }
        xml.append("</Body></Request>");
        return xml.toString();
    }
    /**
     * 向顺丰快递下订单
     * @param sfexpress_obj
     * @return
     */
    public String postSFOrderService(PrescriptionExpressage sfexpress_obj,String sf_code) {
        //head 传入接口接入码
        StringBuilder xml = new StringBuilder("<Request service='OrderService' lang='zh-cn'> <Head>"+sf_code+"</Head><Body>");
        xml.append("<Order ");
        //订单号(快递业务号作为订单号"SF开头的为顺丰快递")
        xml.append("orderid='"+sfexpress_obj.getCode()+"' ");
        /**
         * --------寄方参数------
         */
        //寄件方公司名称
        if(StringUtils.isNotBlank(j_company)){
            xml.append("j_company='"+j_company+"' ");
        }
        //寄方联系人
        if(StringUtils.isNotBlank(j_contact)){
            xml.append("j_contact='"+j_contact+"' ");
        }
        //寄件方联系电话
        if(StringUtils.isNotBlank(j_tel)){
            xml.append("j_tel='"+j_tel+"' ");
        }
        //寄方所在省份
        if(StringUtils.isNotBlank(j_province)){
            xml.append("j_province='"+j_province+"' ");
        }
        //寄方所在城市
        if(StringUtils.isNotBlank(j_city)){
            xml.append("j_city='"+j_city+"' ");
        }
        //寄方所在县/区
        if(StringUtils.isNotBlank(j_county)){
            xml.append("j_county='"+j_county+"' ");
        }
        //寄件方详细地址,包括省市县/区
        if(StringUtils.isNotBlank(j_address)){
            xml.append("j_address='"+j_address+"' ");
        }
        /**
         * --------收方参数------
         */
        //派送地址,必填
        xml.append("d_address='"+sfexpress_obj.getAddress()+"' >");
        //到件方单位名称,因为接口不能为空,这里默认放到件方联系人
        xml.append("d_company='"+sfexpress_obj.getName()+"' >");
        //到件方联系人
        xml.append("d_contact='"+sfexpress_obj.getName()+"' >");
        //到件方联系电话
        xml.append("d_tel='"+sfexpress_obj.getPhone()+"' >");
        //到件方手机
        if(StringUtils.isNotBlank(sfexpress_obj.getMobile())){
            xml.append("d_mobile='"+sfexpress_obj.getMobile()+"' ");
        }
        xml.append("d_province='"+sfexpress_obj.getProvinceName()+"' >");
        xml.append("d_city='"+sfexpress_obj.getCityName()+"' >");
        xml.append("d_county='"+sfexpress_obj.getTownName()+"' >");
        xml.append("d_address='"+sfexpress_obj.getAddress()+"' >");
        //快件产品类别:21,医药专运(常温)
        //付款方式:1:寄方付,2:收方付 3:第三方付
        xml.append("express_type='21' pay_method='2' >");
        //获取订单的药品详情
        List<PrescriptionInfo> prescriptionInfolist = prescriptionInfoService.getPrescriptionInfo(sfexpress_obj.getPrescriptionCode());
        if(!prescriptionInfolist.isEmpty()){
            for (int i = 0; i < prescriptionInfolist.size(); i++) {
                xml.append("<cargo name='"+prescriptionInfolist.get(i).getDrugName()+"' count='"+prescriptionInfolist.get(i).getNum()+"'></cargo>");
            }
        }
        xml.append("</Order></Request>");
        return xml.toString();
    }
    /**
     * 生成接口校验码
     * 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;
    }
}

+ 127 - 0
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/web/patient/express/SFExpressController.java

@ -0,0 +1,127 @@
package com.yihu.wlyy.web.patient.express;
import com.yihu.wlyy.entity.patient.prescription.PrescriptionExpressage;
import com.yihu.wlyy.service.app.express.SFExpressService;
import com.yihu.wlyy.service.app.prescription.PrescriptionExpressageService;
import com.yihu.wlyy.util.DateUtil;
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.util.UUID;
/**
 * 顺丰速运相关接口
 * Created by huangwenjie on 2017/8/1.
 */
@Controller
@RequestMapping(value = "/patient/sfexpress")
@Api(description = "顺丰速运相关接口")
public class SFExpressController extends BaseController {
    @Autowired
    private SFExpressService sfexpressService;
    @Autowired
    private PrescriptionExpressageService prescriptionExpressageService;
    @RequestMapping(value = "/SFOrderFilterService", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询派送地址是否属于顺丰的派送范围")
    public String SFOrderFilterService(
            @ApiParam(name = "d_address", value = "派送地址", defaultValue = "福建省厦门市思明区软件园二期望海路55号")
            @RequestParam(value = "d_address", required = true) String d_address){
        try {
            boolean result = sfexpressService.getSFOrderFilterService(d_address);
            if(result){
                return write(200, "请求成功,地址可派送!");
            }else{
                return write(-1, "请求失败,地址不可派送!");
            }
        }catch (Exception e){
            //日志文件中记录异常信息
            error(e);
            //返回接口异常信息处理结果
            return error(-1, e.getMessage());
        }
    }
    @RequestMapping(value="/SFOrderService",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("向顺丰快递下订单")
    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,
//            @ApiParam(name="j_contact", value="寄件方联系人") @RequestParam(value = "j_contact") String j_contact,
//            @ApiParam(name="j_tel", value="寄件方联系电话") @RequestParam(value = "j_tel") String j_tel,
//            @ApiParam(name="j_mobile", value="寄件方手机") @RequestParam(value = "j_mobile") String j_mobile,
//            @ApiParam(name="j_province", value="寄件方所在省份") @RequestParam(value = "j_province") String j_province,
//            @ApiParam(name="j_city", value="寄件方所在城市名称") @RequestParam(value = "j_city") String j_city,
//            @ApiParam(name="j_county", value="寄件人所在县/区") @RequestParam(value = "j_county") String j_county,
//            @ApiParam(name="j_address", value="寄件方详细地址,不包括省市区") @RequestParam(value = "j_address") String j_address,
//            @ApiParam(name="d_company", value="到件方公司名称") @RequestParam(value = "d_company") String d_company,
//            @ApiParam(name="d_contact", value="到件方联系人") @RequestParam(value = "d_contact") String d_contact,
//            @ApiParam(name="d_tel", value="到件方联系电话") @RequestParam(value = "d_tel") String d_tel,
//            @ApiParam(name="d_mobile", value="到件方手机") @RequestParam(value = "d_mobile") String d_mobile,
//            @ApiParam(name="d_province", value="到件方所在省份") @RequestParam(value = "d_province") String d_province,
//            @ApiParam(name="d_city", value="到件方所在城市名称") @RequestParam(value = "d_city") String d_city,
//            @ApiParam(name="d_county", value="到件方所在县/区") @RequestParam(value = "d_county") String d_county,
//            @ApiParam(name="d_address", value="到件方完整的详细地址") @RequestParam(value = "d_address") String d_address
    ){
        try {
            PrescriptionExpressage sfexpress_obj = prescriptionExpressageService.findByPrescriptionCode(prescriptionCode);
            if(sfexpress_obj == null){
                return write(-1, "顺丰快递下单失败,未找到该处方的派送地址!");
            }
            boolean delivery = sfexpressService.getSFOrderFilterService(sfexpress_obj.getProvinceName()+sfexpress_obj.getCityName()+sfexpress_obj.getTownName()+sfexpress_obj.getAddress());
            //先判断地址是否可派送
            if(delivery){
//                sfexpress_obj.setCode("SF"+UUID.randomUUID().toString().replace("-",""));
//                sfexpress_obj.setPrescriptionCode(prescriptionCode);
//                sfexpress_obj.setName(sfexpress_obj.getName());
//                sfexpress_obj.setPhone(sfexpress_obj.getPhone());
//                sfexpress_obj.setMobile(d_mobile);
//                sfexpress_obj.setProvinceName(d_province);
//                sfexpress_obj.setCityName(d_city);
//                sfexpress_obj.setTownName(d_county);
//                sfexpress_obj.setAddress(d_address);
//                sfexpress_obj.setHospitalName("顺丰速递");
//                sfexpress_obj.setOneselfPickupFlg(0);//是否自取 1是 0否
//                sfexpress_obj.setCreateTime(DateUtil.getNowDate());
                //请求顺丰接口下单,成功下单后,返回快递单号
                sfexpress_obj = sfexpressService.postSFOrderService(sfexpress_obj);
                //保存处方物流记录
                prescriptionExpressageService.addPrescriptionExpressage(sfexpress_obj,getUID());
                return write(200, "顺丰快递下单成功!");
            }else{
                return write(-1, "顺丰快递下单失败,地址不可派送!");
            }
        }catch (Exception e){
            //日志文件中记录异常信息
            error(e);
            //返回接口异常信息处理结果
            return error(-1, e.getMessage());
        }
    }
}