Common.php
7.6 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
<?php
namespace app\admin\controller\cloudapishow;
use app\common\controller\Backend;
use think\Db;
/**
* 展示省、州、市县、水库树公共类
* Class Common
* @package app\admin\controller\cloudapishow
*/
class Common extends Backend
{
//当前用户信息
protected $apiUserInfo = [];
//当前用户拥有的水库ID
protected $reservoirIds = [];
//当前查询权限条件
protected $authWhere = [];
public function _initialize()
{
parent::_initialize();
//获取当前用户拥有的水库权限
$apiUserInfo = Db::name('cloudapi_user')->where(['admin_id' => $this->auth->id])->find();
if (!empty($apiUserInfo)){
$this->apiUserInfo = $apiUserInfo;
if (!empty($apiUserInfo['reservoir_ids'])){
$this->reservoirIds = explode(',', $apiUserInfo['reservoir_ids']);
}
}
$this->authWhere['reservoir_id'] = ['in', $this->reservoirIds];
$reservoirList = Db::name('reservoir_list')
->alias('rl')
->field('rl.id,rl.name,rl.province_id,rl.state_id,rl.county_id,p.name as province_name,s.name as state_name,c.name as county_name')
->join('reservoir_province p','p.id=rl.province_id', 'LEFT')
->join('reservoir_state s','s.id=rl.state_id', 'LEFT')
->join('reservoir_county c','c.id=rl.county_id', 'LEFT')
->where([
'rl.id' => ['in',$this->reservoirIds]
])
->select();
//定义水库列表
$reservoirLists = [];
//定义树
$treeData = [];
//因为省州市县水库分别处理四张表中防止树中的id重复,所以全部加上前缀
$provincePrefix = 'p_';
$statePrefix = 's_';
$countyPrefix = 'c_';
$reservoirPrefix = 'r_';
foreach ($reservoirList as $item){
//整理水库列表
$reservoirLists[$item['id']] = $item['name'];
//省份
$hasProvince = false;
$hasState = false;
$hasCounty = false;
$hasReservoir = false;
foreach ($treeData as $province){
if (trim($province['id'], $provincePrefix) == $item['province_id']){
$hasProvince = true;
}
foreach ($province['children'] as $state){
if (trim($state['id'], $statePrefix) == $item['state_id']){
$hasState = true;
}
foreach ($state['children'] as $county){
if (trim($county['id'], $countyPrefix) == $item['county_id']){
$hasCounty = true;
}
foreach ($county['children'] as $reservoir){
if (trim($reservoir['id'], $reservoirPrefix) == $item['id']){
$hasReservoir = true;
}
}
}
}
}
if ($hasProvince === false){
$provinceData = [
'id' => $provincePrefix . $item['province_id'],
'level' => '1',
'real_id' => $item['province_id'],
'text' => $item['province_name'],
'type' => '1',
// 'parent' => '#',
// 'state' => [
// 'opened' => false, //是否展开
// 'disabled' => false,//是否禁用
// 'selected' => false,//是否被选中
// ]
];
$treeData[] = $provinceData;
}
foreach ($treeData as &$province){
if (trim($province['id'], $provincePrefix) == $item['province_id']){
if ($hasState === false){
$stateData = [
'id' => $statePrefix . $item['state_id'],
'level' => '2',
'real_id' => $item['state_id'],
'text' => $item['state_name'],
'type' => '1',
// 'parent' => $provincePrefix . $item['province_id'],
// 'state' => [
// 'opened' => false, //是否展开
// 'disabled' => false,//是否禁用
// 'selected' => false,//是否被选中
// ]
];
$province['children'][] = $stateData;
}
foreach ($province['children'] as &$state){
if (trim($state['id'], $statePrefix) == $item['state_id']){
if ($hasCounty === false){
$countyData = [
'id' => $countyPrefix . $item['county_id'],
'level' => '3',
'real_id' => $item['county_id'],
'text' => $item['county_name'],
'type' => '1',
// 'parent' => $statePrefix . $item['state_id'],
// 'state' => [
// 'opened' => false, //是否展开
// 'disabled' => false,//是否禁用
// 'selected' => false,//是否被选中
// ]
];
$state['children'][] = $countyData;
}
foreach ($state['children'] as &$county){
if (trim($county['id'], $countyPrefix) == $item['county_id']){
if ($hasReservoir === false){
$reservoirData = [
'id' => $reservoirPrefix . $item['id'],
'level' => '4',
'real_id' => $item['id'],
'text' => $item['name'],
'type' => '2',
// 'parent' => $countyPrefix . $item['county_id'],
// 'state' => [
// 'opened' => false, //是否展开
// 'disabled' => false,//是否禁用
// 'selected' => false,//是否被选中
// ]
];
$county['children'][] = $reservoirData;
}
}
}
unset($county);
}
}
unset($state);
}
}
unset($province);
}
$this->assignconfig('treeList', $treeData);
//查询设备列表
$deviceList = Db::name('reservoir_equipment')->where(['reservoir_id' => ['in', $this->reservoirIds]])->column('deviceId');
$deviceLists = [];
foreach ($deviceList as $value){
$deviceLists[$value] = $value;
}
$this->assignconfig('reservoirList',$reservoirLists);
$this->assignconfig('deviceList',$deviceLists);
}
}