cxgj/models/District.php
2023-11-27 09:45:13 +08:00

263 lines
7.8 KiB
PHP

<?php
namespace app\models;
use Yii;
use app\components\FlashStorage;
use app\components\Serializer;
use Overtrue\Pinyin\Pinyin;
/**
* This is the model class for table "{{%district}}".
*
* @property int $id ID
* @property int $parent_id 父级ID
* @property string $code 区划代码
* @property string $name 名称
* @property string $level 层级
* @property int $created_at 添加时间
* @property int $is_delete 是否删除
* @property string $initial 首字母
* @property string $initials 拼音首字母
*/
class District extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%district}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['parent_id', 'created_at', 'is_delete'], 'integer'],
[['code', 'name', 'level'], 'required'],
[['code'], 'string', 'max' => 32],
[['name'], 'string', 'max' => 64],
[['level'], 'string', 'max' => 16],
[['initial', 'initials'], 'string', 'max' => 50],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'parent_id' => '父级ID',
'code' => '区划代码',
'name' => '名称',
'level' => '层级',
'created_at' => '添加时间',
'is_delete' => '是否删除',
'initial' => '首字母',
'initials' => '拼音首字母',
];
}
public static function getArr($levels = ['province', 'city', 'district'])
{
$level_md5 = md5(json_encode($levels));
$cache_key = "_dh_ul_{$level_md5}";
$data = FlashStorage::getCache($cache_key);
if($data === false){
$data = self::_getArr($levels);
FlashStorage::setCache($cache_key, $data,86400);
}
return $data;
}
public static function _getArr($levels)
{
$list = District::find()
->where([
'is_delete' => 0,
])
->andWhere([
'level' => $levels
])
->select('id,parent_id,name,level')
->asArray()->all();
$district = [];
foreach ($list as $index => $item){
$district[$item['id']] = $item;
}
return $district;
}
/**
* 获取已父级id为$parent_id为根节点的树型结构数组
*
* @param array $arr
* @param int $level 树型当前层
* @param int $parent_id 父级id
* @param int $floor 树型总层数
* @return array
*/
public static function getList(&$arr, $level = 0, $parent_id = 1, $floor = 3)
{
$treeData = [];// 保存结果
$catList = $arr;
foreach ($catList as &$item) {
$parent_id = $item['parent_id'];
if (isset($catList[$parent_id]) && !empty($catList[$parent_id])) {// 肯定是子分类
$catList[$parent_id]['list'][] = &$catList[$item['id']];
} else {// 肯定是一级分类
$treeData[] = &$catList[$item['id']];
}
}
unset($item);
return $treeData[0]['list'];
}
// 根据id获取信息
public static function getDistrict($param)
{
if (is_array($param)) {
$id = $param['id'];
} else {
$id = $param;
}
$arr = self::getArr();
$list = $arr[$id];
$str = (new Serializer())->encode($list);
return (new Serializer())->decode($str);
}
// 根据指定的key=>value查找需要的数组
public static function getInfo($param)
{
$newParam = [];
foreach ($param as $key => $value) {
$newParam[0] = $key;
$newParam[1] = $value;
}
$arr = self::getArr();
$list = array_filter($arr, function ($v) use ($newParam) {
return $v[$newParam[0]] == $newParam[1];
});
$str = (new Serializer())->encode($list);
return (new Serializer())->decode($str);
}
public static function getTerritorial()
{
$data = FlashStorage::getGlobal('district_territorial_list');
if ($data) {
return $data;
}
$arr = self::getArr();
$treeData = [];// 保存结果
$catList = &$arr;
foreach ($catList as &$item) {
$item['selected'] = false;
$parent_id = $item['parent_id'];
if (isset($catList[$parent_id]) && !empty($catList[$parent_id])) {// 肯定是子分类
$catList[$parent_id]['list'][] = &$catList[$item['id']];
} else {// 肯定是一级分类
$treeData[] = &$catList[$item['id']];
}
}
unset($item);
$data = $treeData;
FlashStorage::setGlobal('district_territorial_list', $data);
return $data;
}
public static function convertPy($content)
{
$pinyin = new Pinyin();
$output = $pinyin->abbr($content);
unset($pinyin);
return $output;
}
public static function setRecordInitial($id,$name)
{
$name = trim($name);
$initials = self::convertPy($name);
$initials = strtoupper($initials);
District::updateAll(['initial' => $initials[0], 'initials' => $initials],['id' => $id]);
unset($id);
unset($name);
unset($initials);
}
public static function batchSetRecordInitial($limit = 1000, $max = 5000, $counter = 0)
{
set_time_limit(0);
$list = District::find()
->where([
'is_delete' => 0,
'initial' => NULL
])
->select('id,name')
->limit($limit)->asArray()->all();
if(count($list) == 0){
return true;
}
foreach ($list as $index => $item){
self::setRecordInitial($item['id'], $item['name']);
}
unset($list);
$counter += 1;
echo "已处理:第{$counter}页数据".PHP_EOL;
if($counter * $limit >= $max){
return true;
}
self::batchSetRecordInitial($limit, $max, $counter);
}
public static function getCityIndexList($levels = ['province', 'city', 'district'])
{
$level_md5 = md5(json_encode($levels));
$cache_key = "_dh_ul_cil_{$level_md5}";
$data = FlashStorage::getCache($cache_key);
if($data === false){
$data = self::_getCityIndexList($levels);
FlashStorage::setCache($cache_key, $data,86400);
}
return $data;
}
public static function _getCityIndexList($levels, $filter = [])
{
$chars = District::find()
->where([
'is_delete' => 0,
])
->andWhere([
'level' => $levels
])
->andFilterWhere($filter)
->select('initial')->distinct(true)->orderBy(['initial' => SORT_ASC])->column();
$district = [];
foreach($chars as $index => $char){
$list = District::find()
->where([
'is_delete' => 0,
])
->andWhere([
'level' => $levels,
'initial' => $char
])
->andFilterWhere($filter)
->select('id,parent_id,name,level')
->asArray()->all();
$district[$char] = $list;
}
return $district;
}
}