CityAreaDao.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace app\common\dao\store;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\BaseModel;
  5. use app\common\model\store\CityArea;
  6. class CityAreaDao extends BaseDao
  7. {
  8. protected function getModel(): string
  9. {
  10. return CityArea::class;
  11. }
  12. public function search(array $where)
  13. {
  14. return CityArea::getDB()->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
  15. $query->where('parent_id', $where['pid']);
  16. })->when(isset($where['address']) && $where['address'] !== '', function ($query) use ($where) {
  17. $address = explode('/', trim($where['address'], '/'));
  18. $p = array_shift($address);
  19. if (mb_strlen($p) - 1 === mb_strpos($p, '省')) {
  20. $p = mb_substr($p, 0, -1);
  21. }
  22. $pcity = $this->search([])->where('name', $p)->find();
  23. if ($pcity) {
  24. $query->whereLike('path', "/{$pcity->id}/%");
  25. }
  26. $query->where(function ($query) use ($address) {
  27. foreach ($address as $item) {
  28. if ($item) {
  29. $query->whereOr('name', $item);
  30. }
  31. }
  32. });
  33. });
  34. }
  35. public function getCityList(CityArea $city)
  36. {
  37. if (!$city->parent_id) return [$city];
  38. $lst = $this->search([])->where('id', 'in', explode('/', trim($city->path, '/')))->order('id ASC')->select();
  39. $lst[] = $city;
  40. return $lst;
  41. }
  42. }