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