| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace app\v1\logic\tool\advertCost;
- use app\adapter\ToutiaoAdapter;
- use support\think\Db;
- class ToutiaoCostHourLogic extends BaseAdvertLogic
- {
- public int $mediaId = 1; // 媒体ID
- // 执行正文
- protected function initStart(): void
- {
- $advertiserList = $this->getAdvertiserMap($this->mediaId);
- // 循环执行
- foreach ($advertiserList as $account)
- {
- $accessToken = $account['access_token'] ?? "";
- if(!$accessToken) continue;
- // 获取消耗
- $dataList = (new ToutiaoAdapter)->getHourlyCost($account['advertiser_id'], $accessToken, $this->date);
- // 整理数据入库
- $this->organizeDataList($account, $dataList);
- }
- }
- // 整理入库数据列表
- 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;
- $adId = $val['dimensions']['cdp_project_id'];
- $adName = $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 从广告名称中拆分归因数据
- $siteInfo = $this->getSiteInfo($adName);
- $game_id = $siteInfo['game_id'] ?? 0;
- $agent_id = $siteInfo['agent_id'] ?? 0;
- $site_id = $siteInfo['site_id'] ?? 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'];
- }
- $siteInfo = $this->getSiteInfo($tjurl);
- $game_id = $siteInfo['game_id'] ?? 0;
- $agent_id = $siteInfo['agent_id'] ?? 0;
- $site_id = $siteInfo['site_id'] ?? 0;
- }
- if(!$game_id || empty($this->siteMap[$site_id])) continue;
- $where = [
- 'ad_id' => $adId,
- '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'] ?? 0,
- 'auth_id' => $this->siteMap[$site_id]['auth_id'] ?? 0,
- ];
- $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 . "_" . $adId;
- $adData[$adKey]['game_id'] = $game_id;
- $adData[$adKey]['agent_id'] = $agent_id;
- $adData[$adKey]['site_id'] = $site_id;
- $adData[$adKey]['ad_id'] = $adId;
- $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");
- }
- }
|