123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * Created by PhpStorm.
- * User: gevnc
- * Date: 2018/5/14
- * Time: 17:55
- */
-
- namespace App\Services;
-
- use App\Models\Question;
- use App\Models\QuestionCategory;
- use App\Traits\Singleton;
- use Illuminate\Support\Facades\Redis;
- class QuestionService
- {
- use Singleton;
-
- const QUESTIONS = "bs_questions";
- const QUESTION_COLLECT = "bs_question_collect";
- const QUESTION_CLASS = "bs_question_class";
-
- public function cache($activityId){
-
- $brainstorming = BrainstormingService::getInstance()->get($activityId);
- $questions = Question::where("activity_id",$activityId)->get();
- $classes = QuestionCategory::where("activity_id",$activityId)->get();
-
- $mustQuestionCategorys = explode(",",$brainstorming["must_category_ids"]);
- $blockadeQuestionCategorys = explode(",",$brainstorming["blockade_category_ids"]);
- $pkQuestionCategorys = explode(",",$brainstorming["pk_category_ids"]);
-
- //删除以前的缓存
- Redis::del(self::QUESTION_COLLECT.":".$activityId.":must");
- Redis::del(self::QUESTION_COLLECT.":".$activityId.":blockade");
- Redis::del(self::QUESTION_COLLECT.":".$activityId.":pk");
- Redis::del(self::QUESTION_CLASS.":".$activityId.":0");
- Redis::del(self::QUESTION_CLASS.":".$activityId.":1");
- foreach ($questions as $question){
- $question = $question->toArray();
- Redis::del(self::QUESTION_COLLECT.":".$activityId.":blockade".":".$question["category_id"]);
- }
-
- foreach ($classes as $class) {
- $class = $class -> toArray();
- if (in_array($class["category_id"],$blockadeQuestionCategorys)){
- if ($class["direct_employees"]) {
- Redis::hMset(self::QUESTION_CLASS . ":" . $activityId.":1", $class['category_id'], json_encode($class));
- } else {
- Redis::hMset(self::QUESTION_CLASS . ":" . $activityId.":0", $class['category_id'], json_encode($class));
- }
- }
- }
-
- foreach ($questions as $question){
- $question = $question->toArray();
- unset($question["update_time"]);
- unset($question["create_time"]);
- Redis::hMset(self::QUESTIONS.":".$question["question_id"], $question);
-
- if(in_array($question["category_id"],$mustQuestionCategorys)){
- Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":must", $question["question_id"]);
- Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":must", $question["question_id"]);
- }
- if(in_array($question["category_id"],$blockadeQuestionCategorys)){
- Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":blockade", $question["question_id"]);
- Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":blockade", $question["question_id"]);
- Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":blockade".":".$question["category_id"], $question["question_id"]);
- }
- if(in_array($question["category_id"],$pkQuestionCategorys)){
- Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":pk", $question["question_id"]);
- Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":pk", $question["question_id"]);
- }
- }
- }
-
- public function getClassDirect($activityId){
- return Redis::hGetAll(self::QUESTION_CLASS.":".$activityId.":1");
- }
-
- public function getClassNotDirect($activityId){
- return Redis::hGetAll(self::QUESTION_CLASS.":".$activityId.":0");
- }
-
- /**
- * 获取单个题目信息
- * @param $questionId
- * @return mixed
- */
- public function get($questionId)
- {
- $data = Redis::hGetAll(self::QUESTIONS.":".$questionId);
- if($data){
- $data["question_options"] = unserialize($data["question_options"]);
- }
- return $data;
- }
-
- /**
- * 获取多个题目信息
- * @param array $questionIds
- * @return mixed
- */
- public function mget(array $questionIds)
- {
- $datas = [];
- foreach ($questionIds as $questionId){
- $datas[$questionId] = Redis::hGetAll(self::QUESTIONS.":".$questionId);
- }
- return $datas;
- }
- }
|