| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace App\Command\Init;
- use App\Model\UserModel;
- use App\Model\VideoModel;
- use App\Repositories\CommentRepository;
- use App\Repositories\Service\CommonService;
- use App\Repositories\Utils\RandomUserInfoRepository;
- use Carbon\Carbon;
- use Hyperf\Command\Command as HyperfCommand;
- use Psr\Container\ContainerInterface;
- use Hyperf\Command\Annotation\Command;
- /**
- * @Command
- */
- #[Command]
- class VideoCommentCommand extends HyperfCommand
- {
- /**
- * 执行的命令行
- *
- * @var string
- */
- protected $name = 'init:video_comment';
- /**
- * @var ContainerInterface
- */
- protected $container;
- public function __construct(ContainerInterface $container)
- {
- $this->container = $container;
- parent::__construct($this->name);
- }
- public function configure()
- {
- parent::configure();
- $this->setDescription('初始化视频评论.');
- $this->addUsage(" 无参数");
- }
- public function handle()
- {
- $commentRepo = make(CommentRepository::class);
- $commentPath = self::getCommentPath();
- if (!file_exists($commentPath)) {
- return true;
- }
- $content = file_get_contents($commentPath);
- $arrComments = explode("\n", $content);
- $users = UserModel::where(['ip' => '113.238.142.208'])->where('gender', '!=', 3)->take(1000)->get();
- $arrUsers = $users->pluck('code')->toArray();
- /*$bots = UserModel::where('gender', '=', 3)->take(100)->get();
- $arrBots = $bots->pluck('code')->toArray();*/
- $stepNo = 0;
- VideoModel::orderBy('id', 'ASC')->chunk(200, function ($items) use($commentRepo, $arrComments, $arrUsers, &$stepNo) {
- $oldCommentContent = '_1_';
- $commentContent = '';
- foreach ($items as $item) {
- $stepNo++;
- $videoCreatedAt = $item->created_at;
- $maxCommentNumber = rand(2, 24);
- $nowDate = Carbon::now()->toDateTimeString();
- for ($i = 0; $i < $maxCommentNumber; $i++) {
- if (!empty($commentContent)) {
- $oldCommentContent = $commentContent;
- }
- $userKey = array_rand($arrUsers);
- $userCode = $arrUsers[$userKey];
- $commentKey = array_rand($arrComments);
- $commentContent = $arrComments[$commentKey];
- if ($oldCommentContent == $commentContent) {
- // 避免连续重复,再随机一次.
- $commentKey = array_rand($arrComments);
- $commentContent = $arrComments[$commentKey];
- }
- $setarr = [
- 'user_id' => $userCode,
- 'module' => 'video',
- 'svalue' => $item->id,
- 'subject' => mb_substr($item->title, 0, 120),
- 'content' => $commentContent,
- 'parent_id' => 0,
- 'total_support' => rand(0, 200),
- 'ip' => '113.238.142.208',
- ];
- $commentObj = $commentRepo->createComment($setarr);
- $commentObj->created_at = Carbon::parse($videoCreatedAt)->addHours($i + 3)->addMinutes(rand(5, 43))->toDateTimeString();
- $commentObj->save();
- }
- $this->comment(sprintf('[%s]处理第%s个视频【ID:%s】,评论数【%s】', $nowDate, $stepNo, $item->id, $maxCommentNumber));
- }
- });
- $nowDate = Carbon::now()->toDateTimeString();
- $this->comment(sprintf('[%s]完成初始化视频评论, 总计:%s个视频', $nowDate, $stepNo));
- }
- /**
- * 获取评论路径.
- * @return string
- */
- private function getCommentPath()
- {
- return base_path(). '/storage/txt/comment.txt';
- }
- }
|