admin
2022-01-12 c9fdff7d45ae118eac10397d8f34661e9f2e968a
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
package com.ks.lijin.utils.taobao;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ks.lijin.pojo.DO.TaoBaoOrderRecord;
import net.sf.json.JSONObject;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.TimeUtil;
 
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
 
public class TaoBaoOrderUtil {
 
 
    public static void main(String[] args) throws Exception {
        Date startTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 2);
        Date endTime = new Date();
        List<TaoBaoOrderRecord> orderRecordList = getOrderByModifyTime("1733730164", startTime, endTime);
        System.out.println(orderRecordList);
    }
 
 
    private static Map<String, String> zheTaoKeSidMap = new HashMap<>();
 
    static {
        //渝小妹自营店
        zheTaoKeSidMap.put("472720008", "59407");
        //椰视科技
        zheTaoKeSidMap.put("124933865", "34097");
        //省钱礼金卡
        zheTaoKeSidMap.put("1733730164", "69041");
    }
 
 
    /**
     * 根据修改时间获取订单
     *
     * @param pubId
     * @param startModifyTime
     * @param endModifyTime   最多相差3小时
     * @return
     */
    public static List<TaoBaoOrderRecord> getOrderByModifyTime(String pubId, Date startModifyTime, Date endModifyTime) throws Exception {
        List<TaoBaoOrderRecord> orderRecordList = new ArrayList<>();
        String sid = zheTaoKeSidMap.get(pubId);
        String appKey = "fe65b15d74d84c75b99578543da75264";
        TaoBaoOrderFilter filter = new TaoBaoOrderFilter();
        filter.setStartTime(startModifyTime);
        filter.setEndTime(endModifyTime);
        filter.setQueryType(4);
        TaoBaoOrderResult result = getOrderListByZheTaoKe(appKey, sid, filter);
        orderRecordList.addAll(result.getOrderList());
        while (result.hasNext) {
            filter.setPositionIndex(result.positionIndex);
            result = getOrderListByZheTaoKe(appKey, sid, filter);
            orderRecordList.addAll(result.getOrderList());
        }
        return orderRecordList;
    }
 
 
    /**
     * 折淘客订单查询
     *
     * @param appKey
     * @param sid
     * @param filter
     * @return
     */
    private static TaoBaoOrderResult getOrderListByZheTaoKe(String appKey, String sid, TaoBaoOrderFilter filter) throws Exception {
        Map<String, String> params = new HashMap<>();
        params.put("appkey", appKey);
        params.put("sid", sid);
        if (filter.getStartTime() != null) {
            params.put("start_time", TimeUtil.getGernalTime(filter.getStartTime().getTime(), "yyyy-MM-dd HH:mm:ss"));
        }
 
        if (filter.getEndTime() != null) {
            params.put("end_time", TimeUtil.getGernalTime(filter.getEndTime().getTime(), "yyyy-MM-dd HH:mm:ss"));
        }
 
        if (filter.getQueryType() != null) {
            params.put("query_type", filter.getQueryType() + "");
        }
 
        if (filter.getTkStatus() != null) {
            params.put("tk_status", filter.getTkStatus() + "");
        }
 
        if (filter.getOrderScene() != null) {
            params.put("order_scene", filter.getOrderScene() + "");
        }
 
        if (filter.getPositionIndex() != null) {
            params.put("position_index", filter.getPositionIndex());
        }
        params.put("jump_type", 1 + "");
        params.put("page_size", 100 + "");
        String baseUrl = "https://api.zhetaoke.com:10001/api/open_dingdanchaxun2.ashx";
        List<String> paramsList = new ArrayList<>();
        for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
            String key = its.next();
            paramsList.add(key + "=" + URLEncoder.encode(params.get(key), "UTF-8"));
        }
        baseUrl += "?" + StringUtil.concat(paramsList, "&");
        String result = HttpUtil.get(baseUrl.replace("|", "%7C"));
        JSONObject data = JSONObject.fromObject(result);
        String url = data.optString("url");
        result = HttpUtil.get(url.trim().replace("|", "%7C"));
        return parseTaoBaoOrder(result);
    }
 
    private static TaoBaoOrderResult parseTaoBaoOrder(String result) throws Exception {
        JSONObject root = JSONObject.fromObject(result);
        if (root.optJSONObject("tbk_sc_order_details_get_response") == null) {
            throw new Exception("接口请求出错:" + result);
        }
        JSONObject data = root.optJSONObject("tbk_sc_order_details_get_response").optJSONObject("data");
        if (data == null) {
            throw new Exception("接口请求出错:" + result);
        }
        TaoBaoOrderResult taoBaoOrderResult = new TaoBaoOrderResult(new ArrayList<>(), data.optBoolean("has_pre"), data.optBoolean("has_next"), data.optString("position_index"));
        Type type = new TypeToken<List<TaoBaoOrderRecord>>() {
        }.getType();
 
        if (data.optJSONObject("results") != null && data.optJSONObject("results").optJSONArray("publisher_order_dto") != null) {
            List<TaoBaoOrderRecord> recordList = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(data.optJSONObject("results").optJSONArray("publisher_order_dto").toString(), type);
            taoBaoOrderResult.setOrderList(recordList);
        }
        return taoBaoOrderResult;
    }
 
 
    public static class TaoBaoOrderFilter {
        private Date startTime;
        private Date endTime;//订单查询结束时间,2019-04-25 15:18:22,目前最大可查3个小时内的数据
        private Integer queryType;//1:按照订单淘客创建时间查询,2:按照订单淘客付款时间查询,3:按照订单淘客结算时间查询,4:按照订单更新时间;
        private String positionIndex;//    位点,除第一页之外,都需要传递;前端原样返回。注意:从第二页开始,位点必须传递前一页返回的值,否则翻页无效。
        private Integer pageSize;//页大小,默认20,1~100
        private Integer memberType;//2:二方,3:三方,不传,表示所有角色
        private Integer tkStatus;//12-付款,13-关闭,14-确认收货(暂时无法结算佣金),3-结算成功;不传,表示所有状态
        private Integer orderScene;//1:常规订单,2:渠道订单,3:会员运营订单,默认为1
 
        public Date getStartTime() {
            return startTime;
        }
 
        public void setStartTime(Date startTime) {
            this.startTime = startTime;
        }
 
        public Date getEndTime() {
            return endTime;
        }
 
        public void setEndTime(Date endTime) {
            this.endTime = endTime;
        }
 
        public Integer getQueryType() {
            return queryType;
        }
 
        public void setQueryType(Integer queryType) {
            this.queryType = queryType;
        }
 
        public String getPositionIndex() {
            return positionIndex;
        }
 
        public void setPositionIndex(String positionIndex) {
            this.positionIndex = positionIndex;
        }
 
        public Integer getPageSize() {
            return pageSize;
        }
 
        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }
 
        public Integer getMemberType() {
            return memberType;
        }
 
        public void setMemberType(Integer memberType) {
            this.memberType = memberType;
        }
 
        public Integer getTkStatus() {
            return tkStatus;
        }
 
        public void setTkStatus(Integer tkStatus) {
            this.tkStatus = tkStatus;
        }
 
        public Integer getOrderScene() {
            return orderScene;
        }
 
        public void setOrderScene(Integer orderScene) {
            this.orderScene = orderScene;
        }
    }
 
    public static class TaoBaoOrderResult {
        private List<TaoBaoOrderRecord> orderList;
        boolean hasPre;
        boolean hasNext;
        String positionIndex;
 
        public TaoBaoOrderResult(List<TaoBaoOrderRecord> orderList, boolean hasPre, boolean hasNext, String positionIndex) {
            this.orderList = orderList;
            this.hasPre = hasPre;
            this.hasNext = hasNext;
            this.positionIndex = positionIndex;
        }
 
        public List<TaoBaoOrderRecord> getOrderList() {
            return orderList;
        }
 
        public void setOrderList(List<TaoBaoOrderRecord> orderList) {
            this.orderList = orderList;
        }
 
        public boolean isHasPre() {
            return hasPre;
        }
 
        public void setHasPre(boolean hasPre) {
            this.hasPre = hasPre;
        }
 
        public boolean isHasNext() {
            return hasNext;
        }
 
        public void setHasNext(boolean hasNext) {
            this.hasNext = hasNext;
        }
 
        public String isPositionIndex() {
            return positionIndex;
        }
 
        public void setPositionIndex(String positionIndex) {
            this.positionIndex = positionIndex;
        }
    }
 
 
    /**
     * 获取订单中的佣金
     *
     * @param order
     * @return
     */
    public static BigDecimal getCommission(TaoBaoOrderRecord order) {
        //获取预估佣金
        if (order.getTk_status() != 3 && order.getTk_status() != 12 && order.getTk_status() != 14) {
            return null;
        }
 
        //订单付款
        if (order.getTk_status() == 12) {
            return new BigDecimal(order.getPub_share_pre_fee()).add(new BigDecimal(order.getSubsidy_fee()));
        }
 
        //订单结算/订单成功
        return new BigDecimal(order.getPub_share_fee()).add(new BigDecimal(order.getSubsidy_fee()));
    }
 
 
 
}