Rainfall.php
6.7 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
<?php
namespace app\api\controller\reservoir\rain;
use app\common\controller\Api;
use think\Request;
use think\Db;
/**
* Class Rainfall
* @package app\api\controller
*/
class Rainfall extends Api{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\reservoir\rain\Rainfall;
}
// 降雨量预警记录
public function warningrainlist($ids=null)
{
$page = $this->request->param('page',1);
$limit = $this->request->param('limit',10);
$total = Db::name('reservoir_warning_rainfall')
->alias('t1')
->join('reservoir_list t2','t2.id=t1.reservoir_id','LEFT')
->join('reservoir_rain_rainfall t3','t1.rainfall_id=t3.id','LEFT')
->join('reservoir_equipment t4','t4.deviceId=t3.number','LEFT')
->where('t1.reservoir_id',$ids)
->count();
$list = Db::name('reservoir_warning_rainfall')
->alias('t1')
->join('reservoir_list t2','t2.id=t1.reservoir_id','LEFT')
->join('reservoir_rain_rainfall t3','t1.rainfall_id=t3.id','LEFT')
->join('reservoir_equipment t4','t4.deviceId=t3.number','LEFT')
->field('t2.name as NameTheReservoir,t4.deviceId as EquipmentSerialNumber,t4.name as DeviceName,case t1.deal when 1 then "未处理" else "已处理" end as Alert,t1.warning as Rainfall,t1.beyond as BeyondValue,FROM_UNIXTIME(t1.createtime,"%Y-%m-%d %H:%d") as CreationTime')
->where('t1.reservoir_id',$ids)
->page($page,$limit)
->select();
$data=[
'page' => $page,
'total'=> $total,
'list'=>$list
];
$this->success('',$data);
}
// public function warningrainlist($ids=null)
// {
// $page = $this->request->param('page',1);
// $limit = $this->request->param('limit',10);
// $total = Db::name('reservoir_warning_rainfall')
// ->alias('t1')
// ->join('reservoir_list t2','t2.id=t1.reservoir_id','LEFT')
// ->where('t1.reservoir_id',$ids)
// ->count();
// $list = Db::name('reservoir_warning_rainfall')
// ->alias('t1')
// ->join('reservoir_list t2','t2.id=t1.reservoir_id','LEFT')
// ->field('t2.name as NameTheReservoir,case t1.deal when 1 then "未处理" else "已处理" end as Alert,t1.warning as Rainfall,t1.beyond as BeyondValue,FROM_UNIXTIME(t1.createtime,"%Y-%m-%d %H:%d") as CreationTime')
// ->where('t1.reservoir_id',$ids)
// ->page($page,$limit)
// ->select();
// $data=[
// 'page' => $page,
// 'total'=> $total,
// 'list'=>$list
// ];
// $this->success('',$data);
// }
public function getList(){
$id = input('get.id');
$page = input('get.page')?:1;
$limit = input('get.limit')?:8;
$starttime = strtotime(input('get.starttime'));
$endtime = strtotime(input('get.endtime'));
if($starttime && $endtime){
$res = $this->model->where('reservoir_id','=',$id)->where('createtime','>=',$starttime)->where('createtime','<=',$endtime)->order('createtime desc')->page($page,$limit)->select();
}
else {
$res = $this->model->where('reservoir_id', '=', $id)->order('createtime desc')->page($page, $limit)->select();
}
$this->success('成功',$res);
}
//$id 水库id
public function getLast($id){
$info = $this->model->order('createtime desc')->field('FROM_UNIXTIME(createtime) as createtime,rainfall')->where('reservoir_id',$id)->find();
$this->success('请求成功',$info);
}
public function getInfo($id){
$today = strtotime(date('Y-m-d',time()));
$data['today'] = Db::name('reservoir_rain_rainfall')->where('reservoir_id',$id)->where('createtime','>=',$today)->field('SUM(rainfall) as rainfall,number')->find();
$data['all'] = Db::name('reservoir_rain_rainfall')->where('reservoir_id',$id)->field('SUM(rainfall) as rainfall')->find();
$data['equipment'] = Db::name('reservoir_equipment')->where('deviceId',$data['today']['number'])->field('apiKey,name,status')->find();
$this->success('',$data);
}
//获取降水量设备数量 $id 县市id
public function getEquipmentCount($id){
$reservoirids = Db::name('reservoir_list')->where('county_id',$id)->field('id')->select();
$ids = [];
foreach ($reservoirids as $k => $v){
$ids[] = $v['id'];
}
$res['total'] = Db::name('reservoir_equipment')->where('reservoir_id','in',$ids)->count();
$res['abnormal'] = Db::name('reservoir_equipment')->where('reservoir_id','in',$ids)->where('status','2')->count();
$this->success('',$res);
}
//县市降水量统计 $id 县市id
public function getRainByCounty($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_rain_rainfall')->where('createtime','>=',${'predaystart'.$i})->where('createtime','<',${'predayend'.$i})->where('reservoir_id','in',$ids)->value('SUM(rainfall)');
$res['date'][] = date('Y-m-d',strtotime("-$i day"));
$res['count'][] = ${'predaycount'.$i};
}
$today = strtotime(date("Y-m-d",time()));
$todaylist = Db::name('reservoir_rain_rainfall')->where('createtime','>=',$today)->where('reservoir_id','in',$ids)->value('SUM(rainfall)');;
$res['date'][] = date("Y-m-d",time());
$res['count'][] = $todaylist;
$this->success('',$res);
}
//监管app雨情 $id 水库id
public function getAppRainByCounty($id){
$data['list'] = Db::name('reservoir_rain_rainfall')->where('reservoir_id',$id)->order('reporttime','desc')->limit(5)->field('rainfall,status,FROM_UNIXTIME(reporttime/1000) as time,reporttime,total_rainfall')->select();
$type = Db::name('reservoir_warning_rainfall')->where('reporttime','>=',$data['list'][count($data['list'])-1]['reporttime'])->find();
if($type){
$data['warning'] = '1';
}
else{
$data['warning'] = '0';
}
$this->success('',$data);
}
}