<?php

namespace App\Jobs;

use App\Models\Fpdx\PairModel;
use App\Models\User\AuthKey;
use App\Models\User\UserModel;
use App\Services\QQMiniApp\Template;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

/**
 * Class NewOpenidNotice4Job
 * @package App\Jobs
 */
class NewOpenidNotice4Job implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    private $openid;

    /**
     * @var int 当前执行次数
     */
    private $execCnt;

    /**
     * Create a new job instance.
     *
     * @param  string  $openid
     * @param  int  $execCnt
     */
    public function __construct(string $openid, int $execCnt = 0)
    {
        $this->openid = $openid;
        $this->execCnt = $execCnt;
    }

    /**
     * Execute the job.
     *
     * @return bool
     */
    public function handle()
    {
        if ($this->sendQQ($this->openid)) {
            return true;
        } else {
            if ($this->execCnt < 3) {
                NewOpenidNotice4Job::dispatch($this->openid, $this->execCnt + 1)->delay(Carbon::now()->addDay(1));
            }
        }
    }

    /**
     * @param  string  $openid
     * @return bool
     */
    public function sendQQ(string $openid)
    {
        try {
            $auth = AuthKey::where('auth_key', $openid)->first();
            if (collect($auth)->isEmpty()) {
            } else {
                try {
                    $user = UserModel::findOrFail($auth->uid);
                    if ($user->login_at > mktime(0, 0, 0)) {
                        return true;
                    }
                    if (PairModel::where('uid', $user->uid)->exists() || $user->partner_id > 0) {
                        return true;
                    }
                } catch (\Exception $exception) {
                }
            }

            $templates = array(
                [
                    'template_id' => '8a4e8c0f73be381f49935dd20d79b6b9',
                    'data' => [
                        'keyword1' => array(
                            'value' => '交友信息待完善'
                        ),
                        'keyword2' => array(
                            'value' => "完善你的交友信息，小象就能把你推荐给更多与你契合的人，助你快速脱单~"
                        ),
                        'keyword3' => array(
                            'value' => "暂无访客"
                        )
                    ]
                ]
            );
            $template = $templates[rand(0, count($templates) - 1)];
            $title = '新用户引导隔3日通知';
            $page = "pages/my-friend-card-editor/my-friend-card-editor";
            $app = new Template(config("qqprogram.app_id"));
            $app->setTitle($title)
                ->setUuid(uuid())
                ->setParameters($template['data'])
                ->setTemplateId($template['template_id'])
                ->setPage($page)
                ->toUser($openid)
                ->send();
            if (0 == $app->getCode()) {
                return true;
            }
            return false;
        } catch (\Exception $exception) {
            return false;
        }
    }
}
