<?php 

namespace App\Services; 

use App\Logistic;
use App\Owner;
use App\OwnerPriceExpress;
use App\OwnerPriceExpressProvince;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

Class OwnerPriceExpressService
{ 

    public function paginate($id = null)
    {
        $query = OwnerPriceExpress::query()->with(["owners","logistics"])
            ->orderByDesc("id");
        if ($id)$query->where("id",$id);
        return $query->paginate(50);
    }

    public function find($id, $withs = [])
    {
        return OwnerPriceExpress::query()->with($withs)->find($id);
    }

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

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

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

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

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

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

    public function getExistLogisticName($logistic_id, $id=null):array
    {
        $logistics = Logistic::query()->withCount(["ownerPriceExpresses"=>function($query)use($id){
            if ($id)$query->where("id","!=",$id);
        }])->whereIn("id",$logistic_id)->get();
        $arr = [];
        foreach ($logistics as $logistic){
            if ($logistic->owner_price_expresses_count > 0)$arr[] = $logistic->name;
        }
        return $arr;
    }

    public function destroy($id)
    {
        OwnerPriceExpressProvince::query()->where("owner_price_express_id",$id)->delete();
        DB::table("owner_price_express_owner")->where("owner_price_express_id",$id)->delete();
        DB::table("owner_price_express_logistic")->where("owner_price_express_id",$id)->delete();
        return OwnerPriceExpress::destroy($id);
    }

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

    /**
     * CODE： -1:未找到计费模型
     *
     * @param double $weight
     * @param integer $owner_id
     * @param integer $logistic_id
     * @param integer $province_id
     * @return double
     */
    public function matching($weight, $owner_id, $logistic_id, $province_id)
    {
        $model = OwnerPriceExpress::query()->with(["details"=>function($query)use($province_id){
            /** @var Builder $query */
            $query->where("province_id",$province_id);
        }])->whereHas("owners",function ($query)use($owner_id){
            /** @var Builder $query */
           $query->where("id",$owner_id);
        })->whereHas("logistics",function ($query)use($logistic_id){
            /** @var Builder $query */
            $query->where("id",$logistic_id);
        })->first();
        if (!$model || !$model->details)return -1;
        if ($weight < $model->initial_weight)$weight = $model->initial_weight;
        $initialMoney = $model->initial_weight*$model->details[0]->initial_weight_price;
        $weight -= $model->initial_weight;
        return ($weight*$model->details[0]->additional_weight_price)+$initialMoney;
    }
}