From c9728f1a2097e53095dcadd8d2d6d6ee1dd92225 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Mon, 19 Jul 2021 23:58:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8A=95=E7=A5=A8=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Cache/VoteStatisticsCache.php | 58 +++++++++++++++++++ app/Command/TestCommand.php | 3 +- .../Api/V1/TalkMessageController.php | 4 +- app/Service/Message/FormatMessageService.php | 7 ++- app/Service/TalkMessageService.php | 19 +++--- 5 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 app/Cache/VoteStatisticsCache.php diff --git a/app/Cache/VoteStatisticsCache.php b/app/Cache/VoteStatisticsCache.php new file mode 100644 index 0000000..38cb976 --- /dev/null +++ b/app/Cache/VoteStatisticsCache.php @@ -0,0 +1,58 @@ +first(['answer_num', 'answered_num', 'answer_option']); + if (!$vote) return []; + + $answers = TalkRecordsVoteAnswer::where('vote_id', $vote_id)->pluck('option')->toArray(); + $options = array_map(function () { + return 0; + }, $vote->answer_option); + + foreach ($answers as $answer) { + $options[$answer]++; + } + + $statistics = [ + 'count' => count($answers), + 'options' => $options + ]; + + $this->set(strval($vote_id), json_encode($statistics), 60 * 60 * 24); + + return $statistics; + } + + /** + * 获取或更新投票统计缓存 + * + * @param int $vote_id 投票ID + * @return array + */ + public function getOrSetVoteCache(int $vote_id): array + { + $cache = $this->get(strval($vote_id)); + return $cache ? json_decode($cache, true) : $this->updateVoteCache($vote_id); + } +} diff --git a/app/Command/TestCommand.php b/app/Command/TestCommand.php index 824afbf..9882b0a 100644 --- a/app/Command/TestCommand.php +++ b/app/Command/TestCommand.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Command; +use App\Cache\VoteStatisticsCache; use App\Model\Talk\TalkRecordsVote; use Hyperf\Command\Command as HyperfCommand; use Hyperf\Command\Annotation\Command; @@ -35,6 +36,6 @@ class TestCommand extends HyperfCommand public function handle() { - echo chr(65); + VoteStatisticsCache::getInstance()->updateVoteCache(15); } } diff --git a/app/Controller/Api/V1/TalkMessageController.php b/app/Controller/Api/V1/TalkMessageController.php index 775ada9..f58b1f8 100644 --- a/app/Controller/Api/V1/TalkMessageController.php +++ b/app/Controller/Api/V1/TalkMessageController.php @@ -226,11 +226,11 @@ class TalkMessageController extends CController return $this->response->fail('投票失败,请稍后再试!'); } - $isTrue = $this->talkMessageService->handleVote($this->uid(), $params); + [$isTrue, $cache] = $this->talkMessageService->handleVote($this->uid(), $params); if (!$isTrue) return $this->response->fail('投票失败,请稍后再试!'); - return $this->response->success(); + return $this->response->success($cache); } /** diff --git a/app/Service/Message/FormatMessageService.php b/app/Service/Message/FormatMessageService.php index c469ec0..3adaaac 100644 --- a/app/Service/Message/FormatMessageService.php +++ b/app/Service/Message/FormatMessageService.php @@ -3,6 +3,7 @@ namespace App\Service\Message; use App\Cache\VoteCache; +use App\Cache\VoteStatisticsCache; use App\Constants\TalkMessageType; use App\Model\Talk\TalkRecordsCode; use App\Model\Talk\TalkRecordsFile; @@ -152,10 +153,10 @@ class FormatMessageService } $votes[$row['id']]['answer_option'] = $options; - - $rows[$k]['vote'] = [ + $rows[$k]['vote'] = [ + 'statistics' => VoteStatisticsCache::getInstance()->getOrSetVoteCache($votes[$row['id']]['id']), 'vote_users' => VoteCache::getInstance()->getOrSetVoteCache($votes[$row['id']]['id']), - 'detail' => $votes[$row['id']] + 'detail' => $votes[$row['id']], ]; break; diff --git a/app/Service/TalkMessageService.php b/app/Service/TalkMessageService.php index 53a6164..a5ab8f2 100644 --- a/app/Service/TalkMessageService.php +++ b/app/Service/TalkMessageService.php @@ -4,6 +4,7 @@ namespace App\Service; use App\Cache\LastMessage; use App\Cache\VoteCache; +use App\Cache\VoteStatisticsCache; use App\Constants\TalkMessageEvent; use App\Constants\TalkMessageType; use App\Model\Group\GroupMember; @@ -176,7 +177,7 @@ class TalkMessageService * @param array $params * @return bool */ - public function handleVote(int $user_id, array $params): bool + public function handleVote(int $user_id, array $params): array { $record = TalkRecords::join('talk_records_vote as vote', 'vote.record_id', '=', 'talk_records.id') ->where('talk_records.id', $params['record_id']) @@ -191,15 +192,15 @@ class TalkMessageService if (!$record) return false; if ($record->msg_type != TalkMessageType::VOTE_MESSAGE) { - return false; + return [false, []]; } if (!UserRelation::isFriendOrGroupMember($user_id, $record->receiver_id, $record->talk_type)) { - return false; + return [false, []]; } if (TalkRecordsVoteAnswer::where('vote_id', $record->vote_id)->where('user_id', $user_id)->exists()) { - return false; + return [false, []]; } $options = $params['options']; @@ -207,7 +208,7 @@ class TalkMessageService sort($options); foreach ($options as $value) { - if (!isset($record->answer_option[$value])) return false; + if (!isset($record->answer_option[$value])) return [false, []]; } // 单选模式取第一个 @@ -233,11 +234,15 @@ class TalkMessageService } }); } catch (\Exception $e) { - return false; + return [false, []]; } + // 更新投票缓存 VoteCache::getInstance()->updateVoteCache($record->vote_id); + $cache = VoteStatisticsCache::getInstance()->updateVoteCache($record->vote_id); - return true; + // todo 推送消息 + + return [true, $cache]; } }