<?php

namespace App\Services\Welfare;

use App\Models\User\UserModel;
use App\Models\Welfare\LotteryModel;
use App\Services\QQMiniApp\Template;
use PocketBE\MsyPush\Jobs\RegistEventJob;

class NoticeService
{


    /**
     * 福利中心-活动开奖前提醒
     * @param int $to_uid
     * @param int $id
     */
    public function beforeLottery($to_uid, $id)
    {
        $title = "福利中心-活动开奖前提醒";
        $lottery = LotteryModel::findOrFail($id);
        $user = UserModel::find($to_uid);
        $payload = [
            'user' => $user->toArray(),
            'lottery' => $lottery->toArray(),
        ];
        dispatch(new RegistEventJob(10601, $payload))->onQueue("{push}");


        // QQ通知
        $uuid = uuid();
        $template_id = "a1c1ba47f520de5bd1b5c064dc915108";
        $page = "reward/pages/welfare/welfare";
        $parameters = [
            'keyword1' => [
                'value' => "距本期开奖时间还剩1小时",
            ],
            'keyword2' => [
                'value' => $lottery->title,
            ],
            'keyword3' => [
                'value' => date("Y-m-d 20:00"),
            ],
            'keyword4' => [
                'value' => "点此消息继续邀请好友助力，还可获得更多抽奖码，增加中奖概率~",
            ],
        ];
        $qqTemplate = new Template();
        $qqTemplate->setUuid($uuid)
            ->setTitle($title)
            ->toUser($to_uid)
            ->setTemplateId($template_id)
            ->setParameters($parameters)
            ->setPage($page)
            ->send();
    }


    /**
     * 福利中心-活动开奖结果通知
     * @param int $to_uid
     * @param int $id
     */
    public function afterLottery($to_uid, $id)
    {
        $title = "福利中心-活动开奖结果通知";
        $lottery = LotteryModel::findOrFail($id);
        $user = UserModel::find($to_uid);
        $payload = [
            'user' => $user->toArray(),
            'lottery' => $lottery->toArray(),
        ];
        dispatch(new RegistEventJob(10602, $payload))->onQueue("{push}");

        // QQ通知
        $uuid = uuid();
        $template_id = "a1c1ba47f520de5bd1b5c064dc915108";
        $page = "reward/pages/welfare/welfare";
        $parameters = [
            'keyword1' => [
                'value' => "开奖结果已出",
            ],
            'keyword2' => [
                'value' => $lottery->title,
            ],
            'keyword3' => [
                'value' => date("Y-m-d 20:00"),
            ],
            'keyword4' => [
                'value' => "点此消息查看开奖结果",
            ],
        ];
        $qqTemplate = new Template();
        $qqTemplate->setUuid($uuid)
            ->setTitle($title)
            ->toUser($to_uid)
            ->setTemplateId($template_id)
            ->setParameters($parameters)
            ->setPage($page)
            ->send();
    }


    /**
     * 福利中心-好友助力获得抽奖码-助力者
     * @param int $to_uid
     * @param int $friend_uid
     */
    public function helpToScore($to_uid, $friend_uid)
    {
        $title = "福利中心-好友助力获得抽奖码-助力者";

        $friend_user = UserModel::select('nickname')->find($friend_uid);

        $user = UserModel::find($to_uid);
        $payload = [
            'user' => $user->toArray(),
            'friend_user' => $friend_user->toArray(),
        ];
        dispatch(new RegistEventJob(10603, $payload))->onQueue("{push}");


        // QQ通知
        $uuid = uuid();
        $template_id = "172b97e61188c41c15477a48181ef6c0";
        $page = "reward/pages/welfare/welfare";
        $parameters = [
            'keyword1' => [
                'value' => "你成功为你的好友@{$friend_user->nickname} 助力获取抽奖码",
            ],
            'keyword2' => [
                'value' => "助力成功，你的好友已获取额外抽奖码",
            ],
            'keyword3' => [
                'value' => "福利中心",
            ],
            'keyword4' => [
                'value' => "点此消息可参与福利抽奖，抽皮肤，抽视频会员",
            ],
        ];
        $qqTemplate = new Template();
        $qqTemplate->setUuid($uuid)
            ->setTitle($title)
            ->toUser($to_uid)
            ->setTemplateId($template_id)
            ->setParameters($parameters)
            ->setPage($page)
            ->send();
    }


    /**
     * 福利中心-好友助力获得抽奖码-分享者
     * @param int $to_uid
     * @param int $friend_uid
     */
    public function inviteToScore($to_uid, $friend_uid)
    {
        $title = "福利中心-好友助力获得抽奖码-分享者";

        $user = UserModel::find($to_uid);
        $friend_user = UserModel::find($friend_uid);
        $payload = [
            'user' => $user->toArray(),
            'friend_user' => $friend_user->toArray(),
        ];
        dispatch(new RegistEventJob(10604, $payload))->onQueue("{push}");

        // QQ通知
        $uuid = uuid();
        $template_id = "172b97e61188c41c15477a48181ef6c0";
        $page = "reward/pages/welfare/welfare";
        $parameters = [
            'keyword1' => [
                'value' => "获得额外抽奖码",
            ],
            'keyword2' => [
                'value' => "你的好友@{$friend_user->nickname}成功为你增加了抽奖机会",
            ],
            'keyword3' => [
                'value' => "福利中心",
            ],
            'keyword4' => [
                'value' => "点此查看抽奖详情",
            ],
        ];
        $qqTemplate = new Template();
        $qqTemplate->setUuid($uuid)
            ->setTitle($title)
            ->toUser($to_uid)
            ->setTemplateId($template_id)
            ->setParameters($parameters)
            ->setPage($page)
            ->send();
    }
}
