<?php

namespace App\Services;

use App\City;
use Illuminate\Support\Str;
use App\Traits\ServiceAppAop;


class CityService
{
    use ServiceAppAop;
    protected $modelClass=City::class;
    public static $cities = ['汕头','汕尾'];

    /*
     * array | string $column
     * 默认一些select字段,可传递string 或 array来指定select字段
     */
    public function getSelection($column = ['id','name']){
        if (!is_array($column)) {
            $column = [$column];
        }
        return City::query()->select($column)->get();
    }

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

    public function findByName($name)
    {
        $city = City::query()->where('name',$name)->first();
        if(isset($city))return $city;
        if(!$name)return null;
        foreach (CityService::$cities as $city) {
            if(strstr($city,$name) != null || strstr($name,$city)){
               return City::query()->where('name','like',$city.'%')->first();
            }
        }
        if(str_ends_with($name,'自治州')){
            $city_name = Str::before($name,'自治州');
            return City::query()->where('name','like',$city_name.'%')->first();
        }
        if(str_ends_with($name,'市')){
            $city_name =  Str::before($name,'市');
            return City::query()->where('name','like',$city_name.'%')->first();
        }
        return null;
    }


}
