<?php

namespace App\Jobs;

use App\Models\Deed\FriendsModel;
use App\Models\Partner\ThumbActionModel;
use App\Models\PartnerModel;
use App\Models\PraiseModel;
use App\Models\User\SuperLikeModel;
use App\Models\User\UserModel;
use App\Services\Deed\FriendService;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class StoreSelfPartnerJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    private $uid;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $datas = ThumbActionModel::where([
            ['uid', $this->uid],
            ['thumb', 1]
        ])->get();
        foreach ($datas as $data) {
            try {
                list($uid, $partner_id) = array($data->uid, $data->partner_id);
                dump($uid, $partner_id);
                $user = UserModel::findOrFail($uid);

                $partner = PartnerModel::findOrFail($partner_id);

                $pm = new PraiseModel();
                $praise = $pm->where(array(['partner_id', $partner_id], ['uid', $uid]))->first();

                if (collect($praise)->isEmpty()) {
                    $lock = 0;
                    if (
                        SuperLikeModel::where(array(
                        ['uid', $partner->uid],
                        ['partner_id', $user->partner_id]
                        ))->exists()
                    ) {
                        SuperLikeModel::where(array(
                            ['uid', $partner->uid],
                            ['partner_id', $user->partner_id]
                        ))->update(['reply_at' => time()]);
                        $lock = 100;
                    }

                    if (FriendsModel::where(array(['uid', $uid], ['friend_uid', $partner->uid]))->exists()) {
                        $pm->where(array(
                            ['uid', $partner->uid],
                            ['partner_id', $user->partner_id]
                        ))->update(['lock' => 100]);
                        $lock = 100;
                    }

                    $praise = $pm->fill([
                        'create_at' => time(),
                        'created_at' => $data->created_at->timestamp,
                        'updated_at' => $data->updated_at->timestamp,
                        'partner_id' => $partner_id,
                        'uid' => $uid,
                        'type' => 1,
                        'read' => 0,
                        'lock' => $lock
                    ]);
                    PartnerModel::where('id', $partner_id)->increment('praises', 1);

                    // 检验相互喜欢
                    $fs = new FriendService();
                    if ($datauser = $fs->eachlike($uid, $partner_id, 1)) {
                        $res['eachlike'] = true;
                        $res['user'] = $datauser;
                        $praise->lock = 100;
                        $pm->where(array(
                            ['uid', $partner->uid],
                            ['partner_id', $user->partner_id]
                        ))->update(['lock' => 100]);
                    }

                    // 点赞我通知
                    $ns = new \App\Services\User\NoticeService();
                    $ns->thumbMe($uid, $partner_id);
                    $praise->save();
                }

                $data->thumb = 0;
                $data->save();
            } catch (\Exception $exception) {
                continue;
            }
        }
    }
}
