Ei kuvausta

LiveInfoDataInDb.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Console\Commands\Star;
  3. use App\Models\JxStarLiveInfoList;
  4. use App\Services\RdsLockService;
  5. use App\Support\Log;
  6. use App\Support\RedisModel;
  7. use Illuminate\Console\Command;
  8. class LiveInfoDataInDb extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'Star:LiveInfoDataInDb';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '星直播直播数据入库';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. $this->info(date('H:i') . ' 开始执行');
  39. $this->start();
  40. $this->info(date('H:i') . ' 结束执行');
  41. return 0;
  42. }
  43. public function start()
  44. {
  45. $curTime = time();
  46. while (true) {
  47. if ((time() - $curTime) >= 60) break;
  48. $dataJson = RedisModel::rPop(JxStarLiveInfoList::JUXING_STAR_LIVE_INFO_INDB_LIST);
  49. if (empty($dataJson)) {
  50. sleep(2);
  51. continue;
  52. }
  53. $data = json_decode($dataJson, true);
  54. if (!isset($data['order_id'], $data['data'])) {
  55. # 输出错误日志
  56. Log::error('数据参数不合法', $data, 'Star:LiveInfoDataInDb');
  57. continue;
  58. }
  59. $this->infoDataInDb($data);
  60. }
  61. }
  62. public function infoDataInDb($data)
  63. {
  64. $orderId = $data['order_id'];
  65. $dataVal = $data['data'];
  66. $liveStreamId = $dataVal['live_stream_id'] ?? null;
  67. if (empty($liveStreamId)) {
  68. # 输出错误日志
  69. Log::error('liveStreamId 为空', $data, 'Star:LiveInfoDataInDb');
  70. return false;
  71. }
  72. $lockKey = RdsLockService::getRdsLockKey([
  73. 'starLive', $orderId, $liveStreamId
  74. ]);
  75. if (RdsLockService::addRdsLock($lockKey)) {
  76. $liveInfo = JxStarLiveInfoList::query()
  77. ->where('order_id', $orderId)
  78. ->where('live_stream_id', $liveStreamId)
  79. ->where('enable', 1)
  80. ->first();
  81. if (empty($liveInfo)) {
  82. $liveInfo = new JxStarLiveInfoList([
  83. 'order_id' => $orderId,
  84. 'live_stream_id' => $liveStreamId
  85. ]);
  86. }
  87. $liveInfo->live_duration = $dataVal['live_duration'] ?? 0;
  88. $liveInfo->live_max_online_cnt = $dataVal['live_max_online_cnt'] ?? 0;
  89. $liveInfo->item_show_cnt = $dataVal['item_show_cnt'] ?? 0;
  90. $liveInfo->item_click_cnt = $dataVal['item_click_cnt'] ?? 0;
  91. $liveInfo->item_click_rate = $dataVal['item_click_rate'] ?? 0;
  92. $liveInfo->sale_product_cnt = $dataVal['sale_product_cnt'] ?? 0;
  93. $liveInfo->sale_amount = $dataVal['sale_amount'] ?? 0;
  94. $liveInfo->click_age_percentage = isset($dataVal['click_dim_portrait_info']['age_percentage'])
  95. ? json_encode($dataVal['click_dim_portrait_info']['age_percentage']) : null;
  96. $liveInfo->click_interest_percentage = isset($dataVal['click_dim_portrait_info']['interest_percentage'])
  97. ? json_encode($dataVal['click_dim_portrait_info']['interest_percentage']) : null;
  98. $liveInfo->click_active_percentage = isset($dataVal['click_dim_portrait_info']['active_percentage'])
  99. ? json_encode($dataVal['click_dim_portrait_info']['active_percentage']) : null;
  100. $liveInfo->click_mobile_percentage = isset($dataVal['click_dim_portrait_info']['mobile_percentage'])
  101. ? json_encode($dataVal['click_dim_portrait_info']['mobile_percentage']) : null;
  102. $liveInfo->click_gender_percentage = isset($dataVal['click_dim_portrait_info']['gender_percentage'])
  103. ? json_encode($dataVal['click_dim_portrait_info']['gender_percentage']) : null;
  104. $liveInfo->click_area_percentage = isset($dataVal['click_dim_portrait_info']['area_percentage'])
  105. ? json_encode($dataVal['click_dim_portrait_info']['area_percentage']) : null;
  106. $liveInfo->play_age_percentage = isset($dataVal['play_dim_portrait_info']['age_percentage'])
  107. ? json_encode($dataVal['play_dim_portrait_info']['age_percentage']) : null;
  108. $liveInfo->play_interest_percentage = isset($dataVal['play_dim_portrait_info']['interest_percentage'])
  109. ? json_encode($dataVal['play_dim_portrait_info']['interest_percentage']) : null;
  110. $liveInfo->play_active_percentage = isset($dataVal['play_dim_portrait_info']['active_percentage'])
  111. ? json_encode($dataVal['play_dim_portrait_info']['active_percentage']) : null;
  112. $liveInfo->play_mobile_percentage = isset($dataVal['play_dim_portrait_info']['mobile_percentage'])
  113. ? json_encode($dataVal['play_dim_portrait_info']['mobile_percentage']) : null;
  114. $liveInfo->play_gender_percentage = isset($dataVal['play_dim_portrait_info']['gender_percentage'])
  115. ? json_encode($dataVal['play_dim_portrait_info']['gender_percentage']) : null;
  116. $liveInfo->play_area_percentage = isset($dataVal['play_dim_portrait_info']['area_percentage'])
  117. ? json_encode($dataVal['play_dim_portrait_info']['area_percentage']) : null;
  118. $liveInfo->save();
  119. RdsLockService::delRdsLock($lockKey);
  120. }
  121. // Log::info('直播数据入库成功', ['liveStreamId' => $liveStreamId], 'Star:LiveInfoDataInDb');
  122. return 0;
  123. }
  124. }