admin
2020-11-28 dc5be7d38446f70e6ff86df311119c32b41fe7f8
fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoKeApiUtil.java
@@ -1,28 +1,12 @@
package com.yeshi.fanli.util.taobao;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.taobao.TbImgUtil;
import com.yeshi.fanli.dto.taobao.TaoBaoShopInfoDTO;
import com.yeshi.common.entity.PageEntity;
import com.yeshi.common.entity.taobao.TaoBaoShopInfo;
import com.yeshi.common.entity.taobao.TaoKeAppInfo;
import com.yeshi.fanli.dto.taobao.TaoLiJinDTO;
import com.yeshi.fanli.dto.taobao.api.TaoKeOfficialActivityConvertResultDTO;
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinReport;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.SearchShopFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoHead;
import com.yeshi.fanli.entity.taobao.TaoBaoProvince;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchNav;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.entity.taobao.TaoBaoShopInfo;
import com.yeshi.fanli.entity.taobao.TaoKeAppInfo;
import com.yeshi.fanli.entity.taobao.*;
import com.yeshi.fanli.exception.taobao.TaoBaoAuthException;
import com.yeshi.fanli.exception.taobao.TaoBaoTokenParseException;
import com.yeshi.fanli.exception.taobao.TaoKeApiException;
@@ -31,14 +15,20 @@
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.log.TLJLogHelper;
import com.yeshi.fanli.log.TaoKeLogHelper;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TaoBaoConstant;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.goods.facade.dto.taobao.TaoBaoShopInfoDTO;
import com.yeshi.goods.facade.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.goods.facade.entity.taobao.TaoBaoHead;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.MoneyBigDecimalUtil;
import org.yeshi.utils.TimeUtil;
import org.yeshi.utils.taobao.TbImgUtil;
import java.math.BigDecimal;
import java.util.*;
//淘宝客API接口
public class TaoKeApiUtil {
@@ -260,8 +250,9 @@
    /**
     * 获取商品详情,简版
     *
     * @param id
     * @param ids
     * @return
     * @throws TaobaoGoodsDownException
     */
    public static List<TaoBaoGoodsBrief> getBatchGoodsInfos(String ids) throws TaobaoGoodsDownException {
        List<TaoBaoGoodsBrief> goodsList = new ArrayList<>();
@@ -314,8 +305,10 @@
    /**
     * 获取商品详情,简版
     *
     * @param id
     * @param ids
     * @param ip
     * @return
     * @throws TaobaoGoodsDownException
     */
    public static List<TaoBaoGoodsBrief> getBatchGoodsInfos(String ids, String ip) throws TaobaoGoodsDownException {
        Map<String, String> map = new HashMap<>();
@@ -356,7 +349,7 @@
     * @return
     * @throws TaobaoGoodsDownException
     */
    public static TaoBaoGoodsBrief searchGoodsDetail(Long id) throws TaobaoGoodsDownException {
    public static TaoBaoGoodsBrief searchGoodsDetail(Long id, String specialId, String relationId) throws TaobaoGoodsDownException {
        TaoBaoGoodsBrief goods = getSimpleGoodsInfo(id);
        if (goods == null)
            return null;
@@ -365,7 +358,7 @@
        filter.setPage(1);
        filter.setPageSize(50);
        TaoBaoSearchResult result = searchWuLiaoForDetail(goods.getTitle(), goods.getZkPrice(), goods.getProvcity(),
                goods.getUserType());
                goods.getUserType(), specialId, relationId);
        if (result != null && result.getTaoBaoGoodsBriefs() != null)
            for (TaoBaoGoodsBrief g : result.getTaoBaoGoodsBriefs()) {
                System.out.println(goods.getAuctionId() + ":" + g.getAuctionId());
@@ -381,7 +374,7 @@
        // 从转链搜索
        TaoBaoGoodsBrief newGoods = specialConvertCoupon(goods.getAuctionId(),
                new TaoKeAppInfo(TaoBaoConstant.TAOBAO_AUTH_APPKEY, TaoBaoConstant.TAOBAO_AUTH_APPSECRET,
                        TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT));
                        TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT), specialId, relationId);
        if (newGoods != null) {
            goods.setTkRate(newGoods.getTkRate());
@@ -411,6 +404,10 @@
    }
    public static TaoBaoGoodsBrief searchGoodsDetail(Long id) throws TaobaoGoodsDownException {
        return searchGoodsDetail(id, null, null);
    }
    /**
     * 搜索商品详情-详细
     *
@@ -427,7 +424,7 @@
        filter.setPage(1);
        filter.setPageSize(50);
        TaoBaoSearchResult result = searchWuLiaoForDetail(goods.getTitle(), goods.getZkPrice(), goods.getProvcity(),
                goods.getUserType(), app);
                goods.getUserType(), app, null, null);
        if (result != null && result.getTaoBaoGoodsBriefs() != null)
            for (TaoBaoGoodsBrief g : result.getTaoBaoGoodsBriefs()) {
                if (goods.getAuctionId().longValue() == g.getAuctionId()) {
@@ -458,10 +455,12 @@
        // 从转链搜索
        TaoBaoGoodsBrief newGoods = specialConvertCoupon(goods.getAuctionId(),
                new TaoKeAppInfo(TaoBaoConstant.TAOBAO_AUTH_APPKEY, TaoBaoConstant.TAOBAO_AUTH_APPSECRET,
                        TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT));
                        TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT), null, null);
        if (newGoods != null) {
            goods.setTkRate(newGoods.getTkRate());
            goods.setAuctionUrl(newGoods.getAuctionUrl());
            goods.setCouponLink(newGoods.getCouponLink());
            if (!StringUtil.isNullOrEmpty(newGoods.getCouponInfo())) {
                List<BigDecimal> couponInfo = TaoBaoCouponUtil.getCouponInfo(newGoods.getCouponInfo());
                if (couponInfo != null && couponInfo.size() > 1) {
@@ -495,7 +494,7 @@
     * @return
     * @throws TaobaoGoodsDownException
     */
    public static TaoBaoGoodsBrief searchGoodsDetailForConvert(Long id, TaoKeAppInfo app)
    public static TaoBaoGoodsBrief searchGoodsDetailForConvert(Long id, TaoKeAppInfo app, String specialId, String relationId)
            throws TaobaoGoodsDownException {
        TaoBaoGoodsBrief goods = getSimpleGoodsInfo(id);
        if (goods == null)
@@ -505,7 +504,7 @@
        filter.setPage(1);
        filter.setPageSize(50);
        TaoBaoSearchResult result = searchWuLiaoForDetail(goods.getTitle(), goods.getZkPrice(), goods.getProvcity(),
                goods.getUserType(), app);
                goods.getUserType(), app, specialId, relationId);
        if (result != null && result.getTaoBaoGoodsBriefs() != null)
            for (TaoBaoGoodsBrief g : result.getTaoBaoGoodsBriefs()) {
                if (goods.getAuctionId().longValue() == g.getAuctionId()) {
@@ -546,7 +545,7 @@
            filter.setPage(1);
            filter.setPageSize(50);
            TaoBaoSearchResult result = searchWuLiaoForDetail(goods.getTitle(), goods.getZkPrice(), goods.getProvcity(),
                    goods.getUserType());
                    goods.getUserType(), null, null);
            if (result != null && result.getTaoBaoGoodsBriefs() != null)
                for (TaoBaoGoodsBrief g : result.getTaoBaoGoodsBriefs()) {
                    if (goods.getAuctionId().longValue() == g.getAuctionId()) {
@@ -622,9 +621,11 @@
     * 商品物料搜索
     *
     * @param filter
     * @param specialId
     * @param relationId
     * @return
     */
    public static TaoBaoSearchResult searchWuLiao(SearchFilter filter) {
    public static TaoBaoSearchResult searchWuLiao(SearchFilter filter, String specialId, String relationId) {
        if (filter.getKey() != null && filter.getKey().trim().equalsIgnoreCase(""))
            return null;
@@ -638,6 +639,14 @@
        map.put("method", "taobao.tbk.dg.material.optional");
        map.put("page_size", filter.getPageSize() == 0 ? "20" : filter.getPageSize() + "");
        map.put("page_no", (filter.getPage() <= 0 ? 1 : filter.getPage()) + "");
        if (!StringUtil.isNullOrEmpty(specialId) && !StringUtil.isNullOrEmpty(relationId)) {
            map.put("special_id", specialId);
        } else if (!StringUtil.isNullOrEmpty(specialId)) {
            map.put("special_id", specialId);
        } else if (!StringUtil.isNullOrEmpty(relationId)) {
            map.put("relation_id", relationId);
        }
        // map.put("material_id", "3756");
        pageEntity.setPageIndex(filter.getPage());
@@ -807,11 +816,16 @@
    /**
     * 商品物料搜索
     *
     * @param filter
     * @param title
     * @param zkPrice
     * @param provcity
     * @param userType
     * @param specialId
     * @param relationId
     * @return
     */
    public static TaoBaoSearchResult searchWuLiaoForDetail(String title, BigDecimal zkPrice, String provcity,
                                                           int userType) {
                                                           int userType, String specialId, String relationId) {
        if (provcity.trim().contains(" "))
            provcity = provcity.split(" ")[provcity.split(" ").length - 1];
        TaoBaoSearchResult taoBaoSearchResult = new TaoBaoSearchResult();
@@ -824,6 +838,14 @@
        map.put("is_tmall", (userType == 1) + "");
        map.put("q", title);
        map.put("itemloc", provcity);
        if (!StringUtil.isNullOrEmpty(specialId) && !StringUtil.isNullOrEmpty(relationId)) {
            map.put("special_id", specialId);
        } else if (!StringUtil.isNullOrEmpty(specialId)) {
            map.put("special_id", specialId);
        } else if (!StringUtil.isNullOrEmpty(relationId)) {
            map.put("relation_id", relationId);
        }
        String resultStr = TaoKeBaseUtil.baseRequestForThreeTimes(map, true);
        System.out.println(resultStr);
@@ -858,11 +880,17 @@
    /**
     * 商品物料搜索
     *
     * @param filter
     * @param title
     * @param zkPrice
     * @param provcity
     * @param userType
     * @param app
     * @param specialId
     * @param relationId
     * @return
     */
    public static TaoBaoSearchResult searchWuLiaoForDetail(String title, BigDecimal zkPrice, String provcity,
                                                           int userType, TaoKeAppInfo app) {
                                                           int userType, TaoKeAppInfo app, String specialId, String relationId) {
        if (provcity.trim().contains(" "))
            provcity = provcity.split(" ")[provcity.split(" ").length - 1];
        TaoBaoSearchResult taoBaoSearchResult = new TaoBaoSearchResult();
@@ -875,6 +903,13 @@
        map.put("is_tmall", (userType == 1) + "");
        map.put("q", title);
        map.put("itemloc", provcity);
        if (!StringUtil.isNullOrEmpty(specialId) && !StringUtil.isNullOrEmpty(relationId)) {
            map.put("special_id", specialId);
        } else if (!StringUtil.isNullOrEmpty(specialId)) {
            map.put("special_id", specialId);
        } else if (!StringUtil.isNullOrEmpty(relationId)) {
            map.put("relation_id", relationId);
        }
        String resultStr = TaoKeBaseUtil.baseRequestForThreeTimes(map, app);
        JSONObject data = JSONObject.fromObject(resultStr);
@@ -1027,6 +1062,34 @@
        return null;
    }
    public static String getTKToken(String logo, String text, String url, boolean simpleToken) {
        if (simpleToken) {
            return getSimpleTKToken(logo, text, url);
        } else {
            return getTKToken(logo, text, url);
        }
    }
    public static String getSimpleTKToken(String logo, String text, String url) {
        if (text == null)
            return null;
        if (text.length() < 5)
            text = "好货:" + text;
        Map<String, String> map = new HashMap<>();
        map.put("method", "taobao.tbk.tpwd.create");
        map.put("url", url);
        map.put("text", text);
        map.put("logo", logo);
        String resultStr = TaoKeBaseUtil.baseRequestForThreeTimes(map, true);
        JSONObject data = JSONObject.fromObject(resultStr);
        if (data.optJSONObject("tbk_tpwd_create_response").optJSONObject("data") != null)
            return data.optJSONObject("tbk_tpwd_create_response").optJSONObject("data").optString("password_simple");
        return null;
    }
    /**
     * 获取券详细信息
     *
@@ -1035,7 +1098,7 @@
     * @return
     */
    public static QuanInfo getQuanInfo(Long auctionId, String activityId) {
        QuanInfo info = new QuanInfo();
        QuanInfo info = new TaoKeApiUtil.QuanInfo();
        Map<String, String> map = new HashMap<>();
        map.put("method", "taobao.tbk.coupon.get");
        map.put("item_id", auctionId + "");
@@ -1234,10 +1297,7 @@
    }
    /**
     * 店铺搜索
     *
     * @param key  -店铺名称
     * @param page -页码
     * @param filter
     * @return
     */
    public static List<TaoBaoShopInfo> searchShop(SearchShopFilter filter) {
@@ -1678,23 +1738,26 @@
        }
    }
    public static TaoBaoGoodsBrief specialConvertCoupon(Long auctionId, String specialId,String relationId, TaoKeAppInfo app) {
    public static TaoBaoGoodsBrief specialConvertCoupon(Long auctionId, TaoKeAppInfo app, String specialId, String relationId) {
        String pid = app.getPid();
        String[] sts = pid.split("_");
        Map<String, String> map = new HashMap<>();
        map.put("method", "taobao.tbk.coupon.convert");
        map.put("item_id", auctionId + "");
        map.put("adzone_id", sts[3]);
        if (!StringUtil.isNullOrEmpty(specialId)) {
            map.put("special_id", specialId);
        }
        if (!StringUtil.isNullOrEmpty(relationId)) {
            map.put("relation_id", specialId);
        if (System.currentTimeMillis() > TimeUtil.convertToTimeTemp("2020-07-22", "yyyy-MM-dd")) {
            if (!StringUtil.isNullOrEmpty(specialId) && !StringUtil.isNullOrEmpty(relationId)) {
                map.put("special_id", specialId);
            } else if (!StringUtil.isNullOrEmpty(specialId)) {
                map.put("special_id", specialId);
            } else if (!StringUtil.isNullOrEmpty(relationId)) {
                map.put("relation_id", relationId);
            }
        }
        try {
            String result = TaoKeBaseUtil.baseRequestForThreeTimes(map, app);
            System.out.println(result);
            JSONObject json = JSONObject.fromObject(result);
            JSONObject resultJSON = json.optJSONObject("tbk_coupon_convert_response").optJSONObject("result")
                    .optJSONObject("results");
@@ -1718,6 +1781,10 @@
            }
            String tkRate = resultJSON.optString("max_commission_rate");
            if (StringUtil.isNullOrEmpty(tkRate)) {
                tkRate = resultJSON.optString("min_commission_rate");
            }
            if (!StringUtil.isNullOrEmpty(tkRate)) {
                goods.setTkRate(new BigDecimal(tkRate));
            }
@@ -1726,13 +1793,7 @@
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static TaoBaoGoodsBrief specialConvertCoupon(Long auctionId, TaoKeAppInfo app) {
        return specialConvertCoupon(auctionId, null,null, app);
    }
    public static void juHuaSuan() {
@@ -2259,13 +2320,15 @@
        return null;
    }
    public static class QuanInfo {
        public String coupon_start_time;// 开始时间
        public String coupon_end_time; // 券结束时间
        public BigDecimal coupon_amount;// 券金额
        public int coupon_total_count;// 券总数量
        public int coupon_remain_count;// 券剩余数量
        public BigDecimal coupon_start_fee;// 券起始金额
    }
}
class QuanInfo {
    public String coupon_start_time;// 开始时间
    public String coupon_end_time; // 券结束时间
    public BigDecimal coupon_amount;// 券金额
    public int coupon_total_count;// 券总数量
    public int coupon_remain_count;// 券剩余数量
    public BigDecimal coupon_start_fee;// 券起始金额
}