小说推广数据系统

YouZiPlayletPromoteList.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\VpAccount;
  5. use App\Models\VpPlayletCampaign;
  6. use App\RedisModel;
  7. use App\Services\HttpService;
  8. use App\Services\YouZiService;
  9. use Illuminate\Console\Command;
  10. class YouZiPlayletPromoteList extends Command
  11. {
  12. protected $signature = 'YouZiPlayletPromoteList {accountLabel?}';
  13. protected $description = '柚子短剧推广列表';
  14. protected $year;
  15. protected $page = 1;
  16. protected $perPage = 200;
  17. protected $accountLabel;
  18. protected $platformId;
  19. public function handle()
  20. {
  21. \DB::connection()->disableQueryLog();
  22. $this->accountLabel = $this->argument('accountLabel') ? $this->argument('accountLabel') : 0;
  23. $this->info('本次查询的账号对应键值为:' . $this->accountLabel);
  24. $this->getPromoteList();
  25. }
  26. private function getPromoteList()
  27. {
  28. # 获取账号列表
  29. $fields = ['ma_app_id', 'app_id', 'service_type'];
  30. if($this->accountLabel == 1) {
  31. $this->platformId = 3;
  32. } else {
  33. $this->platformId = 1;
  34. }
  35. $accountList = VpAccount::getAccountList($this->platformId, $fields, []);
  36. foreach ($accountList as $item) {
  37. $maAppId = isset($item->ma_app_id) ? $item->ma_app_id : null;
  38. $appId = isset($item->app_id) ? $item->app_id : null;
  39. $this->info('本次处理账号appId【'.$appId.'】');
  40. if(empty($maAppId) || empty($appId)) {
  41. Log::logError('账号数据存在异常', $item, 'YouZiPlayletPromoteList');
  42. continue;
  43. }
  44. $serviceType = isset($item->service_type) ? $item->service_type : 0;
  45. $this->info('该账号ServiceType【'.$serviceType.'】');
  46. if(!in_array($serviceType, YouZiService::ACCOUNT_SERVICE_TYPE)) {
  47. Log::logError('ServiceType非法', $item, 'YouZiPlayletPromoteList');
  48. continue;
  49. }
  50. $this->getCampaign($appId, $maAppId, $serviceType);
  51. }
  52. }
  53. public function getCampaign($appId, $maAppId, $serviceType, $retry=0)
  54. {
  55. try {
  56. $requestUri = YouZiService::BASE_URI . YouZiService::CAMPAIGN_LIST_URI;
  57. $this->page = 1;
  58. do{
  59. $this->info('公众号:'.$appId);
  60. $this->info('当前页码数:'.$this->page);
  61. $this->info('每页显示条数:'.$this->perPage);
  62. $param = [
  63. 'dbSorted' => array([
  64. 'fieldName' => 'createdTs',
  65. 'order' => 'ASC'
  66. ]),
  67. 'page' => $this->page,
  68. 'limit' => $this->perPage,
  69. 'beginCreatedTs' => '',
  70. 'endCreatedTs' => ''
  71. ];
  72. # 获取令牌并拼装Header
  73. $accessToken = YouZiService::getAccessToken($this->accountLabel);
  74. if(empty($accessToken) && $retry < 5) {
  75. # 清除已缓存的AccessToken
  76. RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
  77. $retry++;
  78. $this->getCampaign($appId, $maAppId, $serviceType, $retry);
  79. }
  80. $headers = [
  81. 'accesstoken: ' . $accessToken,
  82. 'maappid: ' . $maAppId,
  83. 'mpappid: ' . $appId
  84. ];
  85. # 获取列表
  86. $response = HttpService::httpPost($requestUri, json_encode($param), true, $headers);
  87. $responseData = json_decode($response, True);
  88. if(isset($responseData['code']) && $responseData['code'] == -10001 && $retry < 5) {
  89. # 清除已缓存的AccessToken
  90. RedisModel::del(YouZiService::ACCESS_TOKEN_RDS_KEY . '_' .$this->accountLabel);
  91. $retry++;
  92. $this->getCampaign($appId, $maAppId, $serviceType, $retry);
  93. }
  94. # 是否存在合法数据
  95. $totalPage = isset($responseData['data']['totalPage']) ? $responseData['data']['totalPage'] : 0;
  96. $data = isset($responseData['data']['items']) ? $responseData['data']['items'] : [];
  97. $platformId = $this->platformId;
  98. # 检出数据
  99. foreach ($data as $datum) {
  100. $enable = 1;
  101. $isDeleted = isset($datum['isDeleted']) ? $datum['isDeleted'] : 0;
  102. if($isDeleted) $enable = -1;
  103. $playletActivityId = isset($datum['playletActivityId']) ? $datum['playletActivityId'] : null;
  104. if(empty($playletActivityId)) {
  105. Log::logError('短剧活动ID获取异常', $datum, 'YouZiPlayletPromoteList');
  106. return false;
  107. }
  108. // appID/platformId
  109. $platformCreatedTime = isset($datum['createdTs']) ? date('Y-m-d H:i:s', round($datum['createdTs'] / 1000)) : null;
  110. $requestTimestamp = strtotime($platformCreatedTime);
  111. if($requestTimestamp === false ) $requestTimestamp = time();
  112. $year = date('Y', $requestTimestamp);
  113. $playletName = isset($datum['name']) ? $datum['name'] : null;
  114. $playletPromoteDate = getDateFromPlayletTitle($year, $playletName);
  115. if($playletPromoteDate < $platformCreatedTime) { // 兼容跨年
  116. $playletPromoteDate = getDateFromPlayletTitle($year+1, $playletName);
  117. }
  118. $insertData = [
  119. 'created_id' => isset($datum['createdId']) ? $datum['createdId'] : null,
  120. 'platform_created_at' => isset($datum['createdTs']) ? date('Y-m-d H:i:s', round($datum['createdTs'] / 1000)) : null,
  121. 'updated_id' => isset($datum['updatedId']) ? $datum['updatedId'] : null,
  122. 'platform_updated_at' => isset($datum['updatedTs']) ? date('Y-m-d H:i:s', round($datum['updatedTs'] / 1000)) : null,
  123. 'ma_app_id' => isset($datum['maAppId']) ? $datum['maAppId'] : null,
  124. 'dp_admin_id' => isset($datum['dpAdminId']) ? $datum['dpAdminId'] : null,
  125. 'promote_date' => $playletPromoteDate,
  126. 'name' => $playletName,
  127. 'enable' => $enable,
  128. 'playlet_id' => isset($datum['playletId']) ? $datum['playletId'] : null,
  129. 'playlet_title' => isset($datum['playletTitle']) ? $datum['playletTitle'] : null,
  130. 'playlet_cover_img' => isset($datum['playletCoverImg']) ? $datum['playletCoverImg'] : null,
  131. 'playlet_info' => isset($datum['playlet']) ? json_encode($datum['playlet']) : null,
  132. 'mp_follow_tip_type' => isset($datum['mpFollowTipType']) ? $datum['mpFollowTipType'] : null,
  133. 'start_episode_index' => isset($datum['startEpisodeIndex']) ? $datum['startEpisodeIndex'] : null,
  134. 'mp_follow_tip_episode_index' => isset($datum['mpFollowTipEpisodeIndex']) ? $datum['mpFollowTipEpisodeIndex'] : null,
  135. 'cost' => isset($datum['cost']) ? $datum['cost'] : null,
  136. 'copy_id' => isset($datum['copyId']) ? $datum['copyId'] : null,
  137. 'pay_money' => isset($datum['payMoney']) ? $datum['payMoney'] : null,
  138. 'today_pay_money' => isset($datum['todayPayMoney']) ? $datum['todayPayMoney'] : null,
  139. 'view_pv' => isset($datum['viewPv']) ? $datum['viewPv'] : null,
  140. 'view_uv' => isset($datum['viewUv']) ? $datum['viewUv'] : null,
  141. 'pay_uv' => isset($datum['payUv']) ? $datum['payUv'] : null,
  142. 'pay_pv' => isset($datum['payPv']) ? $datum['payPv'] : null,
  143. 'today_pay_pv' => isset($datum['todayPayPv']) ? $datum['todayPayPv'] : null,
  144. 'today_pay_uv' => isset($datum['todayPayUv']) ? $datum['todayPayUv'] : null,
  145. 'subscribe_count' => isset($datum['subscribeCount']) ? $datum['subscribeCount'] : null
  146. ];
  147. # 判断是否存在数据,存在则不更新推广日期
  148. $isExist = VpPlayletCampaign::where('platform_id', $platformId)->where('app_id', $appId)->where('playlet_activity_id', $playletActivityId)->exists();
  149. if($isExist) unset($insertData['promote_date']);
  150. VpPlayletCampaign::updateOrCreate(
  151. ['platform_id'=>$platformId, 'app_id' => $appId, 'playlet_activity_id' => $playletActivityId], $insertData
  152. );
  153. }
  154. $this->page++;
  155. $this->info('本次获取数据条数'.$totalPage);
  156. } while($this->page <= $totalPage);
  157. } catch (\Exception $e) {
  158. Log::logError('短剧推广列表获取异常', [
  159. 'line' => $e->getLine(),
  160. 'msg' => $e->getMessage()
  161. ], 'YouZiPlayletPromoteList');
  162. return false;
  163. }
  164. return true;
  165. }
  166. }