/**
   * 推广单元更新
   *
   * @param list 推广单元列表
   * @return
   * @throws ApiException
   */
  public List<AdgroupType> updateAdgroup(List<AdgroupType> list) throws ApiException {
    if (list == null || list.isEmpty()) {
      return Collections.emptyList();
    }

    UpdateAdgroupRequest request = new UpdateAdgroupRequest();
    request.setAdgroupTypes(list);

    AdgroupService adgroupService = commonService.getService(AdgroupService.class);
    UpdateAdgroupResponse response = adgroupService.updateAdgroup(request);
    if (response == null) return Collections.emptyList();

    return response.getAdgroupTypes();
  }
  public String deleteAdgroup(List<Long> adgroupIds) throws ApiException {
    DeleteAdgroupRequest request = new DeleteAdgroupRequest();
    request.setAdgroupIds(adgroupIds);

    AdgroupService adgroupService = commonService.getService(AdgroupService.class);
    DeleteAdgroupResponse response = adgroupService.deleteAdgroup(request);
    if (response == null) {
      try {
        TimeUnit.SECONDS.sleep(3);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      if (response != null) return response.getResponse();
    }

    return "";
  }
  public List<AdgroupType> addAdgroup(List<AdgroupType> list) throws ApiException {
    AddAdgroupRequest request = new AddAdgroupRequest();
    request.setAdgroupTypes(list);

    AdgroupService adgroupService = commonService.getService(AdgroupService.class);
    AddAdgroupResponse response = adgroupService.addAdgroup(request);
    if (response == null) {
      try {
        TimeUnit.SECONDS.sleep(3);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      if (response != null) return response.getAdgroupTypes();
    }

    return Collections.emptyList();
  }
  /**
   * 计划包含最大1000个单元
   *
   * @param campaignIds
   * @return
   */
  public List<AdgroupType> getAllAdGroup(List<Long> campaignIds) {
    try {
      AdgroupService adgroupService = commonService.getService(AdgroupService.class);

      List<Long> subList = new ArrayList<>(20);
      List<AdgroupType> adgroupTypes = new ArrayList<>();
      for (int i = 1; i <= campaignIds.size(); i++) {
        Long camId = campaignIds.get(i - 1);
        subList.add(camId);

        if (i % 20 == 0) {
          GetAdgroupByCampaignIdRequest getAdgroupByCampaignIdRequest =
              new GetAdgroupByCampaignIdRequest();
          getAdgroupByCampaignIdRequest.setCampaignIds(subList);
          GetAdgroupByCampaignIdResponse response =
              adgroupService.getAdgroupByCampaignId(getAdgroupByCampaignIdRequest);

          if (response == null) {
            Thread.sleep(5000);

            response = adgroupService.getAdgroupByCampaignId(getAdgroupByCampaignIdRequest);
            if (response == null) {
              subList.clear();
              continue;
            }
          }

          for (CampaignAdgroup campaignAdgroup : response.getCampaignAdgroups()) {
            adgroupTypes.addAll(campaignAdgroup.getAdgroupTypes());
          }
          subList.clear();
        }
      }

      if (!subList.isEmpty()) {
        GetAdgroupByCampaignIdRequest getAdgroupByCampaignIdRequest =
            new GetAdgroupByCampaignIdRequest();
        getAdgroupByCampaignIdRequest.setCampaignIds(subList);
        GetAdgroupByCampaignIdResponse response =
            adgroupService.getAdgroupByCampaignId(getAdgroupByCampaignIdRequest);

        if (response == null) {
          Thread.sleep(5000);

          response = adgroupService.getAdgroupByCampaignId(getAdgroupByCampaignIdRequest);
          if (response == null) {
            subList.clear();
          }
        }

        for (CampaignAdgroup campaignAdgroup : response.getCampaignAdgroups()) {
          adgroupTypes.addAll(campaignAdgroup.getAdgroupTypes());
        }
      }

      return adgroupTypes;
    } catch (final Exception e) {
      e.printStackTrace();
    }

    return Collections.emptyList();
  }