TtCostHourLogic.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\v1\logic\tool\advertCost;
  3. use GuzzleHttp\Client;
  4. use support\think\Db;
  5. class TtCostHourLogic extends BaseAdvertLogic
  6. {
  7. // 执行正文
  8. protected function initStart(): void
  9. {
  10. $tokenMap = $this->getTtTokenMap();
  11. $accountList = $this->getTtAccountList();
  12. // 循环执行
  13. foreach ($accountList as $account)
  14. {
  15. $accessToken = $tokenMap[$account['pmid']] ?? "";
  16. if(!$accessToken) continue;
  17. // 获取消耗
  18. $dataList = $this->getTtCost($account['advertiser_id'], $accessToken);
  19. // 整理数据入库
  20. $this->organizeDataList($account, $dataList);
  21. }
  22. }
  23. // 获取媒体消耗
  24. protected function getTtCost($advertiser_id, $accessToken): array
  25. {
  26. $page = 1;
  27. $data = [];
  28. do {
  29. $url = 'https://ad.oceanengine.com/open_api/v3.0/report/custom/get/';
  30. $request_data = [
  31. 'advertiser_id'=>$advertiser_id,
  32. '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"]),
  33. 'metrics'=>json_encode(["stat_cost","show_cnt","click_cnt","convert_cnt"]),
  34. 'filters'=>'[{"operator":4,"values":["0"],"field":"stat_cost","type":3}]',
  35. 'start_time'=>$this->date,
  36. 'end_time'=>$this->date,
  37. 'order_by'=>json_encode([]),
  38. 'page'=>$page,
  39. 'page_size'=>100,
  40. ];
  41. $options = [
  42. "headers" => [
  43. 'Access-Token' => $accessToken,
  44. 'Content-Type' => 'application/json',
  45. ]
  46. ];
  47. $url = $url."?".http_build_query($request_data);
  48. $httpClient = new Client(['timeout' => 10]);
  49. $res = $httpClient->request('GET', $url, $options);
  50. $result = json_decode($res->getBody(), true);
  51. if (empty($result['data']['rows'])) {
  52. break;
  53. }
  54. $data = array_merge($data, $result['data']['rows']);
  55. $totalPage = $result['data']['page_info']['total_page'] ?? 1;
  56. $page++;
  57. } while ($page <= $totalPage);
  58. return $data;
  59. }
  60. // 整理入库数据列表
  61. protected function organizeDataList($account, $dataList): void
  62. {
  63. if(!$dataList) return;
  64. $db = Db::connect('db_advert');
  65. // 返点率
  66. $fandianRate = ($account['son_fandian']>1) ? (1/$account['son_fandian']) : $account['son_fandian'];
  67. $adData = [];
  68. foreach ($dataList as $val) {
  69. $cost = $val['metrics']['stat_cost'];
  70. if($cost=='0.00') continue;
  71. $ad_id = $val['dimensions']['cdp_project_id'];
  72. $ad_name = $val['dimensions']['cdp_project_name'];
  73. $show = $val['metrics']['show_cnt'];
  74. $click = $val['metrics']['click_cnt'];
  75. $convert = $val['metrics']['convert_cnt'];
  76. $hour = (int)explode(' ',$val['dimensions']['stat_time_hour'])[1];
  77. // Todo 从广告名称中拆分归因数据
  78. preg_match("/([\d]*)_([\d]*)_([\d]*)/", $ad_name, $match);
  79. $game_id = $match[1] ?? 0;
  80. $agent_id = $match[2] ?? 0;
  81. $site_id = $match[3] ?? 0;
  82. // 广告名获取不到则获取监测链接或者下载链接
  83. if(!$game_id || empty($this->siteMap[$site_id])){
  84. $tjurl = $val['dimensions']['ad_platform_cdp_project_action_track_url'];
  85. if(!$tjurl){
  86. $tjurl = $val['dimensions']['ad_platform_cdp_project_download_url'];
  87. }
  88. preg_match("/([\d]*)_([\d]*)_([\d]*)/", $tjurl, $urlMatch);
  89. $game_id = $urlMatch[1] ?? 0;
  90. $agent_id = $urlMatch[2] ?? 0;
  91. $site_id = $urlMatch[3] ?? 0;
  92. }
  93. if(!$game_id || empty($this->siteMap[$site_id])) continue;
  94. $where = [
  95. 'ad_id' => $ad_id,
  96. 'game_id' => $game_id,
  97. 'agent_id'=> $agent_id,
  98. 'site_id' => $site_id,
  99. 'tdate' => $this->date,
  100. 'thour' => $hour,
  101. ];
  102. $hourData = [
  103. 'advertiser_id' => $account['advertiser_id'],
  104. 'ad_show' => $show,
  105. 'ad_click' => $click,
  106. 'ad_convert' => $convert,
  107. 'ori_money' => $cost,
  108. 'money' => $cost * $fandianRate,
  109. 'media_id' => $this->siteMap[$site_id]['media_id'],
  110. 'auth_id' => $this->siteMap[$site_id]['auth_id'],
  111. ];
  112. $hourId= $db->table($this->hourTable)->where($where)->value("id");
  113. if($hourId) {
  114. $hourData['id'] = $hourId;
  115. }
  116. // 保存小时数据
  117. $db->table($this->hourTable)->save(array_merge($hourData, $where));
  118. // 计算天的数据
  119. $adKey = $game_id . "_" . $site_id . "_" . $ad_id;
  120. $adData[$adKey]['game_id'] = $game_id;
  121. $adData[$adKey]['agent_id'] = $agent_id;
  122. $adData[$adKey]['site_id'] = $site_id;
  123. $adData[$adKey]['ad_id'] = $ad_id;
  124. $adData[$adKey]['ad_show'] = !empty($adData[$adKey]['ad_show']) ? $adData[$adKey]['ad_show']+$show : $show;
  125. $adData[$adKey]['ad_click'] = !empty($adData[$adKey]['ad_click']) ? $adData[$adKey]['ad_click']+$click : $click;
  126. $adData[$adKey]['ad_convert'] = !empty($adData[$adKey]['ad_convert']) ? $adData[$adKey]['ad_convert']+$convert : $convert;
  127. $adData[$adKey]['cost'] = !empty($adData[$adKey]['cost']) ? $adData[$adKey]['cost']+$cost : $cost;
  128. }
  129. foreach($adData as $value){
  130. $where = [
  131. 'ad_id' => $value['ad_id'],
  132. 'game_id' => $value['game_id'],
  133. 'agent_id'=> $value['agent_id'],
  134. 'site_id' => $value['site_id'],
  135. 'tdate' => $this->date,
  136. ];
  137. $dateData = [
  138. 'advertiser_id' => $account['advertiser_id'],
  139. 'ad_show' => $value['ad_show'],
  140. 'ad_click' => $value['ad_click'],
  141. 'ad_convert'=> $value['ad_convert'],
  142. 'ori_money' => $value['cost'],
  143. 'money' => $value['cost']*$fandianRate,
  144. 'media_id' => $this->siteMap[$site_id]['media_id'],
  145. 'auth_id' => $this->siteMap[$site_id]['auth_id'],
  146. ];
  147. $dateId= $db->table($this->dateTable)->where($where)->value("id");
  148. if($dateId) {
  149. $dateData['id'] = $dateId;
  150. }
  151. // 保存日数据
  152. $db->table($this->dateTable)->save(array_merge($dateData, $where));
  153. }
  154. }
  155. // 获取需要拉取消耗的媒体账户
  156. protected function getTtAccountList(): array
  157. {
  158. $table = "ad_jrtt_account_list";
  159. $where = [
  160. "status" => 1
  161. ];
  162. if($this->advertiserIds){
  163. $where['advertiser_id'] = $this->advertiserIds;
  164. }
  165. return Db::connect('db_advert')
  166. ->table($table)
  167. ->where($where)
  168. ->column("pmid, advertiser_id, advertiser_name, son_fandian");
  169. }
  170. protected function getTtTokenMap(): array
  171. {
  172. $table = "ad_jrtt_account";
  173. $where = [
  174. "status" => 1
  175. ];
  176. if($this->apiId){
  177. $where['id'] = $this->apiId;
  178. }
  179. return Db::connect('db_advert')->table($table)->where($where)->column("access_token", "id");
  180. }
  181. }