<?php

namespace App\Console\Commands;

use App\OwnerAreaReport;
use App\Services\LogService;
use App\Services\OwnerService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'create owner area report';

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

    /**
     * TODO 25号生成盘点面积记录，记录留空由人工填写
     *
     * @return void
     */
    public function handle()
    {
        /** @var OwnerService $ownerService */
        $ownerService = app('OwnerService');
        $chunks = ($ownerService->get([],["ownerStoragePriceModels"],false,true))->chunk(50);
        $month = date('Y-m-d');
        foreach ($chunks as $owners){
            $date = date('Y-m-d H:i:s');
            $createOwnerAreaReport = [];
            foreach ($owners as $owner){
                if (!$owner->ownerStoragePriceModels)continue;
                foreach ($owner->ownerStoragePriceModels as $model){
                    $createOwnerAreaReport[] = [
                        "owner_id"              => $owner->id,
                        "counting_month"        => $month,
                        "user_owner_group_id"   => $owner->user_owner_group_id,
                        "created_at"            => $date,
                        "owner_storage_price_model_id"  => $model->id,
                    ];
                }
            }
            DB::table("owner_area_reports")->insert($createOwnerAreaReport);
            LogService::log(__METHOD__,"客户管理-定时生成盘点记录",json_encode($createOwnerAreaReport));
        }
    }
}
