<?php 

namespace App\Services; 

use App\OwnerPriceLogistic;
use App\OwnerPriceLogisticDetail;
use Illuminate\Database\Eloquent\Builder;

Class OwnerPriceLogisticService
{ 
    public function paginate($id = null)
    {
        $query = OwnerPriceLogistic::query()->with(["owners","logistics","unit","otherUnit"]);
        if ($id)$query->where("id",$id);
        return $query->paginate(50);
    }

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

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

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

    public function destroy($id)
    {
        OwnerPriceLogistic::destroy($id);
        OwnerPriceLogisticDetail::query()->where("owner_price_logistic_id",$id)->delete();
    }

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

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

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

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

    public function checkRange($range):bool
    {
        $arrRanges = explode(",",$range);
        $len = count($arrRanges);
        if ($len==0)return false;   //范围为空 false
        $previous = []; //慢指针数组 不记录第一个值
        foreach ($arrRanges as $index => $value){
            if ($index == $len-1)$preg = "/\d*\-/"; //最后一个范围不允许封闭
            else $preg = "/\d*\-\d*/";
            preg_match($preg, $value, $match);
            if (!$match || $match[0]!=$value)return false;//匹配结果不等于值 false
            $arr = explode("-",$value); //二次切割判断是否连续
            if ($index != 0){
                if ($index == $len-1){
                    if ((int)$arr[0] != (int)$previous[$index-1][1])return false; //最后一个范围只需要判断起始值
                }else{
                    if ((int)$arr[0] != (int)$previous[$index-1][1] || (int)$arr[1] <= (int)$arr[0])return false; //普通范围起始值必须为上一个结束值 当前结束值必须大于起始值
                }
            }else{
                if ((int)$arr[1] <= (int)$arr[0])return false; //第一个范围只需判断当前结束值大于起始值
            }
            $previous[] = $arr;
        }
        return true;
    }

    /**
     * CODE： -1:未找到计费模型
     *
     * @param double $amount
     * @param integer $owner_id
     * @param integer $logistic_id
     * @param integer $unit_id
     * @param integer $province_id
     * @param integer $city_id
     * @return double
     */
    public function matching($amount, $owner_id, $logistic_id, $unit_id, $province_id, $city_id)
    {
        $model = OwnerPriceLogistic::query()->with(["details"=>function($query)use($province_id,$city_id){
            /** @var Builder $query */
            $query->where("province_id",$province_id)->where("city_id",$city_id);
        }])->where(function ($query)use($unit_id){
            /** @var Builder $query */
            $query->where("unit_id",$unit_id)->orWhere("other_unit_id",$unit_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;
        $fee = $model->pick_up_price + $model->fuel_price + $model->service_price; //服务费
        foreach ($model->details as $detail){
            if ($unit_id != $detail->unit_id)continue;
            $arr = explode("-",$detail->range);
            if (count($arr) < 2){
                if ($amount >= $arr[0])return $this->calculation($amount,$detail,$fee);
            }else{
                if ($amount >= $arr[0] && $amount < $arr[1])return $this->calculation($amount,$detail,$fee);
            }
        }
        return -1;
    }

    private function calculation($amount, $detail, $fee)
    {
        if ($amount < $detail->initial_amount)$amount = $detail->initial_amount; //小于起始数以起始数为准
        $money = $amount * $detail->unit_price;
        if ($money < $detail->initial_fee)$money = $detail->initial_fee; //小于起始计费以起始计费为准
        return $money+$detail->delivery_fee+$fee;
    }
}