yujian
2019-08-27 d8359ddb48dab5cc797a9d552e11fde571f4920c
fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoKeApiUtil.java
@@ -11,6 +11,8 @@
import com.taobao.api.ApiException;
import com.yeshi.fanli.dto.taobao.TaoBaoShopInfoDTO;
import com.yeshi.fanli.dto.taobao.TaoLiJinDTO;
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinReport;
import com.yeshi.fanli.entity.taobao.RelateGoods;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
@@ -20,9 +22,12 @@
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.exception.taobao.TaoBaoAuthException;
import com.yeshi.fanli.exception.taobao.TaoKeApiException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.exception.tlj.TaoLiJinCreateException;
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;
@@ -35,46 +40,6 @@
//淘宝客API接口
public class TaoKeApiUtil {
   /**
    * 按关键字和分类搜索券
    *
    * @param key
    * @param catList
    * @return
    */
   public static TaoBaoSearchResult searchCouple(String key, List<Long> catList, int page, int pageSize) {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.dg.item.coupon.get");
      map.put("page_size", pageSize + "");
      map.put("page_no", page + "");
      String cate = "";
      if (catList != null && catList.size() > 10)
         catList = catList.subList(0, 10);
      if (catList != null && catList.size() > 0) {
         for (Long c : catList)
            cate += c + ",";
         if (cate.endsWith(","))
            cate = cate.substring(0, cate.length() - 1);
         map.put("cat", cate);
      }
      if (!StringUtil.isNullOrEmpty(key)) {
         map.put("q", key);
      }
      String result = TaoKeBaseUtil.baseRequestForThreeTimes(map, true);
      System.out.println(result);
      TaoBaoSearchResult finalResult = parseCoupleContent(result);
      if (finalResult == null)
         return null;
      PageEntity pageEntity = finalResult.getPageEntity();
      pageEntity.setPageIndex(page);
      pageEntity.setPageSize(pageSize);
      pageEntity.setTotalPage(pageEntity.getTotalCount() % pageSize == 0
            ? ((int) (pageEntity.getTotalCount() / pageSize)) : (int) (pageEntity.getTotalCount() / pageSize + 1));
      finalResult.setPageEntity(pageEntity);
      return finalResult;
   }
   // 解析券的内容
   private static TaoBaoSearchResult parseCoupleContent(String content) {
@@ -374,22 +339,6 @@
            System.out.println(goods.getAuctionId() + ":" + g.getAuctionId());
            if (goods.getAuctionId().longValue() == g.getAuctionId()) {
               g.setId(goods.getAuctionId());
               // 判断是否有优惠券
               if (!StringUtil.isNullOrEmpty(g.getCouponActivityId())) {
                  // 获取优惠券详情
                  QuanInfo quanInfo = getQuanInfo(g.getAuctionId(), g.getCouponActivityId());
                  if (quanInfo != null) {
                     g.setCouponAmount(quanInfo.coupon_amount);
                     g.setCouponEffectiveEndTime(quanInfo.coupon_end_time);
                     g.setCouponEffectiveStartTime(quanInfo.coupon_start_time);
                     g.setCouponLeftCount(quanInfo.coupon_remain_count);
                     g.setCouponStartFee(quanInfo.coupon_start_fee);
                     g.setCouponTotalCount(quanInfo.coupon_total_count);
                  }
               } else {
                  g.setCouponAmount(new BigDecimal(0));
                  g.setCouponStartFee(new BigDecimal(0));
               }
               g.setCreatetime(new Date());
               g.setMaterialLibType(goods.getMaterialLibType());
               return g;
@@ -927,10 +876,9 @@
      if (!StringUtil.isNullOrEmpty(goods.getCouponInfo())) {
         List<BigDecimal> quanInfo = TaoBaoCouponUtil.getCouponInfo(goods.getCouponInfo());
         goods.setCouponAmount(quanInfo.get(1));
         goods.setCouponEffectiveEndTime(
               TimeUtil.getGernalTime(System.currentTimeMillis() + 1000 * 60 * 60 * 24, "yyyy-MM-dd"));
         goods.setCouponEffectiveStartTime(TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd"));
         goods.setCouponStartFee(quanInfo.get(0));
         goods.setCouponEffectiveEndTime(item.optString("coupon_end_time"));
         goods.setCouponEffectiveStartTime(item.optString("coupon_start_time"));
         goods.setCouponStartFee(new BigDecimal(item.optString("coupon_start_fee")));
         goods.setCouponLeftCount(item.optInt("coupon_remain_count"));
         goods.setCouponLink("https:" + item.optString("coupon_share_url"));
         goods.setCouponTotalCount(item.optInt("coupon_total_count"));
@@ -1238,7 +1186,6 @@
      return list;
   }
   /**
    * 店铺搜索
    * 
@@ -1252,7 +1199,7 @@
      TaoBaoShopInfoDTO dto = new TaoBaoShopInfoDTO();
      if (StringUtil.isNullOrEmpty(key))
         return dto;
      PageEntity pageEntity = new PageEntity();
      List<TaoBaoShopInfo> list = new ArrayList<>();
      Map<String, String> map = new HashMap<>();
@@ -1280,7 +1227,7 @@
               list.add(info);
            }
         }
         JSONObject optJSONObject = resultDate.optJSONObject("tbk_shop_get_response");
         int totalResults = optJSONObject.getInt("total_results");
         int totalPage = totalResults % pageSize == 0 ? totalResults / pageSize : totalResults / pageSize + 1;
@@ -1289,45 +1236,10 @@
      }
      pageEntity.setPageIndex(page);
      pageEntity.setPageSize(pageSize);
      dto.setListInfo(list);
      dto.setPage(pageEntity);
      return dto;
   }
   /**
    * TODO 按设备猜你喜欢
    *
    * @param userNickName
    * @param os
    * @param imei
    * @param idfa
    * @param ip
    * @param ua
    * @param net
    * @param pageNo
    * @param pageSize
    */
   public static void guessLikeByDevice(String userNickName, String os, String imei, String idfa, String ip, String ua,
         String net, int pageNo, int pageSize) {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.item.guess.like");
      if (!StringUtil.isNullOrEmpty(userNickName))
         map.put("user_nick", userNickName);
      map.put("os", os + "");
      if (!StringUtil.isNullOrEmpty(idfa))
         map.put("idfa", idfa);
      if (!StringUtil.isNullOrEmpty(imei)) {
         map.put("imei", imei + "");
         map.put("imei_md5", StringUtil.Md5(imei));
      }
      map.put("ip", ip + "");
      map.put("ua", ua + "");
      map.put("net", net + "");
      map.put("page_no", pageNo + "");
      map.put("page_size", pageSize + "");
      String resultStr = TaoKeBaseUtil.baseRequestForThreeTimes(map, true);
      System.out.println(resultStr);
   }
   /**
@@ -1391,7 +1303,7 @@
      map.put("page_no", page + "");
      map.put("page_size", pageSize + "");
      map.put("material_id", materialId + "");
      map.put("content_id", "561388751621");
      // map.put("content_id", "561388751621");
      String resultStr = TaoKeBaseUtil.baseRequestForThreeTimes(map, true);
      System.out.println(resultStr);
@@ -1645,7 +1557,7 @@
      return null;
   }
   public static String getRelationId(String accessToken, String appKey, String appSecret) {
   public static String getRelationId(String accessToken, String appKey, String appSecret) throws TaoBaoAuthException {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.sc.publisher.info.save");
      map.put("session", accessToken);
@@ -1661,14 +1573,22 @@
         return resultJSON.optJSONObject("tbk_sc_publisher_info_save_response").optJSONObject("data")
               .optString("relation_id");
      } catch (TaoKeApiException e) {
         e.printStackTrace();
         LogHelper.error("渠道ID申请出错:" + resultJSON);
         JSONObject errorJSON = JSONObject.fromObject(e.getMsg()).optJSONObject("error_response");
         if (errorJSON.optInt("code") == 15 && errorJSON.optInt("sub_code") == 1)// 未实名
         {
            throw new TaoBaoAuthException(TaoBaoAuthException.CODE_NOT_REAL_NAME, errorJSON.optString("sub_msg"));
         } else {
            e.printStackTrace();
            LogHelper.error("渠道ID申请出错:" + e.getMsg());
            throw new TaoBaoAuthException(TaoBaoAuthException.CODE_OTHER, errorJSON.optString("sub_msg"));
         }
      } catch (Exception e) {
         LogHelper.error("渠道ID申请出错:" + (resultJSON != null ? resultJSON.toString() : "未知错误"));
      }
      return null;
   }
   public static String getSpecialId(String accessToken, String appKey, String appSecret) {
   public static String getSpecialId(String accessToken, String appKey, String appSecret) throws TaoBaoAuthException {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.sc.publisher.info.save");
      map.put("session", accessToken);
@@ -1684,8 +1604,17 @@
         return resultJSON.optJSONObject("tbk_sc_publisher_info_save_response").optJSONObject("data")
               .optString("special_id");
      } catch (TaoKeApiException e) {
         e.printStackTrace();
         LogHelper.error("会员运营ID申请出错:" + resultJSON);
         JSONObject errorJSON = JSONObject.fromObject(e.getMsg()).optJSONObject("error_response");
         if (errorJSON.optInt("code") == 15 && errorJSON.optInt("sub_code") == 1)// 未实名
         {
            throw new TaoBaoAuthException(TaoBaoAuthException.CODE_NOT_REAL_NAME, errorJSON.optString("sub_msg"));
         } else {
            e.printStackTrace();
            LogHelper.error("会员运营ID申请出错:" + e.getMsg());
            throw new TaoBaoAuthException(TaoBaoAuthException.CODE_OTHER, errorJSON.optString("sub_msg"));
         }
      } catch (Exception e) {
         LogHelper.error("会员运营ID申请出错:" + (resultJSON != null ? resultJSON.toString() : "未知错误"));
      }
      return null;
@@ -1758,50 +1687,45 @@
      }
   }
   public static void taoKeContent() {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.content.get");
      map.put("adzone_id",TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT.split("_")[3]);
      map.put("adzone_id", TaoBaoConstant.TAOBAO_RELATION_PID_DEFAULT.split("_")[3]);
      map.put("type", "1");
      map.put("count", "100");
      TaoKeAppInfo app = new TaoKeAppInfo();
      app.setAppKey(TaoBaoConstant.TAOBAO_AUTH_APPKEY);
      app.setAppSecret(TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
      try {
         JSONObject json = TaoKeBaseUtil.baseRequest(map,app);
         JSONObject json = TaoKeBaseUtil.baseRequest(map, app);
         System.out.println(json);
      } catch (TaoKeApiException e) {
         e.printStackTrace();
      }
   }
   public static void taoKeContentSource(String contentId,String source) {
   public static void taoKeContentSource(String contentId, String source) {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.dg.optimus.material");
      map.put("adzone_id",TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT.split("_")[3]);
      map.put("adzone_id", TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT.split("_")[3]);
      map.put("page_size", "10");
      map.put("page_no", "1");
      map.put("content_id",contentId);
      map.put("content_id", contentId);
      map.put("content_source", source);
      map.put("material_id", "1");
      TaoKeAppInfo app = new TaoKeAppInfo();
      app.setAppKey(TaoBaoConstant.TAOBAO_AUTH_APPKEY);
      app.setAppSecret(TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
      try {
         JSONObject json = TaoKeBaseUtil.baseRequest(map,app);
         JSONObject json = TaoKeBaseUtil.baseRequest(map, app);
         System.out.println(json);
      } catch (TaoKeApiException e) {
         e.printStackTrace();
      }
   }
   // AA5ISJ
@@ -1896,23 +1820,214 @@
      goods.setPictUrlWhite(item.optString("white_image"));
      return goods;
   }
   public static void getShopGoodsList(Long shopId)
   {
   public static void getShopGoodsList(Long shopId) {
      Map<String, String> map = new HashMap<>();
      map.put("method", "tmall.omni.pc.iteminfo.getbyshopid");
      map.put("shop_id",shopId+"");
      map.put("shop_id", shopId + "");
      TaoKeAppInfo app = new TaoKeAppInfo();
      app.setAppKey("24838852");
      app.setAppSecret("bc8265e2bf8d8115329d652f9d3d4cd8");
      try {
         JSONObject json = TaoKeBaseUtil.baseRequest(map,app);
         JSONObject json = TaoKeBaseUtil.baseRequest(map, app);
         System.out.println(json);
      } catch (TaoKeApiException e) {
         e.printStackTrace();
      }
   }
   public static void getTaoKeContentEffective() {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.dg.optimus.material");
      map.put("adzone_id", TaoBaoConstant.TAOBAO_RELATION_AS_SPECIAL_PID.split("_")[3]);
      map.put("page_no", "1");
      map.put("page_size", "20");
      map.put("content_id", "8872035");
      TaoKeAppInfo app = new TaoKeAppInfo();
      app.setAppKey(TaoBaoConstant.TAOBAO_AUTH_APPKEY);
      app.setAppSecret(TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
      try {
         JSONObject json = TaoKeBaseUtil.baseRequest(map, app);
         System.out.println(json);
      } catch (TaoKeApiException e) {
         e.printStackTrace();
      }
   }
   public static void getTaoKeContent() {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.content.get");
      map.put("adzone_id", TaoBaoConstant.TAOBAO_RELATION_AS_SPECIAL_PID.split("_")[3]);
      map.put("type", "1");
      TaoKeAppInfo app = new TaoKeAppInfo();
      app.setAppKey(TaoBaoConstant.TAOBAO_AUTH_APPKEY);
      app.setAppSecret(TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
      try {
         JSONObject json = TaoKeBaseUtil.baseRequest(map, app);
         System.out.println(json);
      } catch (TaoKeApiException e) {
         e.printStackTrace();
      }
   }
   /**
    * 淘口令转商品ID
    *
    * @param token
    * @return
    */
   public static Long tokenConvertAuctionId(String token) {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.tpwd.convert");
      map.put("password_content", token);
      map.put("adzone_id", TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT.split("_")[3]);
      TaoKeAppInfo app = new TaoKeAppInfo();
      app.setAppKey(TaoBaoConstant.TAOBAO_AUTH_APPKEY);
      app.setAppSecret(TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
      try {
         JSONObject json = TaoKeBaseUtil.baseRequest(map, app);
         try {
            return json.optJSONObject("tbk_tpwd_convert_response").optJSONObject("data").optLong("num_iid");
         } catch (Exception e) {
            return null;
         }
      } catch (TaoKeApiException e) {
         e.printStackTrace();
      }
      return null;
   }
   // 淘礼金创建
   public static TaoLiJinDTO createTaoLiJin(Long auctionId, String name, BigDecimal perface, int totalNum,
         Date sendStartTime, Date sendEndTime, Date useStartTime, Date useEndTime, TaoKeAppInfo app)
         throws TaoKeApiException, TaoLiJinCreateException {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.dg.vegas.tlj.create");
      map.put("adzone_id", app.getPid().split("_")[3]);
      map.put("item_id", auctionId + "");
      map.put("total_num", totalNum + "");
      map.put("name", name);
      map.put("user_total_win_num_limit", "1");
      map.put("security_switch", "false");
      map.put("per_face", perface.toString());
      map.put("send_start_time", TimeUtil.getGernalTime(sendStartTime.getTime(), "yyyy-MM-dd HH:mm:ss"));
      if (sendEndTime != null)
         map.put("send_end_time", TimeUtil.getGernalTime(sendEndTime.getTime(), "yyyy-MM-dd HH:mm:ss"));
      if (useEndTime != null) {
         map.put("use_end_time", TimeUtil.getGernalTime(useEndTime.getTime(), "yyyy-MM-dd"));
         map.put("use_end_time_mode", "2");
      }
      if (useStartTime != null)
         map.put("use_start_time", TimeUtil.getGernalTime(useStartTime.getTime(), "yyyy-MM-dd"));
      try {
         String result = TaoKeBaseUtil.baseRequestForThreeTimes(map, app);
         JSONObject json = JSONObject.fromObject(result);
         System.out.println(json);
         JSONObject root = json.optJSONObject("tbk_dg_vegas_tlj_create_response");
         if (root != null && root.optJSONObject("result") != null) {
            if (root.optJSONObject("result").optBoolean("success")) {
               JSONObject modelJson = root.optJSONObject("result").optJSONObject("model");
               TaoLiJinDTO dto = new TaoLiJinDTO();
               dto.setRightsId(modelJson.optString("rights_id"));
               dto.setSendUrl(modelJson.optString("send_url"));
               return dto;
            } else {
               LogHelper.error(json);
            }
            // 接口返回异常
            String msgCode = root.optJSONObject("result").optString("msg_code");
            if (!StringUtil.isNullOrEmpty(msgCode)) {
               TLJLogHelper.info(auctionId, root.toString());// 淘礼金异常信息记录
               switch (msgCode) {
               case "FAIL_BIZ_ITEM_FORBIDDEN":
                  throw new TaoLiJinCreateException(TaoLiJinCreateException.CODE_TLJ_FORBIDDEN, "该商品不支持创建淘礼金红包");
               case "FAIL_BIZ_ACCOUNT_UN_PAID":
               case "PRE_FREEZE_ASSET_ACCOUNT_ERROR":
                  throw new TaoLiJinCreateException(TaoLiJinCreateException.CODE_TLJ_NO_MONEY, "官方玩法钱包余额不足");
               default:
                  throw new TaoKeApiException(Integer.parseInt(msgCode), root.toString());
               }
            }
         }
      } catch (TaoKeApiException e) {
         throw e;
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
      return null;
   }
   // 淘礼金报告
   public static UserTaoLiJinReport getTaoLiJinEffective(String rightsId) {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.dg.vegas.tlj.instance.report");
      map.put("rights_id", rightsId);
      TaoKeAppInfo app = new TaoKeAppInfo();
      app.setAppKey(TaoBaoConstant.TAOBAO_AUTH_APPKEY);
      app.setAppSecret(TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
      try {
         String result = TaoKeBaseUtil.baseRequestForThreeTimes(map, app);
         JSONObject json = JSONObject.fromObject(result);
         System.out.println(json);
         JSONObject root = json.optJSONObject("tbk_dg_vegas_tlj_instance_report_response");
         if (root != null && root.optJSONObject("result") != null) {
            if (root.optJSONObject("result").optBoolean("success")) {
               JSONObject modelJson = root.optJSONObject("result").optJSONObject("model");
               if (modelJson != null && modelJson.size() > 0) {
                  UserTaoLiJinReport report = new UserTaoLiJinReport();
                  report.setId(rightsId);
                  report.setUnfreezeAmount(new BigDecimal(modelJson.optString("unfreeze_amount")));
                  report.setUnfreezeNum(modelJson.optInt("unfreeze_num"));
                  report.setRefundAmount(new BigDecimal(modelJson.optString("refund_amount")));
                  report.setRefundNum(modelJson.optInt("refund_num"));
                  report.setAlipayAmount(new BigDecimal(modelJson.optString("alipay_amount")));
                  report.setUseAmount(new BigDecimal(modelJson.optString("use_amount")));
                  report.setUseNum(modelJson.optInt("use_num"));
                  report.setWinAmount(new BigDecimal(modelJson.optString("win_amount")));
                  report.setWinNum(modelJson.optInt("win_num"));
                  report.setPreCommissionAmount(new BigDecimal(modelJson.optString("pre_commission_amount")));
                  return report;
               }
            }
         }
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
      return null;
   }
   public static void getCouponByAuctionId(Long auctionId) {
      Map<String, String> map = new HashMap<>();
      map.put("method", "taobao.tbk.itemid.coupon.get");
      map.put("platform", 2 + "");
      map.put("pid", TaoBaoConstant.TAOBAO_RELATION_AS_SPECIAL_PID);
      map.put("num_iids", auctionId + "");
      TaoKeAppInfo app = new TaoKeAppInfo();
      app.setAppKey(TaoBaoConstant.TAOBAO_AUTH_APPKEY);
      app.setAppSecret(TaoBaoConstant.TAOBAO_AUTH_APPSECRET);
      try {
         JSONObject json = TaoKeBaseUtil.baseRequest(map, app);
         System.out.print(json);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
class QuanInfo {