Warning.php 11.8 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 Warning extends Api{
    protected $noNeedLogin = ['*'];
    
    public function getWarningCountbyday()
    {
        $time = $this->request->get('day');
        $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 getWarningCountbyday1()
    {
        $time = 1;
        $lasttime = strtotime(date('Y-m-d 23:59:59')) - $time*60*60*24;
        $cond['reservoir_id'] = ['in',$this->reservoir_ids];
        $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);
    }
}