Files
spacestation/common/models/Area_model.php
T
小鱼开发 2a0c64297f 1
2025-11-17 17:41:47 +08:00

192 lines
6.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Created by PhpStorm.
* User: linfan
* Date: 2018/11/5
* Time: 13:47
*/
defined('BASEPATH') or exit('No direct script access allowed');
class Area_model extends HD_Model
{
private $table_name = 'lc_area';
public function __construct()
{
parent::__construct($this->table_name, 'default');
}
public function province($show_limit = false)
{
$result = array();
$where = [];
if($show_limit){
$where["province_id in (350000,430000,520000,460000,440000)"] = null;
}
$list = $this->select($where, null, null, null, 'distinct(province_id), province_name');
if ($list) {
foreach ($list as $v) {
$result[$v['province_id']] = array(
'id' => $v['province_id'],
'name' => $v['province_name'],
);
}
}
return $result;
}
public function city($province_id = '', $type = 0)
{
$result = array();
$list = $this->select(array('province_id' => $province_id), null, null, null, 'distinct(city_id), city_name, firstchar');
$type == 0 && $result[] = array('name' => '城市', 'id' => '');
if ($list) {
foreach ($list as $v) {
$item = array(
'id' => $v['city_id'],
'name' => $v['city_name'],
);
2 == $type && $item['firstchar'] = $v['firstchar'];
$result[$v['city_id']] = $item;
}
}
return $result;
}
public function county($city_id = '', $type = 0)
{
$result = array();
$list = $this->select(array('city_id' => $city_id), null, null, null, 'distinct(county_id), county_name');
$type == 0 && $result[] = array('name' => '行政区', 'id' => '');
if ($list) {
foreach ($list as $v) {
$result[$v['county_id']] = array(
'id' => $v['county_id'],
'name' => $v['county_name'],
);
}
}
return $result;
}
/**
* Notes:根据county_id获取数据
* Created on: 2021/11/16 14:18
* Created by: dengbw
* @param $ids
* @param string $fileds
* @param string $map_key
* @return array
*/
public function get_map_by_county_ids($ids, $fileds = '', $map_key = 'id')
{
$rows = [];
$ids = array_filter($ids);
if ($ids) {
$cf_ids = implode(',', $ids);
$where = [
"county_id in ($cf_ids)" => null
];
$rows = $this->map($map_key, '', $where, '', '', '', $fileds);
}
return $rows;
}
public function get_map_by_city_ids($ids, $fileds = '', $map_key = 'id')
{
$rows = [];
$ids = array_filter($ids);
if ($ids) {
$cf_ids = implode(',', $ids);
$where = [
"city_id in ($cf_ids)" => null
];
$rows = $this->map($map_key, '', $where, '', '', '', $fileds);
}
return $rows;
}
public function get_map_by_province_ids($ids, $fileds = '', $map_key = 'id')
{
$rows = [];
$ids = array_filter($ids);
if ($ids) {
$cf_ids = implode(',', $ids);
$where = [
"province_id in ($cf_ids)" => null
];
$rows = $this->map($map_key, '', $where, '', '', '', $fileds);
}
return $rows;
}
public function getDataByTree()
{
$redis = load_cache("redis");
$cKey = 'SYS_AREA_TREE_DATA';
$tree = $redis->get($cKey);
if(!$tree){
$rows = $this->select([], '', '' ,'', '');
$tree = $this->arrayToTree($rows);
$redis->save($cKey, $tree, 30*24*60*60);
}
return $tree;
}
function arrayToTree($data) {
// 按省份分组(ID 为键,避免重复)
$provinceMap = array_column($data, null, 'province_id');
$provinces = array_values($provinceMap); // 提取所有省份数据(去重)
$tree = [];
foreach ($provinces as $province) {
// 筛选当前省份的所有城市(基于 province_id
$cities = array_filter($data, function($item) use ($province) {
return $item['province_id'] === $province['province_id'];
});
// 按城市 ID 分组,确保每个城市唯一
$cityMap = array_column($cities, null, 'city_id');
$cityList = array_values($cityMap);
$children = [];
foreach ($cityList as $city) {
// 筛选当前城市的所有区县(基于 city_id)
$counties = array_filter($cities, function($item) use ($city) {
return $item['city_id'] === $city['city_id'];
});
// 提取区县数据(去除重复,转为目标格式)
$countyList = array_map(function($county) {
return [
'value' => $county['county_id'],
'label' => $county['county_name']
];
}, array_values(array_column($counties, null, 'county_id'))); // 去重处理
// 仅当有区县时生成 children
$cityNode = [
'value' => $city['city_id'],
'label' => $city['city_name'],
];
if (!empty($countyList)) {
$cityNode['children'] = $countyList;
}
$children[] = $cityNode;
}
// 仅当有城市时生成 children
$provinceNode = [
'value' => $province['province_id'],
'label' => $province['province_name'],
];
if (!empty($children)) {
$provinceNode['children'] = $children;
}
$tree[] = $provinceNode;
}
return $tree;
}
}