0
0

Warning.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. namespace app\api\controller\energy;
  3. use app\hander\HelpHander;
  4. use think\Controller;
  5. use think\Db;
  6. class Warning extends Base
  7. {
  8. public function warningCount(){
  9. $type = input('type','');
  10. if($type == 1){
  11. $map[] = ['eae.create_time','>=',date('Y-m-d').' 00:00:00'];
  12. $map[] = ['eae.create_time','<=',date('Y-m-d').' 23:59:59'];
  13. }elseif ($type == 2){
  14. $map[] = ['eae.create_time','>=',date('Y-m').'-01 00:00:00'];
  15. $map[] = ['eae.create_time','<=',date('Y-m').'-31 23:59:59'];
  16. }elseif($type == 3){
  17. $map[] = ['eae.create_time','>=',date('Y').'-01-01 00:00:00'];
  18. $map[] = ['eae.create_time','<=',date('Y').'-12-31 23:59:59'];
  19. }
  20. $map[] = ['ea.org_id','=',$this->orgId];
  21. $list = [
  22. ['name'=>'严重警告','type'=>'高限报警'],
  23. ['name'=>'一般警告','type'=>'低限报警'],
  24. ];
  25. $total = 0;
  26. foreach ($list as $k=>$v){
  27. $value = Db::name('energy_alarm_event')
  28. ->alias('eae')
  29. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  30. ->where('ea.alarm_type',$v['type'])
  31. ->where($map)
  32. ->count();
  33. $total +=$value;
  34. $list[$k]['value'] = $value;
  35. }
  36. $data = [
  37. 'total'=>$total,
  38. 'list'=>$list
  39. ];
  40. HelpHander::success($data);
  41. }
  42. public function typeList(){
  43. $type = input('type','');
  44. if($type == 1){
  45. $map[] = ['eae.create_time','>=',date('Y-m-d').' 00:00:00'];
  46. $map[] = ['eae.create_time','<=',date('Y-m-d').' 23:59:59'];
  47. }elseif ($type == 2){
  48. $map[] = ['eae.create_time','>=',date('Y-m').'-01 00:00:00'];
  49. $map[] = ['eae.create_time','<=',date('Y-m').'-31 23:59:59'];
  50. }elseif($type == 3){
  51. $map[] = ['eae.create_time','>=',date('Y').'-01-01 00:00:00'];
  52. $map[] = ['eae.create_time','<=',date('Y').'-12-31 23:59:59'];
  53. }
  54. $map[] = ['ea.org_id','=',$this->orgId];
  55. $list = [
  56. ['name'=>'严重警告','type'=>'高限报警'],
  57. ['name'=>'一般警告','type'=>'低限报警'],
  58. ];
  59. $x = $y = [];
  60. foreach ($list as $k=>$v){
  61. $value = Db::name('energy_alarm_event')
  62. ->alias('eae')
  63. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  64. ->where('ea.alarm_type',$v['type'])
  65. ->where($map)
  66. ->count();
  67. $x[] = $v['name'];
  68. $y[] = $value;
  69. }
  70. HelpHander::success(['x'=>$x,'y'=>$y]);
  71. }
  72. public function timeTrend(){
  73. $x = $y = $y1 = [];
  74. $list = [];
  75. for ($i=1;$i<=24;$i++){
  76. $x[] = $i;
  77. $t= $i-1;
  78. if($i < 10){
  79. $i = '0'.$i;
  80. }
  81. if($t < 10){
  82. $t = '0'.$i;
  83. }
  84. $list[$i-1]['st'] = date('Y-m-d').' '.$t.'00:00';
  85. $list[$i-1]['et'] = date('Y-m-d').' '.$i.'00:00';
  86. }
  87. foreach ($list as $k=>$v){
  88. $jin = Db::name('energy_alarm_event')
  89. ->alias('eae')
  90. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  91. ->where('ea.org_id',$this->orgId)
  92. ->where('eae.create_time','>=',$v['st'])
  93. ->where('eae.create_time','<=',$v['et'])
  94. ->count();
  95. $y[] = $jin;
  96. $zuo = Db::name('energy_alarm_event')
  97. ->alias('eae')
  98. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  99. ->where('ea.org_id',$this->orgId)
  100. ->where('eae.create_time','>=',date('Y-m-d H:i:s',strtotime($v['st'])-86400))
  101. ->where('eae.create_time','<=',date('Y-m-d H:i:s',strtotime($v['et'])-86400))
  102. ->count();
  103. $y1[] = $zuo;
  104. }
  105. $data =[
  106. 'x'=>$x,
  107. 'y'=>$y,
  108. 'y1'=>$y1
  109. ];
  110. HelpHander::success($data);
  111. }
  112. public function deviceRanking(){
  113. $list = Db::name('energy_device')
  114. ->field('id,title,sn')
  115. ->where('org_id',$this->orgId)
  116. ->where('del',0)
  117. ->where('cate',5)
  118. ->limit(6)
  119. ->select();
  120. $total = 0;
  121. foreach ($list as $k=>$v){
  122. $value = Db::name('energy_alarm_event')
  123. ->alias('eae')
  124. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  125. ->where('ea.device_sn',$v['sn'])
  126. ->where('org_id',$this->orgId)
  127. ->count();
  128. $total +=$value;
  129. $list[$k]['value'] = $value;
  130. }
  131. if($list){
  132. foreach ($list as $k=>$v){
  133. $list[$k]['bl'] = 0;
  134. if($total > 0){
  135. $list[$k]['bl'] = round($v['value']/$total*100,0);
  136. }
  137. }
  138. }
  139. $newList = list_sort_by($list,'value','desc');
  140. $data = [
  141. 'top1'=>isset($newList[0])&&!empty($newList[0]) ? $newList[0]:'',
  142. 'top2'=>isset($newList[1])&&!empty($newList[1]) ? $newList[1]:'',
  143. 'top3'=>isset($newList[2])&&!empty($newList[2]) ? $newList[2]:'',
  144. 'top4'=>isset($newList[3])&&!empty($newList[3]) ? $newList[3]:'',
  145. 'top5'=>isset($newList[4])&&!empty($newList[4]) ? $newList[4]:'',
  146. 'top6'=>isset($newList[5])&&!empty($newList[5]) ? $newList[5]:'',
  147. ];
  148. HelpHander::success($data);
  149. }
  150. public function roomList(){
  151. $list = Db::name('energy_room')
  152. ->where('org_id',$this->orgId)
  153. ->where('del',0)
  154. ->limit(5)
  155. ->select();
  156. $x = $y = [];
  157. foreach ($list as $k=>$v){
  158. $y[] = Db::name('energy_alarm_event')
  159. ->alias('eae')
  160. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  161. ->join('energy_device ed','ed.sn=ea.device_sn')
  162. ->where('ed.room_id',$v['id'])
  163. ->where('ea.org_id',$this->orgId)
  164. ->where('ed.del',0)
  165. ->count();
  166. $x[] = $v['title'];
  167. }
  168. HelpHander::success(['x'=>$x,'y'=>$y]);
  169. }
  170. //当前告警
  171. public function warningInfoList(){
  172. $num = Db::name('energy_alarm_event')
  173. ->alias('eae')
  174. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  175. ->where('ea.org_id',$this->orgId)
  176. ->where('ea.alarm_type','高限报警')
  177. ->where('eae.status',0)
  178. ->count();
  179. $num1 = Db::name('energy_alarm_event')
  180. ->alias('eae')
  181. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  182. ->where('ea.org_id',$this->orgId)
  183. ->where('ea.alarm_type','低限报警')
  184. ->where('eae.status',0)
  185. ->count();
  186. $count = $num + $num1;
  187. $list = Db::name('energy_alarm_event')
  188. ->alias('eae')
  189. ->field('ea.*,eae.create_time as waring_time')
  190. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  191. ->where('ea.org_id',$this->orgId)
  192. ->where('ea.alarm_type','in',['低限报警','高限报警'])
  193. ->where('eae.status','=',0)
  194. ->select();
  195. foreach ($list as $k=>$v){
  196. $device = Db::name('energy_device')
  197. ->where('sn',$v['device_sn'])
  198. ->find();
  199. $room = '';
  200. if($device){
  201. $room = Db::name('energy_room')
  202. ->where('id',$device['room_id'])
  203. ->value('title');
  204. }
  205. $content = $v['point_label'].$v['alarm_type'];
  206. $list[$k]['room'] = $room;
  207. $list[$k]['device'] = $device ? $device['title']:'';
  208. $list[$k]['infos'] = $content;
  209. }
  210. $data = [
  211. 'countinfo'=>[
  212. 'count'=>$count,
  213. 'num'=>$num,
  214. 'num1'=>$num1
  215. ],
  216. 'list'=>$list
  217. ];
  218. HelpHander::success($data);
  219. }
  220. //事件记录
  221. public function eventRecord(){
  222. $page = input('page',1);
  223. $size = input('size',10);
  224. $date = input('date','');
  225. $roomId = input('roomId','');
  226. $cate = input('cate','');
  227. $type = input('type','');
  228. $deviceName = input('deviceName','');
  229. if($date){
  230. $st = $date[0];
  231. $et = $date[1];
  232. $map[] = ['alarm_time','>=',$st.' 00:00:00'];
  233. $map[] = ['alarm_time','<=',$et.' 23:59:59'];
  234. }
  235. $map[] = ['org_id','=',$this->orgId];
  236. if($roomId !=''){
  237. $rsns = Db::name('energy_device')->where('room_id',$roomId)->column('sn');
  238. $map[] = ['device_sn','in',$rsns];
  239. }
  240. if($cate !=''){
  241. $rsns = Db::name('energy_device')->where('cate',$cate)->column('sn');
  242. $map[] = ['device_sn','in',$rsns];
  243. }
  244. if($type !=''){
  245. $map[] = ['alarm_type','=',$type];
  246. }
  247. if($deviceName !=''){
  248. $rsns = Db::name('energy_device')->where('title','like','%'.$deviceName.'%')->column('sn');
  249. $map[] = ['device_sn','in',$rsns];
  250. }
  251. $lists = Db::name('energy_alarm')
  252. ->where($map)
  253. ->order('id desc')
  254. ->page($page,$size)
  255. ->select();
  256. foreach ($lists as $k=>$v){
  257. $deviceInfo = Db::name('energy_device')
  258. ->where('sn',$v['device_sn'])
  259. ->find();
  260. $cate = $room = '';
  261. if($deviceInfo){
  262. $cate = Db::name('energy_device_cate')
  263. ->where('id',$deviceInfo['cate'])
  264. ->value('title');
  265. $room = Db::name('energy_room')
  266. ->where('id',$v['room_id'])
  267. ->value('title');
  268. }
  269. $content = $v['point_label'].$v['alarm_type'];
  270. $lists[$k]['room'] = $room;
  271. $lists[$k]['cate'] = $cate;
  272. $lists[$k]['device'] = $deviceInfo ?$deviceInfo['title']:'';
  273. $lists[$k]['content'] = $content;
  274. }
  275. $total = Db::name('energy_alarm')
  276. ->where($map)
  277. ->count();
  278. $data = [
  279. 'total'=>$total,
  280. 'list'=>$lists
  281. ];
  282. HelpHander::success($data);
  283. }
  284. public function eventRecordExport(){
  285. $ids = input('ids','');
  286. $date = input('date','');
  287. $roomId = input('roomId','');
  288. $cate = input('cate','');
  289. $type = input('type','');
  290. $deviceName = input('deviceName','');
  291. if($date){
  292. $st = $date[0];
  293. $et = $date[1];
  294. $map[] = ['alarm_time','>=',$st.' 00:00:00'];
  295. $map[] = ['alarm_time','<=',$et.' 23:59:59'];
  296. }
  297. if($ids){
  298. $map[] = ['id','in',explode(',',$ids)];
  299. }
  300. $map[] = ['org_id','=',$this->orgId];
  301. if($roomId !=''){
  302. $rsns = Db::name('energy_device')->where('room_id',$roomId)->column('sn');
  303. $map[] = ['device_sn','in',$rsns];
  304. }
  305. if($cate !=''){
  306. $rsns = Db::name('energy_device')->where('cate',$cate)->column('sn');
  307. $map[] = ['device_sn','in',$rsns];
  308. }
  309. if($type !=''){
  310. $map[] = ['alarm_type','=',$type];
  311. }
  312. if($deviceName !=''){
  313. $rsns = Db::name('energy_device')->where('title','like','%'.$deviceName.'%')->column('sn');
  314. $map[] = ['device_sn','in',$rsns];
  315. }
  316. $lists = Db::name('energy_alarm')
  317. ->where($map)
  318. ->order('id desc')
  319. ->select();
  320. foreach ($lists as $k=>$v){
  321. $deviceInfo = Db::name('energy_device')
  322. ->where('sn',$v['device_sn'])
  323. ->find();
  324. $cate = $room = '';
  325. if($deviceInfo){
  326. $cate = Db::name('energy_device_cate')
  327. ->where('id',$deviceInfo['cate'])
  328. ->value('title');
  329. $room = Db::name('energy_room')
  330. ->where('id',$v['room_id'])
  331. ->value('title');
  332. }
  333. $content = $v['point_label'].$v['alarm_type'];
  334. $lists[$k]['room'] = $room;
  335. $lists[$k]['cate'] = $cate;
  336. $lists[$k]['device'] = $deviceInfo ?$deviceInfo['title']:'';
  337. $lists[$k]['content'] = $content;
  338. }
  339. $columns = [
  340. ["title" => "房间","key" => "room"],
  341. ["title" => "设备名称","key" => "device"],
  342. ["title" => "设备类型","key" => "cate"],
  343. ["title" => "事件信息","key" => "content"],
  344. ["title" => "发生时间","key" => "alarmTime"],
  345. ];
  346. $dt = [
  347. 'columns'=>$columns,
  348. 'list'=>$lists
  349. ];
  350. HelpHander::success($dt);
  351. }
  352. public function htoricalAlarms(){
  353. $page = input('page',1);
  354. $size = input('size',10);
  355. $date = input('date','');
  356. $roomId = input('roomId','');
  357. $cate = input('cate','');
  358. $type = input('type','');
  359. $deviceName = input('deviceName','');
  360. if($date){
  361. $st = $date[0];
  362. $et = $date[1];
  363. $map[] = ['ea.alarm_time','>=',$st.' 00:00:00'];
  364. $map[] = ['ea.alarm_time','<=',$et.' 23:59:59'];
  365. }
  366. $map[] = ['ea.org_id','=',$this->orgId];
  367. if($roomId !=''){
  368. $rsns = Db::name('energy_device')->where('room_id',$roomId)->column('sn');
  369. $map[] = ['ea.device_sn','in',$rsns];
  370. }
  371. if($cate !=''){
  372. $rsns = Db::name('energy_device')->where('cate',$cate)->column('sn');
  373. $map[] = ['ea.device_sn','in',$rsns];
  374. }
  375. if($type !=''){
  376. $map[] = ['ea.alarm_type','=',$type];
  377. }
  378. if($deviceName !=''){
  379. $rsns = Db::name('energy_device')->where('title','like','%'.$deviceName.'%')->column('sn');
  380. $map[] = ['ea.device_sn','in',$rsns];
  381. }
  382. $lists = Db::name('energy_alarm_event')
  383. ->alias('eae')
  384. ->field('ea.device_sn,ea.point_label,ea.alarm_type,ea.level,ea.alarm_time,eae.*')
  385. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  386. ->where($map)
  387. ->order('eae.id desc')
  388. ->page($page,$size)
  389. ->select();
  390. foreach ($lists as $k=>$v){
  391. $deviceInfo = Db::name('energy_device')
  392. ->where('sn',$v['device_sn'])
  393. ->find();
  394. $cate = $room = '';
  395. if($deviceInfo){
  396. $cate = Db::name('energy_device_cate')
  397. ->where('id',$deviceInfo['cate'])
  398. ->value('title');
  399. $room = Db::name('energy_room')
  400. ->where('id',$v['room_id'])
  401. ->value('title');
  402. }
  403. $content = $v['point_label'].$v['alarm_type'];
  404. $lists[$k]['room'] = $room;
  405. $lists[$k]['cate'] = $cate;
  406. $lists[$k]['device'] = $deviceInfo ?$deviceInfo['title']:'';
  407. $lists[$k]['content'] = $content;
  408. $lists[$k]['deal_user_name'] = Db::name('user')->where('id',$v['deal_user'])->value('real_name');
  409. $lists[$k]['is_deal_text'] = $v['is_deal'] == 1 ? '是':'否';
  410. $lists[$k]['is_finish_text'] = $v['is_finish'] == 1 ? '是':'否';
  411. $lists[$k]['xytime'] = round($v['xytime']/60,1).'min';
  412. $lists[$k]['hftime'] = round($v['hftime']/60,1).'min';
  413. }
  414. $total = Db::name('energy_alarm_event')
  415. ->alias('eae')
  416. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  417. ->where($map)
  418. ->count();
  419. $data = [
  420. 'total'=>$total,
  421. 'list'=>$lists
  422. ];
  423. HelpHander::success($data);
  424. }
  425. public function htoricalAlarmsExport(){
  426. $page = input('page',1);
  427. $size = input('size',10);
  428. $ids = input('ids','');
  429. $date = input('date','');
  430. $roomId = input('roomId','');
  431. $cate = input('cate','');
  432. $type = input('type','');
  433. $deviceName = input('deviceName','');
  434. if($ids){
  435. $map[] = ['eae.id','in',explode(',',$ids)];
  436. }
  437. if($date){
  438. $st = $date[0];
  439. $et = $date[1];
  440. $map[] = ['ea.alarm_time','>=',$st.' 00:00:00'];
  441. $map[] = ['ea.alarm_time','<=',$et.' 23:59:59'];
  442. }
  443. $map[] = ['ea.org_id','=',$this->orgId];
  444. if($roomId !=''){
  445. $rsns = Db::name('energy_device')->where('room_id',$roomId)->column('sn');
  446. $map[] = ['ea.device_sn','in',$rsns];
  447. }
  448. if($cate !=''){
  449. $rsns = Db::name('energy_device')->where('cate',$cate)->column('sn');
  450. $map[] = ['ea.device_sn','in',$rsns];
  451. }
  452. if($type !=''){
  453. $map[] = ['ea.alarm_type','=',$type];
  454. }
  455. if($deviceName !=''){
  456. $rsns = Db::name('energy_device')->where('title','like','%'.$deviceName.'%')->column('sn');
  457. $map[] = ['ea.device_sn','in',$rsns];
  458. }
  459. $lists = Db::name('energy_alarm_event')
  460. ->alias('eae')
  461. ->field('ea.device_sn,ea.point_label,ea.alarm_type,ea.level,ea.alarm_time,eae.*')
  462. ->join('energy_alarm ea','ea.id=eae.alarm_id')
  463. ->where($map)
  464. ->order('eae.id desc')
  465. ->page($page,$size)
  466. ->select();
  467. foreach ($lists as $k=>$v){
  468. $deviceInfo = Db::name('energy_device')
  469. ->where('sn',$v['device_sn'])
  470. ->find();
  471. $cate = $room = '';
  472. if($deviceInfo){
  473. $cate = Db::name('energy_device_cate')
  474. ->where('id',$deviceInfo['cate'])
  475. ->value('title');
  476. $room = Db::name('energy_room')
  477. ->where('id',$v['room_id'])
  478. ->value('title');
  479. }
  480. $content = $v['point_label'].$v['alarm_type'];
  481. $lists[$k]['room'] = $room;
  482. $lists[$k]['cate'] = $cate;
  483. $lists[$k]['device'] = $deviceInfo ?$deviceInfo['title']:'';
  484. $lists[$k]['content'] = $content;
  485. $lists[$k]['deal_user_name'] = Db::name('user')->where('id',$v['deal_user'])->value('real_name');
  486. $lists[$k]['is_deal_text'] = $v['is_deal'] == 1 ? '是':'否';
  487. $lists[$k]['is_finish_text'] = $v['is_finish'] == 1 ? '是':'否';
  488. $lists[$k]['xytime'] = round($v['xytime']/60,1).'min';
  489. $lists[$k]['hftime'] = round($v['hftime']/60,1).'min';
  490. }
  491. $columns = [
  492. ["title" => "房间","key" => "room"],
  493. ["title" => "设备名称","key" => "device"],
  494. ["title" => "设备类型","key" => "cate"],
  495. ["title" => "告警信息","key" => "pointLabel"],
  496. ["title" => "告警等级","key" => "alarmType"],
  497. ["title" => "告警时间","key" => "alarmTime"],
  498. ["title" => "处理时间","key" => "dealTime"],
  499. ["title" => "告警解除时间","key" => "finishTime"],
  500. ["title" => "处理人","key" => "dealUserName"],
  501. ["title" => "响应时长","key" => "xytime"],
  502. ["title" => "恢复时长","key" => "hftime"],
  503. ["title" => "及时处理","key" => "isDealText"],
  504. ["title" => "及时恢复","key" => "isFinishText"],
  505. ["title" => "告警原因","key" => "reason"],
  506. ];
  507. $dt = [
  508. 'columns'=>$columns,
  509. 'list'=>$lists
  510. ];
  511. HelpHander::success($dt);
  512. }
  513. //房间列表
  514. public function roomDataList(){
  515. $list = Db::name('energy_room')
  516. ->where('del',0)
  517. ->where('org_id',$this->orgId)
  518. ->select();
  519. HelpHander::success($list);
  520. }
  521. //设备列表
  522. public function deviceDataList(){
  523. $list = Db::name('energy_device')
  524. ->where('del',0)
  525. ->where('org_id',$this->orgId)
  526. ->select();
  527. HelpHander::success($list);
  528. }
  529. //设备类型
  530. public function deviceCateDataList(){
  531. $list = Db::name('energy_device_cate')
  532. ->select();
  533. HelpHander::success($list);
  534. }
  535. }