@Transactional
  public void publish2Item(Long userId, Long activityId, Long itemNumIid, String publishHtml)
      throws TaobaoAccessControlException, TaobaoEnhancedApiException,
          TaobaoSessionExpiredException {
    PosterRecommendPublishItem publishItem =
        posterRecommendPublishItemDAO.selectByUserIdAndActivityIdAndItemNumIidAndStatus(
            userId,
            activityId,
            itemNumIid,
            ImmutableList.of(
                PUBLISH_ITEM_STATUS_WAITING_PUBLISH,
                PUBLISH_ITEM_STATUS_PUBLISHED_FAILED,
                PUBLISH_ITEM_STATUS_UNPUBLISHED_SUCCESSFULLY,
                PUBLISH_ITEM_STATUS_UNPUBLISHED_FAILED));
    if (null == publishItem) {
      return;
    }

    // 更新详情页
    Item item = taobaoApiCache.getItem(userId, itemNumIid);
    // 1 备份
    String backupDesc = item.getDesc();
    // important
    String updatedDesc =
        PublishUtils.addHtml2DetailPage(
            publishItem.getDetailPagePosition(),
            HTML_TAG_RECOMMEND,
            activityId,
            publishHtml,
            backupDesc);
    // 插入详情页备份
    PosterRecommendPublishItemDetailPage posterRecommendPublishItemDetailPage =
        new PosterRecommendPublishItemDetailPage();
    posterRecommendPublishItemDetailPage.setUserId(userId);
    posterRecommendPublishItemDetailPage.setActivityId(activityId);
    posterRecommendPublishItemDetailPage.setItemNumIid(item.getNumIid());
    posterRecommendPublishItemDetailPage.setItemTitle(item.getTitle());
    posterRecommendPublishItemDetailPage.setItemPrice(new BigDecimal(item.getPrice()));
    posterRecommendPublishItemDetailPage.setItemPicUrl(item.getPicUrl());
    posterRecommendPublishItemDetailPage.setBackupDesc(backupDesc);
    posterRecommendPublishItemDetailPage.setDesc(updatedDesc);
    posterRecommendPublishItemDetailPage.setAddTime(appService.getLocalSystemTime().toDate());
    try {
      if (updatedDesc.equals(backupDesc)) {
        ItemUpdateRequest request = new ItemUpdateRequest();
        request.setNumIid(itemNumIid);
        request.setDesc(updatedDesc);
        taobaoApiItemService.updateItem(userId, request);
        taobaoApiCache.invalidItem(itemNumIid);
      }
      posterRecommendPublishItemDetailPage.setStatus(DETAIL_PAGE_STATUS_NORMAL);
      publishSuccessful(userId, activityId, itemNumIid);
    } catch (BaseTaobaoApiException e) {
      publishFailed(userId, activityId, itemNumIid);
      posterRecommendPublishItemDetailPage.setStatus(DETAIL_PAGE_STATUS_PUBLISH_FAILED);
      throw e;
    } finally {
      posterRecommendPublishItemDetailPageDAO.insertSelective(posterRecommendPublishItemDetailPage);
    }
  }
예제 #2
0
 public List<Item> fillItemSkus(List<Item> items) throws ApiException {
   for (Item item : items) {
     item.setSkus(getItemSkus("" + item.getNumIid()));
   }
   return items;
 }
  /**
   * 查询可供选择加入投放的宝贝
   *
   * @param userId
   * @param activityId
   * @param onSale
   * @param query
   * @param sellerCids
   * @param pageNum
   * @param pageSize
   * @return
   * @throws TaobaoAccessControlException
   * @throws TaobaoEnhancedApiException
   * @throws TaobaoSessionExpiredException
   */
  @NotNull
  public Page<PublishItem> paginateItems(
      Long userId,
      Long activityId,
      boolean onSale,
      String query,
      List<Long> sellerCids,
      int pageNum,
      int pageSize)
      throws TaobaoAccessControlException, TaobaoEnhancedApiException,
          TaobaoSessionExpiredException {

    Page<Item> itemPage;
    List<PublishItem> returnPublishItems = Lists.newArrayList();

    // 从淘宝 api 获取商品
    if (onSale) {
      itemPage =
          taobaoApiShopService.paginateOnSaleItems(
              userId,
              query,
              ITEM_FIELDS,
              sellerCids,
              pageNum,
              pageSize,
              true,
              null,
              "modified:asc");

    } else {
      itemPage =
          taobaoApiShopService.paginateInventoryItems(
              userId,
              query,
              ITEM_FIELDS,
              Lists.newArrayList(BANNER_FOR_SHELVED),
              sellerCids,
              pageNum,
              pageSize,
              true,
              "modified:asc");
    }

    List<Item> taobaoItems = itemPage.getItems();

    if (CollectionUtils.isEmpty(taobaoItems)) {
      return Page.create(
          itemPage.getTotalSize(), pageNum, pageSize, Lists.<PublishItem>newArrayList());
    }

    Map<Long, PosterRecommendPublishItem> indexMap = Maps.newHashMap();

    if (null != activityId) {
      // 获取已经加入投放的宝贝并且设置是否加入投放的标志位
      List<PosterRecommendPublishItem> posterRecommendPublishItems =
          posterRecommendPublishItemDAO.selectByUserIdAndActivityId(userId, activityId);

      indexMap =
          Maps.uniqueIndex(
              posterRecommendPublishItems,
              new Function<PosterRecommendPublishItem, Long>() {
                @Nullable
                @Override
                public Long apply(@Nullable PosterRecommendPublishItem input) {
                  return input.getItemNumIid();
                }
              });
    }

    for (Item item : taobaoItems) {
      PublishItem publishItem = new PublishItem();
      publishItem.setItem(item);

      if (indexMap.keySet().contains(item.getNumIid())) {
        publishItem.setPublishItemStatus(indexMap.get(item.getNumIid()).getStatus());
      } else {
        publishItem.setPublishItemStatus(PUBLISH_ITEM_STATUS_NOT_IN_DB);
      }
      returnPublishItems.add(publishItem);
    }
    return Page.create(itemPage.getTotalSize(), pageNum, pageSize, returnPublishItems);
  }