<?php

namespace App\Console\Commands\Upgrade;

use App\Models\PartnerModel;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Redis;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '优化信息流速度:提取卡片信息到缓存中';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        /** @var Collection $partners */
        $partners = PartnerModel::where(array(
            ['is_sell', 1],
            ['score', '>=', 0]
        ))->get(['uid', 'sex', 'feed_push_type', 'is_push_feed']);
        foreach ($partners as $partner) {
            /** @var PartnerModel $partner */
            if (!in_array($partner->sex, [1, 2])) {
                continue;
            }
            if ($partner->feed_push_type == 3 && 1 == $partner->is_push_feed) {
                $key = "feed:80:sex:{$partner->sex}";
            } else {
                $key = "feed:20:sex:{$partner->sex}";
            }
            Redis::sadd($key, [$partner->uid]);
        }

        Redis::expire("feed:80:sex:1", 86400 * 7);
        Redis::expire("feed:80:sex:2", 86400 * 7);
        Redis::expire("feed:20:sex:1", 86400 * 7);
        Redis::expire("feed:20:sex:2", 86400 * 7);
    }
}
