<?php

namespace App\Services;

use App\OracleBasCustomer;
use App\Traits\ServiceAppAop;


class OracleBasCustomerService
{
    use ServiceAppAop;
    protected $modelClass=OracleBasCustomer::class;
    /** @var CacheService $cacheService */
    private $cacheService;

    function __construct(){
        $this->instant($this->cacheService,'CacheService');
    }

    public function get(array $params){
        $query = $this->query($params);
        return $query->get();
    }
    public function first(array $params){
        return $this->query($params)->first();
    }

    public function count(array $params){
        $query = $this->query($params);
        return $query->count();
    }

    private function query(array $params){
        $query = OracleBasCustomer::query();
        foreach ($params as $column => $value){
            if (is_array($value))$query->whereIn($column,$value);
            else $query->where($column,$value);
        }
        return $query;
    }

    public function getWareHouse($customerIDs = null)
    {
        if(!$customerIDs){
            return OracleBasCustomer::query()->selectRaw('Customer_Type,CustomerId,Descr_C')->where('Customer_Type','WH')->get();
        }
        return  OracleBasCustomer::query()
            ->selectRaw('Customer_Type,CustomerId,Descr_C')
            ->where('Customer_Type','WH')
            ->whereIn('CustomerId',$customerIDs)
            ->get();
    }
    public function getCustomers($codes)
    {
        return $this->cacheService->getOrExecute('OracleBasCustomersAll'.md5(json_encode($codes)),function()use($codes){
            return OracleBasCustomer::query()->select('customerid','descr_c')
                ->where('customer_type','OW')
                ->where('active_flag','Y')
                ->whereIn('customerid',$codes)
                ->get();
        },config('cache.expirations.owners'));
    }

}
