|
|
@@ -0,0 +1,106 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services;
|
|
|
+
|
|
|
+use App\Region;
|
|
|
+use App\Traits\ServiceAppAop;
|
|
|
+
|
|
|
+class RegionService
|
|
|
+{
|
|
|
+ use ServiceAppAop;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据省份获取ID
|
|
|
+ *
|
|
|
+ * @param string $province
|
|
|
+ *
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ public function getProvince(string $province):int
|
|
|
+ {
|
|
|
+ $pool = ["省","自治区","市","特别行政"];
|
|
|
+ $province = $this->extractKeyword($province,$pool);
|
|
|
+ $region = Region::withTrashed()->where("name","like",$province."%")
|
|
|
+ ->where("type",1)->first();
|
|
|
+ if (!$region)$region = Region::query()->create([
|
|
|
+ "name" => $province,
|
|
|
+ "type" => 2,
|
|
|
+ ]);
|
|
|
+ return $region->id;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据城市获取ID
|
|
|
+ *
|
|
|
+ * @param string $city
|
|
|
+ * @param string|int|null $parent
|
|
|
+ *
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ public function getCity(string $city, $parent = null):int
|
|
|
+ {
|
|
|
+ $pool = ["市","区","自治州","州","盟"];
|
|
|
+ $city = $this->extractKeyword($city,$pool);
|
|
|
+ $region = Region::withTrashed()->where("name","like",$city."%")
|
|
|
+ ->where("type",2)->first();
|
|
|
+ if (!$region){
|
|
|
+ $region = [
|
|
|
+ "name" => $city,
|
|
|
+ "type" => 2,
|
|
|
+ ];
|
|
|
+ if ($parent){
|
|
|
+ if (is_int($parent))$region["parent_id"] = $parent;
|
|
|
+ else $region["parent_id"] = $this->getProvince($parent);
|
|
|
+ }
|
|
|
+ $region = Region::query()->create($region);
|
|
|
+ }
|
|
|
+ return $region->id;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据区县获取ID
|
|
|
+ *
|
|
|
+ * @param string $district
|
|
|
+ * @param string|int|null $parent
|
|
|
+ *
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ public function getDistrict(string $district, $parent = null):int
|
|
|
+ {
|
|
|
+ $pool = ["市","区","自治县","县","自治旗","旗","特区","林区"];
|
|
|
+ $district = $this->extractKeyword($district,$pool);
|
|
|
+ $region = Region::withTrashed()->where("name","like",$district."%")
|
|
|
+ ->where("type",3)->first();
|
|
|
+ if (!$region){
|
|
|
+ $region = [
|
|
|
+ "name" => $district,
|
|
|
+ "type" => 3,
|
|
|
+ ];
|
|
|
+ if ($parent){
|
|
|
+ if (is_int($parent))$region["parent_id"] = $parent;
|
|
|
+ else $region["parent_id"] = $this->getCity($parent);
|
|
|
+ }
|
|
|
+ $region = Region::query()->create($region);
|
|
|
+ }
|
|
|
+ return $region->id;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据字典池提取关键字
|
|
|
+ *
|
|
|
+ * @param string $name
|
|
|
+ * @param array $pool
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ private function extractKeyword(string $name,array $pool)
|
|
|
+ {
|
|
|
+ foreach ($pool as $keyword){
|
|
|
+ $result = mb_strpos($name,$keyword);
|
|
|
+ if ($result!==false){
|
|
|
+ return mb_substr($name,0,$result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $name;
|
|
|
+ }
|
|
|
+}
|