<?php

namespace App\Services;

use App\Logistic;
use App\LogisticTiming;
use App\Province;
use App\Traits\ServiceAppAop;


class LogisticTimingService
{
    use ServiceAppAop;
    protected $modelClass=LogisticTiming::class;

    public function findByParams($cityName,$provinceName,$logisticId)
    {
        $logistic = Logistic::query()->where('id',$logisticId)->first();
        if(!str_starts_with($logistic->name,'新杰')){
            $city = app(CityService::class)->findByName($cityName);
            $province = app(ProvinceService::class)->findByName($provinceName);
            $logistic = Logistic::query()->where('name','顺丰特惠')->first();
            if(isset($city) && isset($province)){
                return $this->findByCityIdAndProvinceIdAndLogisticName($city->id,$province->id,$logistic->id);
            }
            return null;
        }
        $city = app(CityService::class)->findByName($cityName);
        $province = app(ProvinceService::class)->findByName($provinceName);
        if($city == null|| $province==null)return null;
        return  $this->findByCityIdAndProvinceIdAndLogisticName($city->id,$province->id,$logisticId);
    }

    public function findByCityIdAndProvinceIdAndLogisticName($cityId,$provinceId,$logisticId)
    {
        return LogisticTiming::query()->where('to_province_id',$provinceId)->where('to_city_id',$cityId)->where('logistic_id',$logisticId)->first();
    }


}
