<?php

namespace App\Console\Commands\Notices;

use App\Jobs\PushQQminiprogramNoticeJob;
use App\Models\User\AuthKey;
use Illuminate\Console\Command;

class PushQQMiniprogramNotice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'yy:qqpair';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '72hQQ小程序活动模版消息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $keyword1 = $this->ask("请输入keyword1的内容:");
        $keyword2 = $this->ask("请输入keyword2的内容:");
        $keyword3 = $this->ask("请输入keyword3的内容:");

        $data = array(
            'title' => '72小时活动报名运营通知',
            'template_id' => 'd149209e80157fd6e340a8049f7c02b7',
            'page' => "fpdx/pages/starter/starter",
            'parameters' => [
                'keyword1' => [
                    'value' => $keyword1,
                ],
                'keyword2' => [
                    'value' => $keyword2,
                ],
                'keyword3' => [
                    'value' => $keyword3,
                ]
            ]
        );

        $users = AuthKey::where('auth_type', config("qqprogram.public_id"))->get(['uid']);

        $bar = $this->output->createProgressBar($users->count());
        $bar->start();

        $k = 0;
        $uids = array();
        foreach ($users as $user) {
            array_push($uids, $user->uid);
            if (1000 == $k) {
                PushQQminiprogramNoticeJob::dispatch($uids, $data);
                $uids = array();
                $k = 0;
            }
            $k++;
            $bar->advance();
        }
        !empty($uids) && PushQQminiprogramNoticeJob::dispatch($uids, $data);

        $bar->finish();
    }
}
