AdministrativeDivisionTrait.php
5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* trait :行政区划trait
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Models\Traits
* @package App\Models\Traits
* @author Richer <yangzi1028@163.com>
* @date 2019年10月10日,18:12:16
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Models\Traits;
use App\Models\System\AdministrativeDivision;
/**
* Trait AdministrativeDivisionTrait.
*
* @category App\Models\Traits
* @package App\Models\Traits
* @author Richer <yangzi1028@163.com>
* @date 2019年10月10日,18:12:16
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
trait AdministrativeDivisionTrait
{
/**
* 获取全部的城市
*
* @return mixed|string
*/
public function getCities()
{
return $list = AdministrativeDivision::where('level', 2)->orderBy('initial')->orderBy('name')->get(['code','name','initial']);
}
/**
* 根据地区的code数组获取对应的名称
*
* @param string $code code
*
* @return mixed|string
*/
public function getRegionNameByCode($code)
{
if (!$code) {
return '';
}
$data = AdministrativeDivision::where('code', $code)->first(['name']);
if (!$data) {
return '';
}
return $data['name'];
}
/**
* 根据地区的code数组获取对应的名称
*
* @param string $code code
*
* @return mixed|string
*/
public function getRegionCodeByChildCode($code)
{
if (!$code) {
return '';
}
$data = AdministrativeDivision::where('code', $code)->first(['pcode']);
if (!$data) {
return '';
}
return $data['pcode'];
}
/**
* 根据地区的code数组获取对应的名称
*
* @param array $codes code
*
* @return array|bool|null
*/
public function getRegionNameByCodes($codes)
{
if (empty($codes) || !is_array($codes)) {
return false;
}
$rs =[];
$names = AdministrativeDivision::whereIn('code', $codes)->get(['name']);
if (!$names->isEmpty()) {
foreach ($names as $key => $value) {
$rs[] = $value['name'];
}
return $rs;
}
return null;
}
/**
* 根据地区的code数组获取对应的位置
*
* @param array $codes code
*
* @return array|bool|null
*/
public function getRegionLocationByCodes($codes)
{
if (empty($codes) || !is_array($codes)) {
return false;
}
$list = AdministrativeDivision::whereIn('code', $codes)->get(['code','name','location']);
$rs = $list->map(
function ($item) {
return [
'code' => $item->code,
'name' => $item->name,
'location' => $item->location,
];
}
);
return $rs->toArray();
}
/**
* 根据区域的code获取区域的的代码集:主要为laravel-admin select组件使用
*
* @param string $type 类型
* @param string $code code
* @param string $operate 操作
*
* @return \App\Models\District[]|\Illuminate\Database\Eloquent\Collection
*/
public static function getRegionSelectOptions($type, $code = '', $operate = '')
{
if ($type === 'bizdistrict') {
$model = new Bizdistrict();
return $model->getSelectOptions($code, $operate);
} else {
$model = new AdministrativeDivision();
return $model->getSelectOptions($type, $code, $operate);
}
}
/**
* 根据城市code获取省
*
* @param string $code code
*
* @return string
*/
public function getBizdistrictByCode($code)
{
if (!$code) {
return '';
}
// 从redis中获取城市的名称
//$city = Redis::get('city'.$city_code);
$data = null;
if (!$data) {
$data = Bizdistrict::where('code', $code)-> first(['name']);
if ($data) {
return $data['name'];
}
}
}
/**
* 根据城市名称模糊查询并返回城市code
*
* @param string $cityname
* @param string $level
*
* @return code
*/
public function getLikeCitNameCode($cityname,$level=1)
{
$model = new AdministrativeDivision();
$data = $model->where('name', 'like', '%'.$cityname.'%')->where('level','=',$level)->first();
//dd($data);
if($data){
return $data->code;
}else{
if($level>0 && $level<5){
$level++;
return self::getLikeCitNameCode($cityname,$level);
}else{
return;
}
}
}
}