<?php

namespace App\Services;

use App\OracleBasCustomer;
use App\Owner;
use App\Warehouse;
use Illuminate\Support\Collection;
use Carbon\Carbon;

Class WarehouseService
{

    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()->where('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){
            $wereHouse=Warehouse::query()->create([
                'code'=>$oracleBasCustomer->customerid,
                'name'=>$oracleBasCustomer->descr_c
            ]);
            $collet->push($wereHouse);
        }
        return  $collet;
    }
    public function getByWms($asnHerders)
    {
        $warehouseids = array_unique(data_get($asnHerders, '*.warehouseid'));
        $warehouseids = array_diff($warehouseids, [null, '', '*']);
        $warehouses = Warehouse::query()->whereIn('code', $warehouseids)->get();
        if ($warehouses->count() < count($warehouseids)) {
            $warehouseids = array_diff($warehouseids, data_get($warehouses, '*.code'));
            $warehouse_list = $this->createWarehouseByWms($warehouseids);
            $warehouses=$warehouses->concat($warehouse_list);
        }
        return $warehouses;
    }

    public function createWarehouseByWms($codes)
    {
        if(!$codes) {return [];}
        $insert_params = [];
        $created_at = Carbon::now()->format('Y-m-d H:i:s');
        foreach ($codes as $item) {
            $insert_params[] = [
                'code' => $item,
                'name' => $item,
                'created_at' => $created_at,
            ];
        }
        try {
            if (count($insert_params) > 0) {
                $this->insert($insert_params);
                LogService::log(__METHOD__, __FUNCTION__, '批量创建 warehouse success' . count($insert_params) . json_encode($insert_params) );
            }
        } catch (\Exception $e) {
            LogService::log(__METHOD__, __FUNCTION__, '批量创建 warehouse error' . json_encode($insert_params) . '||' . $e->getMessage() . '||' . $e->getTraceAsString());
        } finally {
            return Warehouse::query()->whereIn('code', $codes)->get();
        }
    }
    public function insert($fillables){
        return Warehouse::query()->insert($fillables);
    }

}
