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'; } }