<?php

namespace App\Console\Commands;

use App\OwnerReport;
use App\Services\OwnerService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class BeforeCreateOwnerReport extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'beforeCreateOwnerReport';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create owner report in advance and build cache';


    const TTL = 2764800;
    /**
     * Execute the console command.
     *
     */
    public function handle()
    {
        /** @var OwnerService $ownerService */
        $ownerService = app('OwnerService');
        $chunks = ($ownerService->get([],["ownerStoragePriceModels"],false,true))->chunk(50);
        foreach ($chunks as $owners){
            $insert = [];
            $date = date("Y-m-d");
            foreach ($owners as $owner){
                $insert[] = [
                    "owner_id"       => $owner->id,
                    "counting_month" => date("Y-m-d"),
                    "created_at"     => $date
                ];
                //A:件 B:business C:customer D:date
                Cache::put(date("Y-m")."|B|".$owner->id,0,self::TTL);
                Cache::put(date("Y-m")."|C|".$owner->id,0,self::TTL);
                Cache::put(date("Y-m")."|D|".$owner->id,time(),self::TTL);
                Cache::put(date("Y-m")."|A|".$owner->id,0,self::TTL);
            }
            OwnerReport::query()->insert($insert);
        }
    }
}
