小说推广数据系统

UserAttrAnalysis.php 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\Account;
  5. use App\Models\Order;
  6. use App\RedisModel;
  7. use Illuminate\Console\Command;
  8. class UserAttrAnalysis extends Command
  9. {
  10. protected $signature = 'UserAttrAnalysis';
  11. protected $description = '由订单数据进行用户属性分析';
  12. protected $page = 1; // 当前页码数
  13. protected $pageSize = 5000; // 每页显示条数
  14. public function handle()
  15. {
  16. \DB::connection()->disableQueryLog();
  17. $this->info(date('m-d H:i:s') . ' 开始整理');
  18. $this->analysis();
  19. $this->info(date('m-d H:i:s') . ' 整理结束');
  20. }
  21. private function analysis()
  22. {
  23. try {
  24. $accountList = RedisModel::getAfterDecode(Account::XM_ACCOUNT_LIST_CONFIRM);
  25. # 获取对应的平台类型ID和渠道ID
  26. $accountData = Account::getAccountInfo($accountList);
  27. foreach($accountList as $appId) {
  28. $accounts = $accountData->where('app_id', $appId);
  29. foreach($accounts as $accountInfo) {
  30. if(
  31. isset($accountInfo->platform_id) && isset($accountInfo->channel_id) &&
  32. !empty($accountInfo->platform_id) && !empty($accountInfo->channel_id)
  33. ) {
  34. $this->page = 1;
  35. do{
  36. # 统计账号粉丝的充值数据
  37. $data = Order::userAnalysis(
  38. $accountInfo->platform_id, $accountInfo->channel_id, $this->page, $this->pageSize
  39. );
  40. $count = $data->count();
  41. $this->info('本次共获取'.$count.'条数据');
  42. if($count) {
  43. # 存入Redis供消费
  44. RedisModel::lPush(Order::USER_ATTR_DATA, json_encode(['app_id' => $appId, 'data' => $data->toArray()]));
  45. } else {
  46. Log::logError('未查询到订单信息', [
  47. 'app_id' => $appId,
  48. 'platform_id' => $accountInfo->platform_id,
  49. 'channel_id' => $accountInfo->channel_id,
  50. 'page' => $this->page
  51. ], 'UserAttrAnalysis-account');
  52. }
  53. $this->page++;
  54. } while($count == $this->pageSize);
  55. } else {
  56. Log::logError('账号的渠道信息获取失败', [
  57. 'app_id' => $appId,
  58. 'account_info' => $accountInfo
  59. ], 'UserAttrAnalysis');
  60. }
  61. }
  62. }
  63. } catch (\Exception $e) {
  64. Log::logError('账号的粉丝数据分析失败', [
  65. 'msg' => $e->getMessage(),
  66. 'line' => $e->getLine()
  67. ], 'UserAttrAnalysis-Exception');
  68. }
  69. }
  70. }