<?php

namespace App\Services;

use App\OracleBasCustomer;
use App\Owner;
use App\Warehouse;
use Illuminate\Support\Collection;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use App\Traits\ServiceAppAop;


Class WarehouseService
{
    use ServiceAppAop;
    /** @var CacheService $cacheService */
    private $cacheService;
    function __construct(){
        $this->instant($this->cacheService,'CacheService');
    }

    public function getSelection($column = ['id', 'name'])
    {
        return Warehouse::query()->select($column)->get();
    }

    public function firstOrCreate(array $params, array $values = null)
    {
        if ($values) return Warehouse::query()->firstOrCreate($params, $values);
        return Warehouse::query()->firstOrCreate($params);
    }

    public function getByWmsOrders($orderHeaders)
    {
        $wareHouse_Ids=array_diff(array_unique(data_get($orderHeaders,'*.warehouseid')),['',' ','*',null]);
        $wareHouses=Warehouse::query()->whereIn('code',$wareHouse_Ids)->get();
        if(count($wareHouse_Ids)>$wareHouses->count()){
            $需要添加_仓库编码=[];
            foreach ($wareHouse_Ids as $warehouse_id) {
                if($wareHouses->where('code')->count()==0){
                    $需要添加_仓库编码[]=$warehouse_id;
                }
            }
            $create_wareHouse=$this->syncWmsWareHouse($需要添加_仓库编码);
            $wareHouses = $wareHouses->concat($create_wareHouse ?? []);
        }
        return $wareHouses;
    }

    public function syncWmsWareHouse($warehouse_ids)
    {
        $collet = new Collection();
        if(!$warehouse_ids)return null;
        /** @var Collection $oracleBasCustomers */
        $oracleBasCustomers=app(OracleBasCustomerService::class)->getWareHouse($warehouse_ids);
        $warehouses=Warehouse::query()->whereIn('code',data_get($oracleBasCustomers,'*.customerid'))->get();
        $oracleBasCustomers=$oracleBasCustomers->filter(function($oracleBasCustomer)use($warehouses){
            return $warehouses->where('code',$oracleBasCustomer->warehouseid)->count()==0;
        });
        foreach ($oracleBasCustomers as $oracleBasCustomer){
            $params =[
                'code' => $oracleBasCustomer->customerid,
                'name' => $oracleBasCustomer->descr_c
            ];
            try {
                $wereHouse = Warehouse::query()->create($params);
                $collet->push($wereHouse);
                app('LogService')->log(__METHOD__,__FUNCTION__,'创建 warehouse success'.json_encode($wereHouse));
            } catch (\Exception $e) {
                app('LogService')->log(__METHOD__,__FUNCTION__,'创建 warehouse error'.json_encode($params).json_encode($e->getMessage()));
            }
        }
        return  $collet;
    }
    public function insert($fillables){
        return Warehouse::query()->insert($fillables);
    }

    public function getWareHouseByCodes($codes)
    {
        $collect = collect();
        if(count($codes)==0)return $collect;
        foreach ($codes as $code) {
            $collect->push($this->getWareHouseByCode($code));
        }
        return $collect;
    }

    public function getWareHouseByCode($code){
        return Cache::remember("WareHouse_{$code}",config('cache.expirations.warehouse'),function()use($code){
            $wareHouse =  Warehouse::query()->where('code',$code)->first();
            if($wareHouse)return $wareHouse;
            return null;
        });
    }
}

