1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-07-13 03:49:11 +08:00
2020-04-23 19:52:46 +08:00

158 lines
4.1 KiB
PHP

<?php
namespace App\Services\Frontend\Chapter;
use App\Models\Chapter as ChapterModel;
use App\Models\ChapterVote as ChapterVoteModel;
use App\Models\User as UserModel;
use App\Repos\ChapterVote as ChapterVoteRepo;
use App\Services\Frontend\ChapterTrait;
use App\Services\Frontend\Service;
use App\Validators\UserDailyLimit as UserDailyLimitValidator;
class ChapterVote extends Service
{
use ChapterTrait;
public function agree($id)
{
$chapter = $this->checkChapter($id);
$user = $this->getLoginUser();
$validator = new UserDailyLimitValidator();
$validator->checkChapterVoteLimit($user);
$chapterVoteRepo = new ChapterVoteRepo();
$chapterVote = $chapterVoteRepo->findChapterVote($chapter->id, $user->id);
if (!$chapterVote) {
$chapterVote = new ChapterVoteModel();
$chapterVote->chapter_id = $chapter->id;
$chapterVote->user_id = $user->id;
$chapterVote->type = ChapterVoteModel::TYPE_AGREE;
$chapterVote->create();
$this->incrAgreeCount($chapter);
} else {
if ($chapterVote->type == ChapterVoteModel::TYPE_AGREE) {
$chapterVote->type = ChapterVoteModel::TYPE_NONE;
$this->decrAgreeCount($chapter);
} elseif ($chapterVote->type == ChapterVoteModel::TYPE_OPPOSE) {
$chapterVote->type = ChapterVoteModel::TYPE_AGREE;
$this->incrAgreeCount($chapter);
$this->decrOpposeCount($chapter);
} elseif ($chapterVote->type == ChapterVoteModel::TYPE_NONE) {
$chapterVote->type = ChapterVoteModel::TYPE_AGREE;
$this->incrAgreeCount($chapter);
}
$chapterVote->update();
}
$this->incrUserDailyChapterVoteCount($user);
return $chapter;
}
public function oppose($id)
{
$chapter = $this->checkChapter($id);
$user = $this->getLoginUser();
$validator = new UserDailyLimitValidator();
$validator->checkChapterVoteLimit($user);
$chapterVoteRepo = new ChapterVoteRepo();
$chapterVote = $chapterVoteRepo->findChapterVote($chapter->id, $user->id);
if (!$chapterVote) {
$chapterVote = new ChapterVoteModel();
$chapterVote->chapter_id = $chapter->id;
$chapterVote->user_id = $user->id;
$chapterVote->type = ChapterVoteModel::TYPE_OPPOSE;
$chapterVote->create();
$this->incrOpposeCount($chapter);
} else {
if ($chapterVote->type == ChapterVoteModel::TYPE_AGREE) {
$chapterVote->type = ChapterVoteModel::TYPE_OPPOSE;
$this->decrAgreeCount($chapter);
$this->incrOpposeCount($chapter);
} elseif ($chapterVote->type == ChapterVoteModel::TYPE_OPPOSE) {
$chapterVote->type = ChapterVoteModel::TYPE_NONE;
$this->decrOpposeCount($chapter);
} elseif ($chapterVote->type == ChapterVoteModel::TYPE_NONE) {
$chapterVote->type = ChapterVoteModel::TYPE_OPPOSE;
$this->incrOpposeCount($chapter);
}
$chapterVote->update();
}
$this->incrUserDailyChapterVoteCount($user);
return $chapter;
}
protected function incrAgreeCount(ChapterModel $chapter)
{
$this->eventsManager->fire('chapterCounter:incrAgreeCount', $this, $chapter);
}
protected function decrAgreeCount(ChapterModel $chapter)
{
$this->eventsManager->fire('chapterCounter:decrAgreeCount', $this, $chapter);
}
protected function incrOpposeCount(ChapterModel $chapter)
{
$this->eventsManager->fire('chapterCounter:incrOpposeCount', $this, $chapter);
}
protected function decrOpposeCount(ChapterModel $chapter)
{
$this->eventsManager->fire('chapterCounter:decrOpposeCount', $this, $chapter);
}
protected function incrUserDailyChapterVoteCount(UserModel $user)
{
$this->eventsManager->fire('userDailyCounter:incrChapterVoteCount', $this, $user);
}
}