CityAreaDao.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\store;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\CityArea;
  15. class CityAreaDao extends BaseDao
  16. {
  17. protected function getModel(): string
  18. {
  19. return CityArea::class;
  20. }
  21. public function search(array $where)
  22. {
  23. return CityArea::getDB()->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
  24. $query->where('parent_id', $where['pid']);
  25. })->when(isset($where['address']) && $where['address'] !== '', function ($query) use ($where) {
  26. $address = explode('/', trim($where['address'], '/'));
  27. $p = array_shift($address);
  28. if (mb_strlen($p) - 1 === mb_strpos($p, '市')) {
  29. $p = mb_substr($p, 0, -1);
  30. } elseif (mb_strlen($p) - 1 === mb_strpos($p, '省')) {
  31. $p = mb_substr($p, 0, -1);
  32. } elseif (mb_strlen($p) - 3 === mb_strpos($p, '自治区')) {
  33. $p = mb_substr($p, 0, -3);
  34. }
  35. $pcity = $this->search([])->where('name', $p)->find();
  36. $street = array_pop($address);
  37. if ($pcity) {
  38. $path = '/' . $pcity->id . '/';
  39. $query->whereLike('path', "/{$pcity->id}/%");
  40. foreach ($address as $item) {
  41. $id = $this->search([])->whereLike('path', $path . '%')->where('name', $item)->value('id');
  42. if ($id) {
  43. $path .= $id . '/';
  44. } else {
  45. break;
  46. }
  47. }
  48. }
  49. $query->whereLike('path', $path . '%')->where('name', $street);
  50. });
  51. }
  52. public function getCityList(CityArea $city)
  53. {
  54. if (!$city->parent_id) return [$city];
  55. $lst = $this->search([])->where('id', 'in', explode('/', trim($city->path, '/')))->order('id ASC')->select();
  56. $lst[] = $city;
  57. return $lst;
  58. }
  59. }