<?php

namespace App\Console\Commands\Recommend;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Recommend brands';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        foreach (DB::table('brands')->select('id')->get() as $brand) {
            DB::statement("UPDATE `brands` SET recom_store = (SELECT JSON_ARRAYAGG(store_id) FROM (SELECT  (store_id), SUM(clicks)   FROM `coupons` c  WHERE JSON_OVERLAPS(brands,'[{$brand->id}]') GROUP BY store_id ORDER BY  2  DESC LIMIT 5) st) WHERE id = " . $brand->id);
        }
    }
}
