123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace App\Console\Commands\Star;
- use App\Models\JxStarLiveInfoList;
- use App\Services\RdsLockService;
- use App\Support\Log;
- use App\Support\RedisModel;
- use Illuminate\Console\Command;
- class LiveInfoDataInDb extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'Star:LiveInfoDataInDb';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '星直播直播数据入库';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function handle()
- {
- $this->info(date('H:i') . ' 开始执行');
- $this->start();
- $this->info(date('H:i') . ' 结束执行');
- return 0;
- }
- public function start()
- {
- $curTime = time();
- while (true) {
- if ((time() - $curTime) >= 60) break;
- $dataJson = RedisModel::rPop(JxStarLiveInfoList::JUXING_STAR_LIVE_INFO_INDB_LIST);
- if (empty($dataJson)) {
- sleep(2);
- continue;
- }
- $data = json_decode($dataJson, true);
- if (!isset($data['order_id'], $data['data'])) {
- # 输出错误日志
- Log::error('数据参数不合法', $data, 'Star:LiveInfoDataInDb');
- continue;
- }
- $this->infoDataInDb($data);
- }
- }
- public function infoDataInDb($data)
- {
- $orderId = $data['order_id'];
- $dataVal = $data['data'];
- $liveStreamId = $dataVal['live_stream_id'] ?? null;
- if (empty($liveStreamId)) {
- # 输出错误日志
- Log::error('liveStreamId 为空', $data, 'Star:LiveInfoDataInDb');
- return false;
- }
- $lockKey = RdsLockService::getRdsLockKey([
- 'starLive', $orderId, $liveStreamId
- ]);
- if (RdsLockService::addRdsLock($lockKey)) {
- $liveInfo = JxStarLiveInfoList::query()
- ->where('order_id', $orderId)
- ->where('live_stream_id', $liveStreamId)
- ->where('enable', 1)
- ->first();
- if (empty($liveInfo)) {
- $liveInfo = new JxStarLiveInfoList([
- 'order_id' => $orderId,
- 'live_stream_id' => $liveStreamId
- ]);
- }
- $liveInfo->live_duration = $dataVal['live_duration'] ?? 0;
- $liveInfo->live_max_online_cnt = $dataVal['live_max_online_cnt'] ?? 0;
- $liveInfo->item_show_cnt = $dataVal['item_show_cnt'] ?? 0;
- $liveInfo->item_click_cnt = $dataVal['item_click_cnt'] ?? 0;
- $liveInfo->item_click_rate = $dataVal['item_click_rate'] ?? 0;
- $liveInfo->sale_product_cnt = $dataVal['sale_product_cnt'] ?? 0;
- $liveInfo->sale_amount = $dataVal['sale_amount'] ?? 0;
- $liveInfo->click_age_percentage = isset($dataVal['click_dim_portrait_info']['age_percentage'])
- ? json_encode($dataVal['click_dim_portrait_info']['age_percentage']) : null;
- $liveInfo->click_interest_percentage = isset($dataVal['click_dim_portrait_info']['interest_percentage'])
- ? json_encode($dataVal['click_dim_portrait_info']['interest_percentage']) : null;
- $liveInfo->click_active_percentage = isset($dataVal['click_dim_portrait_info']['active_percentage'])
- ? json_encode($dataVal['click_dim_portrait_info']['active_percentage']) : null;
- $liveInfo->click_mobile_percentage = isset($dataVal['click_dim_portrait_info']['mobile_percentage'])
- ? json_encode($dataVal['click_dim_portrait_info']['mobile_percentage']) : null;
- $liveInfo->click_gender_percentage = isset($dataVal['click_dim_portrait_info']['gender_percentage'])
- ? json_encode($dataVal['click_dim_portrait_info']['gender_percentage']) : null;
- $liveInfo->click_area_percentage = isset($dataVal['click_dim_portrait_info']['area_percentage'])
- ? json_encode($dataVal['click_dim_portrait_info']['area_percentage']) : null;
- $liveInfo->play_age_percentage = isset($dataVal['play_dim_portrait_info']['age_percentage'])
- ? json_encode($dataVal['play_dim_portrait_info']['age_percentage']) : null;
- $liveInfo->play_interest_percentage = isset($dataVal['play_dim_portrait_info']['interest_percentage'])
- ? json_encode($dataVal['play_dim_portrait_info']['interest_percentage']) : null;
- $liveInfo->play_active_percentage = isset($dataVal['play_dim_portrait_info']['active_percentage'])
- ? json_encode($dataVal['play_dim_portrait_info']['active_percentage']) : null;
- $liveInfo->play_mobile_percentage = isset($dataVal['play_dim_portrait_info']['mobile_percentage'])
- ? json_encode($dataVal['play_dim_portrait_info']['mobile_percentage']) : null;
- $liveInfo->play_gender_percentage = isset($dataVal['play_dim_portrait_info']['gender_percentage'])
- ? json_encode($dataVal['play_dim_portrait_info']['gender_percentage']) : null;
- $liveInfo->play_area_percentage = isset($dataVal['play_dim_portrait_info']['area_percentage'])
- ? json_encode($dataVal['play_dim_portrait_info']['area_percentage']) : null;
- $liveInfo->save();
- RdsLockService::delRdsLock($lockKey);
- }
- // Log::info('直播数据入库成功', ['liveStreamId' => $liveStreamId], 'Star:LiveInfoDataInDb');
- return 0;
- }
- }
|