RegionService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Services;
  3. use App\Region;
  4. use App\Traits\ServiceAppAop;
  5. class RegionService
  6. {
  7. use ServiceAppAop;
  8. /**
  9. * 根据行政级别格式化地域名,提取关键字
  10. *
  11. * @param string $name
  12. * @param int $type
  13. *
  14. * @return string
  15. */
  16. public function formatName($name, $type)
  17. {
  18. switch ($type){
  19. case 1:
  20. $pool = ["省","自治区","市","特别行政"];
  21. break;
  22. case 2:
  23. $pool = ["市","区","自治州"/*,"州"*/,"盟"];
  24. break;
  25. case 3:
  26. $pool = ["市","区","自治县","县","自治旗","旗","特区","林区"];
  27. break;
  28. default:
  29. $pool = [];
  30. }
  31. return $this->extractKeyword($name,$pool);
  32. }
  33. /**
  34. * 根据省份获取ID
  35. *
  36. * @param string $province
  37. *
  38. * @return int
  39. */
  40. public function getProvince(string $province):int
  41. {
  42. $province = $this->formatName($province,1);
  43. $region = Region::withTrashed()->where("name","like",$province."%")
  44. ->where("type",1)->first();
  45. if (!$region)$region = Region::query()->create([
  46. "name" => $province,
  47. "type" => 2,
  48. ]);
  49. return $region->id;
  50. }
  51. /**
  52. * 根据城市获取ID
  53. *
  54. * @param string $city
  55. * @param string|int|null $parent
  56. *
  57. * @return int
  58. */
  59. public function getCity(string $city, $parent = null):int
  60. {
  61. $city = $this->formatName($city,2);
  62. $region = Region::withTrashed()->where("name","like",$city."%")
  63. ->where("type",2)->first();
  64. if (!$region){
  65. $region = [
  66. "name" => $city,
  67. "type" => 2,
  68. ];
  69. if ($parent){
  70. if (is_int($parent))$region["parent_id"] = $parent;
  71. else $region["parent_id"] = $this->getProvince($parent);
  72. }
  73. $region = Region::query()->create($region);
  74. }
  75. return $region->id;
  76. }
  77. /**
  78. * 根据区县获取ID
  79. *
  80. * @param string $district
  81. * @param string|int|null $parent
  82. *
  83. * @return int
  84. */
  85. public function getDistrict(string $district, $parent = null):int
  86. {
  87. $district = $this->formatName($district,3);
  88. $region = Region::withTrashed()->where("name","like",$district."%")
  89. ->where("type",3)->first();
  90. if (!$region){
  91. $region = [
  92. "name" => $district,
  93. "type" => 3,
  94. ];
  95. if ($parent){
  96. if (is_int($parent))$region["parent_id"] = $parent;
  97. else $region["parent_id"] = $this->getCity($parent);
  98. }
  99. $region = Region::query()->create($region);
  100. }
  101. return $region->id;
  102. }
  103. /**
  104. * 根据字典池提取关键字
  105. *
  106. * @param string $name
  107. * @param array $pool
  108. *
  109. * @return string
  110. */
  111. private function extractKeyword(string $name,array $pool)
  112. {
  113. foreach ($pool as $keyword){
  114. $result = mb_strpos($name,$keyword);
  115. if ($result!==false){
  116. return mb_substr($name,0,$result);
  117. }
  118. }
  119. return $name;
  120. }
  121. }