<?php

namespace App\Jobs;

use App\Models\ErrorLog;
use App\Models\ErrorUser;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class AddErrorLog implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 1;
    protected $message;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $log = ErrorLog::create(['message'=>$this->message, 'status'=>0]);
        //通知
        $param = [
            'template_id' => config('wechat.tpls.error_log_notice'),
//            'template_id'=>'7Tj2vYAIGaYrPd2uyQwRp98MhzTCX7SL5LV97W9jn1A',
            'url' => config('app.url').'/error/logs/'.$log->id,
//            'miniprogram' => [
//                'appid' => config('wechat.mini_program.app_id'),
//                'pagepath' => 'pages/tabBar/serve',
//            ],
            'data' => [
                'time2' => ['value'=>date('Y-m-d H:i:s')],
                'thing4' => ['value'=>'友福健康'],
            ],
        ];
        $users = ErrorUser::where('is_banned', 0)->get();
        foreach ($users as $user) {
            $param['touser'] = $user->official_openid;
            SendTemplateNotice::dispatch($param)->onQueue('health');
        }
    }
}
