<?php

namespace App\Http\Controllers\Miniprogram;

use App\Http\Controllers\Controller;
use App\Models\InviteRelationModel;
use App\Models\User\UserModel;

class Invite extends Controller
{
    /**
     * 创建uid专属链接
     * @return array
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function getLink()
    {
        $uid = Auth::auth();
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => [
                'link' => $uid
            ],
        );
    }

    /**
     * 通过邀请进入
     * (sign时间小于一天且未被建立邀请关系的为新用户)
     * @param  string  $link
     * @return array
     * @throws \ApiException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function intoLink(string $link)
    {
        $uid = Auth::auth();
        $inviteModel = new InviteRelationModel();
        $auth = \DB::table('kdgx_user_auth_key')->where(array(
            ['uid', $uid],
            ['auth_type', config('miniprogram.public_id')]
        ))->first();
        if ($auth->created_at < mktime(0, 0, 0) - 3600) {
            return array(
                'code' => 201,
                'message' => '新用户专享福利'
            );
        }
        if ($inviteModel->where('is_invite_uid', $uid)->exists()) {
            return array(
                'code' => 201,
                'message' => '新用户专享福利'
            );
        }
        $invite = $inviteModel->fill([
            'uid' => (int)$link,
            'is_invite_uid' => $uid,
            'state' => 1
        ]);
        if ($invite->save()) {
            UserModel::where('uid', $uid)->increment('red_flower', 1);
            \DB::table('kdgx_partner_charge_pay_logs')->insert([
                'uid' => $uid,
                'create_at' => time(),
                'type' => 10,
                'gold_flower' => 0,
                'red_flower' => 1,
                'jsk' => 0,
                'type_id' => 0,
                'remark' => "邀请新人奖励"
            ]);
            return array(
                'code' => 200,
                'message' => 'success'
            );
        } else {
            throw new \ApiException("数据库异常", 505);
        }
    }

    /**
     * 新用户完成注册任务奖励
     * @param  int  $uid
     * @return bool
     */
    public function task(int $uid)
    {
        $rela = InviteRelationModel::where('is_invite_uid', $uid)->first();
        if (collect($rela)->isEmpty()) {
            return false;
        } else {
            $rela->state = 2;
            $rela->save();
            $ticketObj = new Ticket();
            $ticketObj->checkTicket(5, $rela->uid);
            \DB::table('kdgx_partner_charge_pay_logs')->insert([
                'uid' => $uid,
                'create_at' => time(),
                'type' => 10,
                'remark' => '邀请新人奖励',
                'jsk' => 1,
            ]);
            return true;
        }
    }

    /**
     * 获取邀请统计
     * @return array
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function inviteList()
    {
        $uid = Auth::auth();
        $relas = InviteRelationModel::where('uid', $uid)->get();
        $cnt = $relas->count();
        $users = UserModel::whereIn('uid', $relas->pluck('is_invite_uid'))->get(['headimgurl', 'uid']);
        $jsk_relas = $relas->where('state', 2);
        $jsk_cnt = $jsk_relas->count();
        $jsk_headimgurls = $users->whereIn('uid', $jsk_relas->pluck('is_invite_uid'));
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => [
                'flower' => [
                    'headimgurl' => $users->pluck('headimgurl'),
                    'cnt' => $cnt
                ],
                'jsk' => [
                    'headimgurl' => $jsk_headimgurls,
                    'cnt' => $jsk_cnt
                ],
            ],
        );
    }
}
