<?php
/**
 * Created by PhpStorm.
 * User: guanxl
 * Date: 2018/9/28
 * Time: 11:41
 */

namespace App\Http\Controllers;

use App\Models\Corp;
use App\Services\BrainstormingService;
use App\Services\UserService;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class PersonDingtalkController extends BaseController
{
    /**
     * 活动列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function activityList(Request $request)
    {
        try{
            $status = $request->get("status","running");
            $redisKey = "bs_person_dingtalk:".$status;
            //$redisValue = Redis::get($redisKey);
            $redisValue = "";
            if(empty($redisValue)){
                $curTime = time();
                $where = " and A.status=1 and is_complete_set=1 and activity_type=3";
                switch ($status) {
                    case "upcoming":
                        $where .= " and A.start_time>".$curTime;
                        break;
                    case "finish":
                        $where .= " and A.end_time<".$curTime;
                        break;
                    default:
                        $where .= " and (A.start_time<$curTime and A.end_time>$curTime)";
                        break;
                }
                $data = [];
                $activitys = DB::select("select A.activity_id,A.title,B.share_home_desc,A.start_time,A.end_time,B.top_banner from agh_activitys as A,bs_configs as B where A.activity_id=B.activity_id ".$where." limit 0,100");
                foreach ($activitys as $activity){
                    $userCount = User::where("activity_id",$activity->activity_id)->where("status",1)->count();
                    $data[] = [
                        "activity_id" => $activity->activity_id,
                        "title" => $activity->title,
                        "desc" => $activity->share_home_desc,
                        "start_time" => $activity->start_time*1000,
                        "end_time" => $activity->end_time*1000,
                        "top_banner" => $activity->top_banner,
                        "url" => env("WEB_URL")."?activity_id=".$activity->activity_id,
                        "user_count" => $userCount
                    ];
                }

                Redis::setex($redisKey,300,json_encode($data));

            }else{
                $data = json_decode($redisValue,true);
            }
            return response()->json([
                "error" => 0,
                "message" => "OK",
                "data" => $data
            ]);
        }catch (\Exception $exception){
            Log::info($exception->getTraceAsString());
            return response()->json([
                "error" => 0,
                "message" => "OK",
                "data" => []
            ]);
        }
    }

    /**
     * 我加入的活动
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function joinActivity(Request $request){
        try{
            $openId = $request->get("openid");
            $data = [];
            if($openId){
                $redisKey = "bs_person_dingtalk:activitys:".$openId;
                //$redisValue = Redis::get($redisKey);
                $redisValue = "";
                if(empty($redisValue)){
                    $where = " and B.open_id='$openId' and A.status=1 and activity_type=3";
                    $activitys = DB::select("select A.activity_id,A.title,C.share_home_desc,A.start_time,A.end_time from agh_activitys as A,bs_users as B,bs_configs as C where A.activity_id=C.activity_id and A.activity_id=B.activity_id ".$where." limit 0,100");
                    foreach ($activitys as $activity){
                        $activityRedis = BrainstormingService::getInstance()->get($activity->activity_id);
                        $userCount = User::where("activity_id",$activity->activity_id)->where("status",1)->count();
                        $data[] = [
                            "activity_id" => $activity->activity_id,
                            "title" => $activity->title,
                            "desc" => $activity->share_home_desc,
                            "start_time" => $activity->start_time*1000,
                            "end_time" => $activity->end_time*1000,
                            "top_banner" => $activityRedis["top_banner"],
                            "url" => env("WEB_URL")."?activity_id=".$activity->activity_id,
                            "user_count" => $userCount
                        ];
                    }

                    Redis::setex($redisKey,300,json_encode($data));
                }else{
                    $data = json_decode($redisValue,true);
                }
            }
            return response()->json([
                "error" => 0,
                "message" => "OK",
                "data" => $data
            ]);
        }catch (\Exception $exception){
            Log::info($exception->getTraceAsString());
            return response()->json([
            "error" => 0,
            "message" => "OK",
                "data" => []
            ]);
        }
    }
}