mitao
2024-07-18 95639988ed630dae4b2461b5f6c3ccb6d9af8d86
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
package com.ruoyi.order.service.impl;
 
 
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.utils.SignUtils;
import com.ruoyi.order.mapper.LogisticsMapper;
import com.ruoyi.order.service.ILogisticsService;
import com.ruoyi.system.api.domain.Logistics;
import com.ruoyi.system.api.domain.dto.LogisticsDTO;
import com.ruoyi.system.api.domain.vo.Express100VO;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
/**
 * <p>
 * 物流公司 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-06-07
 */
@Service
public class LogisticsServiceImpl extends ServiceImpl<LogisticsMapper, Logistics> implements
        ILogisticsService {
 
    private String key="BltjQodT7186";
 
    private String kye1="GMtjjhxp9pdEpfz37M";
    private String customer="56DE8E9E3D58CE73C60755C8B7483043";
 
    private static final String AUTONUMBER_AUTO_URL = "http://www.kuaidi100.com/autonumber/auto?num=NUM&key=KEY";
 
    @Autowired
    private RestTemplate restTemplate;
 
 
    @Override
    public Express100VO getLogisticsList(LogisticsDTO logisticsDTO) {
        QueryTrackReq queryTrackReq = new QueryTrackReq();
        QueryTrackParam queryTrackParam = new QueryTrackParam();
        queryTrackParam.setCom(logisticsDTO.getCompany());
        queryTrackParam.setNum(logisticsDTO.getPostid());
        String param = new Gson().toJson(queryTrackParam);
 
        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(customer);
        queryTrackReq.setSign(SignUtils.querySign(param ,key,customer));
 
        IBaseClient baseClient = new QueryTrack();
        HttpResult execute = null;
        try {
            execute = baseClient.execute(queryTrackReq);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 对返回的数据进行反序列化处理
        Gson gson = new Gson();
        String responseBody = execute.getBody();
        Express100VO response = gson.fromJson(responseBody, Express100VO.class);
        LambdaQueryWrapper<Logistics> wrapper= Wrappers.lambdaQuery();
        wrapper.eq(Logistics::getLogisticsNum,logisticsDTO.getCompany());
        Logistics one = this.getOne(wrapper);
        if (Objects.nonNull(one)) {
            response.setLogisticsName(one.getLogisticsName());
        }
        return response;
    }
 
    @Override
    public Boolean isLogisticsOne(LogisticsDTO logisticsDTO) {
        Boolean b=false;
 
 
 
        Map<String,String> params=new HashMap();
        params.put("secret_key", "GMtjjhxp9pdEpfz37M");
        params.put("secret_code", "53514ac63f0447d188423221550b0c3e");
        params.put("secret_sign", "8265A3C04DDA73E5B899F7F750099BB0");
        params.put("num", logisticsDTO.getPostid());
        String string=post(params);
        if (string.equals("[]")){
            return b;
        }else{
                return b=true;
            }
 
    }
 
    public String post(Map<String,String> params){
        StringBuilder response=new StringBuilder("");
        BufferedReader reader = null;
        try {
            StringBuilder builder =new StringBuilder();
            for(Map.Entry param:params.entrySet()){
                if(builder.length()>0){
                    builder.append('&');
                }
                builder.append(URLEncoder.encode(param.getKey().toString(),"UTF-8"));
                builder.append('=');
                builder.append(URLEncoder.encode(String.valueOf(param.getValue()),"UTF-8"));
            }
            byte[]bytes=builder.toString().getBytes("UTF-8");
            URL url=new URL("http://cloud.kuaidi100.com/api");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("accept","*/*");
            conn.setRequestProperty("connection","Keep-Alive");
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length",String.valueOf(bytes.length));
            conn.setDoOutput(true);
            conn.getOutputStream().write(bytes);
            reader=new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line = "";
            while ((line =reader.readLine())!=null){
                response.append(line);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try{
                if (null!=reader){
                    reader.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        return response.toString();
    }
}