cache->get(CommunityEnum::REGION_TREE . $level); if (empty($list)) { $where = [ ['<=', 'level', $level], ['>', 'level', 0], ]; $regions = Region::lists(['where' => $where, 'select' => 'id,name,parent_id,level', 'limit' => 10000]); $list = Region::getTreeRegion($regions); Yii::$app->cache->set(CommunityEnum::REGION_TREE . $level, $list); } $setting_service = new SettingService(); $area = $setting_service->getFieldSetting($this->mall_id, 'area', true); $area = $area ? json_decode($area, true) : $area; $list = $this->filterRegions($area, $list); return $this->handleCommunityData($this->mall_id, $list); } /** * @Description: 将自提点数据放入地区数据 * @Author: zsan * @DateTime 2023-02-09 14:50:16 * @param integer $mall_id * @param array $regions * @return mixed */ protected function handleCommunityData($mall_id, $regions) { $params = [ 'where' => [ ['mall_id' => $mall_id], ['check_status' => CommunityEnum::CHECK_ADOPT], ['status' => [StatusEnum::ENABLED]] ], 'select' => 'id, name, province_id, city_id, district_id', 'index' => 'id' ]; $community_head_list = CommunityHead::lists($params, true); $district_arr = []; foreach ($community_head_list as $v) { if (!isset($district_arr[$v['district_id']])) $district_arr[$v['district_id']] = []; $district_arr[$v['district_id']][] = [ 'id' => $v['id'], 'name' => $v['name'], 'level' => 4 ]; } foreach ($regions as &$region) { if (!isset($region['children'])) continue; if (count($district_arr) <= 0) break; foreach ($region['children'] as &$city) { if (!isset($city['children'])) continue; if (count($district_arr) <= 0) break 2; foreach ($city['children'] as &$district) { if (!array_key_exists($district['id'], $district_arr)) continue; if (count($district_arr) <= 0) break 3; $district['children'] = $district_arr[$district['id']]; unset($district_arr[$district['id']]); } unset($district); } unset($city); } unset($region); return $regions; } protected function filterRegions($area, $regions) { foreach ($regions as $k => $v) { // 过滤省份 - 再没有全选下级的情况下那么省份ID不会被存储 // if (!in_array($v['id'], $area)) { // unset($regions[$k]); // continue; // } $is_unset_province = true; if (!isset($v['children'])) continue; foreach ($v['children'] as $m => $n) { // 过滤城市 - 在没有全选的情况下 是不会保存城市的 // if (!in_array($n['id'], $area)) { // unset($v['children'][$m]); // continue; // } $is_unset_city = true; if (!isset($n['children'])) continue; foreach ($n['children'] as $a => $b) { // 过滤区 if (!in_array($b['id'], $area)) { unset($regions[$k]['children'][$m]['children'][$a]); } else { $is_unset_city = false; $is_unset_province = false; } } if ($is_unset_city) { unset($regions[$k]['children'][$m]); } else { $regions[$k]['children'][$m]['children'] = array_values($regions[$k]['children'][$m]['children']); } } if ($is_unset_province) { unset($regions[$k]); } else { $regions[$k]['children'] = array_values($regions[$k]['children']); } } return array_values($regions); } }