TtCostHourLogic.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace app\v1\logic\tool\advertCost;
  3. use app\adapter\ToutiaoAdapter;
  4. use GuzzleHttp\Client;
  5. use support\think\Db;
  6. class TtCostHourLogic extends BaseAdvertLogic
  7. {
  8. // 执行正文
  9. protected function initStart(): void
  10. {
  11. $tokenMap = $this->getTtTokenMap();
  12. $accountList = $this->getTtAccountList();
  13. // 循环执行
  14. foreach ($accountList as $account)
  15. {
  16. $accessToken = $tokenMap[$account['pmid']] ?? "";
  17. if(!$accessToken) continue;
  18. // 获取消耗
  19. $dataList = (new ToutiaoAdapter)->getHourlyCost($account['advertiser_id'], $accessToken, $this->date);
  20. // 整理数据入库
  21. $this->organizeDataList($account, $dataList);
  22. }
  23. }
  24. // 整理入库数据列表
  25. protected function organizeDataList($account, $dataList): void
  26. {
  27. if(!$dataList) return;
  28. $db = Db::connect('db_advert');
  29. // 返点率
  30. $fandianRate = ($account['son_fandian']>1) ? (1/$account['son_fandian']) : $account['son_fandian'];
  31. $adData = [];
  32. foreach ($dataList as $val) {
  33. $cost = $val['metrics']['stat_cost'];
  34. if($cost=='0.00') continue;
  35. $adId = $val['dimensions']['cdp_project_id'];
  36. $adName = $val['dimensions']['cdp_project_name'];
  37. $show = $val['metrics']['show_cnt'];
  38. $click = $val['metrics']['click_cnt'];
  39. $convert = $val['metrics']['convert_cnt'];
  40. $hour = (int)explode(' ',$val['dimensions']['stat_time_hour'])[1];
  41. // Todo 从广告名称中拆分归因数据
  42. $siteInfo = $this->getSiteInfo($adName);
  43. $game_id = $siteInfo['game_id'] ?? 0;
  44. $agent_id = $siteInfo['agent_id'] ?? 0;
  45. $site_id = $siteInfo['site_id'] ?? 0;
  46. // 广告名获取不到则获取监测链接或者下载链接
  47. if(!$game_id || empty($this->siteMap[$site_id])){
  48. $tjurl = $val['dimensions']['ad_platform_cdp_project_action_track_url'];
  49. if(!$tjurl){
  50. $tjurl = $val['dimensions']['ad_platform_cdp_project_download_url'];
  51. }
  52. $siteInfo = $this->getSiteInfo($tjurl);
  53. $game_id = $siteInfo['game_id'] ?? 0;
  54. $agent_id = $siteInfo['agent_id'] ?? 0;
  55. $site_id = $siteInfo['site_id'] ?? 0;
  56. }
  57. if(!$game_id || empty($this->siteMap[$site_id])) continue;
  58. $where = [
  59. 'ad_id' => $adId,
  60. 'game_id' => $game_id,
  61. 'agent_id'=> $agent_id,
  62. 'site_id' => $site_id,
  63. 'tdate' => $this->date,
  64. 'thour' => $hour,
  65. ];
  66. $hourData = [
  67. 'advertiser_id' => $account['advertiser_id'],
  68. 'ad_show' => $show,
  69. 'ad_click' => $click,
  70. 'ad_convert' => $convert,
  71. 'ori_money' => $cost,
  72. 'money' => $cost * $fandianRate,
  73. 'media_id' => $this->siteMap[$site_id]['media_id'] ?? 0,
  74. 'auth_id' => $this->siteMap[$site_id]['auth_id'] ?? 0,
  75. ];
  76. $hourId= $db->table($this->hourTable)->where($where)->value("id");
  77. if($hourId) {
  78. $hourData['id'] = $hourId;
  79. }
  80. // 保存小时数据
  81. $db->table($this->hourTable)->save(array_merge($hourData, $where));
  82. // 计算天的数据
  83. $adKey = $game_id . "_" . $site_id . "_" . $adId;
  84. $adData[$adKey]['game_id'] = $game_id;
  85. $adData[$adKey]['agent_id'] = $agent_id;
  86. $adData[$adKey]['site_id'] = $site_id;
  87. $adData[$adKey]['ad_id'] = $adId;
  88. $adData[$adKey]['ad_show'] = !empty($adData[$adKey]['ad_show']) ? $adData[$adKey]['ad_show']+$show : $show;
  89. $adData[$adKey]['ad_click'] = !empty($adData[$adKey]['ad_click']) ? $adData[$adKey]['ad_click']+$click : $click;
  90. $adData[$adKey]['ad_convert'] = !empty($adData[$adKey]['ad_convert']) ? $adData[$adKey]['ad_convert']+$convert : $convert;
  91. $adData[$adKey]['cost'] = !empty($adData[$adKey]['cost']) ? $adData[$adKey]['cost']+$cost : $cost;
  92. }
  93. foreach($adData as $value){
  94. $where = [
  95. 'ad_id' => $value['ad_id'],
  96. 'game_id' => $value['game_id'],
  97. 'agent_id'=> $value['agent_id'],
  98. 'site_id' => $value['site_id'],
  99. 'tdate' => $this->date,
  100. ];
  101. $dateData = [
  102. 'advertiser_id' => $account['advertiser_id'],
  103. 'ad_show' => $value['ad_show'],
  104. 'ad_click' => $value['ad_click'],
  105. 'ad_convert'=> $value['ad_convert'],
  106. 'ori_money' => $value['cost'],
  107. 'money' => $value['cost']*$fandianRate,
  108. 'media_id' => $this->siteMap[$site_id]['media_id'],
  109. 'auth_id' => $this->siteMap[$site_id]['auth_id'],
  110. ];
  111. $dateId= $db->table($this->dateTable)->where($where)->value("id");
  112. if($dateId) {
  113. $dateData['id'] = $dateId;
  114. }
  115. // 保存日数据
  116. $db->table($this->dateTable)->save(array_merge($dateData, $where));
  117. }
  118. }
  119. // 获取需要拉取消耗的媒体账户
  120. protected function getTtAccountList(): array
  121. {
  122. $table = "ad_jrtt_account_list";
  123. $where = [
  124. "status" => 1
  125. ];
  126. if($this->advertiserIds){
  127. $where['advertiser_id'] = $this->advertiserIds;
  128. }
  129. return Db::connect('db_advert')
  130. ->table($table)
  131. ->where($where)
  132. ->column("pmid, advertiser_id, advertiser_name, son_fandian");
  133. }
  134. protected function getTtTokenMap(): array
  135. {
  136. $table = "ad_jrtt_account";
  137. $where = [
  138. "status" => 1
  139. ];
  140. if($this->apiId){
  141. $where['id'] = $this->apiId;
  142. }
  143. return Db::connect('db_advert')->table($table)->where($where)->column("access_token", "id");
  144. }
  145. }