<?php
/**
 * Created by PhpStorm.
 * User: guanxl
 * Date: 2018/5/14
 * Time: 16:04
 */

namespace App\Http\Controllers\Console;
use App\Models\Config;
use App\Services\BrainstormingService;
use App\Services\QuestionService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Laravel\Lumen\Routing\Controller as BaseController;

class CacheController extends BaseController
{
    public function question(Request $request){
        $activityId = intval($request->get("activity_id"));
        if($activityId>0){

            try{
                QuestionService::getInstance()->cache($activityId);
                Log::info("更新题库成功！");
                return response()->json([
                    "error" => "0",
                    "message" => "OK"
                ]);
            }catch (\Exception $exception){
                Log::info("cache question:activity=".$activityId." Exception:".$exception->getTrace());
                return response()->json([
                    "error" => "500",
                    "message" => "服务器内部错误！"
                ]);
            }
        }else{
            Log::info("无activityId,参数不正确！");
            return response()->json([
                "error" => "1",
                "message" => "无activityId,参数不正确！"
            ]);
        }
    }

    public function config(Request $request){

        $activityId = intval($request->get("activity_id"));
        if($activityId>0){
            try{
                BrainstormingService::getInstance()->cache($activityId);
                Log::info("更新配置成功！");
                return response()->json([
                    "error" => "0",
                    "message" => "OK"
                ]);
            }catch(\Exception $exception){
                Log::info("cahce config:activity=".$activityId." Exception:".$exception->getMessage());
                return response()->json([
                    "error" => "500",
                    "message" => "服务器内部错误！".$exception->getTraceAsString()
                ]);
            }
        }else{
            Log::info("无activityId,参数不正确！");
            return response()->json([
                "error" => "1",
                "message" => "无activityId,参数不正确！"
            ]);
        }
    }
}