CityAreaDao.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\other;
  12. use app\dao\BaseDao;
  13. use app\model\other\CityArea;
  14. /**
  15. * Class CityAreaDao
  16. * @package app\dao\other
  17. */
  18. class CityAreaDao extends BaseDao
  19. {
  20. /**
  21. * @return string
  22. */
  23. protected function setModel(): string
  24. {
  25. return CityArea::class;
  26. }
  27. public function search(array $where = [])
  28. {
  29. return parent::search($where)->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
  30. $query->where('parent_id', $where['pid']);
  31. })->when(isset($where['address']) && $where['address'] !== '', function ($query) use ($where) {
  32. $address = explode('/', trim($where['address'], '/'));
  33. if (isset($address[0]) && isset($address[1]) && $address[0] == $address[1]) {//直辖市:北京市北京市朝阳区
  34. array_shift($address);
  35. }
  36. $p = array_shift($address);
  37. if (mb_strlen($p) - 1 === mb_strpos($p, '市')) {
  38. $p = mb_substr($p, 0, -1);
  39. } elseif (mb_strlen($p) - 1 === mb_strpos($p, '省')) {
  40. $p = mb_substr($p, 0, -1);
  41. } elseif (mb_strlen($p) - 3 === mb_strpos($p, '自治区')) {
  42. $p = mb_substr($p, 0, -3);
  43. }
  44. $pcity = $this->getModel()->where('name', $p)->value('id');
  45. $path = ['', $pcity];
  46. $street = $p;
  47. $i = 0;
  48. foreach ($address as $item) {
  49. //县级市,只有三级地址;市和县相同
  50. if ($item == ($address[$i-1] ?? '')) continue;
  51. $pcity = $this->getModel()->whereLike('path', implode('/', $path) . '/%')->where('name', $item)->value('id');
  52. if (!$pcity) {
  53. break;
  54. }
  55. $path[] = $pcity;
  56. $street = $item;
  57. $i++;
  58. }
  59. array_pop($path);
  60. $query->whereLike('path', implode('/', $path) . '/%')->where('name', $street);
  61. });
  62. }
  63. /**
  64. * 搜索某个地址
  65. * @param array $where
  66. * @return array|\think\Model|null
  67. * @throws \think\db\exception\DataNotFoundException
  68. * @throws \think\db\exception\DbException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. */
  71. public function searchCity(array $where)
  72. {
  73. return $this->search($where)->order('id DESC')->find();
  74. }
  75. /**
  76. * 获取地址
  77. * @param array $where
  78. * @param string $field
  79. * @param array $with
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function getCityList(array $where, string $field = '*', array $with = [])
  86. {
  87. return $this->getModel()->where($where)->field($field)->with($with)->order('id asc')->select()->toArray();
  88. }
  89. /**
  90. * 删除上级城市和当前城市id
  91. * @param int $cityId
  92. * @return bool
  93. * @throws \Exception
  94. */
  95. public function deleteCity(int $cityId)
  96. {
  97. return $this->getModel()->where('id', $cityId)->whereOr('parent_id', $cityId)->delete();
  98. }
  99. }