Supervision.php
7.0 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
<?php
namespace app\api\controller\v6;
//允许所有的跨域请求
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
date_default_timezone_set('PRC');
use fast\Tree;
use think\Db;
use app\common\controller\Api;
use app\api\controller\v6\Inspection;
//v6 监管端功能
class Supervision extends Api
{
// 无需登录的接口,*表示全部
protected $noNeedLogin = ['*'];
// 无需鉴权的接口,*表示全部
protected $noNeedRight = ['*'];
public $staffInfo;
//初始化
public function _initialize()
{
parent::_initialize();
if ($this->request->action() != "login" && $this->request->action() != "screen_mng_login") {
$staffModel = new \app\admin\model\inspection\Staff();
$staffInfo = $staffModel->where(['user_id' => $this->auth->id])->find();
if (empty($staffInfo)) {
$this->error('当前员工账号异常');
}
$this->staffInfo = $staffInfo;
}
}
/**
* 获取水库结构
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getReservoirStructure()
{
$DepartM = new \app\admin\model\inspection\Depart(); //部门表模型
$array = collection($DepartM->order('weigh asc,id asc')->field('id,pid,depart_name,type')->select())->toArray();
$shu = $this->getMenu($array);
$this->success('成功', $shu);
}
protected function getMenu($array)
{
foreach ($array as $key => $items) {
if ($items['pid'] == "0") {
unset($this->menu[$key]);
$menu[] = $this->buildMenuTree($items, $items['id'], $array);
}
}
return $menu;
}
//生产多级菜单树
protected function buildMenuTree($items, $rid, $array)
{
$childs = $this->getChildMenu($items, $rid, $array);
if (isset($childs['child'])) {
foreach ($childs['child'] as $key => $value) {
$children = $this->buildMenuTree($value, $value['id'], $array);
if (null != $children['child']) {
$childs['child'][$key]['child'] = $children['child'];
}
}
}
return $childs;
}
//获取子菜单
protected function getChildMenu($items, $rid, $array)
{
foreach ($array as $key => $value) {
if ($value['pid'] == $rid) {
unset($array[$key]);
$items['child'][] = $value;
}
}
return $items;
}
/**
* 获取巡检条件信息
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getInspectionWhereInfo()
{
$field = "plan.id as plan_id,plan.plan_name,plan.route_id,plan.staff_id,route.route_name,staff.staff_name";
//巡检计划
$plan = Db::name('inspection_plan')
->alias('plan')
->join('inspection_route route', 'plan.route_id = route.id')
->join('inspection_staff staff', 'plan.staff_id = staff.id')
->field($field)
->select();
//巡检区域
$area = collection(Db::name('inspection_area')->field('id,pid,area_name,reservoir_id')->select())->toArray();
if (!empty($area)) {
$area = $this->getMenu($area);
//获取每个巡检区域下的巡检点
foreach ($area as &$v) {
$area_site_where['area_id'] = $v['id'];
//巡检点
$v['area_site'] = collection(Db::name('inspection_area_site')->where($area_site_where)->field('id,area_id,site_name')->select())->toArray();
}
}
$this->success('成功', ['plan' => $plan, 'area' => $area]);
}
/**
* 发布巡检任务
* @return \think\response\Json
*/
public function releaseInspection()
{
$param = $this->request->param('', []);
if (empty($param['area_site_id'])) {
$this->error('失败');
}
$Data_ = [];
$param['area_site_id'] = json_decode($param['area_site_id'], true);
foreach ($param['area_site_id'] as $v) {
$arr['plan_id'] = $param['plan_id'];
$arr['route_id'] = $param['route_id'];
$arr['area_id'] = $param['area_id'];
$arr['area_site_id'] = $v;
$arr['begintime'] = $param['begintime'];
$arr['endtime'] = $param['endtime'];
$arr['createtime'] = time();
array_push($Data_, $arr);
}
$res = false;
if (!empty($Data_)) {
// 启动事务
Db::startTrans();
try {
//添加巡检任务
$result = Db::name('inspection_project')->insertAll($Data_);
if (!$result) {
Db::rollback();
}else{
//提交
Db::commit();
$res = true;
}
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
}
//返回结果
$res?$this->success('成功'):$this->error('失败');
} else {
$this->error('失败');
}
}
//回复隐患内容
public function replyWarning()
{
$param = $this->request->param('', []);
if (empty($param)) {
$this->error('回复内容不能为空');
}
//回复内容 Warning 表
$id = $param['id'];
$WarningData_['status'] = $param['status'];
$WarningData_['reply'] = $param['reply'];
//回复内容 Warning_reply 表
$WarningReplyData_['wid'] = $param['id'];
$WarningReplyData_['staffid'] = $param['staff_id'];
$WarningReplyData_['status'] = $param['status'];
$WarningReplyData_['reply'] = $param['reply'];
$WarningReplyData_['createtime'] = time();
$res = false;
// 启动事务
Db::startTrans();
try {
//更新隐患表
$warningRes = Db::name('inspection_warning')->where('id', $id)->update($WarningData_);
//更新隐患回复表
$replyRes = Db::name('inspection_warning_reply')->insert($WarningReplyData_);
if ($warningRes == 0 || $replyRes == 0) {
Db::rollback();
}else{
// 提交事务
Db::commit();
$res = true;
}
}
catch (\Exception $e) {
// 回滚事务
Db::rollback();
}
//返回结果
$res?$this->success('成功'):$this->error('失败');
}
}