Index.php 34.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 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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
<?php

namespace app\api\controller\v5;
//允许所有的跨域请求
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 app\api\controller\getui\JgPush;
use app\common\controller\Api;
use app\common\helper\HttpHelper;
use fast\Tree;
use think\Db;
use think\Request;
use Qiniu\Auth as QAuth;
use Qiniu\Storage\UploadManager;

/**
 * 首页
 * @icon fa fa-circle-o
 */
class Index extends Api
{

    /**
     * Depart模型对象
     * @var \app\admin\model\inspection\Depart
     */
    protected $noNeedLogin = ["index_yqlist","selectflow","getEquipmentList", "getwerther", "alarm_monitoring", "index_yqlistv2", "index_swlist", "reservoirTypes", "projectlist"];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 调用高德天气接口
     */
    public function weather()
    {
        $url = "https://restapi.amap.com/v3/weather/weatherInfo?";
        $param = $this->request->param();
        $data['city'] = $param['city'];
        $data['key'] = "c81b56ecfdeaea9c1e7bb67ef07187c3";
        $data['extensions'] = $param['extensions'];

        $res = HttpHelper::get($url, $data);
        $return_data = json_decode($res);
        $this->success("天气数据获取成功", $return_data);
    }

    /**
     * 3、首页(上):待办事项未处理数量、消息通知未查看数量、雨情动态超水位数量,库水位告警数量
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $return_data = [];
            //待办事项数量
            $wh2 = [
                "log.user_id" => $this->auth->id,
                "log.status" => "0"
            ];
            $event_count = Db::name("inspection_task_log")
                ->alias('log')
                ->join('inspection_task t', 'log.insepection_task_id=t.id')
                ->where(['log.optstatus' => 0])
                ->where($wh2)
                ->group('log.insepection_task_id')
                ->count();
            $return_data['eventNum'] = $event_count ? $event_count : 0;

            //消息通知公告数量
            $ids = Db::name("inspection_message")->column("id");
            if (!empty($ids)) {
                $ww = [];
                $ww["insepection_message_id"] = ["in", $ids];
                $ww["status"] = ["=", "1"];
                $ww['user_id'] = $this->auth->id;
                $hasread = Db::name("inspection_message_log")
                    ->where($ww)
                    ->count();
                $noread_num = count($ids) - $hasread > 0 ? count($ids) - $hasread : 0;
            } else {
                $noread_num = 0;
            }

            $return_data['messageUnreadNum'] = $noread_num ? $noread_num : 0;

            //查询当前用户所属部门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];
            } else {
                $where["inspection_depart_id"] = ["=", "-9999"];
            }
            $reservoir_list_ids = Db::name("reservoir_list")->where($where)->column("id");
            $where2 = [];
            if (!empty($reservoir_list_ids)) {
                $where2["reservoir_id"] = ["IN", $reservoir_list_ids];
            } else {
                $where2["reservoir_id"] = ["=", "-9999"];
            }

            //库水位警告
            $water_undeal_num = Db::name("reservoir_warning_waterlevel")
                ->where($where2)
                ->where("deal", "1")
                ->whereTime("createtime", "today")
                ->group("reservoir_id")
                ->count();

            $return_data['waterLevelNum'] = $water_undeal_num ? $water_undeal_num : 0;

            //坝体安全统计
            //坝体渗压预警(无数据)、坝体位移预警
            $warningDispNum = Db::name('reservoir_warning_displacement')
                ->where("deal", "1")
                ->where($where2)
                ->whereTime("createtime", "today")
                ->group("reservoir_id")
                ->count();

            $return_data['warningDispNum'] = $warningDispNum ? $warningDispNum : 0;

            $this->success('获取成功', $return_data);
        }
    }

    /**
     * 3-2、首页(上)-雨情动态弹框列表
     */
    public function index_yqlist()
    {
        $db = Db::name("reservoir_warning_rainfall")->alias("a")
            ->join("reservoir_list l", "l.id=a.reservoir_id")
            ->where("a.deal", "1")
            //->whereTime("createtime", "today")
            ->field("a.value,a.warning,a.createtime,a.equipment_id,l.name,a.reservoir_id as reservoir_list_id")
            ->order("a.createtime desc")
            ->limit(1000000000)->buildSql();

        $list = Db::table("$db od")
            ->group("od.reservoir_list_id")
            ->order("od.createtime desc")
            ->select();

        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['createtime'] = date("Y-m-d H:i:s", $v['createtime']);
                if (!empty($v['warning'])) {
                    $warning = explode(",", $v['warning']);
                    $low = $warning[0];
                    $high = $warning[1];
                    if ($v['value'] > $high) {
                        $list[$k]['exceed'] = bcsub($v['value'], $high, 2);
                    } else {
                        $list[$k]['exceed'] = bcsub($low, $v['value'], 2);
                    }
                    $list[$k]['high'] = $high;
                }
            }
        }
        $this->success("雨情动态获取成功", $list);

    }

    /**
     * 3-3、首页(上)-水位警告动态弹框列表
     */
    public function index_swlist()
    {
        //按部门分权限
        //查询当前用户所属部门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];
        } else {
            $where["inspection_depart_id"] = ["=", "-9999"];
        }
        $reservoir_list_ids = Db::name("reservoir_list")->where($where)->column("id");

        $where2 = [];
        if (!empty($reservoir_list_ids)) {
            $where2["a.reservoir_id"] = ["IN", $reservoir_list_ids];
        } else {
            $where2["a.reservoir_id"] = ["=", "-9999"];
        }
        $db = Db::name("reservoir_warning_waterlevel")
            ->alias("a")
            ->join("reservoir_list l", "l.id=a.reservoir_id")
            ->where('a.deal', "1")
            ->whereTime("a.createtime", "today")
            ->where($where2)
            ->field("a.value,a.warning,a.createtime,a.equipment_id,l.name,a.reservoir_id as reservoir_list_id")
            ->order("a.createtime desc")
            ->limit(1000000000)->buildSql();

        $list = Db::table("$db od")
            ->group("od.reservoir_list_id")
            ->order("od.createtime desc")
            ->select();

        //水位动态
//        $list = Db::name("reservoir_warning_waterlevel")->alias("a")
//            ->join("reservoir_list l", "l.id=a.reservoir_id")
//            ->where("a.deal", "1")
////            ->whereTime("createtime", "today")
//            ->field("a.value,a.warning,a.createtime,a.equipment_id,l.name,a.reservoir_id as reservoir_list_id")
//            ->group("a.reservoir_id")
//            ->order("a.createtime desc,a.id desc")
//            ->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['createtime'] = date("Y-m-d H:i:s", $v['createtime']);

                if (!empty($v['warning'])) {
                    $warning = explode(",", $v['warning']);
                    $low = $warning[0];
                    $high = $warning[1];
                    if ($v['value'] > $high) {
                        $list[$k]['exceed'] = bcsub($v['value'], $high, 2);
                    } else {
                        $list[$k]['exceed'] = 0 - bcsub($low, $v['value'], 2);
                    }
                    $list[$k]['high'] = $high;
                    $kr = Db::name("warterdata_capacityof")->where(["warterdata" => $v['value'], "reservoir_id" => $v['reservoir_list_id']])->value("CapacityOf");
                    $list[$k]["kr"] = $kr > 0 ? $kr : 0;
                }
            }
        }
        $this->success("水位动态警告获取成功", $list);

    }

    /**
     * 4、首页(中):入侵动态(水库类型区分)
     * 小(1)型水库、小(2)型水库、中型水库
     */
    public function alarm_monitoring()
    {
        $return_data = [];
        $all = 0;
        $type_list = [
            "1" => "小(1)型水库",
            "2" => "小(2)型水库",
            "3" => "中型水库"
        ];
        $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);
        }
        $where = [];
        if (!empty($depart_ids)) {
            $where["inspection_depart_id"] = ["IN", $depart_ids];
        } else {
            $where["inspection_depart_id"] = ["=", "-9999"];
        }
        $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];
//        }else{
//            $where2["a.reservoir_list_id"] = ["=","-9999"];
//        }
//        $where2["r.is_read"] = ["=", "0"];
//        $where2["a.status"] = ["=", "0"];
//        $all = Db::name("reservoir_hkws_alarm_monitoring_read")->alias("r")
//            ->join("reservoir_hkws_alarm_monitoring a", "a.id=r.reservoir_hkws_alarm_monitoring_id")
//            ->where($where2)
//            ->count();
        if (!empty($reservoir_list_ids)) {
            $where2["reservoir_list_id"] = ["IN", $reservoir_list_ids];
        } else {
            $where2["reservoir_list_id"] = ["=", "-9999"];
        }
        $where2["status"] = ["=", "0"];
        $all = Db::name("reservoir_hkws_alarm_monitoring")->where($where2)
            ->count();

        foreach ($type_list as $k => $v) {
            $where = [];
            if (!empty($depart_ids)) {
                $where["inspection_depart_id"] = ["IN", $depart_ids];
            } else {
                $where["inspection_depart_id"] = ["=", "-9999"];
            }
            $where["reservoir_level"] = ["eq", $v];
            $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];
            } else {
                $where2["a.reservoir_list_id"] = ["=", "-9999"];
            }
            $where2["r.is_read"] = ["=", "0"];
            $where2["a.status"] = ["=", "0"];
            //1、【首页_入侵动态】显示的条数与后台【海康威视_报警监测】查询到的数据不一样 所以注释代码
//            $count = Db::name("reservoir_hkws_alarm_monitoring_read")->alias("r")
//                ->join("reservoir_hkws_alarm_monitoring a", "a.id=r.reservoir_hkws_alarm_monitoring_id")
//                ->where($where2)
//                ->count();
            if (!empty($reservoir_list_ids)) {
                $where3["reservoir_list_id"] = ["IN", $reservoir_list_ids];
            } else {
                $where3["reservoir_list_id"] = ["=", "-9999"];
            }
            $where3["status"] = ["=", "0"];
            $count = Db::name("reservoir_hkws_alarm_monitoring")->where($where3)->count();

            $dd = [];
            $dd = [
                "id" => $k,
                "name" => $v,
                "num" => $count,
            ];
            $list[] = $dd;
        }

        $return_data = [
            "all" => $all,
            "list" => $list,
        ];
        $this->success("数据获取成功", $return_data);

    }

    /**
     * 4-2、首页(中)-入侵动态-弹框列表
     */
    public function unReadList()
    {
        $return_data = [];
        $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);
        }
        $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.is_read"] = ["=", "0"];
        $where2["a.status"] = ["=", "0"];
        if (!empty($reservoir_list_ids)) {
            $where2["a.reservoir_list_id"] = ["IN", $reservoir_list_ids];
        }
        /*$list = 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")
            ->group("a.reservoir_list_id")
            ->select();
        */
        //@pcl 2023-08-04 修改
        $list = Db::name("reservoir_hkws_alarm_monitoring")
            ->alias("a")
            ->join("reservoir_list l", "l.id=a.reservoir_list_id")
            ->where($where2)
            ->field("count(*) as total,l.name,a.reservoir_list_id")
            ->group("a.reservoir_list_id")
            ->select();


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

    }

    /**
     * 5、首页(下):当天巡查动态
     */
    public function projectlist222()
    {
        $projectModel = new \app\admin\model\inspection\Project();
        $ww = [];
        $start_time = strtotime(date("Y-m-d"));
        $ww["project.begintime"] = ["egt", $start_time];
        $ww["project.endtime"] = ["lt", $start_time + 3600 * 24];
        $total = $projectModel
            ->alias('project')
            ->join('inspection_plan plan', 'plan.id=project.plan_id', 'LEFT')
            ->join('inspection_area area', 'area.id=project.area_id', 'LEFT')
            ->join('inspection_route route', 'route.id=project.route_id', 'LEFT')
            ->where($ww)
            ->count();

        $status = [
            'yxj' => 0,
            'wxj' => 0
        ];
        $list = $projectModel
            ->alias('project')
            ->join('inspection_plan plan', 'plan.id=project.plan_id', 'LEFT')
            ->join('inspection_area area', 'area.id=project.area_id', 'LEFT')
            ->join('inspection_route route', 'route.id=project.route_id', 'LEFT')
            ->join('reservoir_list reservoir', 'reservoir.id=area.reservoir_id', 'LEFT')
            ->field('plan.plan_name,reservoir.name,reservoir.thumbnail_images,reservoir.user_name,project.id,from_unixtime(project.createtime,"%Y-%m-%d") as crts,project.status,project.state,plan.num')
            ->field('plan.plan_name,route.route_name,area.area_name,project.checktime,project.begintime,project.endtime,project.jumptime')
            ->where($ww)
            ->order('project.createtime desc')
            ->select();
        foreach ($list as $key => $item) {
            if ($item['status'] == '0') {
                $status['wxj']++;
            } elseif ($item['status'] == '1') {
                $status['yxj']++;
            }
        }

        $return_data = [
            'total' => $total,
            'yxj' => $status['yxj'],
            'wxj' => $status['wxj']
        ];
        $this->success('', $return_data);
    }

    /***
     * 坝体安全预警列表
     */
    public function warningDispList()
    {
        $data = [];

        $list = Db::name('reservoir_warning_displacement')
            ->alias('d')
            ->join('reservoir_list r', 'd.reservoir_id=r.id')
            ->where("d.deal", "1")
            ->whereTime("d.createtime", "today")
            ->group("d.reservoir_id")
            ->field('d.reservoir_id,r.name,r.id')
            ->select();
        if ($list) {
            foreach ($list as $k => $v) {
                $data[$k]['id'] = $v['id'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['isotonic_num'] = 0;
                //统计预警记录
                $dispcount = Db::name('reservoir_warning_displacement')
                    ->where("deal", "1")
                    ->whereTime("createtime", "today")
                    ->count();
                $data[$k]['disp_num'] = $dispcount ? $dispcount : 0;
            }
        }

        $this->success('获取成功', $data);
    }

    /***
     * 水库动态
     * 入侵记录
     */
    public function getAllRiveorWarning()
    {
        $data = $this->getAllRiveorWarningList();

        unset($data['riverArr']);
        $this->success('获取成功', $data);
    }

    /***
     * 水库动态
     * 入侵记录
     */
    protected function getAllRiveorWarningList()
    {
        //获得水库列表,判断最新1条水位是否超过最大值(用库容的最大水位值)
        $riverlist = Db::name('reservoir_list')
            ->select();
        $data['reservoir_num'] = !empty($riverlist) ? count($riverlist) : 0;
        $data['reservoir_num_2'] = 0;
        if ($riverlist) {
            $reivids_hk = [];
            foreach ($riverlist as $k => $v) {
                //判断今日是否有入侵记录没有处理
                $khcks = Db::name('reservoir_hkws_alarm_monitoring')
                    ->where(['reservoir_list_id' => $v['id'], 'status' => '0'])
                    ->whereTime('createtime', 'today')
                    ->order('id desc')
                    ->find();
                if (!empty($khcks)) {
                    array_push($reivids_hk, $v['id']);
                }
            }
        }

        $newArr = $reivids_hk;

        $data['riverArr'] = $newArr;
        if (!empty($newArr)) {
            $data['reservoir_num_2'] = count($newArr);
            $data['reservoir_num'] = count($riverlist) - count($newArr) > 0 ? count($riverlist) - count($newArr) : 0;
        }

        return $data;
    }

    /***
     * 获得水库动态告警的列表
     */
    public function getRiveorWarnList()
    {
        $list = [];
        $data = $this->getAllRiveorWarningList();
        if ($data['reservoir_num_2'] > 0) {
            //有异常
            foreach ($data['riverArr'] as $k => $v) {
                $list[$k]['id'] = $v;
                $list[$k]['name'] = get_reservoir_name($v);
                //水位异常
                $list[$k]['water_level_num'] = 0;
                //入侵未处理条数
                $khcks = Db::name('reservoir_hkws_alarm_monitoring')
                    ->where(['reservoir_list_id' => $v, 'status' => '0'])
                    ->whereTime('createtime', 'today')
                    ->order('id desc')
                    ->count();
                $list[$k]['hkws_alarm_num'] = $khcks ? $khcks : 0;
            }
        }

        $this->success('获取成功', $list);
    }

    //水库类型统计
    public function reservoirTypes()
    {
        $all = Db::name('reservoir_list')->count();
        $x1 = Db::name('reservoir_list')
            ->where('reservoir_level', '小(1)型水库')
            ->count();
        $x2 = Db::name('reservoir_list')
            ->where('reservoir_level', '小(2)型水库')
            ->count();

        $z = Db::name('reservoir_list')
            ->where('reservoir_level', '中型水库')
            ->count();

        $data = [
            'sktj' => [
                ['value' => $x1, 'name' => '小(1)型水库'],
                ['value' => $x2, 'name' => '小(2)型水库'],
                ['value' => $z, 'name' => '中型水库'],
            ],
            'total' => $all,//已接入

        ];

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

    }

    /**
     * 5、首页:巡检统计
     */
    public function projectlist()
    {
        $projectModel = new \app\admin\model\inspection\Project();
        $ww = [];
        $start_time = strtotime(date("Y-m-d"));
        $ww1["project.endtime"] = ["gt", time()];
        $total = $projectModel
            ->alias('project')
            ->join('inspection_plan plan', 'plan.id=project.plan_id', 'LEFT')
            ->join('inspection_area area', 'area.id=project.area_id', 'LEFT')
            ->join('inspection_route route', 'route.id=project.route_id', 'LEFT')
            ->where($ww)
            ->where($ww1)
            ->count();

        $status = [
            'yxj' => 0,
            'wxj' => 0
        ];
        $ww2["project.begintime"] = ["egt", $start_time];
        $ww2["project.endtime"] = ["lt", $start_time + 3600 * 24];
        $list = $projectModel
            ->alias('project')
            ->join('inspection_plan plan', 'plan.id=project.plan_id', 'LEFT')
            ->join('inspection_area area', 'area.id=project.area_id', 'LEFT')
            ->join('inspection_route route', 'route.id=project.route_id', 'LEFT')
            ->join('reservoir_list reservoir', 'reservoir.id=area.reservoir_id', 'LEFT')
            ->field('plan.plan_name,reservoir.name,reservoir.thumbnail_images,reservoir.user_name,project.id,from_unixtime(project.createtime,"%Y-%m-%d") as crts,project.status,project.state,plan.num')
            ->field('plan.plan_name,route.route_name,area.area_name,project.checktime,project.begintime,project.endtime,project.jumptime')
            ->where($ww)
            ->where($ww2)
            ->order('project.createtime desc')
            ->select();
        foreach ($list as $key => $item) {
            if ($item['status'] == '0') {
                $status['wxj']++;
            } elseif ($item['status'] == '1') {
                $status['yxj']++;
            }
        }

        $return_data = [
            'wks' => $total,
            'wxj' => $status['wxj'],
            'yxj' => $status['yxj'],
            'status_name' => ['未开始', '巡检中', '已完成'],
            'status_value' => [$total, $status['wxj'], $status['yxj']],
        ];
        $this->success('', $return_data);
    }

    /***
     * 获得水库搜索最新10条数据
     */
    public function getNewSerchList()
    {
        $uid = $this->auth->id;
        $search = Db::name("reservoir_search_log")->where("user_id", $uid)->order('id desc')->limit(10)->select();
        if ($search) {
            $this->success('请求成功', $search);
        } else {
            $this->error("系统繁忙");
        }
    }

    /***
     * 删除搜索记录
     */
    public function deleteSerchList()
    {
        $uid = $this->auth->id;
        $search = Db::name("reservoir_search_log")->where("user_id", $uid)->delete();
        if ($search) {
            $this->success('请求成功');
        } else {
            $this->error("系统繁忙");
        }

    }

    public function addSerchList()
    {
        $serach = $this->request->param('search', '');
        $reservoir_id = $this->request->param('reservoir_id', '');

        if (!$serach) {
            $this->error("系统繁忙");
        }
        //查询是否有搜索记录
        $search = Db::name("reservoir_search_log")->where([
            "user_id" => $this->auth->id,
            "search" => $serach
        ])->find();
        if ($search) {
            $deletesearch = Db::name("reservoir_search_log")->where('id', $search['id'])->delete();
        }
        //添加搜索记录
        $searchlog = Db::name("reservoir_search_log")->insert([
            "user_id" => $this->auth->id,
            "search" => $serach,
            "reservoir_id" => $reservoir_id,
        ]);
        if ($searchlog) {
            $this->success('请求成功');
        } else {
            $this->error("系统繁忙", $searchlog);

        }
    }

    /***
     * 获得水库断面列表
     */
    public function getDmList()
    {
        $ids = input('post.ids');
        $elist = Db::name('reservoir_equipment')
            ->where(['reservoir_id' => $ids, 'type' => '3'])
            ->group('dmname')
            ->field('dmname')
            ->select();

        $this->success('获取成功', $elist);
    }

    /***
     * 水库详情-获得最近5天渗压图数据
     */
    public function getdamisotonicListDay5()
    {
        $ids = input('post.ids');
        $day = input('post.day', 5) - 1;
        $dmname = input('post.dmname');

        $categories = [];
        $series = [];

        //最近五天最后一条数据
        //获得选择的断面对应的设备列表
        $elist = Db::name('reservoir_equipment')
            ->where(['reservoir_id' => $ids, 'type' => '3', 'dmname' => $dmname])
            ->order('id asc')
            ->select();
        $date = [];
        $date_value = [];
        $isotonic_value = [];
        $distance = [];
        for ($i = $day; $i >= 0; $i--) {
            $beginTime = mktime(00, 00, 00, date('m'), date('d') - $i, date('Y'));
            $endTime = mktime(23, 59, 59, date("m"), date("d") - $i, date("Y"));
            array_push($date, date("m/d", $beginTime));
            $rr = "";
            $wh = [];
            $wh["reservoir_id"] = ["=", $ids];
            $wh["createtime"] = ["between", [$beginTime, $endTime]];
            $rr = Db::name("reservoir_rain_water_level")->where($wh)->order("createtime desc")->value("water_level");
            array_push($date_value, $rr);
            //获得设备的最新1条数据
            $orifice_water_surface_distance = [];
            $iotname = [];
            if ($elist) {
                foreach ($elist as $k => $v) {
                    $damisotonic = Db::name('reservoir_dam_isotonic')
                        ->where(['equipment_id' => $v['deviceId']])
                        ->where($wh)
                        ->order('id desc')
                        ->find();
                    //水面高程(m)
                    $orifice_water_surface_distance[][] = dam_isotonic_attr_data($v['orifice_elevation'], $v['hole_depth'], $damisotonic['dx_value'], 2);
                    $iotname[][] = $v['name'];
                }
            }

            array_push($distance, $orifice_water_surface_distance);
            array_push($isotonic_value, $iotname);
        }
        $categories = $date;
        $series['waterlevel']['data'] = $date_value;
        $series['waterlevel']['name'] = "水位";
        $series['waterlevel']['type'] = "area";
        $series['waterlevel']['style'] = "curve";
        //处理渗压
        $result = [];
        if ($distance) {
            foreach ($distance as $k => $v) {
                for ($i = 0; $i < count($v); $i++) {
                    $result[$i][] = $v[$i];
                }
            }
        }

        $result2 = [];
        foreach ($result as $arr) {
            $temp = [];
            foreach ($arr as $innerArr) {
                $temp[] = $innerArr[0];
            }
            $result2[] = $temp;
        }

        if ($isotonic_value) {
            foreach ($isotonic_value[0] as $k => $v) {
                $series['dam'][$k]['name'] = $v[0];
                $series['dam'][$k]['type'] = "line";
                $series['dam'][$k]['style'] = "curve";
                $series['dam'][$k]['data'] = $result2[$k];
            }
        }

        $data = [
            "categories" => $categories,
            "series" => $series,
        ];
        $this->success("数据获取成功", $data);
    }

    /**
     * 水库详情-获得水库位移设备列表
     */
    public function getEquipmentList()
    {
        $ids = input('post.ids');
        $elist = Db::name('reservoir_equipment')
            ->where(['reservoir_id' => $ids, 'type' => '6'])
            ->field('deviceId,name')
            ->select();
        if(empty($elist)){
            $elist = Db::name('reservoir_equipment')
                ->where(['reservoir_id' => $ids, 'type' => '4'])
                ->field('deviceId,name')
                ->select();
        }

        $this->success('获取成功', $elist);
    }

    /***
     * 水库详情-最近5天位移数据图
     */
    public function displacement5Days()
    {
        $ids = input('post.ids');
        $day = input('post.day', 5) - 1;
        $number = $this->request->param('deviceId', '');
        $categories = [];
        $series = [];

        //最近五天最后一条数据
        $date = [];
        $date_x = [];
        $date_y = [];
        $date_z = [];
        for ($i = $day; $i >= 0; $i--) {
            $beginTime = mktime(00, 00, 00, date('m'), date('d') - $i, date('Y'));
            $endTime = mktime(23, 59, 59, date("m"), date("d") - $i, date("Y"));
            array_push($date, date("m/d", $beginTime));
            $rr_x = "";
            $wh = [];
            $wh["reservoir_id"] = ["=", $ids];
            $wh['number'] = ['=', $number];
            $wh["createtime"] = ["between", [$beginTime, $endTime]];
            $rr_x = Db::name("reservoir_dam_displacement")
                ->where($wh)
                ->field('horizontal,horizontalY,vertical')
                ->order("createtime desc")
                ->find();
            array_push($date_x, $rr_x['horizontal']);
            array_push($date_y, $rr_x['horizontalY']);
            array_push($date_z, $rr_x['vertical']);
        }
        $categories = $date;
        $series1['name'] = "X轴偏移";
        $series1['style'] = "curve";
        $series1['data'] = $date_x;
        $series2['name'] = "Y轴偏移";
        $series2['style'] = "curve";
        $series2['data'] = $date_y;
        $series3['name'] = "沉降";
        $series3['style'] = "curve";
        $series3['data'] = $date_z;

        $series = [$series1, $series2, $series3];
        //计算最大值和最小值
        $maxValue = PHP_INT_MIN;
        $minValue = PHP_INT_MAX;

        // 遍历三维数组
        foreach ($series as $level1) {
            foreach ($level1['data'] as $level2) {
                // 更新最大值和最小值
                $maxValue = max($maxValue, $level2);
                $minValue = min($minValue, $level2);
            }
        }
        $max = [];
        $max['maxValue'] = $maxValue;
        $max['minValue'] = $minValue;

        $data = [
            "categories" => $categories,
            "series" => $series,
            "max" => $max,
        ];
        $this->success("数据获取成功", $data);
    }

    /**
     * 获取天气雨云
     * @return void
     */
    public function getwerther()
    {
        $lat = $this->request->param('lat', '23.36422');
        $lng = $this->request->param('lng', '103.37560');
        $apiurl = "https://api.caiyunapp.com/v1/radar/forecast_images?lon=" . $lng . "&lat=" . $lat . "&level=2&token=EfrXeQoMMZUbDhZ4&world_map=true";
        $res = $this->httpRequest($apiurl);
        $this->success('获取成功', $res);

    }

    /***
     * @param $url
     * @param int $timeout
     * @param array $header
     * @return mixed
     * curl请求数据 返回array()
     */
    protected function httpRequest($url, $timeout = 30, $header = array())
    {
        if (!function_exists('curl_init')) {
            throw new Exception('server not install curl');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        $data = curl_exec($ch);
        list($header, $data) = explode("\r\n\r\n", $data);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($http_code == 301 || $http_code == 302) {
            $matches = array();
            preg_match('/Location:(.*?)\n/', $header, $matches);
            $url = trim(array_pop($matches));
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, false);
            $data = curl_exec($ch);
        }

        if ($data == false) {
            curl_close($ch);
        }
        @curl_close($ch);

        $data = json_decode($data, true);
        return $data;
    }

    /**
     * 流量计查询
     * @return void
     */
    public function selectflow(){
        $res=Db::name("reservoir_equipment")
            ->where("type",1)
            ->select();
        $returndata=[];
        foreach ($res as $k=>$v){
            $resflow=Db::name("reservoir_flow")
                ->where("number",$res[$k]['apiKey'])
                ->order("id desc")
                ->find();
            $returndata[$k]=[
                "id"=>$res[$k]['id'],
                "name"=>$res[$k]['name'],
                "number"=>$res[$k]['apiKey'],
                "instantaneous_value"=>$resflow['instantaneous_value'],
                "cumulative_value"=>$resflow['cumulative_value'],
            ];
        }
        $this->success('获取成功', $returndata);
    }
}