RegionService.php 3.5 KB

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