QuestionService.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: gevnc
  5. * Date: 2018/5/14
  6. * Time: 17:55
  7. */
  8. namespace App\Services;
  9. use App\Models\Question;
  10. use App\Models\QuestionCategory;
  11. use App\Traits\Singleton;
  12. use Illuminate\Support\Facades\Redis;
  13. class QuestionService
  14. {
  15. use Singleton;
  16. const QUESTIONS = "bs_questions";
  17. const QUESTION_COLLECT = "bs_question_collect";
  18. const QUESTION_CLASS = "bs_question_class";
  19. public function cache($activityId){
  20. $brainstorming = BrainstormingService::getInstance()->get($activityId);
  21. $questions = Question::where("activity_id",$activityId)->get();
  22. $classes = QuestionCategory::where("activity_id",$activityId)->get();
  23. $mustQuestionCategorys = explode(",",$brainstorming["must_category_ids"]);
  24. $blockadeQuestionCategorys = explode(",",$brainstorming["blockade_category_ids"]);
  25. $pkQuestionCategorys = explode(",",$brainstorming["pk_category_ids"]);
  26. //删除以前的缓存
  27. Redis::del(self::QUESTION_COLLECT.":".$activityId.":must");
  28. Redis::del(self::QUESTION_COLLECT.":".$activityId.":blockade");
  29. Redis::del(self::QUESTION_COLLECT.":".$activityId.":pk");
  30. Redis::del(self::QUESTION_CLASS.":".$activityId.":0");
  31. Redis::del(self::QUESTION_CLASS.":".$activityId.":1");
  32. foreach ($questions as $question){
  33. $question = $question->toArray();
  34. Redis::del(self::QUESTION_COLLECT.":".$activityId.":blockade".":".$question["category_id"]);
  35. }
  36. foreach ($classes as $class) {
  37. $class = $class -> toArray();
  38. if (in_array($class["category_id"],$blockadeQuestionCategorys)){
  39. if ($class["direct_employees"]) {
  40. Redis::hMset(self::QUESTION_CLASS . ":" . $activityId.":1", $class['category_id'], json_encode($class));
  41. } else {
  42. Redis::hMset(self::QUESTION_CLASS . ":" . $activityId.":0", $class['category_id'], json_encode($class));
  43. }
  44. }
  45. }
  46. foreach ($questions as $question){
  47. $question = $question->toArray();
  48. unset($question["update_time"]);
  49. unset($question["create_time"]);
  50. Redis::hMset(self::QUESTIONS.":".$question["question_id"], $question);
  51. if(in_array($question["category_id"],$mustQuestionCategorys)){
  52. Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":must", $question["question_id"]);
  53. Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":must", $question["question_id"]);
  54. }
  55. if(in_array($question["category_id"],$blockadeQuestionCategorys)){
  56. Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":blockade", $question["question_id"]);
  57. Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":blockade", $question["question_id"]);
  58. Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":blockade".":".$question["category_id"], $question["question_id"]);
  59. }
  60. if(in_array($question["category_id"],$pkQuestionCategorys)){
  61. Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":pk", $question["question_id"]);
  62. Redis::sAdd(self::QUESTION_COLLECT.":".$activityId.":pk", $question["question_id"]);
  63. }
  64. }
  65. }
  66. public function getClassDirect($activityId){
  67. return Redis::hGetAll(self::QUESTION_CLASS.":".$activityId.":1");
  68. }
  69. public function getClassNotDirect($activityId){
  70. return Redis::hGetAll(self::QUESTION_CLASS.":".$activityId.":0");
  71. }
  72. /**
  73. * 获取单个题目信息
  74. * @param $questionId
  75. * @return mixed
  76. */
  77. public function get($questionId)
  78. {
  79. $data = Redis::hGetAll(self::QUESTIONS.":".$questionId);
  80. if($data){
  81. $data["question_options"] = unserialize($data["question_options"]);
  82. }
  83. return $data;
  84. }
  85. /**
  86. * 获取多个题目信息
  87. * @param array $questionIds
  88. * @return mixed
  89. */
  90. public function mget(array $questionIds)
  91. {
  92. $datas = [];
  93. foreach ($questionIds as $questionId){
  94. $datas[$questionId] = Redis::hGetAll(self::QUESTIONS.":".$questionId);
  95. }
  96. return $datas;
  97. }
  98. }