Procházet zdrojové kódy

coding:投流配置

zhangqin před 2 dny
rodič
revize
fdd04c4c08

+ 3 - 0
fs-ad-new-api/src/main/java/com/fs/app/controller/CallbackController.java

@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletRequest;
+import java.time.LocalDateTime;
 
 /**
  * 广告商回调接口
@@ -114,10 +115,12 @@ public class CallbackController {
                 .appId(byId.getAppId())
                 .authCode(authCode)
                 .appSecret(byId.getAppSecret())
+                .appSecret(byId.getAppSecret())
                 .build());
         if (ObjectUtil.isNotEmpty(accessToken)) {
             byId.setAccessToken(accessToken.getAccessToken());
             byId.setRefreshToken(accessToken.getRefreshToken());
+            byId.setUpdateTime(LocalDateTime.now());
             promotionAccountService.updateById(byId);
         } else {
             log.error("获取accessToken失败:{}", byId.getId());

+ 12 - 1
fs-ad-new-api/src/main/java/com/fs/app/facade/CallbackProcessingFacadeServiceImpl.java

@@ -5,6 +5,8 @@ import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.fs.newAdv.integration.adapter.IAdvertiserAdapter;
 import com.fs.newAdv.integration.factory.AdvertiserHandlerFactory;
 import com.fs.common.exception.base.BusinessException;
@@ -134,10 +136,13 @@ public class CallbackProcessingFacadeServiceImpl implements CallbackProcessingFa
         Lead byTraceId = leadService.getByTraceId(traceId);
         boolean isNewLead = ObjectUtil.isEmpty(byTraceId);
         if (isNewLead) {
-            byTraceId = new Lead();
+            IAdvertiserAdapter advertiserAdapter = handlerFactory.getAdapter(AdvertiserTypeEnum.getByCode(advertiserId));
+            byTraceId = advertiserAdapter.adaptCallbackData(allParams);
             byTraceId.setAdvertiserId(advertiserId);
             byTraceId.setSiteId(siteId);
             byTraceId.setTraceId(traceId);
+            // 设置站点和落地页的关联
+            setSiteByIdeaId(siteId, byTraceId.getIdeaId());
         } else {
             // 检查站点和广告商信息是否异常
             if (!Objects.equals(byTraceId.getSiteId(), siteId)) {
@@ -182,6 +187,12 @@ public class CallbackProcessingFacadeServiceImpl implements CallbackProcessingFa
         return res;
     }
 
+    private void setSiteByIdeaId(Long siteId, String ideaId) {
+        siteService.update(new LambdaUpdateWrapper<Site>()
+                .eq(Site::getId, siteId)
+                .set(Site::getIdeaId, ideaId));
+    }
+
     /**
      * 更新模板中的二维码信息
      */

+ 1 - 1
fs-ad-new-api/src/main/java/com/fs/app/task/DataSyncTask.java

@@ -57,7 +57,7 @@ public class DataSyncTask {
      * 数据同步任务->当日数据
      * cron: 每1小时统计站点数据
      */
-    @Scheduled(cron = "0 0 0/1 * * ?")
+    @Scheduled(cron = "0 0/1 * * * ?")
     public void syncTodayData() {
         String batchNo = DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd");
         statisticsService.syncData(batchNo, 1);

+ 2 - 2
fs-ad-new-api/src/main/resources/application.yml

@@ -4,6 +4,6 @@ server:
 # Spring配置
 spring:
   profiles:
-#    active: dev
-    active: druid-ylrz
+    active: dev
+#    active: druid-ylrz
 

+ 2 - 2
fs-company/src/main/resources/application.yml

@@ -1,9 +1,9 @@
 server:
-  port: 7773
+  port: 8006
 # Spring配置
 spring:
   profiles:
-    active: druid-ylrz
+    active: dev
 #    active: druid-jnsyj-test
 #    active: druid-jnmy-test
 #    active: druid-jzzx-test

+ 1 - 1
fs-service/src/main/java/com/fs/newAdv/domain/Lead.java

@@ -148,7 +148,7 @@ public class Lead implements Serializable {
      */
     private String unitId;
     /**
-     * 创意ID
+     * 广告投流的最小单元 创意ID
      */
     private String ideaId;
     /**

+ 4 - 0
fs-service/src/main/java/com/fs/newAdv/domain/Site.java

@@ -120,6 +120,10 @@ public class Site implements Serializable {
      */
     private Long allocationRuleId;
 
+    /**
+     * 广告投流的最小单元 创意ID
+     */
+    private String ideaId;
     /**
      * 创建时间
      */

+ 1 - 1
fs-service/src/main/java/com/fs/newAdv/integration/client/IApiClient.java

@@ -20,6 +20,6 @@ public interface IApiClient {
 
     AdvertiserTypeEnum getAdvertiserType();
 
-    SiteStatistics getDataReport(PromotionAccount account,String startDate,String endDate);
+    SiteStatistics getDataReport(PromotionAccount account,String ideaId,String startDate,String endDate);
 }
 

+ 1 - 1
fs-service/src/main/java/com/fs/newAdv/integration/client/advertiser/BaiduApiClient.java

@@ -118,7 +118,7 @@ public class BaiduApiClient extends AbstractApiClient implements IAccessTokenCli
     }
 
     @Override
-    public SiteStatistics getDataReport(PromotionAccount account, String startDate, String endDate) {
+    public SiteStatistics getDataReport(PromotionAccount account,String ideaId, String startDate, String endDate) {
         // 构建请求参数
         Map<String, Object> map = new HashMap<>();
         Map<String, Object> header = new HashMap<>();

+ 1 - 1
fs-service/src/main/java/com/fs/newAdv/integration/client/advertiser/IQIYIApiClient.java

@@ -70,7 +70,7 @@ public class IQIYIApiClient extends AbstractApiClient {
     }
 
     @Override
-    public SiteStatistics getDataReport(PromotionAccount account,String startDate,String endDate) {
+    public SiteStatistics getDataReport(PromotionAccount account,String ideaId,String startDate,String endDate) {
         return null;
     }
 }

+ 1 - 1
fs-service/src/main/java/com/fs/newAdv/integration/client/advertiser/OPPOApiClient.java

@@ -92,7 +92,7 @@ public class OPPOApiClient extends AbstractApiClient {
     }
 
     @Override
-    public SiteStatistics getDataReport(PromotionAccount account,String startDate,String endDate) {
+    public SiteStatistics getDataReport(PromotionAccount account,String ideaId,String startDate,String endDate) {
         return null;
     }
 }

+ 88 - 9
fs-service/src/main/java/com/fs/newAdv/integration/client/advertiser/OceanEngineApiClient.java

@@ -3,22 +3,28 @@ package com.fs.newAdv.integration.client.advertiser;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
+import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
-import com.fs.newAdv.integration.client.AbstractApiClient;
-import com.fs.newAdv.integration.client.IAccessTokenClient;
+import com.baidu.dev2.thirdparty.jackson.core.JsonProcessingException;
+import com.baidu.dev2.thirdparty.jackson.databind.ObjectMapper;
 import com.fs.common.constant.SystemConstant;
 import com.fs.common.exception.ThirdPartyException;
 import com.fs.newAdv.domain.PromotionAccount;
 import com.fs.newAdv.domain.SiteStatistics;
 import com.fs.newAdv.enums.AdvertiserTypeEnum;
+import com.fs.newAdv.integration.client.AbstractApiClient;
+import com.fs.newAdv.integration.client.IAccessTokenClient;
 import com.fs.newAdv.vo.AccessTokenByAuthCodeVo;
 import com.fs.newAdv.vo.AccessTokenVo;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.utils.URIBuilder;
 import org.springframework.stereotype.Component;
 
-import java.util.HashMap;
-import java.util.Map;
+import java.math.BigDecimal;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.*;
 
 /**
  * 巨量引擎API客户端
@@ -35,7 +41,8 @@ public class OceanEngineApiClient extends AbstractApiClient implements IAccessTo
     /**
      * 巨量引擎转化回传API地址
      */
-    private static final String CONVERSION_API_URL = "https://ad.oceanengine.com/track/activate";
+    private static final String CONVERSION_API_URL = "https://ad.oceanengine.com/track/activate/";
+    private static final String REPORT_API_URL = "https://api.oceanengine.com/open_api/v3.0/report/custom/get/";
     /**
      * 获取Access Token
      */
@@ -127,13 +134,83 @@ public class OceanEngineApiClient extends AbstractApiClient implements IAccessTo
     }
 
     @Override
-    public SiteStatistics getDataReport(PromotionAccount account,String startDate,String endDate) {
-        return null;
+    public SiteStatistics getDataReport(PromotionAccount account,String ideaId, String startDate, String endDate) {
+        // 构建请求参数
+        Map<String, Object> map = new HashMap<>();
+        map.put("advertiser_id", Long.valueOf(account.getAdAccountId()));
+        // 纬度--天
+        map.put("dimensions", Arrays.asList("stat_time_day"));
+        // 过滤条件
+        Map<String,Object> filters = new HashMap<>();
+        filters.put("field","cdp_promotion_id");
+        filters.put("type",2);
+        filters.put("operator",1);
+        filters.put("values ",Arrays.asList(ideaId));
+        map.put("filters", Arrays.asList(filters));
+        /**
+         * stat_cost 消耗(元)
+         * show_cnt 展示数
+         * cpm_platform 平均千次展现费用(元)
+         * click_cnt 点击数
+         * ctr 点击率
+         * cpc_platform 平均点击单价(元)
+         * convert_cnt 转化数
+         * conversion_cost 平均转化成本
+         * conversion_rate 转化率
+         */
+        map.put("metrics", Arrays.asList("stat_cost",
+                "show_cnt", "cpm_platform", "click_cnt", "ctr", "cpc_platform", "cpc_platform", "convert_cnt",
+                "conversion_cost", "conversion_rate"));
+        map.put("start_time", startDate);
+        map.put("end_time", endDate);
+        map.put("order_by", Collections.emptyList());
+        String url;
+        try {
+            URIBuilder ub = new URIBuilder(REPORT_API_URL);
+            ObjectMapper objectMapper = new ObjectMapper();
+            map.forEach((k, v) -> {
+                try {
+                    ub.addParameter(k, v instanceof String ? (String) v : objectMapper.writeValueAsString(v));
+                } catch (JsonProcessingException e) {
+                    throw new RuntimeException(e);
+                }
+            });
+            url = ub.build().toURL().toString();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        HttpResponse execute = HttpRequest.get(url)
+                .header("Access-Token", account.getAccessToken())
+                .timeout(SystemConstant.API_TIMEOUT)
+                .execute();
+        JSONObject jsonObject = JSONUtil.parseObj(execute.body());
+        if (jsonObject.getInt("code") != 0) {
+            // 刷新token重新请求
+            log.info("巨量刷新token重新请求");
+            AccessTokenVo accessTokenVo = refreshAccessToken(account.getAppId(), account.getAppSecret(), account.getRefreshToken());
+            execute = HttpRequest.get(url)
+                    .header("Access-Token", accessTokenVo.getAccessToken())
+                    .timeout(SystemConstant.API_TIMEOUT)
+                    .execute();
+            jsonObject = JSONUtil.parseObj(execute.body());
+        }
+        JSONObject data = jsonObject.getJSONObject("data");
+        JSONArray rows = data.getJSONArray("rows");
+        JSONObject jsonObject1 = rows.getJSONObject(0);
+        JSONObject jsonObject2 = jsonObject1.getJSONObject("metrics");
+        SiteStatistics siteStatistics = new SiteStatistics();
+        siteStatistics.setImpressionCount(Integer.valueOf(jsonObject2.getStr("show_cnt")));
+        siteStatistics.setClickCount(Integer.valueOf(jsonObject2.getStr("click_cnt")));
+        siteStatistics.setActualCost(new BigDecimal(jsonObject2.getStr("stat_cost")));
+        siteStatistics.setAccountCost(new BigDecimal(jsonObject2.getStr("stat_cost")));
+        siteStatistics.setClickRate(new BigDecimal(jsonObject2.getStr("ctr")));
+        siteStatistics.setAvgClickPrice(new BigDecimal(jsonObject2.getStr("cpc_platform")));
+        return siteStatistics;
     }
 
     @Override
     public AccessTokenVo refreshAccessToken(String appId, String appSecret, String refreshToken) {
-        Map<String,Object> map = new HashMap<>();
+        Map<String, Object> map = new HashMap<>();
         map.put("app_id", appId);
         map.put("secret", appSecret);
         map.put("refresh_token", refreshToken);
@@ -157,7 +234,7 @@ public class OceanEngineApiClient extends AbstractApiClient implements IAccessTo
 
     @Override
     public AccessTokenVo getAccessTokenByAuthCode(AccessTokenByAuthCodeVo request) {
-        Map<String,Object> map = new HashMap<>();
+        Map<String, Object> map = new HashMap<>();
         map.put("app_id", request.getAppId());
         map.put("secret", request.getAppSecret());
         map.put("auth_code", request.getAuthCode());
@@ -179,5 +256,7 @@ public class OceanEngineApiClient extends AbstractApiClient implements IAccessTo
         }
         return null;
     }
+
+
 }
 

+ 1 - 1
fs-service/src/main/java/com/fs/newAdv/integration/client/advertiser/TencentApiClient.java

@@ -83,7 +83,7 @@ public class TencentApiClient extends AbstractApiClient implements IAccessTokenC
     }
 
     @Override
-    public SiteStatistics getDataReport(PromotionAccount account, String startDate, String endDate) {
+    public SiteStatistics getDataReport(PromotionAccount account,String ideaId, String startDate, String endDate) {
         return null;
     }
 

+ 1 - 1
fs-service/src/main/java/com/fs/newAdv/integration/client/advertiser/VIVOApiClient.java

@@ -86,7 +86,7 @@ public class VIVOApiClient extends AbstractApiClient {
     }
 
     @Override
-    public SiteStatistics getDataReport(PromotionAccount account,String startDate,String endDate) {
+    public SiteStatistics getDataReport(PromotionAccount account,String ideaId,String startDate,String endDate) {
         return null;
     }
 }

+ 4 - 4
fs-service/src/main/java/com/fs/newAdv/service/impl/SiteStatisticsServiceImpl.java

@@ -82,7 +82,7 @@ public class SiteStatisticsServiceImpl extends ServiceImpl<SiteStatisticsMapper,
                                 log.warn("账户 {} 未关联站点,跳过同步", account.getAccountName());
                                 return false;
                             }
-                            syncAccountData(account, site.getId(), batchNo);
+                            syncAccountData(account, site.getId(),site.getIdeaId(), batchNo);
                             return true;
                         } catch (Exception e) {
                             log.error("同步账户 {} 数据失败", account.getAccountName(), e);
@@ -98,9 +98,9 @@ public class SiteStatisticsServiceImpl extends ServiceImpl<SiteStatisticsMapper,
     }
 
     /**
-     * 同步账户数据
+     * 同步账户广告数据
      */
-    private void syncAccountData(PromotionAccount account, Long siteId, String batchNo) {
+    private void syncAccountData(PromotionAccount account, Long siteId,String ideaId, String batchNo) {
         log.info("开始同步账户:{}", account.getAccountName());
 
         // 查询现有统计记录
@@ -115,7 +115,7 @@ public class SiteStatisticsServiceImpl extends ServiceImpl<SiteStatisticsMapper,
         if (Integer.valueOf(1).equals(account.getApiSwitch())) {
             IApiClient apiClient = advertiserHandlerFactory.getApiClient(AdvertiserTypeEnum.getByCode(account.getAdvertiserId()));
             try {
-                siteStatistics = apiClient.getDataReport(account, batchNo, batchNo);
+                siteStatistics = apiClient.getDataReport(account,ideaId, batchNo, batchNo);
             } catch (Exception e) {
                 siteStatistics = new SiteStatistics();
                 log.error("获取账户数据失败:{} {}", account.getAccountName(), siteId, e);