<?php

namespace App\Http\Controllers\PartnerPay;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;
use App\Models\BuyInviteListModel;
use App\Models\BuyInviteModel;
use App\Models\RecodeModel;
use App\Models\PartnerModel;
use App\Services\Share\LockService;

class BuyInvite extends Controller
{
    /**
     * 获取邀请信息
     * @param  int  $invite_id
     * @return array
     */
    public function get(int $invite_id)
    {
        $invite = BuyInviteListModel::find($invite_id);
        if (collect($invite)->isEmpty()) {
            return array(
                'code' => 101,
                'message' => '参数错误'
            );
        } else {
            return array(
                'code' => 200,
                'message' => 'success',
                'data' => [
                    'uid' => $invite->uid,
                    'cnt' => $invite->effectiveInvite($invite_id),
                ],
            );
        }
    }

    /**
     * 解锁助力邀请检验
     * @param  int  $invite_id
     * @return array
     * @throws \ApiException
     * @throws \App\Exceptions\ApiException
     * @throws \App\Exceptions\DBException
     * @throws \Throwable
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function invite(int $invite_id)
    {
        $uid = Auth::auth();
        $ls = new LockService();
        $data = $ls->check($uid, $invite_id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    public function sync()
    {
        $comment = array(
            '希望这世界上从此少一个单身狗',
            '希望你不在是一个人过',
            '祝脱单',
            '成了别忘了发红包',
            '等着吃你的喜糖',
            '直觉告诉我，你这次要脱单',
            '坐等吃狗粮',
            '我已经准备好，喝喜酒的红包',
            '你这波突然袭击，很skr',
            '单身的终点，浪漫的节点，幸福的起点',
            '从此后，天更蓝，月更圆，你对未来有了更多期盼',
            '单薄的爱情填不满宽阔的岁月，你终于有长进了',
            '这波操作很骚',
            '大吉大利，今晚脱单',
            '请接收我1w点的助力暴击',
            '行动才是脱单的纲领，你做到了',
            '赶紧脱单，我是认真的，一秒钟都不许你浪费',
            '我很介意你单身，so赶紧脱单',
            '明天，我希望可以给你发来脱单贺电',
            '我感受到爱情的航班在呼唤你',
            '有种，有种，有种你现在就给我丢1w斤的狗粮'
        );
        $listModel = new BuyInviteListModel();
        $lists = $listModel->get();
        foreach ($lists as $list) {
            $recode = new RecodeModel();
            if (
                $recode->where([
                ['partner_id', $list->partner_id],
                ['uid', $list->uid]
                ])->exists()
            ) {
                $invModel = new BuyInviteModel();
                $invites = $invModel->where('invite_id', $list->id)->get();
                $cnt = $invites->count();
                $dis_score = floor(100 / $cnt);
                $invModel->whereIn('id', $invites->pluck('id'))->update([
                    'dis_score' => $dis_score,
                    'comment' => $comment[rand(0, 20)]
                ]);
            } else {
                $partner = PartnerModel::find($list->partner_id);
                $total = $partner->charm > 9 ? 9 : $partner->charm;
                $invModel = new BuyInviteModel();
                $invites = $invModel->where('invite_id', $list->id)->get();
                $dis_score = floor(100 / $total);
                $invModel->whereIn('id', $invites->pluck('id'))->update([
                    'dis_score' => $dis_score,
                    'comment' => $comment[rand(0, 20)]
                ]);
            }
        }
    }
}
