VideoCommentCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\RandomUserInfoRepository;
  8. use Carbon\Carbon;
  9. use Hyperf\Command\Command as HyperfCommand;
  10. use Psr\Container\ContainerInterface;
  11. use Hyperf\Command\Annotation\Command;
  12. /**
  13. * @Command
  14. */
  15. #[Command]
  16. class VideoCommentCommand extends HyperfCommand
  17. {
  18. /**
  19. * 执行的命令行
  20. *
  21. * @var string
  22. */
  23. protected $name = 'init:video_comment';
  24. /**
  25. * @var ContainerInterface
  26. */
  27. protected $container;
  28. public function __construct(ContainerInterface $container)
  29. {
  30. $this->container = $container;
  31. parent::__construct($this->name);
  32. }
  33. public function configure()
  34. {
  35. parent::configure();
  36. $this->setDescription('初始化视频评论.');
  37. $this->addUsage(" 无参数");
  38. }
  39. public function handle()
  40. {
  41. $commentRepo = make(CommentRepository::class);
  42. $commentPath = self::getCommentPath();
  43. if (!file_exists($commentPath)) {
  44. return true;
  45. }
  46. $content = file_get_contents($commentPath);
  47. $arrComments = explode("\n", $content);
  48. $users = UserModel::where(['ip' => '113.238.142.208'])->where('gender', '!=', 3)->take(1000)->get();
  49. $arrUsers = $users->pluck('code')->toArray();
  50. /*$bots = UserModel::where('gender', '=', 3)->take(100)->get();
  51. $arrBots = $bots->pluck('code')->toArray();*/
  52. $stepNo = 0;
  53. VideoModel::orderBy('id', 'ASC')->chunk(200, function ($items) use($commentRepo, $arrComments, $arrUsers, &$stepNo) {
  54. $oldCommentContent = '_1_';
  55. $commentContent = '';
  56. foreach ($items as $item) {
  57. $stepNo++;
  58. $videoCreatedAt = $item->created_at;
  59. $maxCommentNumber = rand(2, 24);
  60. $nowDate = Carbon::now()->toDateTimeString();
  61. for ($i = 0; $i < $maxCommentNumber; $i++) {
  62. if (!empty($commentContent)) {
  63. $oldCommentContent = $commentContent;
  64. }
  65. $userKey = array_rand($arrUsers);
  66. $userCode = $arrUsers[$userKey];
  67. $commentKey = array_rand($arrComments);
  68. $commentContent = $arrComments[$commentKey];
  69. if ($oldCommentContent == $commentContent) {
  70. // 避免连续重复,再随机一次.
  71. $commentKey = array_rand($arrComments);
  72. $commentContent = $arrComments[$commentKey];
  73. }
  74. $setarr = [
  75. 'user_id' => $userCode,
  76. 'module' => 'video',
  77. 'svalue' => $item->id,
  78. 'subject' => mb_substr($item->title, 0, 120),
  79. 'content' => $commentContent,
  80. 'parent_id' => 0,
  81. 'total_support' => rand(0, 200),
  82. 'ip' => '113.238.142.208',
  83. ];
  84. $commentObj = $commentRepo->createComment($setarr);
  85. $commentObj->created_at = Carbon::parse($videoCreatedAt)->addHours($i + 3)->addMinutes(rand(5, 43))->toDateTimeString();
  86. $commentObj->save();
  87. }
  88. $this->comment(sprintf('[%s]处理第%s个视频【ID:%s】,评论数【%s】', $nowDate, $stepNo, $item->id, $maxCommentNumber));
  89. }
  90. });
  91. $nowDate = Carbon::now()->toDateTimeString();
  92. $this->comment(sprintf('[%s]完成初始化视频评论, 总计:%s个视频', $nowDate, $stepNo));
  93. }
  94. /**
  95. * 获取评论路径.
  96. * @return string
  97. */
  98. private function getCommentPath()
  99. {
  100. return base_path(). '/storage/txt/comment.txt';
  101. }
  102. }