<?php


namespace App\Services;


use App\Batch;
use App\OrderCommodity;
use App\StationTaskCommodity;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;

class StationTaskCommodityService
{
    /** @var StationService $stationService */
    private $stationService;
    /** @var StationTypeService $stationTypeService */
    private $stationTypeService;
    /** @var StationTaskService $stationTaskService */
    private $stationTaskService;
    /** @var MaterialBoxService $materialBoxService */
    private $materialBoxService;
    public function __construct(){
        $this->stationService=null;
        $this->stationTypeService=null;
        $this->stationTaskService=null;
        $this->materialBoxService=null;
    }
    function get(array $kvPairs){
        ksort($kvPairs);
        return Cache::remember('stationTaskChild_'.md5(json_encode($kvPairs)), config('cache.expirations.oftenChange'), function ()use($kvPairs) {
            $query = StationTaskCommodity::query();
            foreach ($kvPairs as $column => $value){
                if (is_array($value))$query->whereIn($column,$value);
                else $query->where($column,$value);
            }
            return $query->get();
        });
    }
    function createByBatches(array $batches,Collection $stationTasks_toAttach): Collection
    {
        $this->stationTaskService=app('StationTaskService');
        $stationTaskCommodities_listByBatch=new Collection();
        foreach ($batches as $batch){
            $stationTaskCommodities_listByBatch->push(
                $this->createByBatch($batch)
            );
        }
        $this->stationTaskService->registerSubTasks($stationTasks_toAttach,$stationTaskCommodities_listByBatch);

    }

    function createByBatch(Batch $batch): Collection
    {
        $this->stationTypeService=app('StationTypeService');
        $this->stationService=app('StationService');
        $this->materialBoxService=app('MaterialBoxService');
        $stationTaskCommodities_toCreate=new Collection();
        $order_ids=data_get($batch['orders'],'*.id');
        $orderCommodities=OrderCommodity::query()->with('orderBin')->whereIn('order_id',$order_ids)->get();
        foreach ($orderCommodities as $orderCommodity){
            $stationType=$this->stationTypeService->getForCommodity();
            $station=$this->stationService->getStation_byType($stationType['name']);
            $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
            $stationTaskCommodities_toCreate->push([
                'station_id'=>$station['id'],
                'material_box_id'=>$materialBox['id'],
                'commodity_id'=>$orderCommodity['commodity_id'],
                'amount'=>$orderCommodity['amount'],
                'bin_number'=>$orderCommodity['orderBin']['number']??'',
                'order_id'=>$orderCommodity['order_id'],
                'status'=>'待处理'
            ]);
        }
        $this->insert($stationTaskCommodities_toCreate->toArray());
    }

    public function insert(array $stationTaskCommodities): bool
    {
        $inserted = StationTaskCommodity::query()->insert($stationTaskCommodities);
        LogService::log(__METHOD__,__FUNCTION__,json_encode($stationTaskCommodities).
            '||'.json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),0,3)));
        return $inserted;
    }
}
