123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace app\services\system;
- use app\model\system\CityArea;
- use Exception;
- use qiniu\basic\BaseServices;
- use qiniu\services\CacheService;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Model;
- use Throwable;
- class CityAreaServices extends BaseServices
- {
-
- public $type = [
- '1' => 'province',
- '2' => 'city',
- '3' => 'area',
- '4' => 'street'
- ];
-
- public function __construct(CityArea $model)
- {
- $this->model = $model;
- }
-
- public function getRelationCityIds(int $id)
- {
- $cityInfo = $this->model->get($id);
- $ids = [];
- if ($cityInfo) {
- $ids = explode('/', trim($cityInfo['path'], '/'));
- }
- return array_merge([$id], $ids);
- }
-
- public function getRelationCityIdsCache(int $id, int $expire = 1800)
- {
- return CacheService::redisHandler('apiCity')->remember('city_ids_' . $id, function () use ($id) {
- $cityInfo = $this->model->get($id);
- $ids = [];
- if ($cityInfo) {
- $ids = explode('/', trim($cityInfo['path'], '/'));
- }
- return array_merge([$id], $ids);
- }, $expire);
- }
-
- public function getCityTreeList(int $pid = 0, int $type = 0)
- {
- $parent_name = '中国';
- if ($pid) {
- $city = $this->model->get($pid);
- $parent_name = $city ? $city['name'] : '';
- }
- $cityList = $this->getCityList(['parent_id' => $pid], 'id as value,id,name as label,parent_id as pid,level', ['children']);
- foreach ($cityList as &$item) {
- $item['parent_name'] = $parent_name;
- if (isset($item['children']) && $item['children']) {
- $item['children'] = [];
- $item['loading'] = false;
- $item['_loading'] = false;
- } else {
- unset($item['children']);
- }
- }
- if ($cityList) {
- switch ($type) {
- case 0:
- case 3:
- break;
- case 1:
- foreach ($cityList as &$item) {
- if ($item['level'] == 2) {
- unset($item['children'], $item['loading'], $item['_loading']);
- }
- }
- break;
- case 2:
- foreach ($cityList as &$item) {
- if ($item['level'] == 3) {
- unset($item['children'], $item['loading'], $item['_loading']);
- }
- }
- break;
- case 4:
- foreach ($cityList as &$item) {
- if ($item['level'] == 1) {
- unset($item['children'], $item['loading'], $item['_loading']);
- }
- }
- break;
- }
- }
- return $cityList;
- }
-
- public function searchCity(array $where)
- {
- return $this->model->search($where)->order('id DESC')->find();
- }
-
- public function getCityList(array $where, string $field = '*', array $with = [])
- {
- return $this->model->where($where)->field($field)->with($with)->order('id asc')->select()->toArray();
- }
-
- public function deleteCity(int $cityId)
- {
- return $this->model->where('id', $cityId)->whereOr('parent_id', $cityId)->delete();
- }
- }
|