GdtCostHourLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\v1\logic\tool\advertCost;
  3. use GuzzleHttp\Client;
  4. use support\think\Db;
  5. class GdtCostHourLogic extends BaseAdvertLogic
  6. {
  7. // 执行正文
  8. protected function initStart(): void
  9. {
  10. $tokenMap = $this->getGdtTokenMap();
  11. $accountList = $this->getGdtAccountList();
  12. // 循环执行
  13. foreach ($accountList as $account)
  14. {
  15. $accessToken = $tokenMap[$account['pmid']] ?? "";
  16. if(!$accessToken) continue;
  17. // 获取消耗
  18. $dataList = $this->getGdtCost($account['advertiser_id'], $accessToken);
  19. // 整理数据入库
  20. $this->organizeDataList($account, $dataList);
  21. }
  22. }
  23. // 获取媒体消耗
  24. protected function getGdtCost($advertiser_id, $accessToken): array
  25. {
  26. $page = 1;
  27. $data = [];
  28. do {
  29. $nonce = md5(time().rand(00000, 99999));
  30. $url = 'https://api.e.qq.com/v3.0/hourly_reports/get?access_token='.$accessToken.'&timestamp='.time().'&nonce='.$nonce;
  31. $request_data = [
  32. 'account_id' => $advertiser_id,
  33. 'level' => 'REPORT_LEVEL_ADGROUP',
  34. 'date_range' => json_encode(['start_date' => $this->date, 'end_date' => $this->date]),
  35. 'group_by' => json_encode(['hour', 'adgroup_id']),
  36. 'fields' => json_encode(['hour', 'adgroup_id', 'adgroup_name', 'view_count', 'valid_click_count', 'cost', 'activated_count']),
  37. 'page' => $page,
  38. 'page_size' => 100,
  39. ];
  40. $url = $url."&".http_build_query($request_data);
  41. $httpClient = new Client(['timeout' => 10]);
  42. $res = $httpClient->request('GET', $url);
  43. $result = json_decode($res->getBody(), true);
  44. if (empty($result['data']['list'])) {
  45. break;
  46. }
  47. $data = array_merge($data, $result['data']['list']);
  48. $totalPage = $result['data']['page_info']['total_page'] ?? 1;
  49. $page++;
  50. } while ($page <= $totalPage);
  51. return $data;
  52. }
  53. // 整理入库数据列表
  54. protected function organizeDataList($account, $dataList): void
  55. {
  56. if(!$dataList) return;
  57. $db = Db::connect('db_advert');
  58. // 返点率
  59. $fandianRate = ($account['son_fandian']>1) ? (1/$account['son_fandian']) : $account['son_fandian'];
  60. $adData = [];
  61. foreach ($dataList as $val) {
  62. $ad_id = $val['adgroup_id'];
  63. $ad_name = $val['adgroup_name'];
  64. $cost = $val['cost']/100;
  65. $show = $val['view_count'];
  66. $click = $val['valid_click_count'];
  67. $convert = $val['activated_count'];
  68. // Todo 从广告名称中拆分归因数据
  69. preg_match("/([\d]*)_([\d]*)_([\d]*)/", $ad_name, $matchs);
  70. $game_id = $matchs[1] ?? 0;
  71. $agent_id = $matchs[2] ?? 0;
  72. $site_id = $matchs[3] ?? 0;
  73. if(!$game_id || empty($this->siteMap[$site_id])) continue;
  74. $where = [
  75. 'ad_id' => $ad_id,
  76. 'game_id' => $game_id,
  77. 'agent_id'=> $agent_id,
  78. 'site_id' => $site_id,
  79. 'tdate' => $this->date,
  80. 'thour' => (int)$val['hour'],
  81. ];
  82. $hourData = [
  83. 'advertiser_id' => $account['advertiser_id'],
  84. 'ad_show' => $show,
  85. 'ad_click' => $click,
  86. 'ad_convert' => $convert,
  87. 'ori_money' => $cost,
  88. 'money' => $cost * $fandianRate,
  89. 'media_id' => $this->siteMap[$site_id]['media_id'],
  90. 'auth_id' => $this->siteMap[$site_id]['auth_id'],
  91. ];
  92. $hourId= $db->table($this->hourTable)->where($where)->value("id");
  93. if($hourId) {
  94. $hourData['id'] = $hourId;
  95. }
  96. // 保存小时数据
  97. $db->table($this->hourTable)->save(array_merge($hourData, $where));
  98. // 计算天的数据
  99. $adKey = $game_id . "_" . $site_id . "_" . $ad_id;
  100. $adData[$adKey]['game_id'] = $game_id;
  101. $adData[$adKey]['agent_id'] = $agent_id;
  102. $adData[$adKey]['site_id'] = $site_id;
  103. $adData[$adKey]['ad_id'] = $ad_id;
  104. $adData[$adKey]['ad_show'] = !empty($adData[$adKey]['ad_show']) ? $adData[$adKey]['ad_show']+$show : $show;
  105. $adData[$adKey]['ad_click'] = !empty($adData[$adKey]['ad_click']) ? $adData[$adKey]['ad_click']+$click : $click;
  106. $adData[$adKey]['ad_convert'] = !empty($adData[$adKey]['ad_convert']) ? $adData[$adKey]['ad_convert']+$convert : $convert;
  107. $adData[$adKey]['cost'] = !empty($adData[$adKey]['cost']) ? $adData[$adKey]['cost']+$cost : $cost;
  108. }
  109. foreach($adData as $value){
  110. $where = [
  111. 'ad_id' => $value['ad_id'],
  112. 'game_id' => $value['game_id'],
  113. 'agent_id'=> $value['agent_id'],
  114. 'site_id' => $value['site_id'],
  115. 'tdate' => $this->date,
  116. ];
  117. $dateData = [
  118. 'advertiser_id' => $account['advertiser_id'],
  119. 'ad_show' => $value['ad_show'],
  120. 'ad_click' => $value['ad_click'],
  121. 'ad_convert'=> $value['ad_convert'],
  122. 'ori_money' => $value['cost'],
  123. 'money' => $value['cost']*$fandianRate,
  124. 'media_id' => $this->siteMap[$site_id]['media_id'],
  125. 'auth_id' => $this->siteMap[$site_id]['auth_id'],
  126. ];
  127. $dateId= $db->table($this->dateTable)->where($where)->value("id");
  128. if($dateId) {
  129. $dateData['id'] = $dateId;
  130. }
  131. // 保存日数据
  132. $db->table($this->dateTable)->save(array_merge($dateData, $where));
  133. }
  134. }
  135. // 获取需要拉取消耗的媒体账户
  136. protected function getGdtAccountList(): array
  137. {
  138. $table = "ad_gdt_account_list";
  139. $where = [
  140. "status" => 1
  141. ];
  142. if($this->advertiserIds){
  143. $where['advertiser_id'] = $this->advertiserIds;
  144. }
  145. return Db::connect('db_advert')
  146. ->table($table)
  147. ->where($where)
  148. ->column("pmid, advertiser_id, advertiser_name, son_fandian");
  149. }
  150. protected function getGdtTokenMap(): array
  151. {
  152. $table = "ad_gdt_account";
  153. $where = [
  154. "status" => 1
  155. ];
  156. if($this->apiId){
  157. $where['id'] = $this->apiId;
  158. }
  159. return Db::connect('db_advert')->table($table)->where($where)->column("access_token", "id");
  160. }
  161. }