123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Console\Commands\Star;
- use App\Models\JxStarLiveTaskList;
- use App\Services\RdsLockService;
- use App\Support\Log;
- use App\Support\RedisModel;
- use Illuminate\Console\Command;
- class LiveTaskDataInDb extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'Star:LiveTaskDataInDb';
- /**
- * 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(JxStarLiveTaskList::JUXING_STAR_LIVE_TASK_INDB_LIST);
- if (empty($dataJson)) {
- sleep(2);
- continue;
- }
- $data = json_decode($dataJson, true);
- if (!isset($data['advertiser_id'], $data['data'])) {
- # 输出错误日志
- Log::error('数据参数不合法', $data, 'Star:LiveTaskDataInDb');
- continue;
- }
- $this->taskDataInDb($data);
- }
- }
- public function taskDataInDb($data)
- {
- $advertiserId = $data['advertiser_id'];
- $dataVal = $data['data'];
- $taskId = $dataVal['task_id'] ?? null;
- if (empty($taskId)) {
- # 输出错误日志
- Log::error('taskId 为空', $data, 'Star:LiveTaskDataInDb');
- return false;
- }
- $lockKey = RdsLockService::getRdsLockKey([
- 'starLive', $advertiserId, $taskId
- ]);
- if (RdsLockService::addRdsLock($lockKey)) {
- $taskInfo = JxStarLiveTaskList::query()
- ->where('advertiser_id', $advertiserId)
- ->where('task_id', $taskId)
- ->where('enable', 1)
- ->first();
- if (empty($taskInfo)) {
- $taskInfo = new JxStarLiveTaskList([
- 'advertiser_id' => $advertiserId,
- 'task_id' => $taskId
- ]);
- }
- $taskInfo->task_name = $dataVal['task_name'] ?? null;
- $taskInfo->task_status = $dataVal['task_status'] ?? null;
- $taskInfo->task_status_desc = $dataVal['task_status_desc'] ?? null;
- $taskInfo->create_time = $dataVal['create_time'] ?? null;
- $taskInfo->tips_card_config = $dataVal['tips_card_config'] ?? null;
- $taskInfo->tips_card_theme_pic = $dataVal['tips_card_theme_pic'] ?? null;
- $taskInfo->tips_card_sub_title = $dataVal['tips_card_sub_title'] ?? null;
- $taskInfo->app_download_component = isset($dataVal['app_download_component'])
- ? json_encode($dataVal['app_download_component']) : null;
- $taskInfo->brand_component = isset($dataVal['brand_component'])
- ? json_encode($dataVal['brand_component']) : null;
- $taskInfo->clue_collection_component = isset($dataVal['clue_collection_component'])
- ? json_encode($dataVal['clue_collection_component']) : null;
- $taskInfo->landing_component = isset($dataVal['landing_component'])
- ? json_encode($dataVal['landing_component']) : null;
- $taskInfo->save();
- RdsLockService::delRdsLock($lockKey);
- }
- // Log::info('任务数据入库成功', ['taskId' => $taskId], 'Star:LiveTaskDataInDb');
- return 0;
- }
- }
|