/**
   * 卸载活动
   *
   * @param userId
   * @param activityId
   */
  public void unpublishActivity(Long userId, Long activityId) {
    logger.info("start to unpublish activity, userId[{}] activityId[{}]", userId, activityId);

    PosterRecommendActivity activity =
        posterRecommendActivityDAO.selectByUserIdAndActivityId(userId, activityId);
    if (null == activity) {
      return;
    }
    if (!ALL_PUBLISHED_ACTIVITY_STATUS.contains(activity.getStatus())) {
      logger.info(
          "userId[{}] activity[{}] status[{}] is not one of [{}], skip.",
          userId,
          activity,
          activity.getStatus(),
          ALL_PUBLISHED_ACTIVITY_STATUS);
      return;
    }
    // 开始卸载详情页海报
    PosterRecommendActivity newActivity = new PosterRecommendActivity();
    newActivity.setUserId(userId);
    newActivity.setId(activityId);
    newActivity.setStatus(PosterRecommendConstants.ACTIVITY_STATUS_UNPUBLISHING);
    newActivity.setUnpublishTime(appService.getLocalSystemTime().toDate());
    newActivity.setUnpublishOwner(appService.getOwner());
    posterRecommendActivityDAO.updateByUserIdAndActivityIdSelective(newActivity);

    // 卸载单个宝贝
    List<PosterRecommendPublishItem> publishItems =
        posterRecommendPublishItemDAO.selectByUserIdAndActivityId(userId, activityId);
    List<Exception> exceptions = Lists.newArrayList();
    for (PosterRecommendPublishItem publishItem : publishItems) {
      try {
        unpublishItem(userId, activityId, publishItem.getItemNumIid());
      } catch (TaobaoAccessControlException e) {
        exceptions.add(e);
      } catch (TaobaoEnhancedApiException e) {
        exceptions.add(e);
      } catch (TaobaoSessionExpiredException e) {
        exceptions.add(e);
      }
    }

    // 结束投放活动
    if (CollectionUtils.isEmpty(exceptions)) {
      newActivity.setStatus(ACTIVITY_STATUS_UNPUBLISHED);
    } else {
      newActivity.setStatus(ACTIVITY_STATUS_UNPUBLISHED_WITH_ERROR);
    }
    posterRecommendActivityDAO.updateByUserIdAndActivityIdSelective(newActivity);
  }
  /**
   * 投放活动到详情页
   *
   * @param userId
   * @param activityId
   */
  public void publishActivity(Long userId, Long activityId) {
    PosterRecommendActivity activity =
        posterRecommendActivityDAO.selectByUserIdAndActivityId(userId, activityId);
    if (null == activity) {
      return;
    }
    // 开始投放活动
    PosterRecommendActivity newActivity = new PosterRecommendActivity();
    newActivity.setUserId(userId);
    newActivity.setId(activityId);
    newActivity.setStatus(PosterRecommendConstants.ACTIVITY_STATUS_PUBLISHING);
    newActivity.setPublishTime(appService.getLocalSystemTime().toDate());
    newActivity.setPublishOwner(appService.getOwner());
    posterRecommendActivityDAO.updateByUserIdAndActivityIdSelective(newActivity);

    // 投放单个宝贝
    List<PosterRecommendPublishItem> publishItems =
        posterRecommendPublishItemDAO.selectByUserIdAndActivityId(userId, activityId);
    List<Exception> exceptions = Lists.newArrayList();
    for (PosterRecommendPublishItem publishItem : publishItems) {
      try {
        publish2Item(userId, activityId, publishItem.getItemNumIid(), activity.getPublishHtml());
      } catch (TaobaoAccessControlException e) {
        exceptions.add(e);
      } catch (TaobaoEnhancedApiException e) {
        exceptions.add(e);
      } catch (TaobaoSessionExpiredException e) {
        exceptions.add(e);
      }
    }

    // 结束投放活动
    if (CollectionUtils.isEmpty(exceptions)) {
      newActivity.setStatus(PosterRecommendConstants.ACTIVITY_STATUS_PUBLISHED);
    } else {
      newActivity.setStatus(PosterRecommendConstants.ACTIVITY_STATUS_PUBLISHED_WITH_ERROR);
    }
    posterRecommendActivityDAO.updateByUserIdAndActivityIdSelective(newActivity);
  }
  /**
   * 查询可供选择加入投放的宝贝
   *
   * @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);
  }