SyncVideoToMediaCenter.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Command\Init;
  3. use App\Model\UserModel;
  4. use App\Model\VideoModel;
  5. use App\Repositories\CommentRepository;
  6. use App\Repositories\Service\CommonService;
  7. use App\Repositories\Utils\MediaCenterRepository;
  8. use App\Repositories\Utils\RandomUserInfoRepository;
  9. use Carbon\Carbon;
  10. use Hyperf\Command\Command as HyperfCommand;
  11. use Hyperf\Guzzle\ClientFactory;
  12. use Psr\Container\ContainerInterface;
  13. use Hyperf\Command\Annotation\Command;
  14. /**
  15. * @Command
  16. */
  17. #[Command]
  18. class SyncVideoToMediaCenter extends HyperfCommand
  19. {
  20. /**
  21. * 执行的命令行
  22. *
  23. * @var string
  24. */
  25. protected $name = 'init:sync_video_to_media_center';
  26. /**
  27. * @var ContainerInterface
  28. */
  29. protected $container;
  30. public function __construct(ContainerInterface $container)
  31. {
  32. $this->container = $container;
  33. parent::__construct($this->name);
  34. }
  35. public function configure()
  36. {
  37. parent::configure();
  38. $this->setDescription('同步所有视频信息到媒资库.');
  39. $this->addUsage(" 无参数");
  40. }
  41. public function handle()
  42. {
  43. $startDateObj = Carbon::now();
  44. $totalNum = 0;
  45. $appAliasName = env('STATIC_DIR_NAME', 'doukui');
  46. $columns = ['id', 'cover', 'title', 'tags', 'file_hash', 'deleted_at', 'uid'];
  47. $mediaCenterRepo = make(MediaCenterRepository::class);
  48. VideoModel::query()->whereNull('deleted_at')->select($columns)->orderBy('id', 'desc')->chunk(100, function ($items) use (&$totalNum, $appAliasName, $mediaCenterRepo) {
  49. $postData = [
  50. 'force_sync_state' => 'no',
  51. 'items' => [],
  52. ];
  53. foreach ($items as $v) {
  54. $postData['items'][] = [
  55. 'app_alias_name' => $appAliasName,
  56. 'app_module_name' => 'video',
  57. 'app_item_code' => $v->id,
  58. 'file_hash' => $v->file_hash,
  59. 'cover' => $v->cover,
  60. 'title' => $v->title,
  61. 'tags' => !empty($v->tags) ? $v->tags : '',
  62. ];
  63. $totalNum++;
  64. }
  65. // 开始阻塞同步到媒资库.
  66. $mediaCenterRepo->syncAppVideoToMediaCenter($postData);
  67. unset($postData);
  68. });
  69. $endDateObj = Carbon::now();
  70. $diffSeconds = abs($endDateObj->diffInSeconds($startDateObj));
  71. $this->comment(sprintf('[%s]完成同步视频到媒资库, 总计:%s个视频, 耗时:%s秒', $endDateObj->toDateTimeString(), strval($totalNum), strval($diffSeconds)));
  72. }
  73. }