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()));
|
}
|
|
|
|
}
|