<?php

namespace App\Console\Commands\Upgrade;

use App\Models\PartnerModel;
use App\Models\User\FeedcheckLogModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * 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()
    {
        $deamon = $this->option('d');
        $npassDatas = FeedcheckLogModel::where([['is_pass', 0], ['expire_at', 0]])
            ->groupBy('partner_id')
            ->orderBy('partner_id', 'asc')->select([DB::raw("`partner_id`, COUNT(*) * 5 as cnt")])->get();
        $passDatas = FeedcheckLogModel::where([['is_pass', 1], ['expire_at', 0]])
            ->groupBy('partner_id')
            ->orderBy('partner_id', 'asc')->select([DB::raw("`partner_id`, COUNT(*) * 5 as cnt")])->get();
        $bar = null;
        if (!$deamon) {
            $bar = $this->output->createProgressBar($npassDatas->count() + $passDatas->count());
            $bar->start();
        }
        foreach ($npassDatas as $data) {
            PartnerModel::where([
                ['id', $data->partner_id],
                ['feed_tag_cnt', 0]
            ])->update([
                'feed_tag_cnt' => $data->cnt
            ]);
            if (!$deamon) {
                $bar->advance(1);
            }
        }

        foreach ($passDatas as $data) {
            PartnerModel::where([
                ['id', $data->partner_id],
                ['feed_tag_like', 0]
            ])->increment('feed_tag_cnt', $data->cnt);
            PartnerModel::where([
                ['id', $data->partner_id],
                ['feed_tag_like', 0]
            ])->increment('feed_tag_like', $data->cnt);
            if (!$deamon) {
                $bar->advance(1);
            }
        }
        $sql = "UPDATE `kdgx_partner_charge_partner`
   set `feed_tag_cnt`= 100,
       `feed_tag_like`= IF(1= `sex`, 30, 60)
 where `is_recommend`= 1";
        DB::statement($sql);
        if (!$deamon) {
            $bar->finish();
        }
    }
}
