<?php

namespace App\Http\Controllers\Log;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;
use App\Http\Controllers\Core\Ding;
use App\Jobs\GrowingIO\ClickWeixinReportJob;
use App\Models\Log\ClickUserHomeLogModel;
use App\Models\Log\ClickWeixinLogModel;
use App\Models\Log\FeedLogModel;
use App\Models\PraiseModel;
use App\Models\User\UserModel;
use App\Services\Log\NoticeLogService;
use App\Services\User\VipService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

/**
 * Class LogController
 * @package App\Http\Controllers\Log
 */
class LogController extends Controller
{
    /**
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function clickUserHome(Request $request)
    {
        $into_at = $request->input('into_at');
        $leave_at = $request->input('leave_at');
        $click_uid = $request->input('click_uid');
        $home_uid = $request->input('home_uid');
        $scene = $request->input('scene', 0);
        ClickUserHomeLogModel::create([
            'into_at' => $into_at,
            'leave_at' => $leave_at,
            'click_uid' => $click_uid,
            'home_uid' => $home_uid,
            'scene' => $scene,
        ]);
        try {
            /** @var UserModel $user */
            $user = UserModel::findorFail($home_uid);
            if (!PraiseModel::where(array(['uid', $click_uid], ['partner_id', $user->partner_id]))->exists()) {
                FeedLogModel::create([
                    'created_at' => time(),
                    'updated_at' => 98047,
                    'uid' => $click_uid,
                    'partner_id' => $user->partner_id
                ]);
            }
        } catch (\Exception $exception) {
        }
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    /**
     * 通过通知进入
     */
    public function logAfterNoticeLessHourInto()
    {
        $uid = Auth::auth();
        $nls = new NoticeLogService();
        $nls->logAfterNoticeLessHourInto($uid);
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    /**
     * redis报警
     */
    public function redisWarning()
    {
        Redis::setex("apilog:off", 86400, "true");
        Ding::robot([
            'title' => 'API队列暂停',
            'text' => "已关闭API日志"
        ]);
    }

    /**
     * 标记点击了成为会员页
     */
    public function tagBeVip()
    {
        $uid = Auth::auth();

        Redis::sadd("log:bevip:page:tag", $uid);
        try {
            $vs = new VipService();
            $vs->beVip($uid);
        } catch (\Exception $exception) {
        }
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }


    /**
     * 点击微信号事件记录
     * @param  Request  $request
     */
    public function clickWeiXin(Request $request)
    {
        $uid = Auth::auth();
        $attribute = $request->all();
        $attribute['uid'] = $uid;
        ClickWeixinLogModel::create($attribute);
        ClickWeixinReportJob::dispatch($uid, $attribute);
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }
}
