jyy
2021-04-09 1443f8eb9f3b001e84f101852aa5f6ec7f49f4c9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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<ShopLogisticsInfo> shopLogisticsList = new ArrayList<ShopLogisticsInfo>();
        AjaxResult result = new AjaxResult();
        Map<String, String> logisticsInfo = new HashMap<String, String>();
        try {
            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<ShopLogisticsInfo> 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<String, String> param = new HashMap<String, String>();
            param.put("ShipperCode", shopDeliveryInfo.getLogisticsCompanyCode());
            param.put("LogisticCode", shopDeliveryInfo.getWaybillNo());
            Map<String, String> 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<Map<String, String>> 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);
                    }
                }
            } else {
                throw new GlobleException(logisticsInfoVo.getReason());
            }
 
        } catch (Exception e) {
            throw new GlobleException("物流信息查询失败");
        }
        List<ShopLogisticsInfo> 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<String, String> getparams(Map<String, String> map) throws Exception {
        String params = JSON.toJSONString(map);
        Map<String, String> paramsUrl = new HashMap<String, String>();
        paramsUrl.put("RequestData", URLEncoder.encode(params, "UTF-8"));
        paramsUrl.put("EBusinessID", eBusinessID);
        paramsUrl.put("RequestType", requestType);
        String dataSign = encrypt(params, appKey, "UTF-8");
        paramsUrl.put("DataSign", URLEncoder.encode(dataSign, "UTF-8"));
        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();
    }
}