|
@@ -1,10 +1,7 @@
|
|
|
package cn.com.ctop.toutiao.modules.material.service.impl;
|
|
|
|
|
|
import cn.com.ctop.common.module.entity.CtopOauthToken;
|
|
|
-import cn.com.ctop.common.module.utils.HttpUtils;
|
|
|
-import cn.com.ctop.common.module.utils.PropertiesUtils;
|
|
|
-import cn.com.ctop.common.module.utils.ResultMapUtils;
|
|
|
-import cn.com.ctop.common.module.utils.StatusCode;
|
|
|
+import cn.com.ctop.common.module.utils.*;
|
|
|
import cn.com.ctop.toutiao.modules.material.entity.ByteDanceCampaign;
|
|
|
import cn.com.ctop.toutiao.modules.material.mapper.ByteDanceCampaignMapper;
|
|
|
import cn.com.ctop.toutiao.modules.material.service.IByteDanceCampaignService;
|
|
@@ -14,10 +11,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @Description: 今日头条广告组信息
|
|
@@ -29,62 +24,6 @@ import java.util.Map;
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
public class ByteDanceCampaignServiceImpl extends ServiceImpl<ByteDanceCampaignMapper, ByteDanceCampaign> implements IByteDanceCampaignService {
|
|
|
- @Override
|
|
|
- /**
|
|
|
- * 获取广告组数据
|
|
|
- */
|
|
|
- public List<ByteDanceCampaign> getGroupListByParams(CtopOauthToken token, JSONArray ids, String campaignName, String landingType, String status, String campaignCreateTime, Integer page, Integer pageSize) {
|
|
|
- List<ByteDanceCampaign> campaigns = new ArrayList<>();
|
|
|
- // 请求地址
|
|
|
- String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_campaign_get");
|
|
|
- // 请求参数
|
|
|
- JSONObject filtering = new JSONObject();
|
|
|
- if(null!=ids&&!ids.isEmpty()){
|
|
|
- filtering.put("ids",ids);
|
|
|
- }
|
|
|
- if(null!=campaignName&&!campaignName.equals("")){
|
|
|
- filtering.put("campaign_name",campaignName);
|
|
|
- }
|
|
|
- if(null!=landingType&&!landingType.equals("")){
|
|
|
- filtering.put("landing_type",landingType);
|
|
|
- }
|
|
|
- if(null!=status&&!status.equals("")){
|
|
|
- filtering.put("status",status);
|
|
|
- }
|
|
|
- if(null!=campaignCreateTime&&!campaignCreateTime.equals("")){
|
|
|
- filtering.put("campaign_create_time",landingType);
|
|
|
- }
|
|
|
-
|
|
|
- JSONObject params = new JSONObject();
|
|
|
- params.put("advertiser_id", token.getAccountId());
|
|
|
- params.put("page", page);
|
|
|
- params.put("page_size", pageSize);
|
|
|
- params.put("filtering", filtering);
|
|
|
- JSONObject result = HttpUtils.bytedanceGetRequest(token.getAccessToken(),url,params);
|
|
|
- if(result == null){
|
|
|
- log.error("获取广告组数据异常=>param:{}",params.toJSONString());
|
|
|
- return campaigns;
|
|
|
- }
|
|
|
- Integer code = result.getInteger("code");
|
|
|
- String message = result.getString("message");
|
|
|
- if(null == code||!code.equals(0)){
|
|
|
- log.error("获取广告组数据异常=>{},param:{}",message,params.toJSONString());
|
|
|
- return campaigns;
|
|
|
- }
|
|
|
- JSONArray dataList = result.getJSONObject("data").getJSONArray("list");
|
|
|
- if(null == dataList||dataList.isEmpty()){
|
|
|
- log.info("获取广告组数据为空=>param:{}",params.toJSONString());
|
|
|
- return campaigns;
|
|
|
- }
|
|
|
- for(int i=0;i<dataList.size();i++){
|
|
|
- JSONObject data = dataList.getJSONObject(i);
|
|
|
- ByteDanceCampaign campaign = new ByteDanceCampaign(data,token.getAccountId());
|
|
|
- this.saveOrUpdate(campaign);
|
|
|
- campaigns.add(campaign);
|
|
|
- }
|
|
|
- return campaigns;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 每个广告主账号下最多可允许创建500个广告组,如超出需要先删除一部分广告组后才可继续创建。
|
|
|
* @param token
|
|
@@ -97,7 +36,7 @@ public class ByteDanceCampaignServiceImpl extends ServiceImpl<ByteDanceCampaignM
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public Map<String, Object> createGroup(CtopOauthToken token, String campaignName, String operation, String budgetMode, String budget, String landingType, String uniqueFk) {
|
|
|
+ public Map<String, Object> createCampaign(CtopOauthToken token, String campaignName, String operation, String budgetMode, String budget, String landingType, String uniqueFk) {
|
|
|
Map<String,Object>result = new HashMap<>();
|
|
|
// 请求地址
|
|
|
String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_campaign_create");
|
|
@@ -136,17 +75,167 @@ public class ByteDanceCampaignServiceImpl extends ServiceImpl<ByteDanceCampaignM
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Map<String, Object> updateGroup(CtopOauthToken token, String campaignId, String campaignName, String modifyTime,String budget) {
|
|
|
- // 请求地址
|
|
|
- String url = "https://ad.oceanengine.com/open_api/2/campaign/update/";
|
|
|
- // 请求参数
|
|
|
- JSONObject data = new JSONObject();
|
|
|
- data.put("advertiser_id",token.getAccountId());
|
|
|
- data.put("campaign_name",campaignName);
|
|
|
- data.put("budget",budget);
|
|
|
- data.put("campaign_id", campaignId);
|
|
|
- data.put("modify_time", modifyTime);
|
|
|
- JSONObject result = HttpUtils.bytedancePostRequest(token.getAccessToken(),url,data);
|
|
|
- return new HashMap<>();
|
|
|
+ public Map<String, Object> updateCampaign(CtopOauthToken token, String campaignId, String campaignName,String budgetMode, String budget) {
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
+ ByteDanceCampaign campaign = this.getById(campaignId);
|
|
|
+ //2: 根据token以及用户id获取用户信息数据
|
|
|
+ String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_campaign_update");
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
+ headers.put("Access-Token", token.getAccessToken());
|
|
|
+
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.put("advertiser_id", token.getAccountId());
|
|
|
+ params.put("campaign_id", campaignId);
|
|
|
+ params.put("modify_time", campaign.getModifyTime());
|
|
|
+ if (null != campaignName && !"".equals(campaignName.trim())) {
|
|
|
+ params.put("campaign_name", campaignName);
|
|
|
+ }
|
|
|
+ if (null != budgetMode && !"".equals(budgetMode.trim())) {
|
|
|
+ params.put("budget_mode", budgetMode);
|
|
|
+ }
|
|
|
+ if (null != budget) {
|
|
|
+ params.put("budget", budget);
|
|
|
+ }
|
|
|
+ String result = HttpUtils.httpPostRequest(url, params, headers);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ Integer code = jsonObject.getInteger("code");
|
|
|
+
|
|
|
+ if (null == code || !code.equals(0)) {
|
|
|
+ log.info("广告组更新接口异常==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
|
|
|
+ resultMap.put("code", -1);
|
|
|
+ resultMap.put("message", "广告组更新接口异常");
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
+ if (null == data) {
|
|
|
+ log.info("广告组更新异常==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
|
|
|
+ resultMap.put("code", -1);
|
|
|
+ resultMap.put("message", "广告组更新异常");
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ if (null != campaignName && !"".equals(campaignName.trim())) {
|
|
|
+ campaign.setName(campaignName);
|
|
|
+ }
|
|
|
+ if (null != budgetMode && !"".equals(budgetMode.trim())) {
|
|
|
+ params.put("budget_mode", budgetMode);
|
|
|
+ campaign.setBudgetMode(budgetMode);
|
|
|
+ }
|
|
|
+ if (null != budget) {
|
|
|
+ campaign.setBudget(new BigDecimal(budget));
|
|
|
+ }
|
|
|
+ this.updateById(campaign);
|
|
|
+
|
|
|
+ resultMap.put("code", 0);
|
|
|
+ resultMap.put("message", "广告组信息修改成功");
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> getAdvertiserCampaign(CtopOauthToken token, String ids, String date) {
|
|
|
+ getAdvertiserCampaignByPageNumber(token, 1, ids, date);
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
+ resultMap.put("code", 0);
|
|
|
+ resultMap.put("message", "获取广告組信息成功");
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getAdvertiserCampaignByPageNumber(CtopOauthToken token, Integer pageNumber, String ids, String date) {
|
|
|
+ //2: 根据token以及用户id获取用户信息数据
|
|
|
+ String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_campaign_get");
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
+ headers.put("Access-Token", token.getAccessToken());
|
|
|
+
|
|
|
+ TreeMap<String, Object> params = new TreeMap<>();
|
|
|
+ JSONObject filtering = new JSONObject();
|
|
|
+ if (null != ids && !"".equals(ids.trim())) {
|
|
|
+ String[] idsArray = ids.split(StringUtils.COMMA);
|
|
|
+ JSONArray filterIdsArray = new JSONArray();
|
|
|
+ for (int i = 0; i < idsArray.length; i++) {
|
|
|
+ Long id = Long.parseLong(idsArray[i]);
|
|
|
+ filterIdsArray.add(id);
|
|
|
+ }
|
|
|
+ filtering.put("ids", filterIdsArray);
|
|
|
+ }
|
|
|
+ if (null != date && !"".equals(date)) {
|
|
|
+ filtering.put("campaign_create_time", date);
|
|
|
+ }
|
|
|
+ params.put("filtering", filtering.toJSONString());
|
|
|
+ params.put("advertiser_id", token.getAccountId());
|
|
|
+ params.put("page", pageNumber + "");
|
|
|
+ String result = HttpUtils.httpGetRequest(url, headers, params);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ Integer code = jsonObject.getInteger("code");
|
|
|
+
|
|
|
+ if (null == code || !code.equals(0)) {
|
|
|
+ log.error("获取广告组信息接口异常==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONArray data = jsonObject.getJSONObject("data").getJSONArray("list");
|
|
|
+ if (null == data || data.isEmpty()) {
|
|
|
+ log.error("获取广告组信息不存在==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ System.out.println(data.toJSONString());
|
|
|
+ for (int i = 0; i < data.size(); i++) {
|
|
|
+ JSONObject dataObject = data.getJSONObject(i);
|
|
|
+ ByteDanceCampaign campaign = new ByteDanceCampaign(dataObject,token.getAccountId());
|
|
|
+ this.saveOrUpdate(campaign);
|
|
|
+ }
|
|
|
+ getAdvertiserCampaignByPageNumber(token, pageNumber + 1, ids, date);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> updateCampaignStatus(CtopOauthToken token, String campaignIds, String optStatus) {
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
+ JSONArray ids = new JSONArray();
|
|
|
+ String[] getIds = campaignIds.split(StringUtils.COMMA);
|
|
|
+ if (null != getIds && getIds.length > 0) {
|
|
|
+ for (int i = 0; i < getIds.length; i++) {
|
|
|
+ ids.add(Long.parseLong(getIds[i]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //2: 根据token以及用户id获取用户信息数据
|
|
|
+ String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_campaign_update_status");
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
+ headers.put("Access-Token", token.getAccessToken());
|
|
|
+
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.put("advertiser_id", token.getAccountId());
|
|
|
+ params.put("campaign_ids", ids);
|
|
|
+ params.put("opt_status", optStatus);
|
|
|
+ String result = HttpUtils.httpPostRequest(url, params, headers);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ Integer code = jsonObject.getInteger("code");
|
|
|
+
|
|
|
+ if (null == code || !code.equals(0)) {
|
|
|
+ log.info("广告组更新状态接口异常==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
|
|
|
+ resultMap.put("code", -1);
|
|
|
+ resultMap.put("message", "广告组更新状态接口异常");
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
+ if (null == data) {
|
|
|
+ log.info("广告组更新状态异常==》accountId:{},message:{}", token.getAccountId(), jsonObject.getString("message"));
|
|
|
+ resultMap.put("code", -1);
|
|
|
+ resultMap.put("message", "广告组更新状态异常");
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ JSONArray getCampaignIds = data.getJSONArray("campaign_ids");
|
|
|
+ if (null != getCampaignIds && !getCampaignIds.isEmpty()) {
|
|
|
+ for (int i = 0; i < getCampaignIds.size(); i++) {
|
|
|
+ Long id = getCampaignIds.getLong(i);
|
|
|
+ ByteDanceCampaign campaign = this.getById(id);
|
|
|
+ if (null != campaign) {
|
|
|
+ campaign.setOptStatus(optStatus);
|
|
|
+ }
|
|
|
+ this.updateById(campaign);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ resultMap.put("code", 0);
|
|
|
+ resultMap.put("message", "广告组状态修改成功");
|
|
|
+ return resultMap;
|
|
|
}
|
|
|
}
|