public String getOAuth2Url() {
    String base = wiyReportConfiguration.getBaseAuthUrl();
    String clientId = wiyReportConfiguration.getAppKey();
    String responseType = "code";
    String redirectUrl = wiyReportConfiguration.getCallbackUrl();

    return String.format(
        "%s?client_id=%s&response_type=%s&redirect_uri=%s&state=1212&view=web",
        base, clientId, responseType, redirectUrl);
  }
  public String getToken(String code, String state, boolean sandbox) {
    String url =
        sandbox
            ? wiyReportConfiguration.getSandboxTokenUrl()
            : wiyReportConfiguration.getProductionTokenUrl();
    Map<String, String> props = new HashMap<String, String>();
    props.put("grant_type", "authorization_code");
    props.put("code", code);
    props.put(
        "client_id",
        sandbox
            ? wiyReportConfiguration.getSandboxAppKey()
            : wiyReportConfiguration.getProductionAppKey());
    props.put(
        "client_secret",
        sandbox
            ? wiyReportConfiguration.getSandboxAppSecret()
            : wiyReportConfiguration.getProductionAppSecret());
    props.put(
        "redirect_uri",
        sandbox
            ? wiyReportConfiguration.getSandboxCallbackUrl()
            : wiyReportConfiguration.getProductionCallbackUrl());
    props.put("view", "web");
    props.put("state", state);
    String jsonResponse = null;
    try {
      jsonResponse = WebUtils.doPost(url, props, 30000, 30000);
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
    }

    return jsonResponse;
  }
  public String getRefreshTokenUrl(boolean sandbox, String refreshToken, String sessionKey) {
    String refreshUrl = null;
    try {
      Map<String, String> signParams = new TreeMap<String, String>();
      signParams.put("appkey", wiyReportConfiguration.getAppKey());
      signParams.put("refresh_token", refreshToken);
      signParams.put("sessionkey", sessionKey);

      StringBuilder paramsString = new StringBuilder();
      Set<Map.Entry<String, String>> paramsEntry = signParams.entrySet();
      for (Map.Entry paramEntry : paramsEntry) {
        paramsString.append(paramEntry.getKey()).append(paramEntry.getValue());
      }
      String sign =
          DigestUtils.md5Hex(
                  (paramsString.toString() + wiyReportConfiguration.getAppSecret())
                      .getBytes("utf-8"))
              .toUpperCase();
      String signEncoder = URLEncoder.encode(sign, "utf-8");
      String appkeyEncoder = URLEncoder.encode(wiyReportConfiguration.getAppKey(), "utf-8");
      String refreshTokenEncoder = URLEncoder.encode(refreshToken, "utf-8");
      String sessionKeyEncoder = URLEncoder.encode(sessionKey, "utf-8");
      refreshUrl =
          wiyReportConfiguration.getRefreshTokenUrl()
              + "appkey="
              + appkeyEncoder
              + "&refresh_token="
              + refreshTokenEncoder
              + "&sessionkey="
              + sessionKeyEncoder
              + "&sign="
              + signEncoder;
    } catch (UnsupportedEncodingException e) {
      logger.error(e.getMessage(), e);
    }

    return refreshUrl;
  }
 public String getTopAuthUrl() {
   String base = wiyReportConfiguration.getTopAuthUrl();
   String clientId = wiyReportConfiguration.getAppKey();
   return base + clientId;
 }
  public void syncTrades(Visitor visitor) {
    if (visitor == null) {
      return;
    }
    long pageSize = 50;
    String sessionKey = visitor.getSessionKey();
    if (StringUtils.isEmpty(sessionKey)) {
      return;
    }

    Date createdStart = getQueryStartDate(visitor);

    logger.debug(
        String.format(
            "About to synchronize trades for %s since %s",
            visitor.getVisitorNick(), createdStart.toString()));

    DefaultTaobaoClient client =
        new DefaultTaobaoClient(
            wiyReportConfiguration.getRestfulApi(),
            wiyReportConfiguration.getAppKey(),
            wiyReportConfiguration.getAppSecret(),
            Constants.FORMAT_JSON,
            30000,
            300000);

    TradesSoldGetRequest req = new TradesSoldGetRequest();
    req.setUseHasNext(true);
    req.setStartCreated(createdStart);
    req.setPageSize(pageSize);
    req.setFields(
        "seller_nick,buyer_nick,title,type,created,sid,tid,seller_rate,buyer_rate,status,payment,discount_fee,adjust_fee,post_fee,total_fee,pay_time,end_time,modified,consign_time,buyer_obtain_point_fee,point_fee,real_point_fee,received_payment,commission_fee,pic_path,num_iid,num_iid,num,price,cod_fee,cod_status,shipping_type,receiver_name,receiver_state,receiver_city,receiver_district,receiver_address,receiver_zip,receiver_mobile,receiver_phone,orders.title,orders.pic_path,orders.price,orders.num,orders.iid,orders.num_iid,orders.sku_id,orders.refund_status,orders.status,orders.oid,orders.total_fee,orders.payment,orders.discount_fee,orders.adjust_fee,orders.sku_properties_name,orders.item_meal_name,orders.buyer_rate,orders.seller_rate,orders.outer_iid,orders.outer_sku_id,orders.refund_id,orders.seller_type");
    try {
      long pageNumber = 1;
      while (true) {
        req.setPageNo(pageNumber);

        TradesSoldGetResponse response = client.execute(req, sessionKey);
        List<Trade> trades = response.getTrades();

        if (trades != null) {
          Hashtable<Long, TradeEntity> existed = getLongTradeEntityHashtable(trades);

          for (Trade trade : trades) {
            TradeEntity entity = EntityBuilder.buildTradeEntity(trade, visitor.getVisitorId());

            if (existed.containsKey(trade.getTid())) {
              TradeEntity existedTradeEntity = existed.get(trade.getTid());
              EntityBuilder.updateTradeEntity(existedTradeEntity, entity);
              entity = existedTradeEntity;
            }

            tradeEntityRepository.save(entity);
          }
        }

        if (response.getHasNext() == null || !response.getHasNext()) {
          break;
        }
        ++pageNumber;
      }
    } catch (ApiException e) {
      logger.error(e.getMessage(), e);
    } finally {
      // rebuild consumer entities
      daoService.buildConsumersBy(visitor.getVisitorId());
      // rebuild product purchase
      Date today = Calendar.getInstance().getTime();
      Date yesterday = DateTimeUtils.dateAdjust(today, Calendar.DAY_OF_YEAR, -1);
      daoService.buildProductPurchaseForSeller(visitor.getVisitorId(), yesterday, today);
      // rebuild produce entities
      daoService.buildProductEntitiesForSeller(visitor.getVisitorId(), yesterday, today);
      // build product purchase combo measurements
      daoService.buildProductPurchaseComboMeasurements();

      logger.debug(CommonService.class.getCanonicalName() + " finished.");
    }
  }
 public Date now() {
   Calendar c = Calendar.getInstance();
   c.setTimeZone(TimeZone.getTimeZone(wiyReportConfiguration.getTaobaoTimeZone()));
   return c.getTime();
 }