AreaTrait.php
8.3 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* trait :区域
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Models\Traits
* @package App\Models\Traits
* @author Richer <yangzi1028@163.com>
* @date 2021年1月29日10:59:31
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Models\Traits;
use App\Models\Admin\User;
use App\Models\Area\Building;
use App\Models\Area\Group;
use App\Models\Area\Room;
use App\Models\Area\Village;
use App\Models\Lock\Lock;
use App\Models\Lock\LockPassword;
use App\Models\Smoke\Smoke;
use App\Models\Smoke\SmokeReplaceRecord;
use App\Models\System\Area;
use App\Models\User\Tenant;
use Encore\Admin\Auth\Database\Administrator;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Arr;
/**
* Trait AreaTrait
*
* @category App\Models\Traits
* @package App\Models\Traits
* @author Richer <yangzi1028@163.com>
* @date 2021年1月29日10:59:31
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
trait AreaTrait
{
/**
* @return BelongsTo
*/
public function village(): BelongsTo
{
// return $this->belongsTo(Area::class, 'village_id', 'id');
return $this->belongsTo(Village::class, 'village_id', 'id');
}
/**
* @return BelongsTo
*/
public function group(): BelongsTo
{
// return $this->belongsTo(Area::class, 'group_id', 'id');
return $this->belongsTo(Group::class, 'group_id', 'id');
}
/**
* @return BelongsTo
*/
public function building(): BelongsTo
{
// return $this->belongsTo(Area::class, 'building_id', 'id');
return $this->belongsTo(Building::class, 'building_id', 'id');
}
/**
* @return BelongsTo
*/
public function room(): BelongsTo
{
// return $this->belongsTo(Area::class, 'room_id', 'id');
return $this->belongsTo(Room::class, 'room_id', 'id');
}
/**
* 根据 社区、小区、楼栋的id获取房间的信息
* @param $id
* @return
*/
public function getHouseByAreaId($id)
{
$list = Area::where('level', Area::LEVEL_ROOM)->where(function ($query) use ($id) {
return $query->where('path', 'LIKE', '%,'.$id.',%')->orWhere('id', $id);
})->get(['id', 'pid', 'admin_id', 'title', 'level', 'phone', 'address']);
return $list;
}
/**
* @param $nodes
* @param string $prefix
* @return array
*/
public function getAreaTrees($nodes = [], $prefix = '')
{
$options = [];
if (empty($nodes)) {
$nodes = $this->allNodes();
}
$space = " ";
$decoration = '┝';
foreach ($nodes as $index => $node) {
$title = $prefix. $space. $node->title;
$childrenPrefix = str_replace($decoration, str_repeat($space, 6), $prefix).$decoration.str_replace([$decoration, $space], '', $prefix);
$children = $this->getAreaTrees($node->allChildren, $childrenPrefix);
$options[$node->id] = $title;
if ($children) {
$options += $children;
}
}
return $options;
}
/**
* 树形结构
*
* @return array
*/
public function toTree($user_id = 0)
{
// add by Richer 于 2021年6月25日16:57:56 由于修改了数据库表结构,修改逻辑
// $village_ids = [];
// $group_ids = [];
// $building_ids = [];
// $room_ids = [];
// if ($user_id) {
// $user = User::find($user_id);
// $village_ids = $user->village_ids;
// $group_ids = $user->group_ids;
// $building_ids = $user->building_ids;
// $room_ids = $user->room_ids;
// }
//
//// dump($village_ids);
//
// // 获取全部的社区、村
// $villages = Village::where('status', config('constants.STATUS_OK'))->get(['id','title']);
// foreach ($villages as &$village) {
// $village['field'] = 'village_ids[]';
// $village['spread'] = true;
// if ($village_ids && in_array($village->id, $village_ids)) {
//// $village['checked'] = true;
// }
//
// // 获取全部的小区
// $groups = Group::where('village_id', $village->id)->where('status', config('constants.STATUS_OK'))->get(['id','title']);
// foreach ($groups as &$group) {
// $group['field'] = 'group_ids[]';
// $group['spread'] = true;
//
//
// // 获取全部的小区
// $buildings = Building::where('group_id', $group->id)->where('status', config('constants.STATUS_OK'))->get(['id','title']);
// foreach ($buildings as &$building) {
// $building['field'] = 'building_ids[]';
// $building['spread'] = true;
//
// // 获取全部的小区
// $rooms = Room::where('building_id', $building->id)->where('status', config('constants.STATUS_OK'))->get(['id','title']);
// foreach ($rooms as &$room) {
// $room['field'] = 'room_ids[]';
// $room['spread'] = true;
// $room['checked'] = true;
//
// }
//
// $building['children'] = $rooms->toArray();
// }
// $group['children'] = $buildings->toArray();
// }
// $village['children'] = $groups->toArray();
// }
//
//// dd($villages->toArray());
//
// return $villages->toArray();
$branch = [];
$nodes = $this->allNodes();
foreach ($nodes as $node) {
$data = [
'title' => Arr::get($node, 'title'),
'id' => Arr::get($node, 'id'),
'spread' => true,
'field' => 'toAreas[]'
];
$children = Arr::get($node, 'all_children');
if ($children) {
$data['children'] = $this->buildNestedArray($children);
}
$branch[] = $data;
}
return $branch;
}
/**
* @param array $children
* @return array
*/
protected function buildNestedArray(array $children = [])
{
$data = [];
foreach ($children as $child) {
$node = [
'title' => Arr::get($child, 'title'),
'id' => Arr::get($child, 'id'),
'spread' => true,
'field' => 'toAreas[]',
];
$children = Arr::get($child, 'all_children');
if ($children) {
$node['children'] = $this->buildNestedArray($children);
}
$data[] = $node;
}
return $data;
}
/**
* @return mixed
*/
public function allNodes()
{
return Area::where('pid', 0)->with('allChildren')->get()->toArray();
}
/**
* 删除关联数据
*
* @param $model
*/
public static function deleteArea($model)
{
// 删除管理账号
$model->admin()->delete();
// 删除关联的层级
$admin_ids = Area::where('path', 'LIKE', '%,'.$model->id.',%')->all()->pluck('admin_id');
Area::where('path', 'LIKE', '%,'.$model->id.',%')->delete();
// 删除关联层级管理账号
if ($admin_ids) {
Administrator::whereIn('id', $admin_ids)->delete();
}
// 所有房间
$room_ids = self::getHouseByAreaId($model->id)->map(function ($item) {
return $item->id;
})->all();
if ($room_ids) {
// 删除设备
Lock::whereIn('room_id', $room_ids)->delete();
LockPassword::whereIn('room_id', $room_ids)->delete();
Smoke::whereIn('room_id', $room_ids)->delete();
SmokeReplaceRecord::whereIn('room_id', $room_ids)->delete();
// 删除租客
Tenant::whereIn('room_id', $room_ids)->delete();
//
}
}
}