|
@@ -0,0 +1,205 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace app\v1\logic\tool\advertCost;
|
|
|
|
|
+
|
|
|
|
|
+use GuzzleHttp\Client;
|
|
|
|
|
+use support\think\Db;
|
|
|
|
|
+
|
|
|
|
|
+class TtCostVideoLogic extends BaseAdvertLogic
|
|
|
|
|
+{
|
|
|
|
|
+ // 执行正文
|
|
|
|
|
+ protected function initStart(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $tokenMap = $this->getTtTokenMap();
|
|
|
|
|
+
|
|
|
|
|
+ $accountList = $this->getTtAccountList();
|
|
|
|
|
+
|
|
|
|
|
+ // 循环执行
|
|
|
|
|
+ foreach ($accountList as $account)
|
|
|
|
|
+ {
|
|
|
|
|
+ $accessToken = $tokenMap[$account['pmid']] ?? "";
|
|
|
|
|
+ if(!$accessToken) continue;
|
|
|
|
|
+
|
|
|
|
|
+ // 获取消耗
|
|
|
|
|
+ $dataList = $this->getTtVideoCost($account['advertiser_id'], $accessToken);
|
|
|
|
|
+
|
|
|
|
|
+ // 数据加工,获取素材信息
|
|
|
|
|
+ if($dataList){
|
|
|
|
|
+ $videoMap = $this->getTtVideoNameMap($account['advertiser_id'], $accessToken, $dataList);
|
|
|
|
|
+
|
|
|
|
|
+ // 整理数据入库
|
|
|
|
|
+ $this->organizeDataList($account, $dataList, $videoMap);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取媒体消耗
|
|
|
|
|
+ protected function getTtVideoCost($advertiserId, $accessToken): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $page = 1;
|
|
|
|
|
+ $data = [];
|
|
|
|
|
+ do {
|
|
|
|
|
+ $url = 'https://ad.oceanengine.com/open_api/v3.0/report/custom/get/';
|
|
|
|
|
+
|
|
|
|
|
+ $request_data = [
|
|
|
|
|
+ 'advertiser_id'=>$advertiserId,
|
|
|
|
|
+ 'dimensions'=>json_encode(["ad_platform_material_name","material_id","cdp_project_id","cdp_project_name","stat_time_day", "ad_platform_cdp_project_action_track_url"]),
|
|
|
|
|
+ 'metrics'=>json_encode(["stat_cost","show_cnt","click_cnt","active","active_register","active_pay","valid_play","download_finish_cnt","total_play", "stat_pay_amount"]),
|
|
|
|
|
+ 'filters'=>'[{"field":"image_mode","type":1,"operator":0,"values":["5","15"]},{"operator":4,"values":["0"],"field":"stat_cost","type":3}]',
|
|
|
|
|
+ 'start_time'=>$this->date,
|
|
|
|
|
+ 'end_time'=>$this->date,
|
|
|
|
|
+ 'order_by'=>json_encode([]),
|
|
|
|
|
+ 'page'=>1,
|
|
|
|
|
+ 'page_size'=>100,
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $options = [
|
|
|
|
|
+ "headers" => [
|
|
|
|
|
+ 'Access-Token' => $accessToken,
|
|
|
|
|
+ 'Content-Type' => 'application/json',
|
|
|
|
|
+ ]
|
|
|
|
|
+ ];
|
|
|
|
|
+ $url = $url."?".http_build_query($request_data);
|
|
|
|
|
+
|
|
|
|
|
+ $httpClient = new Client(['timeout' => 10]);
|
|
|
|
|
+ $res = $httpClient->request('GET', $url, $options);
|
|
|
|
|
+ $result = json_decode($res->getBody(), true);
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($result['data']['rows'])) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $data = array_merge($data, $result['data']['rows']);
|
|
|
|
|
+ $totalPage = $result['data']['page_info']['total_page'] ?? 1;
|
|
|
|
|
+ $page++;
|
|
|
|
|
+ } while ($page <= $totalPage);
|
|
|
|
|
+
|
|
|
|
|
+ return $data;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected function getTtVideo($advertiserId, $accessToken, $materialIds): array
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ $url = 'https://api.oceanengine.com/open_api/2/file/video/get/';
|
|
|
|
|
+ $request_data = [
|
|
|
|
|
+ 'advertiser_id'=>$advertiserId,
|
|
|
|
|
+ 'filtering' => json_encode([
|
|
|
|
|
+ "material_ids" => $materialIds,
|
|
|
|
|
+ ]),
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $options = [
|
|
|
|
|
+ "headers" => [
|
|
|
|
|
+ 'Access-Token' => $accessToken,
|
|
|
|
|
+ 'Content-Type' => 'application/json',
|
|
|
|
|
+ ]
|
|
|
|
|
+ ];
|
|
|
|
|
+ $url = $url."?".http_build_query($request_data);
|
|
|
|
|
+
|
|
|
|
|
+ $httpClient = new Client(['timeout' => 10]);
|
|
|
|
|
+ $res = $httpClient->request('GET', $url, $options);
|
|
|
|
|
+ $result = json_decode($res->getBody(), true);
|
|
|
|
|
+
|
|
|
|
|
+ return $result['data']['list'] ?? [];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected function getTtVideoNameMap($advertiserId, $accessToken, $dataList): array
|
|
|
|
|
+ {
|
|
|
|
|
+ if(!$dataList) return $dataList;
|
|
|
|
|
+
|
|
|
|
|
+ $videoMaterialIds = [];
|
|
|
|
|
+ foreach ($dataList as $vv){
|
|
|
|
|
+ $videoMaterialIds[] = $vv['dimensions']['material_id'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 判断是否超过100条,切割处理
|
|
|
|
|
+ $videoIdList = array_chunk($videoMaterialIds, 100);
|
|
|
|
|
+ $videoList = [];
|
|
|
|
|
+ foreach ($videoIdList as $videoIds) {
|
|
|
|
|
+ $videos = $this->getTtVideo($advertiserId, $accessToken, $videoIds);
|
|
|
|
|
+ $videoList = array_merge($videoList, $videos);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $videoData = [];
|
|
|
|
|
+ if($videoList) {
|
|
|
|
|
+ foreach ($videoList as $val){
|
|
|
|
|
+ $videoData[$val['material_id']] = $val;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $videoData;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 整理入库数据列表
|
|
|
|
|
+ protected function organizeDataList($account, $dataList, $videoMap): void
|
|
|
|
|
+ {
|
|
|
|
|
+ if(!$dataList) return;
|
|
|
|
|
+
|
|
|
|
|
+ $db = Db::connect('db_advert');
|
|
|
|
|
+ // 返点率
|
|
|
|
|
+ $fandianRate = ($account['son_fandian']>1) ? (1/$account['son_fandian']) : $account['son_fandian'];
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($dataList as $val) {
|
|
|
|
|
+ $cost = $val['metrics']['stat_cost'];
|
|
|
|
|
+ if($cost=='0.00') continue;
|
|
|
|
|
+ $materialId = $val['dimensions']['material_id'];
|
|
|
|
|
+ $materialName = $val['dimensions']['ad_platform_material_name'];
|
|
|
|
|
+ $adId = $val['dimensions']['cdp_project_id'];
|
|
|
|
|
+ $adName = $val['dimensions']['cdp_project_name'];
|
|
|
|
|
+ $materialUrl = $videoMap[$materialId]['url'] ?? "";
|
|
|
|
|
+ $materialImg = $videoMap[$materialId]['poster_url'] ?? "";
|
|
|
|
|
+
|
|
|
|
|
+ // Todo 从素材名称中获取归属人
|
|
|
|
|
+ $authorId = $this->getAuthorId($materialName);
|
|
|
|
|
+
|
|
|
|
|
+ // Todo 从广告名称中拆分归因数据
|
|
|
|
|
+ $siteInfo = $this->getSiteInfo($adName);
|
|
|
|
|
+
|
|
|
|
|
+ if(!$siteInfo['game_id'] || empty($this->siteMap[$siteInfo['site_id']])){
|
|
|
|
|
+ $trackUrl = $val['dimensions']['ad_platform_cdp_project_action_track_url'] ?? "";
|
|
|
|
|
+ $siteInfo = $this->getSiteInfo($trackUrl);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(!$siteInfo['game_id'] || empty($this->siteMap[$siteInfo['site_id']])) continue;
|
|
|
|
|
+
|
|
|
|
|
+ $materialData = [
|
|
|
|
|
+ 'ad_id' => $adId,
|
|
|
|
|
+ 'game_id' => $siteInfo['game_id'],
|
|
|
|
|
+ 'agent_id'=> $siteInfo['agent_id'],
|
|
|
|
|
+ 'site_id' => $siteInfo['site_id'],
|
|
|
|
|
+ 'media_id' => $this->siteMap[$siteInfo['site_id']]['media_id'] ?? 0,
|
|
|
|
|
+ 'auth_id' => $this->siteMap[$siteInfo['site_id']]['auth_id'] ?? 0,
|
|
|
|
|
+ 'material_name' => $materialName,
|
|
|
|
|
+ 'material_url' => $materialUrl,
|
|
|
|
|
+ 'material_img' => $materialImg,
|
|
|
|
|
+ 'ori_money' => $cost,
|
|
|
|
|
+ 'money' => $cost*$fandianRate,
|
|
|
|
|
+ 'show' => $val['metrics']['show_cnt'],
|
|
|
|
|
+ 'click' => $val['metrics']['click_cnt'],
|
|
|
|
|
+ 'total_play' => $val['metrics']['total_play'],
|
|
|
|
|
+ 'valid_play' => $val['metrics']['valid_play'],
|
|
|
|
|
+ 'download' => $val['metrics']['download_finish_cnt'],
|
|
|
|
|
+ 'active' => $val['metrics']['active'],
|
|
|
|
|
+ 'register' => $val['metrics']['active_register'],
|
|
|
|
|
+ 'pay_count' => $val['metrics']['active_pay'],
|
|
|
|
|
+ 'pay_amount' => $val['metrics']['stat_pay_amount'],
|
|
|
|
|
+ 'author_id' => $authorId
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $where = [
|
|
|
|
|
+ 'tdate' => $this->date,
|
|
|
|
|
+ 'ad_id' => $adId,
|
|
|
|
|
+ 'material_id' => $materialId,
|
|
|
|
|
+ 'advertiser_id' => $account['advertiser_id'],
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $findId= $db->table($this->materialTable)->where($where)->value("id");
|
|
|
|
|
+ if($findId) {
|
|
|
|
|
+ $materialData['id'] = $findId;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 保存数据
|
|
|
|
|
+ $db->table($this->materialTable)->save(array_merge($materialData, $where));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|