<?php

namespace App\Services;

use App\ValueStore;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Yajra\Oci8\Query\OracleBuilder;
use App\Traits\ServiceAppAop;


class DocWaveHeaderService
{
    use ServiceAppAop;
    protected $modelClass=DocWaveHeader::class;

    public function get(array $params, $rule = [])
    {
        /** @var OracleBuilder $query */
        $query = DB::connection("oracle")->table("doc_wave_header")->orderBy("edittime");
        foreach ($params as $column=>$param){
            if (isset($rule[$column]))$query = $this->rule($query,$rule[$column],$column,$param);
            else $query->where($column,$param);
        }
        return $query->get();
    }
    private function rule(OracleBuilder $query, $rule, $column, $value)
    {
        switch ($rule){
            case "gtOrEqual":
                $query->where($column,">=",$value);
                break;
            case "gt":
                $query->where($column,">",$value);
                break;
            case "raw":
                $query->whereRaw($value);
                break;
            case "in":
                $query->whereIn($column,$value);
                break;
        }
        return $query;
    }


    public function getSyncDate()
    {
        $valueStore = ValueStore::query()->where("name","wave_last_sync_date")->first();
        return $valueStore->value ?? Carbon::now()->subSeconds(65)->toDateTimeString();
    }

    public function setSyncDate(string $value)
    {
        $valueStore = ValueStore::query()->where("name","wave_last_sync_date");
        return $valueStore->update(["value"=>$value]);
    }
}
