| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace app\v1\logic\tool\advertCost;
- use GuzzleHttp\Client;
- use support\think\Db;
- class GdtCostHourLogic extends BaseAdvertLogic
- {
- // 执行正文
- protected function initStart(): void
- {
- $tokenMap = $this->getGdtTokenMap();
- $accountList = $this->getGdtAccountList();
- // 循环执行
- foreach ($accountList as $account)
- {
- $accessToken = $tokenMap[$account['pmid']] ?? "";
- if(!$accessToken) continue;
- // 获取消耗
- $dataList = $this->getGdtCost($account['advertiser_id'], $accessToken);
- // 整理数据入库
- $this->organizeDataList($account, $dataList);
- }
- }
- // 获取媒体消耗
- protected function getGdtCost($advertiserId, $accessToken): array
- {
- $page = 1;
- $data = [];
- do {
- $nonce = md5(time().rand(00000, 99999));
- $url = 'https://api.e.qq.com/v3.0/hourly_reports/get?access_token='.$accessToken.'×tamp='.time().'&nonce='.$nonce;
- $request_data = [
- 'account_id' => $advertiserId,
- 'level' => 'REPORT_LEVEL_ADGROUP',
- 'date_range' => json_encode(['start_date' => $this->date, 'end_date' => $this->date]),
- 'group_by' => json_encode(['hour', 'adgroup_id']),
- 'fields' => json_encode(['hour', 'adgroup_id', 'adgroup_name', 'view_count', 'valid_click_count', 'cost', 'activated_count']),
- 'page' => $page,
- 'page_size' => 100,
- ];
- $url = $url."&".http_build_query($request_data);
- $httpClient = new Client(['timeout' => 10]);
- $res = $httpClient->request('GET', $url);
- $result = json_decode($res->getBody(), true);
- if (empty($result['data']['list'])) {
- break;
- }
- $data = array_merge($data, $result['data']['list']);
- $totalPage = $result['data']['page_info']['total_page'] ?? 1;
- $page++;
- } while ($page <= $totalPage);
- return $data;
- }
- // 整理入库数据列表
- 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) {
- $adId = $val['adgroup_id'];
- $adName = $val['adgroup_name'];
- $cost = $val['cost']/100;
- if($cost==0) continue;
- $show = $val['view_count'];
- $click = $val['valid_click_count'];
- $convert = $val['activated_count'];
- // Todo 从广告名称中拆分归因数据
- $siteInfo = $this->getSiteInfo($adName);
- if(!$siteInfo['game_id'] || empty($this->siteMap[$siteInfo['site_id']])) continue;
- $where = [
- 'ad_id' => $adId,
- 'game_id' => $siteInfo['game_id'],
- 'agent_id'=> $siteInfo['agent_id'],
- 'site_id' => $siteInfo['site_id'],
- 'tdate' => $this->date,
- 'thour' => (int)$val['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[$siteInfo['site_id']]['media_id'],
- 'auth_id' => $this->siteMap[$siteInfo['site_id']]['auth_id'],
- ];
- $hourId= $db->table($this->hourTable)->where($where)->value("id");
- if($hourId) {
- $hourData['id'] = $hourId;
- }
- // 保存小时数据
- $db->table($this->hourTable)->save(array_merge($hourData, $where));
- // 计算天的数据
- $adKey = $siteInfo['game_id'] . "_" . $siteInfo['site_id'] . "_" . $adId;
- $adData[$adKey]['game_id'] = $siteInfo['game_id'];
- $adData[$adKey]['agent_id'] = $siteInfo['agent_id'];
- $adData[$adKey]['site_id'] = $siteInfo['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[$siteInfo['site_id']]['media_id'] ?? 0,
- 'auth_id' => $this->siteMap[$siteInfo['site_id']]['auth_id'] ?? 0,
- ];
- $dateId= $db->table($this->dateTable)->where($where)->value("id");
- if($dateId) {
- $dateData['id'] = $dateId;
- }
- // 保存日数据
- $db->table($this->dateTable)->save(array_merge($dateData, $where));
- }
- }
- }
|