<?php

namespace App\Console\Commands;

use App\Http\Models\PrKeywordExtend;
use App\Http\Models\Site;
use Illuminate\Console\Command;

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

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



    /**
     * 删除备注
     * @return mixed
     */
    public function handle()
    {
        //每个项目最新的一次的记录的id
        $maxIds = PrKeywordExtend::query()->selectRaw('max(id) as id_max,site_id')
            ->groupBy('site_id')
            ->pluck('id_max', 'site_id')->toArray();
        //3个月内没有拓展过
        $list = PrKeywordExtend::query()->whereIn('id', $maxIds)->get();
        $ids = [];
        $date = date("Ym", strtotime("last day of -3 month", strtotime(date("Ym"))));

        foreach ($list as $value) {
            if ($value->ym <= $date) {
                $ids[] = $value->site_id;
            }
        }
        Site::query()
            ->whereIn('id', $ids)
            ->whereIn('status', [2, 3])
            ->update(['keyword_memo' => '']);

        //从未拓展过
        $date = date("Y-m-d", strtotime("last day of -3 month", strtotime(date("Y-m-d"))));
        $id = Site::query()
            ->where('online_at', '<=', $date)
            ->whereIn('status', [2, 3])
            ->pluck('id')->toArray();

        $ids1 = [];
        foreach ($id as $value) {
            $result = PrKeywordExtend::query()->where('site_id', $value)->first();
            if (empty($result)) {
                $ids1[] = $value;
            }
        }

        Site::query()
            ->whereIn('id', $ids1)
            ->whereIn('status', [2, 3])
            ->update(['keyword_memo' => '']);
    }

}