<?php


namespace App\Filters;


use App\Commodity;
use App\Order;
use App\OrderPackage;
use App\OrderPackageCommodities;
use App\Services\UserService;
use App\Traits\ModelSearchWay;
use App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;

class OrderTrackFilters
{
    use ModelSearchWay;

    protected $request;
    protected $queryBuilder;
    protected $params = [];
    protected $filters = ['client_code', 'order_client_code_wms',
        'logistic_id', 'client', 'web_order_number', 'sku', 'logistic_number','remark','ids',
        'start_at', 'end_at', 'start_planning_sent_at', 'end_planning_sent_at'];
    protected $array_filter;

    /** @var Builder $orderPackageCommodityQuery */
    protected $commodityQuery;
    /** @var Builder $orderPackageCommodityQuery */
    protected $orderPackageCommodityQuery;
    /** @var Builder $orderPackageQuery */
    protected $orderPackageQuery;
    /** @var Builder $orderQuery */
    protected $orderQuery;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->params = $request->all();
        $this->array_filter = array_filter($this->request->only($this->filters));
    }

    public function apply($builder)
    {
        $this->queryBuilder = $builder;
        $this->afterApply();
        foreach ($this->array_filter as $filter => $value) {
            if (method_exists($this, $filter)) {
                $this->$filter($value, $this->queryBuilder);
            }
        }
        $this->beforeApply();
        return $this->queryBuilder;
    }

    public function afterApply()
    {
        if(isset($this->params['data']))
            $this->id(explode(',',$this->params['data']));

        // 货主
        $user = Auth::user();
        $owner_ids = $user ? app(UserService::class)->getPermittingOwnerIds($user) : [];
        if (isset($this->params['owner_id'])) {
            $ownerIds = explode(",", $this->params['owner_id']);
            $owner_ids = array_intersect($owner_ids, $ownerIds);
        }
        $this->ownerId($owner_ids);
    }

    public function getCommodityQuery(): Builder
    {
        if(!$this->commodityQuery)
            $this->commodityQuery = Commodity::query()->selectRaw('id');
        return $this->commodityQuery;
    }

    public function getOrderPackageCommodityQuery(): Builder
    {
        if(!$this->orderPackageCommodityQuery)
            $this->orderPackageCommodityQuery = OrderPackageCommodities::query()->selectRaw('id');
        return $this->orderPackageCommodityQuery;
    }

    public function getOrderPackageQuery(): Builder
    {
        if(!$this->orderPackageQuery)
            $this->orderPackageQuery = OrderPackage::query()->selectRaw('id');
        return $this->orderPackageQuery;
    }

    public function getOrderQuery(): Builder
    {
        if(!$this->orderQuery)
            $this->orderQuery = Order::query()->selectRaw('id');
        return $this->orderQuery;
    }

    public function client_code($client_code)
    {
        $this->searchWay($this->queryBuilder,$client_code,'order_trackings.order_client_code');
    }

    public function order_client_code_wms($order_client_code_wms)
    {
        $this->searchWay($this->queryBuilder,$order_client_code_wms,'order_trackings.order_client_code_wms');
    }

    public function logistic_id($logistic_id)
    {
        $this->queryBuilder->where('order_trackings.logistic_id',$logistic_id);
    }

    public function client($client)
    {
        $this->queryBuilder->where('order_trackings.client','like',$client.'%');
    }

    public function web_order_number($web_order_number)
    {
        $this->searchWay($this->queryBuilder,$web_order_number,'order_trackings.web_order_number');
    }

    public function sku($sku)
    {
        $this->searchWay($this->getCommodityQuery(),$sku,'sku');
        $this->getOrderPackageCommodityQuery()->whereIn('commodity_id',$this->getCommodityQuery());
    }

    public function logistic_number($logistic_number)
    {
        $this->searchWay($this->getOrderPackageQuery(),$logistic_number,'logistic_number');
    }

    public function remark($remark)
    {
        $this->queryBuilder->where('order_trackings.remark','like','%'.$remark.'%');
        if(isset($this->params['remark_addtime'])){
            $this->queryBuilder->where('order_trackings.created_at','>',Carbon::now()->subDays($this->params['remark_addtime']));
        }else{
            $this->queryBuilder->where('order_trackings.created_at','>=',Carbon::now()->subDays(15));
        }
    }

    public function id($id)
    {
        if(is_array($id))$this->queryBuilder->whereIn('order_trackings.id',$id);
        else $this->queryBuilder->where('order_trackings.id',$id);
    }

    public function ownerId($ids)
    {
        $this->getOrderQuery()->whereIn('orders.owner_id',$ids);
    }


    public function start_at($start_at)
    {
        $this->queryBuilder->where('order_trackings.pick_up_at','>=',$start_at.' 00:00:00');
    }

    public function end_at($end_at)
    {
        $this->queryBuilder->where('order_trackings.pick_up_at','<=',$end_at.' 23:59:59');
    }

    public function start_planning_sent_at($start_planning_sent_at)
    {
        $this->queryBuilder->where('order_trackings.planning_sent_at','>=',$start_planning_sent_at.' 00:00:00');
    }

    public function end_planning_sent_at($end_planning_sent_at)
    {
        $this->queryBuilder->where('order_trackings.planning_sent_at','<=',$end_planning_sent_at.' 23:59:59');
    }

    public function beforeApply()
    {
        if($this->orderQuery)
            $this->getOrderPackageQuery()->whereIn('order_packages.order_id',$this->orderQuery);

        if($this->orderPackageQuery)
            $this->getOrderPackageCommodityQuery()->whereIn('order_package_commodities.order_package_id',$this->getOrderPackageQuery());

        if($this->commodityQuery)
            $this->getOrderPackageCommodityQuery()->whereIn('order_package_commodities.commodity_id',$this->commodityQuery);

        if($this->orderPackageCommodityQuery)
            $this->queryBuilder->whereIn('order_trackings.order_package_commodity_id',$this->getOrderPackageCommodityQuery());

    }
}
