Warningv2.php 20.1 KB
<?php

namespace app\api\controller\reservoir\warning;

use app\common\controller\Api;
use think\Request;
use think\Db;

/**
 * Class Waterlevel
 * @package app\api\controller
 */
class Warningv2 extends Api
{
    protected $noNeedLogin = ['*'];

    public function getWarningCountbyday()
    {
        $count = [];
        //当日
        $time = 1;
        $lasttime = strtotime(date('Y-m-d 23:59:59')) - $time * 60 * 60 * 24;
        $cond['reservoir_id'] = ['in', '4,6,9'];
        $cond['createtime'] = ['between', [$lasttime, strtotime(date('Y-m-d 23:59:59'))]];
        $crack = Db::name('reservoir_warning_crack')->where($cond)->count();
        //$displacement = Db::name('reservoir_warning_displacement')->where($cond)->count();
        $rainfall = Db::name('reservoir_warning_rainfall')->where($cond)->count();
        $settlement = Db::name('reservoir_warning_settlement')->where($cond)->count();
        $waterlevel = Db::name('reservoir_warning_waterlevel')->where($cond)->count();
        $waterseepage = Db::name('reservoir_warning_waterseepage')->where($cond)->count();
        $weather = Db::name('reservoir_warning_weather')->where($cond)->count();
        $count['day'] = $crack + $rainfall + $settlement + $waterlevel + $waterseepage + $weather;
        //$count['day'] = $crack+$displacement+$rainfall+$settlement+$waterlevel+$waterseepage+$weather;

        //当月
        $time = 30;
        $lasttime = strtotime(date('Y-m-d 23:59:59')) - $time * 60 * 60 * 24;
        $cond['reservoir_id'] = ['in', '4,6,9'];
        $cond['createtime'] = ['between', [$lasttime, strtotime(date('Y-m-d 23:59:59'))]];
        $crack = Db::name('reservoir_warning_crack')->where($cond)->count();
        //$displacement = Db::name('reservoir_warning_displacement')->where($cond)->count();
        $rainfall = Db::name('reservoir_warning_rainfall')->where($cond)->count();
        $settlement = Db::name('reservoir_warning_settlement')->where($cond)->count();
        $waterlevel = Db::name('reservoir_warning_waterlevel')->where($cond)->count();
        $waterseepage = Db::name('reservoir_warning_waterseepage')->where($cond)->count();
        $weather = Db::name('reservoir_warning_weather')->where($cond)->count();
        $count['month'] = $crack + $rainfall + $settlement + $waterlevel + $waterseepage + $weather;
        //$count['month'] = $crack+$displacement+$rainfall+$settlement+$waterlevel+$waterseepage+$weather;

        //当季
        $time = 90;
        $lasttime = strtotime(date('Y-m-d 23:59:59')) - $time * 60 * 60 * 24;
        $cond['reservoir_id'] = ['in', '4,6,9'];
        $cond['createtime'] = ['between', [$lasttime, strtotime(date('Y-m-d 23:59:59'))]];
        $crack = Db::name('reservoir_warning_crack')->where($cond)->count();
        //$displacement = Db::name('reservoir_warning_displacement')->where($cond)->count();
        $rainfall = Db::name('reservoir_warning_rainfall')->where($cond)->count();
        $settlement = Db::name('reservoir_warning_settlement')->where($cond)->count();
        $waterlevel = Db::name('reservoir_warning_waterlevel')->where($cond)->count();
        $waterseepage = Db::name('reservoir_warning_waterseepage')->where($cond)->count();
        $weather = Db::name('reservoir_warning_weather')->where($cond)->count();
        //$count['quarter'] = $crack+$displacement+$rainfall+$settlement+$waterlevel+$waterseepage+$weather;
        $count['quarter'] = $crack + $rainfall + $settlement + $waterlevel + $waterseepage + $weather;

        //当年
        $time = 365;
        $lasttime = strtotime(date('Y-m-d 23:59:59')) - $time * 60 * 60 * 24;
        $cond['reservoir_id'] = ['in', '4,6,9'];
        $cond['createtime'] = ['between', [$lasttime, strtotime(date('Y-m-d 23:59:59'))]];
        $crack = Db::name('reservoir_warning_crack')->where($cond)->count();
        //$displacement = Db::name('reservoir_warning_displacement')->where($cond)->count();
        $rainfall = Db::name('reservoir_warning_rainfall')->where($cond)->count();
        $settlement = Db::name('reservoir_warning_settlement')->where($cond)->count();
        $waterlevel = Db::name('reservoir_warning_waterlevel')->where($cond)->count();
        $waterseepage = Db::name('reservoir_warning_waterseepage')->where($cond)->count();
        $weather = Db::name('reservoir_warning_weather')->where($cond)->count();
        //$count['year'] = $crack+$displacement+$rainfall+$settlement+$waterlevel+$waterseepage+$weather;
        $count['year'] = $crack + $rainfall + $settlement + $waterlevel + $waterseepage + $weather;
        $this->success('成功', $count);
    }

    public function getWarningCountbyday1()
    {
        $time = 1;
        $lasttime = strtotime(date('Y-m-d 23:59:59')) - $time * 60 * 60 * 24;
        $cond['reservoir_id'] = ['in', '4,6,9'];
        $cond['createtime'] = ['between', [$lasttime, strtotime(date('Y-m-d 23:59:59'))]];
        $crack = Db::name('reservoir_warning_crack')->where($cond)->count();
        $displacement = Db::name('reservoir_warning_displacement')->where($cond)->count();
        $rainfall = Db::name('reservoir_warning_rainfall')->where($cond)->count();
        $settlement = Db::name('reservoir_warning_settlement')->where($cond)->count();
        $waterlevel = Db::name('reservoir_warning_waterlevel')->where($cond)->count();
        $waterseepage = Db::name('reservoir_warning_waterseepage')->where($cond)->count();
        $weather = Db::name('reservoir_warning_weather')->where($cond)->count();
        $count = $crack + $displacement + $rainfall + $settlement + $waterlevel + $waterseepage + $weather;
        $this->success('成功', $count);
    }


    //获取预警总数
    public function getWarningCount()
    {

        $id = input('get.id');//水库id
        $crack = Db::name('reservoir_warning_crack')->where('reservoir_id', $id)->count();
        $displacement = Db::name('reservoir_warning_displacement')->where('reservoir_id', $id)->count();
        $rainfall = Db::name('reservoir_warning_rainfall')->where('reservoir_id', $id)->count();
        $settlement = Db::name('reservoir_warning_settlement')->where('reservoir_id', $id)->count();
        $waterlevel = Db::name('reservoir_warning_waterlevel')->where('reservoir_id', $id)->count();
        $waterseepage = Db::name('reservoir_warning_waterseepage')->where('reservoir_id', $id)->count();
        $weather = Db::name('reservoir_warning_weather')->where('reservoir_id', $id)->count();
        $count = $crack + $displacement + $rainfall + $settlement + $waterlevel + $waterseepage + $weather;
        $this->success('成功', $count);
    }

    //获取预警条数
    public function getWarningList()
    {
        $id = input('get.id');//水库id
        $res['crack'] = Db::name('reservoir_warning_crack')->where('reservoir_id', $id)->order('createtime desc')->limit(1)->select();
        $res['displacement'] = Db::name('reservoir_warning_displacement')->where('reservoir_id', $id)->order('createtime desc')->limit(1)->select();
        $res['rainfall'] = Db::name('reservoir_warning_rainfall')->where('reservoir_id', $id)->order('createtime desc')->limit(1)->select();
        $res['settlement'] = Db::name('reservoir_warning_settlement')->where('reservoir_id', $id)->order('createtime desc')->limit(1)->select();
        $res['waterlevel'] = Db::name('reservoir_warning_waterlevel')->where('reservoir_id', $id)->order('createtime desc')->limit(1)->select();
        $res['waterseepage'] = Db::name('reservoir_warning_waterseepage')->where('reservoir_id', $id)->order('createtime desc')->limit(1)->select();
        $res['weather'] = Db::name('reservoir_warning_weather')->where('reservoir_id', $id)->order('createtime desc')->limit(1)->select();
        $this->success('成功', $res);
    }

    //通过县市id获取降水量正常异常座数
    public function getRainWarningByCounty($id)
    {
        $reservoirids = Db::name('reservoir_list')->where('county_id', $id)->field('id')->select();
        $ids = [];
        foreach ($reservoirids as $k => $v) {
            $ids[] = $v['id'];
        }
        $res['abnormal'] = Db::name('reservoir_warning_rainfall')->where('reservoir_id', 'in', $ids)->where('deal', 1)->group('reservoir_id')->count();
        $res['normal'] = count($ids) - $res['abnormal'];
        $this->success('', $res);
    }

    public function getReservoirWaterlevel($id)
    {
        $list = Db::name('reservoir_warning_waterlevel')
            ->alias('w')
            ->join('reservoir_list l', 'l.id=w.reservoir_id', 'LEFT')
            ->field('l.name,w.value,from_unixtime(w.createtime,"%Y-%m-%d") as crts')
            ->where('l.county_id', $id)
            ->order('w.createtime desc')
            ->limit(10)
            ->select();
        $arr = [];
        foreach ($list as $key => $item) {
            $arr[$key] = [$item['name'], $item['value'], $item['crts']];
        }
        $this->success('', $arr);
    }


    //县市预警数量
    public function getCountyWarningCount($id)
    {
        $reservoirids = Db::name('reservoir_list')->where('county_id', $id)->field('id')->select();
        $ids = [];
        foreach ($reservoirids as $k => $v) {
            $ids[] = $v['id'];
        }
        $res['crack'] = Db::name('reservoir_warning_crack')->where('reservoir_id', 'in', $ids)->where('deal', 1)->count();
        $res['displacement'] = Db::name('reservoir_warning_displacement')->where('reservoir_id', 'in', $ids)->where('deal', 1)->count();
        $res['rainfall'] = Db::name('reservoir_warning_rainfall')->where('reservoir_id', 'in', $ids)->where('deal', 1)->count();
        $res['settlement'] = Db::name('reservoir_warning_settlement')->where('reservoir_id', 'in', $ids)->where('deal', 1)->count();
        $res['waterlevel'] = Db::name('reservoir_warning_waterlevel')->where('reservoir_id', 'in', $ids)->where('deal', 1)->count();
        $res['waterseepage'] = Db::name('reservoir_warning_waterseepage')->where('reservoir_id', 'in', $ids)->where('deal', 1)->count();
        $res['weather'] = Db::name('reservoir_warning_weather')->where('reservoir_id', 'in', $ids)->where('deal', 1)->count();
        $this->success('', $res);
    }

    //近七天县市预警趋势 $id 县市id
    public function getCountyWarningCountWeek($id)
    {
        $reservoirids = Db::name('reservoir_list')->where('county_id', $id)->field('id')->select();
        $ids = [];
        foreach ($reservoirids as $k => $v) {
            $ids[] = $v['id'];
        }
        for ($i = 6; $i >= 1; $i--) {
            $k = $i - 1;
            ${'predaystart' . $i} = strtotime(date('Y-m-d', strtotime("-$i day")));
            ${'predayend' . $i} = strtotime(date('Y-m-d', strtotime("-$k day")));
            ${'predaycount' . $i} = Db::name('reservoir_warning_rainfall')->where('createtime', '>=', ${'predaystart' . $i})->where('createtime', '<', ${'predayend' . $i})->where('reservoir_id', 'in', $ids)->count();
            $res['date'][] = date('Y-m-d', strtotime("-$i day"));
            $res['count'][] = ${'predaycount' . $i};
        }
        $today = strtotime(date("Y-m-d", time()));
        $todaylist = Db::name('reservoir_warning_rainfall')->where('createtime', '>=', $today)->where('reservoir_id', 'in', $ids)->count();
        $res['date'][] = date("Y-m-d", time());
        $res['count'][] = $todaylist;
        $this->success('', $res);
    }


    public function getRainfallValueWeek($id)
    {

        for ($i = 6; $i >= 1; $i--) {
            $k = $i - 1;
            ${'predaystart' . $i} = strtotime(date('Y-m-d', strtotime("-$i day")));
            ${'predayend' . $i} = strtotime(date('Y-m-d', strtotime("-$k day")));
            ${'predayvalue' . $i} = Db::name('reservoir_warning_rainfall')
                ->where('createtime', '>=', ${'predaystart' . $i})
                ->where('createtime', '<', ${'predayend' . $i})
                ->where('reservoir_id', $id)
                ->order('createtime desc')
                ->value('value');
            ${'predayvalue' . $i} = ${'predayvalue' . $i} ? ${'predayvalue' . $i} : 0;
            $res['date'][] = date('Y-m-d', strtotime("-$i day"));
            $res['value'][] = ${'predayvalue' . $i};
        }
        $today = strtotime(date("Y-m-d", time()));
        $todaylist = Db::name('reservoir_warning_rainfall')->where('createtime', '>=', $today)->where('reservoir_id', $id)->order('createtime desc')->value('value');
        $res['date'][] = date("Y-m-d", time());
        $res['value'][] = $todaylist;
        $this->success('', $res);
    }


    public function getWaterlevelValueWeek($id)
    {

        for ($i = 6; $i >= 1; $i--) {
            $k = $i - 1;
            ${'predaystart' . $i} = strtotime(date('Y-m-d', strtotime("-$i day")));
            ${'predayend' . $i} = strtotime(date('Y-m-d', strtotime("-$k day")));
            ${'predayvalue' . $i} = Db::name('reservoir_warning_waterlevel')
                ->where('createtime', '>=', ${'predaystart' . $i})
                ->where('createtime', '<', ${'predayend' . $i})
                ->where('reservoir_id', $id)
                ->order('createtime desc')
                ->value('value');
            ${'predayvalue' . $i} = ${'predayvalue' . $i} ? ${'predayvalue' . $i} : 0;
            $res['date'][] = date('Y-m-d', strtotime("-$i day"));
            $res['value'][] = ${'predayvalue' . $i};
        }
        $today = strtotime(date("Y-m-d", time()));
        $todaylist = Db::name('reservoir_warning_waterlevel')->where('createtime', '>=', $today)->where('reservoir_id', $id)->order('createtime desc')->value('value');
        $res['date'][] = date("Y-m-d", time());
        $res['value'][] = $todaylist;
        $this->success('', $res);
    }

    //县市预警状态
    public function getCountyWarningList($id)
    {
        $reservoirids = Db::name('reservoir_list')->where('county_id', $id)->field('id,name')->select();
        foreach ($reservoirids as $k => $v) {
            $res[$k]['crack'] = Db::name('reservoir_warning_crack')->where('reservoir_id', $v['id'])->where('deal', 1)->count();
            $res[$k]['displacement'] = Db::name('reservoir_warning_displacement')->where('reservoir_id', $v['id'])->where('deal', 1)->count();
            $res[$k]['rainfall'] = Db::name('reservoir_warning_rainfall')->where('reservoir_id', $v['id'])->where('deal', 1)->count();
            $res[$k]['settlement'] = Db::name('reservoir_warning_settlement')->where('reservoir_id', $v['id'])->where('deal', 1)->count();
            $res[$k]['waterlevel'] = Db::name('reservoir_warning_waterlevel')->where('reservoir_id', $v['id'])->where('deal', 1)->count();
            $res[$k]['waterseepage'] = Db::name('reservoir_warning_waterseepage')->where('reservoir_id', $v['id'])->where('deal', 1)->count();
            $res[$k]['weather'] = Db::name('reservoir_warning_weather')->where('reservoir_id', $v['id'])->where('deal', 1)->count();
            $res[$k]['name'] = $v['name'];
            $res[$k]['time'] = date('Y-m-d', time());
            if (($res[$k]['crack'] + $res[$k]['displacement'] + $res[$k]['rainfall'] + $res[$k]['settlement'] + $res[$k]['waterlevel'] + $res[$k]['waterseepage'] + $res[$k]['weather']) == 0) {
                $res[$k]['status'] = '正常';
            } else {
                $res[$k]['status'] = '异常';
            }
        }
        $this->success('', $res);
    }

    //实时水位数据
    public function getWarningListtime()
    {
        $data = Db::name('reservoir_warning_waterlevel')->order('createtime desc')->limit(10)->select();

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

    /**
     * 7、【右中1】入侵动态
     */
    public function alarm_monitoring_count()
    {
        $count = [];
        //当日未处理
        $time = 1;
        $lasttime = strtotime(date('Y-m-d 23:59:59')) - $time * 60 * 60 * 24;
        $cond['createtime'] = ['between', [$lasttime, strtotime(date('Y-m-d 23:59:59'))]];
        $crack = Db::name('reservoir_warning_crack')->where($cond)->count();
        $displacement = Db::name('reservoir_warning_displacement')->where($cond)->count();
        $rainfall = Db::name('reservoir_warning_rainfall')->where($cond)->count();
        $settlement = Db::name('reservoir_warning_settlement')->where($cond)->count();
        $waterlevel = Db::name('reservoir_warning_waterlevel')->where($cond)->count();
        $waterseepage = Db::name('reservoir_warning_waterseepage')->where($cond)->count();
        $weather = Db::name('reservoir_warning_weather')->where($cond)->count();
        $hkday = Db::name('reservoir_hkws_alarm_monitoring')
            ->where(['status' => '0'])
            ->whereTime('createtime', 'today')
            ->count();
        $count['day'] = $crack + $displacement + $rainfall + $settlement + $waterlevel + $waterseepage + $weather + $hkday;

        //总预警
        $crack = Db::name('reservoir_warning_crack')->count();
        $displacement = Db::name('reservoir_warning_displacement')->count();
        $rainfall = Db::name('reservoir_warning_rainfall')->count();
        $settlement = Db::name('reservoir_warning_settlement')->count();
        $waterlevel = Db::name('reservoir_warning_waterlevel')->count();
        $waterseepage = Db::name('reservoir_warning_waterseepage')->count();
        $weather = Db::name('reservoir_warning_weather')->count();
        $hkall = Db::name('reservoir_hkws_alarm_monitoring')->count();
        $count['all'] = $crack + $displacement + $rainfall + $settlement + $waterlevel + $waterseepage + $weather + $hkall;
        $return_data['warning'] = $count;

        $crackres = Db::name('reservoir_warning_crack')->order("id desc")->find();
        $displacementres = Db::name('reservoir_warning_displacement')->order("id desc")->find();
        $rainfallres = Db::name('reservoir_warning_rainfall')->order("id desc")->find();
        $settlementres = Db::name('reservoir_warning_settlement')->order("id desc")->find();
        $waterlevelres = Db::name('reservoir_warning_waterlevel')->order("id desc")->find();
        $waterseepageres = Db::name('reservoir_warning_waterseepage')->order("id desc")->find();
        $weatherres = Db::name('reservoir_warning_weather')->order("id desc")->find();

        $hknew = Db::name('reservoir_hkws_alarm_monitoring')
            ->field('id,reservoir_list_id as reservoir_id,alarm_type,createtime,status,deal_time')
            ->order("id desc")
            ->find();

        $warninginfo['info'] = $crackres;
        $warninginfo['type'] = "warning_crack";
        //warning_displacement   坝体位移
        if ($warninginfo['info']['createtime'] < $displacementres['createtime']) {
            $warninginfo['info'] = $displacementres;
            $warninginfo['type'] = "warning_displacement";
        }
        //warning_rainfall   降雨量
        if ($warninginfo['info']['createtime'] < $rainfallres['createtime']) {
            $warninginfo['info'] = $rainfallres;
            $warninginfo['type'] = "warning_rainfall";
        }
        //warning_settlement   坝体沉降
        if ($warninginfo['info']['createtime'] < $settlementres['createtime']) {
            $warninginfo['info'] = $settlementres;
            $warninginfo['type'] = "warning_settlement";
        }

        //warning_waterlevel   水位预警
        if ($warninginfo['info']['createtime'] < $waterlevelres['createtime']) {
            $warninginfo['info'] = $waterlevelres;
            $warninginfo['type'] = "warning_waterlevel";
        }

        //warning_waterseepage   坝体渗水
        if ($warninginfo['info']['createtime'] < $waterseepageres['createtime']) {
            $warninginfo['info'] = $waterseepageres;
            $warninginfo['type'] = "warning_waterseepage";
        }

        //warning_weather   极端天气
        if ($warninginfo['info']['createtime'] < $weatherres['createtime']) {
            $warninginfo['info'] = $weatherres;
            $warninginfo['type'] = "warning_weather";
        }

        //$hknew hkws_alarm
        if ($warninginfo['info']['createtime'] < $hknew['createtime']) {
            $warninginfo['info'] = $hknew;
            $warninginfo['type'] = "hkws_alarm";
        }

        $warninginfo['info']['createtime'] = date("Y-m-d H:i:s", $warninginfo['info']['createtime']);
        $reservoir = Db::name("reservoir_list")->where($warninginfo['info']['reservoir_id'])->find();

        $warninginfo['info']['reservoir_name'] = $reservoir['name'];
        $return_data['warninginfo'] = $warninginfo;
        $this->success("", $return_data);

    }
}