|
|
@@ -0,0 +1,213 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\v1\logic\tool\advertCost;
|
|
|
+
|
|
|
+use GuzzleHttp\Client;
|
|
|
+use support\think\Db;
|
|
|
+
|
|
|
+class TtCostHourLogic 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->getTtCost($account['advertiser_id'], $accessToken);
|
|
|
+
|
|
|
+ // 整理数据入库
|
|
|
+ $this->organizeDataList($account, $dataList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取媒体消耗
|
|
|
+ protected function getTtCost($advertiser_id, $accessToken): array
|
|
|
+ {
|
|
|
+ $page = 1;
|
|
|
+ $data = [];
|
|
|
+ do {
|
|
|
+ $url = 'https://ad.oceanengine.com/open_api/v3.0/report/custom/get/';
|
|
|
+ $request_data = [
|
|
|
+ 'advertiser_id'=>$advertiser_id,
|
|
|
+ 'dimensions'=>json_encode(["cdp_project_id","cdp_project_name","ad_platform_cdp_project_action_track_url","ad_platform_cdp_project_download_url","stat_time_hour"]),
|
|
|
+ 'metrics'=>json_encode(["stat_cost","show_cnt","click_cnt","convert_cnt"]),
|
|
|
+ 'filters'=>'[{"operator":4,"values":["0"],"field":"stat_cost","type":3}]',
|
|
|
+ 'start_time'=>$this->date,
|
|
|
+ 'end_time'=>$this->date,
|
|
|
+ 'order_by'=>json_encode([]),
|
|
|
+ 'page'=>$page,
|
|
|
+ '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 organizeDataList($account, $dataList): void
|
|
|
+ {
|
|
|
+ if(!$dataList) return;
|
|
|
+
|
|
|
+ $db = Db::connect('db_advert');
|
|
|
+ // 返点率
|
|
|
+ $fandianRate = ($account['son_fandian']>1) ? (1/$account['son_fandian']) : $account['son_fandian'];
|
|
|
+
|
|
|
+ $adData = [];
|
|
|
+ foreach ($dataList as $val) {
|
|
|
+ $cost = $val['metrics']['stat_cost'];
|
|
|
+ if($cost=='0.00') continue;
|
|
|
+
|
|
|
+ $ad_id = $val['dimensions']['cdp_project_id'];
|
|
|
+ $ad_name = $val['dimensions']['cdp_project_name'];
|
|
|
+ $show = $val['metrics']['show_cnt'];
|
|
|
+ $click = $val['metrics']['click_cnt'];
|
|
|
+ $convert = $val['metrics']['convert_cnt'];
|
|
|
+ $hour = (int)explode(' ',$val['dimensions']['stat_time_hour'])[1];
|
|
|
+
|
|
|
+ // Todo 从广告名称中拆分归因数据
|
|
|
+ preg_match("/([\d]*)_([\d]*)_([\d]*)/", $ad_name, $match);
|
|
|
+ $game_id = $match[1] ?? 0;
|
|
|
+ $agent_id = $match[2] ?? 0;
|
|
|
+ $site_id = $match[3] ?? 0;
|
|
|
+
|
|
|
+ // 广告名获取不到则获取监测链接或者下载链接
|
|
|
+ if(!$game_id || empty($this->siteMap[$site_id])){
|
|
|
+ $tjurl = $val['dimensions']['ad_platform_cdp_project_action_track_url'];
|
|
|
+ if(!$tjurl){
|
|
|
+ $tjurl = $val['dimensions']['ad_platform_cdp_project_download_url'];
|
|
|
+ }
|
|
|
+ preg_match("/([\d]*)_([\d]*)_([\d]*)/", $tjurl, $urlMatch);
|
|
|
+ $game_id = $urlMatch[1] ?? 0;
|
|
|
+ $agent_id = $urlMatch[2] ?? 0;
|
|
|
+ $site_id = $urlMatch[3] ?? 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!$game_id || empty($this->siteMap[$site_id])) continue;
|
|
|
+
|
|
|
+ $where = [
|
|
|
+ 'ad_id' => $ad_id,
|
|
|
+ 'game_id' => $game_id,
|
|
|
+ 'agent_id'=> $agent_id,
|
|
|
+ 'site_id' => $site_id,
|
|
|
+ 'tdate' => $this->date,
|
|
|
+ 'thour' => $hour,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $hourData = [
|
|
|
+ 'advertiser_id' => $account['advertiser_id'],
|
|
|
+ 'ad_show' => $show,
|
|
|
+ 'ad_click' => $click,
|
|
|
+ 'ad_convert' => $convert,
|
|
|
+ 'ori_money' => $cost,
|
|
|
+ 'money' => $cost * $fandianRate,
|
|
|
+ 'media_id' => $this->siteMap[$site_id]['media_id'],
|
|
|
+ 'auth_id' => $this->siteMap[$site_id]['auth_id'],
|
|
|
+ ];
|
|
|
+
|
|
|
+ $hourId= $db->table($this->hourTable)->where($where)->value("id");
|
|
|
+ if($hourId) {
|
|
|
+ $hourData['id'] = $hourId;
|
|
|
+ }
|
|
|
+ // 保存小时数据
|
|
|
+ $db->table($this->hourTable)->save(array_merge($hourData, $where));
|
|
|
+
|
|
|
+ // 计算天的数据
|
|
|
+ $adKey = $game_id . "_" . $site_id . "_" . $ad_id;
|
|
|
+ $adData[$adKey]['game_id'] = $game_id;
|
|
|
+ $adData[$adKey]['agent_id'] = $agent_id;
|
|
|
+ $adData[$adKey]['site_id'] = $site_id;
|
|
|
+ $adData[$adKey]['ad_id'] = $ad_id;
|
|
|
+ $adData[$adKey]['ad_show'] = !empty($adData[$adKey]['ad_show']) ? $adData[$adKey]['ad_show']+$show : $show;
|
|
|
+ $adData[$adKey]['ad_click'] = !empty($adData[$adKey]['ad_click']) ? $adData[$adKey]['ad_click']+$click : $click;
|
|
|
+ $adData[$adKey]['ad_convert'] = !empty($adData[$adKey]['ad_convert']) ? $adData[$adKey]['ad_convert']+$convert : $convert;
|
|
|
+ $adData[$adKey]['cost'] = !empty($adData[$adKey]['cost']) ? $adData[$adKey]['cost']+$cost : $cost;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach($adData as $value){
|
|
|
+ $where = [
|
|
|
+ 'ad_id' => $value['ad_id'],
|
|
|
+ 'game_id' => $value['game_id'],
|
|
|
+ 'agent_id'=> $value['agent_id'],
|
|
|
+ 'site_id' => $value['site_id'],
|
|
|
+ 'tdate' => $this->date,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $dateData = [
|
|
|
+ 'advertiser_id' => $account['advertiser_id'],
|
|
|
+ 'ad_show' => $value['ad_show'],
|
|
|
+ 'ad_click' => $value['ad_click'],
|
|
|
+ 'ad_convert'=> $value['ad_convert'],
|
|
|
+ 'ori_money' => $value['cost'],
|
|
|
+ 'money' => $value['cost']*$fandianRate,
|
|
|
+ 'media_id' => $this->siteMap[$site_id]['media_id'],
|
|
|
+ 'auth_id' => $this->siteMap[$site_id]['auth_id'],
|
|
|
+ ];
|
|
|
+
|
|
|
+ $dateId= $db->table($this->dateTable)->where($where)->value("id");
|
|
|
+ if($dateId) {
|
|
|
+ $dateData['id'] = $dateId;
|
|
|
+ }
|
|
|
+ // 保存日数据
|
|
|
+ $db->table($this->dateTable)->save(array_merge($dateData, $where));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取需要拉取消耗的媒体账户
|
|
|
+ protected function getTtAccountList(): array
|
|
|
+ {
|
|
|
+ $table = "ad_jrtt_account_list";
|
|
|
+ $where = [
|
|
|
+ "status" => 1
|
|
|
+ ];
|
|
|
+
|
|
|
+ if($this->advertiserIds){
|
|
|
+ $where['advertiser_id'] = $this->advertiserIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ return Db::connect('db_advert')
|
|
|
+ ->table($table)
|
|
|
+ ->where($where)
|
|
|
+ ->column("pmid, advertiser_id, advertiser_name, son_fandian");
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function getTtTokenMap(): array
|
|
|
+ {
|
|
|
+ $table = "ad_jrtt_account";
|
|
|
+ $where = [
|
|
|
+ "status" => 1
|
|
|
+ ];
|
|
|
+
|
|
|
+ if($this->apiId){
|
|
|
+ $where['id'] = $this->apiId;
|
|
|
+ }
|
|
|
+
|
|
|
+ return Db::connect('db_advert')->table($table)->where($where)->column("access_token", "id");
|
|
|
+ }
|
|
|
+}
|