AdministrativeDivisionService.php
7.5 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
203
204
205
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 逻辑层:中国行政区划
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Services
* @package App\Services\System
* @author Richer <yangzi1028@163.com>
* @date 2019年10月10日,18:31:22
* @copyright 2019-2020 Richer (http://www.umu888.com)
* @license http://www.umu888.com License
* @link http://www.umu888.com
*/
namespace App\Services\System;
use App\Models\System\AdministrativeDivision;
use App\Services\BaseService;
use Illuminate\Support\Facades\Cache;
/**
* Class AdministrativeDivisionService
*
* @category App\Services
* @package App\Services\System
* @author Richer <yangzi1028@163.com>
* @date 2019年10月10日,18:31:22
* @copyright 2019-2020 Richer (http://www.umu888.com)
* @license http://www.umu888.com License
* @link http://www.umu888.com
*/
class AdministrativeDivisionService extends BaseService
{
/**
* AdministrativeDivisionService constructor.
*
* @param AdministrativeDivision $model 模型
*/
public function __construct(AdministrativeDivision $model)
{
// 执行父类构造方法
parent::__construct($model);
}
/**
* 根据区县的code获取区县的名称
*
* @param string $type 类型
* @param string $pcode code
*
* @return mixed
*/
public function getRegions($type, $pcode = '')
{
//根据不同的类别
$level = $this->model->getLevel($type);
return $this->model->where('level', $level)->when($pcode, function ($query) use ($type, $pcode) {
// 根据城市查询:需要截取前面四个字符,并且补零
if ($type == AdministrativeDivision::CITY || $type == AdministrativeDivision::COUNTY) {
$pcode = substr($pcode, 0, 4) .'00';
}
$query->where('pcode', $pcode);
})->orderBy('initial')->get(['level','code','name','initial']);
$where['level'] = $level;
if ($pcode) {
// 根据城市查询:需要截取前面四个字符,并且补零
if ($type == AdministrativeDivision::CITY || $type == AdministrativeDivision::COUNTY) {
$pcode = substr($pcode, 0, 4) .'00';
}
$where['pcode'] =$pcode;
}
return $this->all(['level','code','name','initial'], $where, 'initial')->toArray();
}
/**
* 获取热门城市
*
* @return \Illuminate\Support\Collection
*/
public function getHotCities()
{
$list = HotCity::orderBy('order')->orderBy('initial')->get(['code','name','initial']);
$collection = collect(['code'=>'', 'name' => '全部']);
$list->prepend($collection);
return $list;
}
/**
* 获取商圈
*
* @param string $pcode code
*
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
*/
public function getBizdistricts($pcode)
{
if ($pcode) {
// 根据城市查询:需要截取前面四个字符,并且补零
//$pcode= substr($pcode, 0, 4) .'00';
$where['district_code'] = $pcode;
}
$list = Bizdistrict::where($where)->orderBy('initial')->get(['code','name','initial'])->toArray();
return $list;
}
/**
* 获取行政区域列表-select-option
* 主要为laravel-admin select控件使用,数据格式有两种:
* 1、一种是在初始化的时候用格式为['110100'=>'北京市'],
* 主要在编辑页面用,通过上级的code获取到该options并渲染到select控件中。
* 2、第二种是联动的数据,数据格式为[{"id": 110100,"text": "北京市"}],
* 通常用着select控件在选择的时候触发的事件调用
*
* @param string $type 行政区域类型:province,city,district,bizdistrict
* @param string $pcode 上级 code
* @param string $operate 当前获取数据的方法,默认是联动获取数据。如果是init说明是初始化option
*
* @return District[]|\Illuminate\Database\Eloquent\Collection
*/
public function getSelectOptions($type = 'province', $pcode = '', $operate = 'load')
{
// 根据城市查询:需要截取前面四个字符,并且补零
if ($type == AdministrativeDivision::COUNTY) {
$pcode = substr($pcode, 0, 4) .'00';
}
return $this ->model-> getSelectOptions($type, $pcode, $operate);
}
/**
* 获取行政区域列表-select-option
* 主要为laravel-admin select控件使用,数据格式有两种:
* 1、一种是在初始化的时候用格式为['110100'=>'北京市'],
* 主要在编辑页面用,通过上级的code获取到该options并渲染到select控件中。
* 2、第二种是联动的数据,数据格式为[{"id": 110100,"text": "北京市"}],
* 通常用着select控件在选择的时候触发的事件调用
*
* @param string $type 行政区域类型:province,city,district,bizdistrict
* @param string $keyword 关键字
* @param string $operate 当前获取数据的方法,默认是联动获取数据。如果是init说明是初始化option
*
* @return District[]|\Illuminate\Database\Eloquent\Collection
*/
public function getSelectOptionsByKeyword($type = 'province', $keyword = '', $operate = 'ajax')
{
return $this ->model-> getSelectOptionsByKeyword($type, $keyword, $operate);
}
/**
* 获取行政区域列表-select-option
* 主要为laravel-admin select控件使用,数据格式有两种:
* 1、一种是在初始化的时候用格式为['110100'=>'北京市'],
* 主要在编辑页面用,通过上级的code获取到该options并渲染到select控件中。
* 2、第二种是联动的数据,数据格式为[{"id": 110100,"text": "北京市"}],
* 通常用着select控件在选择的时候触发的事件调用
*
* @return District[]|\Illuminate\Database\Eloquent\Collection
*/
public function getBizdistrictSelectOptions()
{
// 设置缓存保存时间
$minutes = AdministrativeDivision::MINUTES ;// 360天 : 1分钟 * 60 * 24 * 360
// 区县code
$district_code = request('q');
// 定义缓存key
$cache_key = 'admin_select_options_bizdistrict' . $district_code;
// 从 Cache 中获取
$options = Cache::get($cache_key);
//$options = null;
// 在缓存中获取到,直接返回
if (is_array($options) && !empty($options)) {
return $options;
}
if ($district_code) {
$bizdistricts = Bizdistrict::where('district_code', $district_code)->get(['code','name','initial']);
$options = $bizdistricts->map(
function ($bizdistrict, $key) {
return [
'id' => $bizdistrict->code,
'text' => $bizdistrict->initial.'-'.$bizdistrict->name,
];
// return [
// $bizdistrict->code => $bizdistrict->initial.'-'.$bizdistrict->name
// ];
}
);
}
Cache::put($cache_key, $options, $minutes);
return $options;
}
}