123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603 |
- <?php
- namespace app\api\controller\energy;
- use app\hander\HelpHander;
- use think\Controller;
- use think\Db;
- class Warning extends Base
- {
- public function warningCount(){
- $type = input('type','');
- if($type == 1){
- $map[] = ['eae.create_time','>=',date('Y-m-d').' 00:00:00'];
- $map[] = ['eae.create_time','<=',date('Y-m-d').' 23:59:59'];
- }elseif ($type == 2){
- $map[] = ['eae.create_time','>=',date('Y-m').'-01 00:00:00'];
- $map[] = ['eae.create_time','<=',date('Y-m').'-31 23:59:59'];
- }elseif($type == 3){
- $map[] = ['eae.create_time','>=',date('Y').'-01-01 00:00:00'];
- $map[] = ['eae.create_time','<=',date('Y').'-12-31 23:59:59'];
- }
- $map[] = ['ea.org_id','=',$this->orgId];
- $list = [
- ['name'=>'严重警告','type'=>'高限报警'],
- ['name'=>'一般警告','type'=>'低限报警'],
- ];
- $total = 0;
- foreach ($list as $k=>$v){
- $value = Db::name('energy_alarm_event')
- ->alias('eae')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where('ea.alarm_type',$v['type'])
- ->where($map)
- ->count();
- $total +=$value;
- $list[$k]['value'] = $value;
- }
- $data = [
- 'total'=>$total,
- 'list'=>$list
- ];
- HelpHander::success($data);
- }
- public function typeList(){
- $type = input('type','');
- if($type == 1){
- $map[] = ['eae.create_time','>=',date('Y-m-d').' 00:00:00'];
- $map[] = ['eae.create_time','<=',date('Y-m-d').' 23:59:59'];
- }elseif ($type == 2){
- $map[] = ['eae.create_time','>=',date('Y-m').'-01 00:00:00'];
- $map[] = ['eae.create_time','<=',date('Y-m').'-31 23:59:59'];
- }elseif($type == 3){
- $map[] = ['eae.create_time','>=',date('Y').'-01-01 00:00:00'];
- $map[] = ['eae.create_time','<=',date('Y').'-12-31 23:59:59'];
- }
- $map[] = ['ea.org_id','=',$this->orgId];
- $list = [
- ['name'=>'严重警告','type'=>'高限报警'],
- ['name'=>'一般警告','type'=>'低限报警'],
- ];
- $x = $y = [];
- foreach ($list as $k=>$v){
- $value = Db::name('energy_alarm_event')
- ->alias('eae')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where('ea.alarm_type',$v['type'])
- ->where($map)
- ->count();
- $x[] = $v['name'];
- $y[] = $value;
- }
- HelpHander::success(['x'=>$x,'y'=>$y]);
- }
- public function timeTrend(){
- $x = $y = $y1 = [];
- $list = [];
- for ($i=1;$i<=24;$i++){
- $x[] = $i;
- $t= $i-1;
- if($i < 10){
- $i = '0'.$i;
- }
- if($t < 10){
- $t = '0'.$i;
- }
- $list[$i-1]['st'] = date('Y-m-d').' '.$t.'00:00';
- $list[$i-1]['et'] = date('Y-m-d').' '.$i.'00:00';
- }
- foreach ($list as $k=>$v){
- $jin = Db::name('energy_alarm_event')
- ->alias('eae')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where('ea.org_id',$this->orgId)
- ->where('eae.create_time','>=',$v['st'])
- ->where('eae.create_time','<=',$v['et'])
- ->count();
- $y[] = $jin;
- $zuo = Db::name('energy_alarm_event')
- ->alias('eae')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where('ea.org_id',$this->orgId)
- ->where('eae.create_time','>=',date('Y-m-d H:i:s',strtotime($v['st'])-86400))
- ->where('eae.create_time','<=',date('Y-m-d H:i:s',strtotime($v['et'])-86400))
- ->count();
- $y1[] = $zuo;
- }
- $data =[
- 'x'=>$x,
- 'y'=>$y,
- 'y1'=>$y1
- ];
- HelpHander::success($data);
- }
- public function deviceRanking(){
- $list = Db::name('energy_device')
- ->field('id,title,sn')
- ->where('org_id',$this->orgId)
- ->where('del',0)
- ->where('cate',5)
- ->limit(6)
- ->select();
- $total = 0;
- foreach ($list as $k=>$v){
- $value = Db::name('energy_alarm_event')
- ->alias('eae')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where('ea.device_sn',$v['sn'])
- ->where('org_id',$this->orgId)
- ->count();
- $total +=$value;
- $list[$k]['value'] = $value;
- }
- if($list){
- foreach ($list as $k=>$v){
- $list[$k]['bl'] = 0;
- if($total > 0){
- $list[$k]['bl'] = round($v['value']/$total*100,0);
- }
- }
- }
- $newList = list_sort_by($list,'value','desc');
- $data = [
- 'top1'=>isset($newList[0])&&!empty($newList[0]) ? $newList[0]:'',
- 'top2'=>isset($newList[1])&&!empty($newList[1]) ? $newList[1]:'',
- 'top3'=>isset($newList[2])&&!empty($newList[2]) ? $newList[2]:'',
- 'top4'=>isset($newList[3])&&!empty($newList[3]) ? $newList[3]:'',
- 'top5'=>isset($newList[4])&&!empty($newList[4]) ? $newList[4]:'',
- 'top6'=>isset($newList[5])&&!empty($newList[5]) ? $newList[5]:'',
- ];
- HelpHander::success($data);
- }
- public function roomList(){
- $list = Db::name('energy_room')
- ->where('org_id',$this->orgId)
- ->where('del',0)
- ->limit(5)
- ->select();
- $x = $y = [];
- foreach ($list as $k=>$v){
- $y[] = Db::name('energy_alarm_event')
- ->alias('eae')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->join('energy_device ed','ed.sn=ea.device_sn')
- ->where('ed.room_id',$v['id'])
- ->where('ea.org_id',$this->orgId)
- ->where('ed.del',0)
- ->count();
- $x[] = $v['title'];
- }
- HelpHander::success(['x'=>$x,'y'=>$y]);
- }
- //当前告警
- public function warningInfoList(){
- $num = Db::name('energy_alarm_event')
- ->alias('eae')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where('ea.org_id',$this->orgId)
- ->where('ea.alarm_type','高限报警')
- ->where('eae.status',0)
- ->count();
- $num1 = Db::name('energy_alarm_event')
- ->alias('eae')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where('ea.org_id',$this->orgId)
- ->where('ea.alarm_type','低限报警')
- ->where('eae.status',0)
- ->count();
- $count = $num + $num1;
- $list = Db::name('energy_alarm_event')
- ->alias('eae')
- ->field('ea.*,eae.create_time as waring_time')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where('ea.org_id',$this->orgId)
- ->where('ea.alarm_type','in',['低限报警','高限报警'])
- ->where('eae.status','=',0)
- ->select();
- foreach ($list as $k=>$v){
- $device = Db::name('energy_device')
- ->where('sn',$v['device_sn'])
- ->find();
- $room = '';
- if($device){
- $room = Db::name('energy_room')
- ->where('id',$device['room_id'])
- ->value('title');
- }
- $content = $v['point_label'].$v['alarm_type'];
- $list[$k]['room'] = $room;
- $list[$k]['device'] = $device ? $device['title']:'';
- $list[$k]['infos'] = $content;
- }
- $data = [
- 'countinfo'=>[
- 'count'=>$count,
- 'num'=>$num,
- 'num1'=>$num1
- ],
- 'list'=>$list
- ];
- HelpHander::success($data);
- }
- //事件记录
- public function eventRecord(){
- $page = input('page',1);
- $size = input('size',10);
- $date = input('date','');
- $roomId = input('roomId','');
- $cate = input('cate','');
- $type = input('type','');
- $deviceName = input('deviceName','');
- if($date){
- $st = $date[0];
- $et = $date[1];
- $map[] = ['alarm_time','>=',$st.' 00:00:00'];
- $map[] = ['alarm_time','<=',$et.' 23:59:59'];
- }
- $map[] = ['org_id','=',$this->orgId];
- if($roomId !=''){
- $rsns = Db::name('energy_device')->where('room_id',$roomId)->column('sn');
- $map[] = ['device_sn','in',$rsns];
- }
- if($cate !=''){
- $rsns = Db::name('energy_device')->where('cate',$cate)->column('sn');
- $map[] = ['device_sn','in',$rsns];
- }
- if($type !=''){
- $map[] = ['alarm_type','=',$type];
- }
- if($deviceName !=''){
- $rsns = Db::name('energy_device')->where('title','like','%'.$deviceName.'%')->column('sn');
- $map[] = ['device_sn','in',$rsns];
- }
- $lists = Db::name('energy_alarm')
- ->where($map)
- ->order('id desc')
- ->page($page,$size)
- ->select();
- foreach ($lists as $k=>$v){
- $deviceInfo = Db::name('energy_device')
- ->where('sn',$v['device_sn'])
- ->find();
- $cate = $room = '';
- if($deviceInfo){
- $cate = Db::name('energy_device_cate')
- ->where('id',$deviceInfo['cate'])
- ->value('title');
- $room = Db::name('energy_room')
- ->where('id',$v['room_id'])
- ->value('title');
- }
- $content = $v['point_label'].$v['alarm_type'];
- $lists[$k]['room'] = $room;
- $lists[$k]['cate'] = $cate;
- $lists[$k]['device'] = $deviceInfo ?$deviceInfo['title']:'';
- $lists[$k]['content'] = $content;
- }
- $total = Db::name('energy_alarm')
- ->where($map)
- ->count();
- $data = [
- 'total'=>$total,
- 'list'=>$lists
- ];
- HelpHander::success($data);
- }
- public function eventRecordExport(){
- $ids = input('ids','');
- $date = input('date','');
- $roomId = input('roomId','');
- $cate = input('cate','');
- $type = input('type','');
- $deviceName = input('deviceName','');
- if($date){
- $st = $date[0];
- $et = $date[1];
- $map[] = ['alarm_time','>=',$st.' 00:00:00'];
- $map[] = ['alarm_time','<=',$et.' 23:59:59'];
- }
- if($ids){
- $map[] = ['id','in',explode(',',$ids)];
- }
- $map[] = ['org_id','=',$this->orgId];
- if($roomId !=''){
- $rsns = Db::name('energy_device')->where('room_id',$roomId)->column('sn');
- $map[] = ['device_sn','in',$rsns];
- }
- if($cate !=''){
- $rsns = Db::name('energy_device')->where('cate',$cate)->column('sn');
- $map[] = ['device_sn','in',$rsns];
- }
- if($type !=''){
- $map[] = ['alarm_type','=',$type];
- }
- if($deviceName !=''){
- $rsns = Db::name('energy_device')->where('title','like','%'.$deviceName.'%')->column('sn');
- $map[] = ['device_sn','in',$rsns];
- }
- $lists = Db::name('energy_alarm')
- ->where($map)
- ->order('id desc')
- ->select();
- foreach ($lists as $k=>$v){
- $deviceInfo = Db::name('energy_device')
- ->where('sn',$v['device_sn'])
- ->find();
- $cate = $room = '';
- if($deviceInfo){
- $cate = Db::name('energy_device_cate')
- ->where('id',$deviceInfo['cate'])
- ->value('title');
- $room = Db::name('energy_room')
- ->where('id',$v['room_id'])
- ->value('title');
- }
- $content = $v['point_label'].$v['alarm_type'];
- $lists[$k]['room'] = $room;
- $lists[$k]['cate'] = $cate;
- $lists[$k]['device'] = $deviceInfo ?$deviceInfo['title']:'';
- $lists[$k]['content'] = $content;
- }
- $columns = [
- ["title" => "房间","key" => "room"],
- ["title" => "设备名称","key" => "device"],
- ["title" => "设备类型","key" => "cate"],
- ["title" => "事件信息","key" => "content"],
- ["title" => "发生时间","key" => "alarmTime"],
- ];
- $dt = [
- 'columns'=>$columns,
- 'list'=>$lists
- ];
- HelpHander::success($dt);
- }
- public function htoricalAlarms(){
- $page = input('page',1);
- $size = input('size',10);
- $date = input('date','');
- $roomId = input('roomId','');
- $cate = input('cate','');
- $type = input('type','');
- $deviceName = input('deviceName','');
- if($date){
- $st = $date[0];
- $et = $date[1];
- $map[] = ['ea.alarm_time','>=',$st.' 00:00:00'];
- $map[] = ['ea.alarm_time','<=',$et.' 23:59:59'];
- }
- $map[] = ['ea.org_id','=',$this->orgId];
- if($roomId !=''){
- $rsns = Db::name('energy_device')->where('room_id',$roomId)->column('sn');
- $map[] = ['ea.device_sn','in',$rsns];
- }
- if($cate !=''){
- $rsns = Db::name('energy_device')->where('cate',$cate)->column('sn');
- $map[] = ['ea.device_sn','in',$rsns];
- }
- if($type !=''){
- $map[] = ['ea.alarm_type','=',$type];
- }
- if($deviceName !=''){
- $rsns = Db::name('energy_device')->where('title','like','%'.$deviceName.'%')->column('sn');
- $map[] = ['ea.device_sn','in',$rsns];
- }
- $lists = Db::name('energy_alarm_event')
- ->alias('eae')
- ->field('ea.device_sn,ea.point_label,ea.alarm_type,ea.level,ea.alarm_time,eae.*')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where($map)
- ->order('eae.id desc')
- ->page($page,$size)
- ->select();
- foreach ($lists as $k=>$v){
- $deviceInfo = Db::name('energy_device')
- ->where('sn',$v['device_sn'])
- ->find();
- $cate = $room = '';
- if($deviceInfo){
- $cate = Db::name('energy_device_cate')
- ->where('id',$deviceInfo['cate'])
- ->value('title');
- $room = Db::name('energy_room')
- ->where('id',$v['room_id'])
- ->value('title');
- }
- $content = $v['point_label'].$v['alarm_type'];
- $lists[$k]['room'] = $room;
- $lists[$k]['cate'] = $cate;
- $lists[$k]['device'] = $deviceInfo ?$deviceInfo['title']:'';
- $lists[$k]['content'] = $content;
- $lists[$k]['deal_user_name'] = Db::name('user')->where('id',$v['deal_user'])->value('real_name');
- $lists[$k]['is_deal_text'] = $v['is_deal'] == 1 ? '是':'否';
- $lists[$k]['is_finish_text'] = $v['is_finish'] == 1 ? '是':'否';
- $lists[$k]['xytime'] = round($v['xytime']/60,1).'min';
- $lists[$k]['hftime'] = round($v['hftime']/60,1).'min';
- }
- $total = Db::name('energy_alarm_event')
- ->alias('eae')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where($map)
- ->count();
- $data = [
- 'total'=>$total,
- 'list'=>$lists
- ];
- HelpHander::success($data);
- }
- public function htoricalAlarmsExport(){
- $page = input('page',1);
- $size = input('size',10);
- $ids = input('ids','');
- $date = input('date','');
- $roomId = input('roomId','');
- $cate = input('cate','');
- $type = input('type','');
- $deviceName = input('deviceName','');
- if($ids){
- $map[] = ['eae.id','in',explode(',',$ids)];
- }
- if($date){
- $st = $date[0];
- $et = $date[1];
- $map[] = ['ea.alarm_time','>=',$st.' 00:00:00'];
- $map[] = ['ea.alarm_time','<=',$et.' 23:59:59'];
- }
- $map[] = ['ea.org_id','=',$this->orgId];
- if($roomId !=''){
- $rsns = Db::name('energy_device')->where('room_id',$roomId)->column('sn');
- $map[] = ['ea.device_sn','in',$rsns];
- }
- if($cate !=''){
- $rsns = Db::name('energy_device')->where('cate',$cate)->column('sn');
- $map[] = ['ea.device_sn','in',$rsns];
- }
- if($type !=''){
- $map[] = ['ea.alarm_type','=',$type];
- }
- if($deviceName !=''){
- $rsns = Db::name('energy_device')->where('title','like','%'.$deviceName.'%')->column('sn');
- $map[] = ['ea.device_sn','in',$rsns];
- }
- $lists = Db::name('energy_alarm_event')
- ->alias('eae')
- ->field('ea.device_sn,ea.point_label,ea.alarm_type,ea.level,ea.alarm_time,eae.*')
- ->join('energy_alarm ea','ea.id=eae.alarm_id')
- ->where($map)
- ->order('eae.id desc')
- ->page($page,$size)
- ->select();
- foreach ($lists as $k=>$v){
- $deviceInfo = Db::name('energy_device')
- ->where('sn',$v['device_sn'])
- ->find();
- $cate = $room = '';
- if($deviceInfo){
- $cate = Db::name('energy_device_cate')
- ->where('id',$deviceInfo['cate'])
- ->value('title');
- $room = Db::name('energy_room')
- ->where('id',$v['room_id'])
- ->value('title');
- }
- $content = $v['point_label'].$v['alarm_type'];
- $lists[$k]['room'] = $room;
- $lists[$k]['cate'] = $cate;
- $lists[$k]['device'] = $deviceInfo ?$deviceInfo['title']:'';
- $lists[$k]['content'] = $content;
- $lists[$k]['deal_user_name'] = Db::name('user')->where('id',$v['deal_user'])->value('real_name');
- $lists[$k]['is_deal_text'] = $v['is_deal'] == 1 ? '是':'否';
- $lists[$k]['is_finish_text'] = $v['is_finish'] == 1 ? '是':'否';
- $lists[$k]['xytime'] = round($v['xytime']/60,1).'min';
- $lists[$k]['hftime'] = round($v['hftime']/60,1).'min';
- }
- $columns = [
- ["title" => "房间","key" => "room"],
- ["title" => "设备名称","key" => "device"],
- ["title" => "设备类型","key" => "cate"],
- ["title" => "告警信息","key" => "pointLabel"],
- ["title" => "告警等级","key" => "alarmType"],
- ["title" => "告警时间","key" => "alarmTime"],
- ["title" => "处理时间","key" => "dealTime"],
- ["title" => "告警解除时间","key" => "finishTime"],
- ["title" => "处理人","key" => "dealUserName"],
- ["title" => "响应时长","key" => "xytime"],
- ["title" => "恢复时长","key" => "hftime"],
- ["title" => "及时处理","key" => "isDealText"],
- ["title" => "及时恢复","key" => "isFinishText"],
- ["title" => "告警原因","key" => "reason"],
- ];
- $dt = [
- 'columns'=>$columns,
- 'list'=>$lists
- ];
- HelpHander::success($dt);
- }
- //房间列表
- public function roomDataList(){
- $list = Db::name('energy_room')
- ->where('del',0)
- ->where('org_id',$this->orgId)
- ->select();
- HelpHander::success($list);
- }
- //设备列表
- public function deviceDataList(){
- $list = Db::name('energy_device')
- ->where('del',0)
- ->where('org_id',$this->orgId)
- ->select();
- HelpHander::success($list);
- }
- //设备类型
- public function deviceCateDataList(){
- $list = Db::name('energy_device_cate')
- ->select();
- HelpHander::success($list);
- }
- }
|