<?php 

namespace App\Services; 

use App\Owner;
use App\OwnerPriceDirectLogistic;
use App\OwnerPriceDirectLogisticCar;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

Class OwnerPriceDirectLogisticService
{ 
    public function paginate($id = null)
    {
        $query = OwnerPriceDirectLogistic::query()->with("owners");
        if($id)$query->where("id",$id);
        return $query->paginate(50);
    }

    public function create(array $params)
    {
        return OwnerPriceDirectLogistic::query()->create($params);
    }

    public function destroy($id)
    {
        OwnerPriceDirectLogisticCar::query()->where("owner_price_direct_logistic_id",$id)->delete();
        DB::table("owner_price_direct_logistic_owner")->where("owner_price_direct_logistic_id",$id)->delete();
        return OwnerPriceDirectLogistic::destroy($id);
    }

    public function find($id)
    {
        return OwnerPriceDirectLogistic::query()->find($id);
    }

    public function update(array $params, array $values)
    {
        $query = OwnerPriceDirectLogistic::query();
        foreach ($params as $column=>$param){
            $query->where($column,$param);
        }
        return $query->update($values);
    }

    public function updateDetail(array $params, array $values)
    {
        $query = OwnerPriceDirectLogisticCar::query();
        foreach ($params as $column => $param){
            $query->where($column,$param);
        }
        return $query->update($values);
    }

    public function isExistDetail(array $params)
    {
        $query = OwnerPriceDirectLogisticCar::query();
        foreach ($params as $column => $param){
            $query->where($column,$param);
        }
        return $query->count();
    }

    public function createDetail(array $params)
    {
        return OwnerPriceDirectLogisticCar::query()->create($params);
    }

    public function destroyDetail($id)
    {
        return OwnerPriceDirectLogisticCar::destroy($id);
    }

    public function getExistOwnerName($owner_id, $id) :array
    {
        if (!is_array($owner_id))$owner_id = [$owner_id];
        $owners = Owner::query()->withCount(["ownerPriceDirectLogistics"=>function($query)use($id){
            if ($id)$query->where("id","!=",$id);
        }])->whereIn("id",$owner_id)->get();
        $arr = [];
        foreach ($owners as $owner){
            if ($owner->owner_price_direct_logistics_count > 0)$arr[] = $owner->name;
        }
        return $arr;
    }


    /**
     * CODE： -1:未找到计费模型
     *
     * @param double $amount
     * @param integer $owner_id
     * @param integer $car_id
     * @return double
     */
    public function matching($amount, $owner_id, $car_id)
    {
        $model = OwnerPriceDirectLogistic::query()->with(["details"=>function($query)use($car_id){
            /** @var Builder $query */
            $query->where("car_type_id",$car_id);
        }])->whereHas("owners",function ($query)use($owner_id){
            /** @var Builder $query */
            $query->where("id",$owner_id);
        })->first();
        if (!$model || !$model->details)return -1;
        if ($amount < $model->base_km)$amount = $model->base_km;
        $initialMoney = $model->base_km*$model->details[0]->base_fee;
        $amount -= $model->base_km;
        return ($amount*$model->details[0]->additional_fee)+$initialMoney;
    }
}