<?php

namespace App\Jobs;

use App\Models\PraiseModel;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Redis;

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

    private $uid;
    private $key;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $max = time() - 7 * 86400;
        $ids = Redis::Zrevrangebyscore($this->key, $max, "-inf");
        $thumbIds = PraiseModel::where([['uid', $this->uid], ['type', 1]])->whereIn(
            'partner_id',
            $ids
        )->get(['partner_id'])->pluck('partner_id')->toArray();
        $cleanIds = array_merge(array_diff($ids, $thumbIds), [1]);
        Redis::zrem($this->key, ...$cleanIds);
        $mockids = Redis::zrevrangebyscore("fpdx:mock:feed:{$this->uid}", $max, "-inf");
        $cleanIds = array_merge(array_diff($mockids, $thumbIds), [1]);
        Redis::zrem("fpdx:mock:feed:{$this->uid}", ...$cleanIds);
    }
}
