AlarmMonitoring.php 18.8 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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
<?php

namespace app\api\controller\reservoir\hkws;

use app\api\controller\getui\JgPush;
use app\api\controller\getui\XjPush;
use app\common\controller\Api;
use fast\Tree;
use think\exception\PDOException;
use think\Request;
use think\Db;

/**
 * 报警监测
 * Class AlarmMonitoring
 * @package app\api\controller
 */
class AlarmMonitoring extends Api
{
    protected $noNeedLogin = ["alarmList", 'crontabAppointStaffDealAlarm', 'getLastAlarmdata'];
    protected $noNeedRight = '*';


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\reservoir\hkws\AlarmMonitoring;
    }

    /**
     * 16、获取水库最新50条预警
     */
    public function getLastAlarmdata()
    {
        $w = [];
        $w["reservoir_list_id"] = ["gt", 0];
        $sy = Db::name("reservoir_hkws_alarm_monitoring")
            ->where($w)
            ->order("createtime desc")
            ->limit(50)
            ->column("id,reservoir_list_id,cameraIndexCode,createtime");
        $data['data1'] = [];
        $data['data2'] = [];
        if (!empty($sy)) {
            foreach ($sy as $k => $v) {
                $arr = [];
                $sk = Db::name("reservoir_list")->where("id", $v['reservoir_list_id'])->field("name,reservoir_level")->find();
                array_push($arr, $sk['name']);
                array_push($arr, $sk['reservoir_level']);
                $jk_name = Db::name("reservoir_hkws_hardware")->where("indexCode", $v['cameraIndexCode'])->value("name");
                array_push($arr, $jk_name);
                array_push($arr, date("m-d H:i", $v['createtime']));
                $data['data1'][] = $arr;
            }
            $data['data2'] = array_keys($sy);
        }


        $this->success('', $data);
    }

    /**
     * 7. 获取登录用户未读消息数(水库区分)
     */
    public function unReadList()
    {

        //查询当前用户所属部门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);
        }

        $where = [];
        if (!empty($depart_ids)) {
            $where["inspection_depart_id"] = ["IN", $depart_ids];
        }
        $reservoir_list_ids = Db::name("reservoir_list")->where($where)->column("id");

        $where2 = [];
        $data = [];
        if (!empty($reservoir_list_ids)) {
            $where2["a.reservoir_list_id"] = ["IN", $reservoir_list_ids];
        }
        $where2["r.user_id"] = ["=", $this->auth->id];
        $where2["r.is_read"] = ["=", "0"];
        $where2["a.status"] = ["=", "0"];
        $data = Db::name("reservoir_hkws_alarm_monitoring_read")->alias("r")
            ->join("reservoir_hkws_alarm_monitoring a", "a.id=r.reservoir_hkws_alarm_monitoring_id")
            ->join("reservoir_list l", "l.id=a.reservoir_list_id")
            ->where($where2)
            ->field("count(*) as total,l.name,a.reservoir_list_id,l.lat,l.lng")
            ->group("a.reservoir_list_id")
            ->select();

        $this->success("数据获取成功", $data);

    }

    /**
     * 8. 根据大屏登陆账号查看某个水库对应报警列表
     */
    public function getAlarmListBySkid()
    {
        $skid = $this->request->param("skid");
        $uid = $this->auth->id;
        $curPage = $this->request->param("curPage", 1);
        $pageSize = $this->request->param("pageSize", 10);

        $where2 = [];
        $where2["a.reservoir_list_id"] = ["=", $skid];
        $where2["r.user_id"] = ["=", $this->auth->id];
        $where2["r.is_read"] = ["=", "0"];
        $where2["a.status"] = ["=", "0"];
        $data = Db::name("reservoir_hkws_alarm_monitoring_read")->alias("r")
            ->join("reservoir_hkws_alarm_monitoring a", "a.id=r.reservoir_hkws_alarm_monitoring_id")
            ->join("reservoir_list l", "l.id=a.reservoir_list_id")
            ->join("reservoir_hkws_hardware h", "h.indexCode=a.cameraIndexCode", "LEFT")
            ->where($where2)
            ->field("h.name as jk_name,l.name,a.id,a.cameraIndexCode,a.alarm_type,a.alarmimage,a.reservoir_list_id,a.createtime,a.status,r.is_read,r.readtime")
            ->order("a.createtime desc")
            ->page($curPage, $pageSize)
            ->select();

        $model = new \app\admin\model\reservoir\hkws\AlarmMonitoring();
        $typeList = $model->getAlarmTypeList();
        $colorList = ['LINE_CROSSING_DETECTION' => "color:#ffdb5c;", 'REGION_ENTRANCE_DETECTION' => "color:#32c5e9;", 'INTRUSION_DETECTION' => "color:#ff9f7f;"];

        $statusList = $model->getStatusList();

        $return_data = [];
        $return_data["head"] = ["报警时间", "报警类型", "水库名称", "处理状态"];
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $data[$k]['alarmimage'] = $v['alarmimage'] ? "https://qiniu.ynzhsk.cn" . $v['alarmimage'] : $v['alarmimage'];
                $data[$k]['createtime'] = date("Y-m-d H:i:s", $v['createtime']);
                $data[$k]['alarm_type'] = "<span style=\"" . $colorList[$v['alarm_type']] . "\">" . $typeList[$v['alarm_type']] . "</span>";
                $data[$k]['status'] = $statusList[$v['status']];
            }
        }
        $this->success("数据获取成功", $data);
    }

    /**
     * 9. 根据监控点返回入侵信息
     */
    public function alarmList()
    {
        $cameraIndexCode = $this->request->param("cameraIndexCode");//监控点编号
        $curPage = $this->request->param("curPage", 1);
        $pageSize = $this->request->param("pageSize", 10);
        $type = $this->request->param('type', 0);
        $starttime = $this->request->param('starttime', '');
        $endtime = $this->request->param('endtime', '');

        $wh['status'] = ['=', $type];
        if (!empty($starttime) && !empty($endtime)) {
            $starttime = strtotime($starttime);
            $endtime = strtotime($endtime);
            $wh["createtime"] = ["between", [$starttime, $endtime]];
        }
        $data = [];
        if (!empty($cameraIndexCode)) {
            $data = $this->model
                ->where("cameraIndexCode", $cameraIndexCode)
                ->where($wh)
                ->order("createtime desc")
                ->page($curPage, $pageSize)
                ->select();
        }
        $return_data = [];
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $pic = $v['alarmimage'] ? "https://qiniu.ynzhsk.cn" . $v['alarmimage'] : $v['alarmimage'];
                $return_data[$k]["id"] = $v['id'];
                $return_data[$k]['alarm_type'] = $v['alarm_type'];
                $return_data[$k]['alarm_type_text'] = $v['alarm_type_text'];
                $return_data[$k]['status_text'] = $v['status_text'];
                $return_data[$k]['alarmimage'] = $pic;
                $return_data[$k]['createtime'] = date("Y-m-d H:i:s", $v['createtime']);
            }
        }
        $this->success("入侵记录获取成功", $return_data);
    }

    /**
     * 9. 最新一条报警数据(根据登陆者账号判断)
     */
    public function lastAlarmData()
    {
        //查询当前用户所属部门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);
        }

        $where = [];
        if (!empty($depart_ids)) {
            $where["inspection_depart_id"] = ["IN", $depart_ids];
        }
        $reservoir_list_ids = Db::name("reservoir_list")->where($where)->column("id");
        $where2 = [];
        $data = [];
        if (!empty($reservoir_list_ids)) {
            $where2["a.reservoir_list_id"] = ["IN", $reservoir_list_ids];
        }
        $where2["r.user_id"] = ["=", $this->auth->id];
        $where2["r.is_read"] = ["=", "0"];
        //30秒内的
//        $where2["a.createtime"] = ["gt",time()-30];
        $data = Db::name("reservoir_hkws_alarm_monitoring_read")->alias("r")
            ->join("reservoir_hkws_alarm_monitoring a", "a.id=r.reservoir_hkws_alarm_monitoring_id")
            ->join("reservoir_list l", "l.id=a.reservoir_list_id")
            ->join("reservoir_hkws_hardware h", "h.indexCode=a.cameraIndexCode", "LEFT")
            ->where($where2)
            ->field("h.name as jk_name,l.name,l.lat,l.lng,a.cameraIndexCode,a.alarm_type,a.alarmimage,a.reservoir_list_id,a.createtime,a.status,r.is_read,r.readtime")
            ->order("r.id desc")
            ->find();
        if (!empty($data)) {
            $model = new \app\admin\model\reservoir\hkws\AlarmMonitoring();
            $typeList = $model->getAlarmTypeList();
            $statusList = $model->getStatusList();

            $data['alarmimage'] = $data['alarmimage'] ? "https://qiniu.ynzhsk.cn" . $data['alarmimage'] : $data['alarmimage'];
            $data['createtime'] = date("Y-m-d H:i:s", $data['createtime']);
            $data['alarm_type'] = $typeList[$data['alarm_type']];
            $data['status'] = $statusList[$data['status']];
        }

        $this->success("", $data);
//
//        $model = new \app\admin\model\reservoir\hkws\AlarmMonitoring();
//        $typeList = $model->getAlarmTypeList();
//        $colorList = ['LINE_CROSSING_DETECTION' => "color:#ffdb5c;", 'REGION_ENTRANCE_DETECTION' => "color:#32c5e9;", 'INTRUSION_DETECTION' => "color:#ff9f7f;"];
//
//        $statusList = $model->getStatusList();
//
//        if (!empty($data)) {
//            $pic = "";
//            $pic = $data['alarmimage'] ? "https://qiniu.ynzhsk.cn" . $data['alarmimage'] : $data['alarmimage'];
//            $return_data[] = date("Y-m-d H:i:s", $data['createtime']);
//            $return_data[] = "<span style=\"" . $colorList[$data['alarm_type']] . "\">" . $typeList[$data['alarm_type']] . "</span>";
//            $return_data[] = $data['name'];
//            $return_data[] = $statusList[$data['status']];
//
//            $return_data["attr"][$k] = ["cameraIndexCode" => $data['cameraIndexCode'], "alarmimage" => $pic, "jk_name" => $data['jk_name']];
//        }
    }

    /**
     * 10. 获取某个水库巡检人员列表
     */
    public function getReservoirStaffList()
    {
        $reservoir_id = $this->request->param("reservoir_id");
        if (empty($reservoir_id)) {
            $this->error("水库id不能为空");
        }
        $where = [];
        $where = [
            "reservoir_id" => $reservoir_id,
            "is_screen_mng" => "0",
            "is_regulatory_mng" => "0"
        ];
        $field = "id,user_id,staff_code,staff_name,mobile,duty,reservoir_id";
        $staff = Db::name("inspection_staff")->where($where)->field($field)->select();
        if (!empty($staff)) {
            $this->success("水库巡检人员获取成功", $staff);
        } else {
            $this->error("此水库暂无巡检人员,请联系管理员配置水库巡检员");
        }
    }

    /**
     * 11. 确认指派巡检人处理异常
     */
    public function appointStaffDealAlarm()
    {
        $param = $this->request->param();

        if (empty($param['reservoir_id'])) {
            $this->error("水库id不能为空");
        }
        if (empty($param['alarm_id'])) {
            $this->error("报警记录id不能为空");
        }
        $alarm = Db::name("reservoir_hkws_alarm_monitoring")->where("id", $param['alarm_id'])->find();
        if (empty($alarm)) {
            $this->error("未获取到报警记录");
        } elseif ($alarm['status'] != "0") {
            $this->error("此报警已处理过,无需重复操作");
        }
        //处理类型(1:当前大屏登陆者自己已处理 2:派给巡检员处理)
        if (!in_array($param['deal_type'], [1, 2])) {
            $this->error("请选择有效的处理类型");
        }
        if ($param['deal_type'] == 2) {
            //派给巡检员处理
            if (empty($param['staff_id'])) {
                $this->error("请选择巡检员");
            } else {
                $w_staff = [
                    "id" => $param['staff_id'],
                    "is_screen_mng" => "0",
                    "is_regulatory_mng" => "0"
                ];
                $staff = Db::name("inspection_staff")->where($w_staff)->count();
                if ($staff == 0) {
                    $this->error("无效的巡检员");
                }
            }
        }
        if ($param['deal_type'] == 1) {
            $staff_id = Db::name("inspection_staff")->where("user_id", $this->auth->id)->value("id");
        } else {
            $staff_id = $param['staff_id'];
        }
        $update_data = [
            "status" => 1,
            "deal_type" => $param['deal_type'],
            "inspection_staff_id" => $staff_id,
            "deal_time" => time()
        ];
        $res = Db::name("reservoir_hkws_alarm_monitoring")->where("id", $param['alarm_id'])->update($update_data);
        if ($res) {
            Db::name("reservoir_hkws_alarm_monitoring_read")
                ->where(["reservoir_hkws_alarm_monitoring_id" => $param['alarm_id'], "user_id" => $this->auth->id])
                ->update(["is_read" => "1", "readtime" => time()]);

            if ($param['deal_type'] == 2) {

                //插入数据到隐患表
                $rr = Db::name("reservoir_hkws_alarm_monitoring")->where("id", $param['alarm_id'])->find();
                $AlarmMonitoringModel = new \app\admin\model\reservoir\hkws\AlarmMonitoring();
                $alarm_type_list = $AlarmMonitoringModel->getAlarmTypeList();
                $yh_data = [
                    "title" => $alarm_type_list[$rr['alarm_type']],
                    "message" => $alarm_type_list[$rr['alarm_type']],
//                    "area_id" => null,
                    "staff_id" => $staff_id,
                    "status" => "1",
                    "createtime" => time(),
                    "cover_images" => $rr['alarmimage'],
                ];
                $new_id = Db::name("inspection_warning")->insertGetId($yh_data);
                if ($new_id) {
                    //发送推送通知
                    $pushApi = new XjPush();
                    $res2 = $pushApi->pushPitfall($new_id);
//                    file_put_contents("1111.log", date("Y-m-d H:i:s") . json_encode($res2, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
                }
            }

            $msg = $param['deal_type'] == 1 ? "报警处理成功" : "报警指派处理成功";
            $this->success($msg);
        } else {
            $this->error("报警处理失败,请刷新后重试");
        }
    }

    /**
     * 14. 批量删除入侵报警
     */
    public function bat_delete_alarm_log()
    {
        //查询当前用户所属部门id
        $id = $this->auth->id;
        $depart_id = Db::name("inspection_staff")->where("user_id", $id)->value("depart_id");
        $param = $this->request->param();
        if (empty($param['delete_ids'])) {
            $this->error("请先选择您要删除的报警数据");
        }

        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);
        }

        $where = [];
        if (!empty($depart_ids)) {
            $where["inspection_depart_id"] = ["IN", $depart_ids];
        }
        $reservoir_list_ids = Db::name("reservoir_list")->where($where)->column("id");

        $where2 = [];
        $data = [];
        if (!empty($reservoir_list_ids)) {
            $w = [];
            $w["id"] = ["in", $param['delete_ids']];
            $reservoir_list_id = Db::name("reservoir_hkws_alarm_monitoring")->where($w)->group("reservoir_list_id")->column("reservoir_list_id");
            if (empty($reservoir_list_id)) {
                $this->error("您操作的数据不存在");
            }
            foreach ($reservoir_list_id as $k => $v) {
                if (!in_array($v, $reservoir_list_ids)) {
                    $this->error("您操作的数据中部分无删除权限,请重新选择或联系管理员");
                }
            }
            //删除报警数据
            $res = Db::name("reservoir_hkws_alarm_monitoring")->where($w)->delete();
            if ($res) {
                //删除已读未读数据
                $w2 = [];
                $w2["reservoir_hkws_alarm_monitoring_id"] = ["in", $param['delete_ids']];
                Db::name("reservoir_hkws_alarm_monitoring_read")->where($w2)->delete();
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }

        } else {
            $this->error("暂无删除水库报警权限,请联系管理员");
        }
    }

    /**
     * 15. 每半小时执行一次自动处理异常
     */
    public function crontabAppointStaffDealAlarm()
    {
        $time = 60 * 30;//半小时
        $where2["createtime"] = ["lt", time() - $time];
        $where2['status'] = ["=", "0"];//未处理
        $now_time = time();

        $alarm = Db::name("reservoir_hkws_alarm_monitoring")->where($where2)->order("id asc")->select();
        if (!empty($alarm)) {
            // 启动事务
            Db::startTrans();
            try {
                foreach ($alarm as $k => $v) {
                    //staff表id为67;指挥中心
                    $upt_data = [];
                    $upt_data = [
                        "status" => "1",
                        "deal_type" => "1",
                        "inspection_staff_id" => 67,
                        "deal_time" => $now_time,
                        "auto_deal" => "1"
                    ];
                    $res = Db::name("reservoir_hkws_alarm_monitoring")->where("id", $v['id'])->update($upt_data);
                    if (!$res) {
                        Db::rollback();
                        return false;
                    }
                }
                // 执行提交操作
                Db::commit();
                return date("Y-m-d H:i:s", $now_time) . "处理报警数量:" . count($alarm);
            } catch (PDOException $e) {
                Db::rollback();
                return date("Y-m-d H:i:s", $now_time) . "请求异常" . $e->getMessage();
            }

        }

    }

}