Staff.php
7.9 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
<?php
namespace app\api\controller\reservoir;
use app\api\controller\tencentim\Openim;
use app\common\controller\Api;
use fast\Tree;
use think\Request;
use think\Db;
/**
* 大屏管理巡检信息
* Class Staff
* @package app\api\controller
*/
class Staff extends Api
{
// protected $noNeedLogin = ["alarmList"];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\inspection\Staff();
}
/**
* 12. 获取今日有上报经纬度的巡检人员最新位置列表
*/
public function last_staff_location_list()
{
//查询当前大屏管理员所属部门id
$id = $this->auth->id;
$depart_id = Db::name("inspection_staff")->where("user_id", $id)->value("depart_id");
if (!empty($depart_id)) {
//获取下级所有子部门ID
$tree = Tree::instance();
$departModel = new \app\admin\model\inspection\Depart();
$tree->init(collection($departModel->order('weigh asc,id asc')->select())->toArray(), 'pid');
$depart_ids = $tree->getChildrenIds($depart_id, true);
}
//找到部门及其子部门包含的水库id集合
$where = [];
if (!empty($depart_ids)) {
$where["inspection_depart_id"] = ["IN", $depart_ids];
}
$reservoir_list_ids = Db::name("reservoir_list")->where($where)->column("id");
//寻找巡检员
$where2 = [];
if (!empty($reservoir_list_ids)) {
$where2["s.reservoir_id"] = ["IN", $reservoir_list_ids];
}
$where2["s.is_regulatory_mng"] = ["=", "0"];
$where2["s.is_screen_mng"] = ["=", "0"];
$list = Db::name("inspection_staff")->alias("s")
->join("user u", "u.id=s.user_id", "LEFT")
->join("reservoir_list l", "l.id=s.reservoir_id", "LEFT")
->where($where2)
->order("s.id desc")
->field("s.id,s.user_id,s.staff_name,s.staff_code,s.mobile,u.avatar,l.name as reservoir_name")
->select();
if (!empty($list)) {
foreach ($list as $k => $v) {
$w = [];
$w = ["inspection_staff_id" => $v['id'], "person_type" => "1"];
$record = Db::name("inspection_staff_locus")->where($w)->whereTime('createtime', 'today')->field("createtime,lat,lng")->order("createtime desc")->find();
if (empty($record)) {
unset($list[$k]);
} else {
if (!empty($record)) {
$record['createtime_format'] = date("Y-m-d H:i:s", $record['createtime']);
}
$list[$k]['lat_lng_record'] = $record;
$list[$k]['avatar'] = $v['avatar'] ? cdnurl($v['avatar'], true) : letter_avatar($v['staff_name']);
// //追加当日轨迹
// $record_list = Db::name("inspection_staff_locus")->where($w)->whereTime('createtime', 'today')->field("lat,lng")->order("createtime asc")->select();
// if(!empty($record_list)){
// foreach ($record_list as $rk=>$rv){
// $list[$k]['polyArray'][$rk] =[$rv['lat'],$rv['lng']];
// }
// }
}
}
}
$list = array_values($list);
$back_data = [];
if (!empty($list)) {
$count = count($list);
$addnum = 500;//因为即时通讯人员状态每次限制查500人;所以得分开查
for ($i = 0; $i < $count; $i = $i + $addnum) {
if ($count - $i > 0) {
$j = $i - 1 > 0 ? $i : 0;
$data = array_slice($list, $j, $addnum);
$mobile_list = array_column($data, "mobile");
$openim_api = new Openim();
$r = $openim_api->query_online_status(["To_Account" => $mobile_list]);
if ($r['state'] == true) {
$back_data = $back_data + $r['data'];
}
}
}
foreach ($list as $lk => $lv) {
//追加在线状态
$list[$lk]["online_status"] = $back_data[$lv['mobile']]['Status'] ? $back_data[$lv['mobile']]['Status'] : "Offline";
$list[$lk]["online_status_cn"] = $back_data[$lv['mobile']]['StatusCn'] ? $back_data[$lv['mobile']]['StatusCn'] : "下班";
}
}
$this->success("巡检人员获取成功", $list);
}
/**
* 查询某个巡检人员的在线状态
*/
public function staff_online_status()
{
$id = $this->request->param("id");
//寻找巡检员
if (empty($id)) {
$this->error("人员id不能为空");
}
$where2 = [];
$where2["s.id"] = ["=", $id];
$where2["s.is_regulatory_mng"] = ["=", "0"];
$where2["s.is_screen_mng"] = ["=", "0"];
$list = Db::name("inspection_staff")->alias("s")
->join("user u", "u.id=s.user_id", "LEFT")
->join("reservoir_list l", "l.id=s.reservoir_id", "LEFT")
->where($where2)
->order("s.id desc")
->field("s.id,s.user_id,s.staff_name,s.staff_code,s.mobile,u.avatar,l.name as reservoir_name")
->select();
if (!empty($list)) {
foreach ($list as $k => $v) {
$w = [];
$w = ["inspection_staff_id" => $v['id'], "person_type" => "1"];
$record = Db::name("inspection_staff_locus")->where($w)->whereTime('createtime', 'today')->field("createtime,lat,lng")->order("createtime desc")->find();
if (empty($record)) {
unset($list[$k]);
} else {
if (!empty($record)) {
$record['createtime_format'] = date("Y-m-d H:i:s", $record['createtime']);
}
$list[$k]['lat_lng_record'] = $record;
$list[$k]['avatar'] = $v['avatar'] ? cdnurl($v['avatar'], true) : letter_avatar($v['staff_name']);
//追加当日轨迹
$record_list = Db::name("inspection_staff_locus")->where($w)->whereTime('createtime', 'today')->field("lat,lng")->order("createtime asc")->select();
if (!empty($record_list)) {
foreach ($record_list as $rk => $rv) {
$list[$k]['polyArray'][$rk] = [$rv['lat'], $rv['lng']];
}
}
}
}
}
$list = array_values($list);
$back_data = [];
if (!empty($list)) {
$count = count($list);
$addnum = 500;//因为即时通讯人员状态每次限制查500人;所以得分开查
for ($i = 0; $i < $count; $i = $i + $addnum) {
if ($count - $i > 0) {
$j = $i - 1 > 0 ? $i : 0;
$data = array_slice($list, $j, $addnum);
$mobile_list = array_column($data, "mobile");
$openim_api = new Openim();
$r = $openim_api->query_online_status(["To_Account" => $mobile_list]);
if ($r['state'] == true) {
$back_data = $back_data + $r['data'];
}
}
}
foreach ($list as $lk => $lv) {
//追加在线状态
$list[$lk]["online_status"] = $back_data[$lv['mobile']]['Status'] ? $back_data[$lv['mobile']]['Status'] : "Offline";
$list[$lk]["online_status_cn"] = $back_data[$lv['mobile']]['StatusCn'] ? $back_data[$lv['mobile']]['StatusCn'] : "下班";
}
}
if (!empty($list)) {
$this->success("巡检人员信息获取成功", $list);
} else {
$this->error("巡检人员信息获取失败");
}
}
}