<?php


namespace App\Services;


use App\MaterialBox;
use Illuminate\Support\Facades\Cache;

class MaterialBoxService
{

    function get(array $kvPairs){
        ksort($kvPairs);
        return Cache::remember('MaterialBox_'.md5(json_encode($kvPairs)), config('cache.expirations.rarelyChange'), function ()use($kvPairs) {
            $query = MaterialBox::query();
            foreach ($kvPairs as $column => $value){
                if (is_array($value))$query->whereIn($column,$value);
                else $query->where($column,$value);
            }
            return $query->get();
        });
    }

    function firstOrCreate(array $kvPairs){
        $materialBox=$this->get($kvPairs);
        if(!$materialBox)
            return MaterialBox::query()->create($kvPairs);
    }
}
