<?php

namespace App\Http\Controllers\Fpdx;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;

class NoticeController extends Controller
{

    /**
     * 获取发送状态
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function getSendState(Request $request)
    {
        $this->validate($request, [
            'room_id' => 'required',
            'type' => 'required'
        ]);
        $uid = Auth::auth();
        $type = $request->input('type', 'sms');
        $bel = Redis::get("send_{$type}_{$uid}_{$request->room_id}");
        if (empty($bel)) {
            return response([
                'code' => 200,
                'message' => '可发送'
            ]);
        } else {
            return response([
                'code' => 501,
                'message' => '超过限制'
            ]);
        }
    }

    /**
     * 发送通知消息
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function sendNotive(Request $request)
    {
        $this->validate($request, [
            'room_id' => 'required',
            'pair_uid' => 'required',
            'type' => 'required'
        ]);
        $uid = Auth::auth();
        $type = $request->input('type', 'sms');
        $room_id = $request->input('room_id');
        $bel = Redis::get("send_{$type}_{$uid}_{$room_id}");
        if (empty($bel)) {
            Redis::setex("send_{$type}_{$uid}_{$room_id}", mktime(0, 0, 0) + 86400 - time(), time());
            return response(['code' => 200, 'message' => 'success']);
        } else {
            return response(['code' => 501, 'message' => '超过限制']);
        }
    }
}
