GdtCostHourLogic.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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($advertiserId, $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' => $advertiserId,
  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. $adId = $val['adgroup_id'];
  63. $adName = $val['adgroup_name'];
  64. $cost = $val['cost']/100;
  65. if($cost==0) continue;
  66. $show = $val['view_count'];
  67. $click = $val['valid_click_count'];
  68. $convert = $val['activated_count'];
  69. // Todo 从广告名称中拆分归因数据
  70. $siteInfo = $this->getSiteInfo($adName);
  71. if(!$siteInfo['game_id'] || empty($this->siteMap[$siteInfo['site_id']])) continue;
  72. $where = [
  73. 'ad_id' => $adId,
  74. 'game_id' => $siteInfo['game_id'],
  75. 'agent_id'=> $siteInfo['agent_id'],
  76. 'site_id' => $siteInfo['site_id'],
  77. 'tdate' => $this->date,
  78. 'thour' => (int)$val['hour'],
  79. ];
  80. $hourData = [
  81. 'advertiser_id' => $account['advertiser_id'],
  82. 'ad_show' => $show,
  83. 'ad_click' => $click,
  84. 'ad_convert' => $convert,
  85. 'ori_money' => $cost,
  86. 'money' => $cost * $fandianRate,
  87. 'media_id' => $this->siteMap[$siteInfo['site_id']]['media_id'],
  88. 'auth_id' => $this->siteMap[$siteInfo['site_id']]['auth_id'],
  89. ];
  90. $hourId= $db->table($this->hourTable)->where($where)->value("id");
  91. if($hourId) {
  92. $hourData['id'] = $hourId;
  93. }
  94. // 保存小时数据
  95. $db->table($this->hourTable)->save(array_merge($hourData, $where));
  96. // 计算天的数据
  97. $adKey = $siteInfo['game_id'] . "_" . $siteInfo['site_id'] . "_" . $adId;
  98. $adData[$adKey]['game_id'] = $siteInfo['game_id'];
  99. $adData[$adKey]['agent_id'] = $siteInfo['agent_id'];
  100. $adData[$adKey]['site_id'] = $siteInfo['site_id'];
  101. $adData[$adKey]['ad_id'] = $adId;
  102. $adData[$adKey]['ad_show'] = !empty($adData[$adKey]['ad_show']) ? $adData[$adKey]['ad_show']+$show : $show;
  103. $adData[$adKey]['ad_click'] = !empty($adData[$adKey]['ad_click']) ? $adData[$adKey]['ad_click']+$click : $click;
  104. $adData[$adKey]['ad_convert'] = !empty($adData[$adKey]['ad_convert']) ? $adData[$adKey]['ad_convert']+$convert : $convert;
  105. $adData[$adKey]['cost'] = !empty($adData[$adKey]['cost']) ? $adData[$adKey]['cost']+$cost : $cost;
  106. }
  107. foreach($adData as $value){
  108. $where = [
  109. 'ad_id' => $value['ad_id'],
  110. 'game_id' => $value['game_id'],
  111. 'agent_id'=> $value['agent_id'],
  112. 'site_id' => $value['site_id'],
  113. 'tdate' => $this->date,
  114. ];
  115. $dateData = [
  116. 'advertiser_id' => $account['advertiser_id'],
  117. 'ad_show' => $value['ad_show'],
  118. 'ad_click' => $value['ad_click'],
  119. 'ad_convert'=> $value['ad_convert'],
  120. 'ori_money' => $value['cost'],
  121. 'money' => $value['cost']*$fandianRate,
  122. 'media_id' => $this->siteMap[$siteInfo['site_id']]['media_id'] ?? 0,
  123. 'auth_id' => $this->siteMap[$siteInfo['site_id']]['auth_id'] ?? 0,
  124. ];
  125. $dateId= $db->table($this->dateTable)->where($where)->value("id");
  126. if($dateId) {
  127. $dateData['id'] = $dateId;
  128. }
  129. // 保存日数据
  130. $db->table($this->dateTable)->save(array_merge($dateData, $where));
  131. }
  132. }
  133. }