package com.matrix.system.shopXcx.api.service.impl; import com.alibaba.fastjson.JSON; import com.matrix.component.tools.HttpCurlUtil; import com.matrix.core.exception.GlobleException; import com.matrix.core.pojo.AjaxResult; import com.matrix.core.tools.LogUtil; import com.matrix.core.tools.StringUtils; import com.matrix.system.common.constance.AppConstance; import com.matrix.system.shopXcx.api.service.WxShopLogisticsQueryService; import com.matrix.system.shopXcx.api.vo.LogisticsInfoVo; import com.matrix.system.shopXcx.bean.ShopDeliveryInfo; import com.matrix.system.shopXcx.bean.ShopLogisticsInfo; import com.matrix.system.shopXcx.dao.ShopDeliveryInfoDao; import com.matrix.system.shopXcx.dao.ShopLogisticsInfoDao; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.security.MessageDigest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author pengliang * @description 物流信息查询 * @date 2019-06-21 10:00 */ @Service(value = "WxShopLogisticsQueryService") public class WxShopLogisticsQueryServiceImpl implements WxShopLogisticsQueryService { /** * 快递鸟用户id */ @Value("${logistics.eBusinessID}") private String eBusinessID; /** * 快递鸟密钥 */ @Value("${logistics.appKey}") private String appKey; /** * 快递鸟接口地址 */ @Value("${logistics.url}") private String logisticsUrl; /** * 快递鸟接口指令 */ @Value("${logistics.requestType}") private String requestType; @Autowired private ShopDeliveryInfoDao shopDeliveryInfoDao; @Autowired private ShopLogisticsInfoDao shopLogisticsInfoDao; /** * 查询物流信息 * * @param shopDeliveryInfo * @return */ @Transactional(rollbackFor = Exception.class) @Override public AjaxResult selectLogisticsInfo(ShopDeliveryInfo shopDeliveryInfo) { List shopLogisticsList = new ArrayList(); AjaxResult result = new AjaxResult(); Map logisticsInfo = new HashMap(); if (shopDeliveryInfo == null) { return new AjaxResult(AjaxResult.STATUS_FAIL, "找不到发货信息"); } if (!StringUtils.isNotBlank(shopDeliveryInfo.getWaybillNo()) || !StringUtils.isNotBlank(shopDeliveryInfo.getLogisticsCompanyCode())) { return new AjaxResult(AjaxResult.STATUS_FAIL, "物流公司信息为空"); } logisticsInfo.put("logisticsCompany", shopDeliveryInfo.getLogisticsCompany()); logisticsInfo.put("waybillNo", shopDeliveryInfo.getWaybillNo()); //如果已经签收直接查数据库 if (AppConstance.LOGISTICS_STATUS_OF_SIGN_FOR.equals(shopDeliveryInfo.getLogisticsStatus())) { List shopLogisticsInfos = shopLogisticsInfoDao.selectByDelieryId(shopDeliveryInfo.getId()); logisticsInfo.put("logisticsStatus", String.valueOf(AppConstance.LOGISTICS_STATUS_OF_SIGN_FOR)); result.setStatus(AjaxResult.STATUS_SUCCESS); result.setRows(shopLogisticsInfos); result.putInMap("logisticsInfo", logisticsInfo); return result; } Map param = new HashMap(); param.put("ShipperCode", shopDeliveryInfo.getLogisticsCompanyCode()); param.put("LogisticCode", shopDeliveryInfo.getWaybillNo()); Map paramsUrl = getparams(param); LogUtil.info("物流查询参数" + paramsUrl); String logisticsResult = HttpCurlUtil.sendPost(logisticsUrl, paramsUrl); LogUtil.info("查询物流返回信息" + logisticsResult); LogisticsInfoVo logisticsInfoVo = JSON.parseObject(logisticsResult, LogisticsInfoVo.class); if (logisticsInfoVo.isSuccess()) { ShopDeliveryInfo shopDelivery = new ShopDeliveryInfo(); shopDelivery.setLogisticsStatus(Integer.valueOf(logisticsInfoVo.getState())); shopDelivery.setId(shopDeliveryInfo.getId()); logisticsInfo.put("logisticsStatus", logisticsInfoVo.getState()); //更新物流状态 shopDeliveryInfoDao.updateByModel(shopDelivery); if (CollectionUtils.isNotEmpty(logisticsInfoVo.getTraces())) { List> mapTraces = logisticsInfoVo.getTraces(); for (int i = 0; i < mapTraces.size(); i++) { ShopLogisticsInfo shopLogisticsInfo = new ShopLogisticsInfo(); shopLogisticsInfo.setCreateBy(AppConstance.SYSTEM_USER); shopLogisticsInfo.setUpdateBy(AppConstance.SYSTEM_USER); shopLogisticsInfo.setLogisticsTime(mapTraces.get(i).get("AcceptTime")); String acceptStation = mapTraces.get(i).get("AcceptStation"); shopLogisticsInfo.setDescribe(acceptStation); shopLogisticsInfo.setDelieryId(shopDeliveryInfo.getId()); if (acceptStation.indexOf(AppConstance.KDN_IS_SIGN) != -1) { shopLogisticsInfo.setState(AppConstance.LOGISTICS_STATUS_OF_SIGN_FOR); } else if (acceptStation.indexOf(AppConstance.KDN_IS_RECEIVE) != -1) { shopLogisticsInfo.setState(AppConstance.LOGISTICS_STATUS_OF_RECEIVE); } else if (acceptStation.indexOf(AppConstance.KDN_IS_ON_WAY) != -1) { shopLogisticsInfo.setState(AppConstance.LOGISTICS_STATUS_OF_ON_WAY); } else if (acceptStation.indexOf(AppConstance.KDN_IS_MISTAKE) != -1) { shopLogisticsInfo.setState(AppConstance.LOGISTICS_STATUS_OF_MISTAKE); } else { shopLogisticsInfo.setState(AppConstance.LOGISTICS_STATUS_OF_NONE); } shopLogisticsList.add(shopLogisticsInfo); } shopLogisticsInfoDao.deleteByDelieryId(shopDeliveryInfo.getId()); if (CollectionUtils.isNotEmpty(shopLogisticsList)) { shopLogisticsInfoDao.batchInsert(shopLogisticsList); } } } List shopLogisticsInfos = shopLogisticsInfoDao.selectByDelieryId(shopDeliveryInfo.getId()); result.setStatus(AjaxResult.STATUS_SUCCESS); result.setRows(shopLogisticsInfos); result.putInMap("logisticsInfo", logisticsInfo); return result; } /** * 根据订单Id查询发货信息 * * @param orderId * @return */ @Override public ShopDeliveryInfo selectByorderId(Integer orderId) { ShopDeliveryInfo shopDeliveryInfo = shopDeliveryInfoDao.selectByOrderId(orderId); return shopDeliveryInfo; } /** * 根据订单Id查询最新一条物流信息 * * @param orderId * @return */ @Override public String selectDescribeByOrderId(Integer orderId) { String describe = shopDeliveryInfoDao.selectDescribeByOrderId(orderId); return describe; } /** * 组装请求参数 * * @param map * @return */ private Map getparams(Map map) { String params = JSON.toJSONString(map); Map paramsUrl = new HashMap(); try { paramsUrl.put("RequestData", URLEncoder.encode(params, "UTF-8")); String dataSign = encrypt(params, appKey, "UTF-8"); paramsUrl.put("DataSign", URLEncoder.encode(dataSign, "UTF-8")); } catch (Exception e) { throw new GlobleException(e.getMessage()); } paramsUrl.put("EBusinessID", eBusinessID); paramsUrl.put("RequestType", requestType); paramsUrl.put("DataType", AppConstance.DATATYPE); return paramsUrl; } /** * 电商Sign签名生成 * * @param content 内容 * @param keyValue Appkey * @param charset 编码方式 * @return DataSign签名 * @throws UnsupportedEncodingException ,Exception */ private String encrypt(String content, String keyValue, String charset) throws UnsupportedEncodingException, Exception { if (keyValue != null) { return base64(MD5(content + keyValue, charset), charset); } return base64(MD5(content, charset), charset); } /** * base64编码 * * @param str 内容 * @param charset 编码方式 * @throws UnsupportedEncodingException */ private String base64(String str, String charset) throws UnsupportedEncodingException { String encoded = base64Encode(str.getBytes(charset)); return encoded; } /** * MD5加密 * * @param str 内容 * @param charset 编码方式 * @throws Exception */ private String MD5(String str, String charset) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes(charset)); byte[] result = md.digest(); StringBuffer sb = new StringBuffer(32); for (int i = 0; i < result.length; i++) { int val = result[i] & 0xff; if (val <= 0xf) { sb.append("0"); } sb.append(Integer.toHexString(val)); } return sb.toString().toLowerCase(); } private static char[] base64EncodeChars = new char[]{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; private String base64Encode(byte[] data) { StringBuffer sb = new StringBuffer(); int len = data.length; int i = 0; int b1, b2, b3; while (i < len) { b1 = data[i++] & 0xff; if (i == len) { sb.append(base64EncodeChars[b1 >>> 2]); sb.append(base64EncodeChars[(b1 & 0x3) << 4]); sb.append("=="); break; } b2 = data[i++] & 0xff; if (i == len) { sb.append(base64EncodeChars[b1 >>> 2]); sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); sb.append(base64EncodeChars[(b2 & 0x0f) << 2]); sb.append("="); break; } b3 = data[i++] & 0xff; sb.append(base64EncodeChars[b1 >>> 2]); sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]); sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]); sb.append(base64EncodeChars[b3 & 0x3f]); } return sb.toString(); } }