Geen omschrijving

LiveTaskDataInDb.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Console\Commands\Star;
  3. use App\Models\JxStarLiveTaskList;
  4. use App\Services\RdsLockService;
  5. use App\Support\Log;
  6. use App\Support\RedisModel;
  7. use Illuminate\Console\Command;
  8. class LiveTaskDataInDb extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'Star:LiveTaskDataInDb';
  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(JxStarLiveTaskList::JUXING_STAR_LIVE_TASK_INDB_LIST);
  49. if (empty($dataJson)) {
  50. sleep(2);
  51. continue;
  52. }
  53. $data = json_decode($dataJson, true);
  54. if (!isset($data['advertiser_id'], $data['data'])) {
  55. # 输出错误日志
  56. Log::error('数据参数不合法', $data, 'Star:LiveTaskDataInDb');
  57. continue;
  58. }
  59. $this->taskDataInDb($data);
  60. }
  61. }
  62. public function taskDataInDb($data)
  63. {
  64. $advertiserId = $data['advertiser_id'];
  65. $dataVal = $data['data'];
  66. $taskId = $dataVal['task_id'] ?? null;
  67. if (empty($taskId)) {
  68. # 输出错误日志
  69. Log::error('taskId 为空', $data, 'Star:LiveTaskDataInDb');
  70. return false;
  71. }
  72. $lockKey = RdsLockService::getRdsLockKey([
  73. 'starLive', $advertiserId, $taskId
  74. ]);
  75. if (RdsLockService::addRdsLock($lockKey)) {
  76. $taskInfo = JxStarLiveTaskList::query()
  77. ->where('advertiser_id', $advertiserId)
  78. ->where('task_id', $taskId)
  79. ->where('enable', 1)
  80. ->first();
  81. if (empty($taskInfo)) {
  82. $taskInfo = new JxStarLiveTaskList([
  83. 'advertiser_id' => $advertiserId,
  84. 'task_id' => $taskId
  85. ]);
  86. }
  87. $taskInfo->task_name = $dataVal['task_name'] ?? null;
  88. $taskInfo->task_status = $dataVal['task_status'] ?? null;
  89. $taskInfo->task_status_desc = $dataVal['task_status_desc'] ?? null;
  90. $taskInfo->create_time = $dataVal['create_time'] ?? null;
  91. $taskInfo->tips_card_config = $dataVal['tips_card_config'] ?? null;
  92. $taskInfo->tips_card_theme_pic = $dataVal['tips_card_theme_pic'] ?? null;
  93. $taskInfo->tips_card_sub_title = $dataVal['tips_card_sub_title'] ?? null;
  94. $taskInfo->app_download_component = isset($dataVal['app_download_component'])
  95. ? json_encode($dataVal['app_download_component']) : null;
  96. $taskInfo->brand_component = isset($dataVal['brand_component'])
  97. ? json_encode($dataVal['brand_component']) : null;
  98. $taskInfo->clue_collection_component = isset($dataVal['clue_collection_component'])
  99. ? json_encode($dataVal['clue_collection_component']) : null;
  100. $taskInfo->landing_component = isset($dataVal['landing_component'])
  101. ? json_encode($dataVal['landing_component']) : null;
  102. $taskInfo->save();
  103. RdsLockService::delRdsLock($lockKey);
  104. }
  105. // Log::info('任务数据入库成功', ['taskId' => $taskId], 'Star:LiveTaskDataInDb');
  106. return 0;
  107. }
  108. }