<?php

namespace App\Listeners;

use App\Events\CreatedPartner;
use App\Models\PraiseModel;
use App\Models\User\AuthKey;
use App\Models\User\MockThumbModel;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Contracts\Queue\ShouldQueue;

/**
 * 同步mock点赞数据
 * Class MockThumb
 * @package App\Listeners
 */
class MockThumb implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  CreatedPartner  $event
     * @return void
     */
    public function handle(CreatedPartner $event)
    {
        try {
            $auth = AuthKey::where([
                ['uid', $event->model->uid],
                ['auth_type', config("miniprogram.public_id")]
            ])->firstOrFail();
        } catch (ModelNotFoundException $exception) {
            return;
        }
        $mocks = MockThumbModel::where('thumb_user', $auth->auth_key)->get();
        foreach ($mocks as $mock) {
            /** @var MockThumbModel $mock */
            PraiseModel::create([
                'uid' => $mock->uid,
                'partner_id' => $event->model->id,
                'create_at' => $mock->created_at->timestamp,
                'created_at' => $mock->created_at->timestamp,
                'updated_at' => $mock->created_at->timestamp,
                'type' => 1
            ]);
        }
        MockThumbModel::where('thumb_user', $auth->auth_key)->update(['sync' => 1]);
    }
}
