Alarm.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. include_once(FCPATH . 'application/controllers/Base_Controller.php');
  3. class Alarm extends Base_Controller {
  4. public function __construct() {
  5. parent::__construct();
  6. $this->load->model('Alarm_model');
  7. $this->load->model('Project_model');
  8. $this->load->model('Network_model');
  9. $this->load->model('Lamp_model');
  10. $this->load->model('Patrol_model');
  11. $this->load->model('Repair_model');
  12. }
  13. // 获取告警信息列表
  14. public function get(){
  15. $filter = array();
  16. $where = array('1=1');
  17. $role = $this->get_user_info('role');
  18. $user_id = $this->get_user_info('id');
  19. $lampid = $this->input->post('lampid',true);
  20. $page = intval($this->input->post('page',true));
  21. $count = intval($this->input->post('count',true));
  22. $print = $this->input->post('print',true);
  23. // 告警状态筛选
  24. $status = $this->input->post('status',true);
  25. // 关键字搜索
  26. $keyword = $this->input->post('keyword',true);
  27. $projectid = $this->input->post('projectid',true);
  28. // 是否导出
  29. $download = $this->input->post('download',true);
  30. // 告警类型筛选
  31. $alarmtype = trim($this->input->post('alarmtype',true));
  32. // var_dump($alarmtype);
  33. // 时间筛选
  34. $endDate = $this->input->post('endDate',true);
  35. $startDate = $this->input->post('startDate',true);
  36. $type = intval($this->input->post('type',true));
  37. $version = $this->version;
  38. // var_dump($version);
  39. // if (!empty($version)) {
  40. // $alarmtype = alarm_translate($alarmtype);
  41. // }
  42. $page = empty($page) ? 1 : $page;
  43. $count = empty($count) ? 20 : $count;
  44. $limit = $count;
  45. $offset = ($page - 1)*$count;
  46. $section = $this->input->post('section',true);
  47. if (!empty($section)) $where[] = 'L.section = "'.$section.'"';
  48. if (!empty($lampid)) $where[] = 'L.id = '.$lampid;
  49. if (!empty($keyword)) $where[] = 'L.number like "%'.$keyword.'%"';
  50. if (!empty($projectid)) $where[] = 'L.projectid = '.$projectid;
  51. if(!isset($lampid) && isset($status) && $status >= 0) $where[] = 'AI.status = '.$status;
  52. if ($type == 0) {
  53. $status = empty($filter['status']) ? 0 : $filter['status'];
  54. $where[] = 'AI.status = '.$status;
  55. }
  56. // if(!empty($alarmtype)) $filter['alarmtype'] = $alarmtype;
  57. if(!empty($alarmtype) && is_numeric($alarmtype)) {
  58. $t_ty = substr($alarmtype, 0,1);
  59. $t_ty2 = substr($alarmtype, 1,1);
  60. if ($t_ty == 1) {
  61. $t_1 = 'batstatus';
  62. }elseif ($t_ty == 2) {
  63. $t_1 = 'panelstatus';
  64. }elseif ($t_ty == 2) {
  65. $t_1 = 'lampstatus';
  66. }else {
  67. $t_1 = 'tempstatus';
  68. }
  69. $where[] = 'AI.'.$t_1.' = '.$t_ty2;
  70. }
  71. // var_dump($alarmtype);
  72. // if(isset($endDate)) $filter['endDate'] = $endDate;
  73. if(isset($endDate) && !empty($endDate)) $where[] = 'AI.updatetime <= "'.$endDate.'"';
  74. if(isset($startDate) && !empty($startDate)) $where[] = 'AI.updatetime >= "'.$startDate.'"';
  75. // if(isset($startDate)) $filter['startDate'] = $startDate;
  76. if (empty($download)) {
  77. if (empty($type)) {
  78. $join = array();
  79. $join[] = ['table'=>'(select lampid,max(updatetime) as maxTime from warning_info_log group by lampid) as WI','cond'=>'WI.lampid = AI.lampid AND WI.maxTime = AI.updatetime','type'=>'inner'];
  80. $join[] = ['table'=>'lampinfo as L','cond'=>'L.id = AI.lampid','type'=>'inner'];
  81. $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'inner'];
  82. $join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'inner'];
  83. $where[] = '(AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';
  84. $where = implode(' AND ', $where);
  85. $alarm = $this->Alarm_model->get_list_by_join($where, 'AI.batstatus,AI.panelstatus,AI.lampstatus,AI.tempstatus,AI.status,AI.updatetime,G.timezone,L.number,P.projectname as project,AI.id',$limit, $offset, $join, 'AI.updatetime DESC,id DESC', NUll, 'AI');
  86. $total = $this->Alarm_model->get_list_by_multi_join($where, 'count(*) as total',null, null, $join, null, NUll, 'AI',true);
  87. $total = $total['total'];
  88. // $alarm = $this->Alarm_model->getList($filter, $role, $user_id, $page, $count,1);
  89. }else{
  90. $join = array();
  91. // $join[] = ['table'=>'(select lampid,max(updatetime) as maxTime from warning_info_log group by lampid) as WI','cond'=>'WI.lampid = AI.lampid AND WI.maxTime = AI.updatetime','type'=>'inner'];
  92. $join[] = ['table'=>'lampinfo as L','cond'=>'L.id = AI.lampid','type'=>'inner'];
  93. $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'inner'];
  94. $join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'inner'];
  95. $where[] = '(AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';
  96. $where = implode(' AND ', $where);
  97. $alarm = $this->Alarm_model->get_list_by_join($where, 'AI.batstatus,AI.panelstatus,AI.lampstatus,AI.tempstatus,AI.status,AI.updatetime,G.timezone,L.number,P.projectname as project,AI.id',$limit, $offset, $join, 'AI.updatetime DESC,id DESC', NUll, 'AI');
  98. $total = $this->Alarm_model->get_list_by_multi_join($where, 'count(*) as total',null, null, $join, null, NUll, 'AI',true);
  99. $total = $total['total'];
  100. }
  101. $alarm = empty($alarm) ? array() : $alarm;
  102. $batstatus = $this->config->item('batstatus');
  103. $panelstatus = $this->config->item('panelstatus');
  104. $lampstatus = $this->config->item('lampstatus');
  105. $tempstatus = $this->config->item('tempstatus');
  106. foreach($alarm as &$value){
  107. $temp2 = array();
  108. if (isset($batstatus[$value['batstatus']])) $temp2[] = $batstatus[$value['batstatus']];
  109. if (isset($panelstatus[$value['panelstatus']])) $temp2[] = $panelstatus[$value['panelstatus']];
  110. if (isset($lampstatus[$value['lampstatus']])) $temp2[] = $lampstatus[$value['lampstatus']];
  111. if (isset($tempstatus[$value['tempstatus']])) $temp2[] = $tempstatus[$value['tempstatus']];
  112. $value['stralarmtype'] = implode(',', $temp2);
  113. $value['updatetime'] = set_timezone($value['updatetime'],$value['timezone']);
  114. }
  115. $data = array('history_list'=>$alarm, 'total'=>ceil($total/$count));
  116. if (empty($type)) {
  117. $filter['status'] = 0;
  118. $filter['fault'] = 0;
  119. // $data['fault'] = $this->Alarm_model->getDataCount($filter, $role, $user_id);
  120. }
  121. exit(json_result('0000', $this->response['0000'], array('list'=>$data)));
  122. }else{
  123. $alarm = $this->Alarm_model->getList($filter, $role, $user_id, ($page-1)*$count, 3000);
  124. $alarm = empty($alarm) ? array() : $alarm;
  125. // 导出到Excel
  126. if (empty($version)) {
  127. $title = array(
  128. array('编号','项目名称','报警路灯','报警事件','是否已处理','更新时间'),
  129. );
  130. }else{ // 英文导出
  131. $title = array(
  132. array('Number','Project name','Alarm lamp','Alarm event','Has it been processed','Update time'),
  133. );
  134. }
  135. $temp = array();
  136. $i = ($page-1)*$count + 1;
  137. foreach ($alarm as $k=>$v) {
  138. $temp[$k][] = $i;
  139. $temp[$k][] = $v['project'];
  140. $temp[$k][] = $v['number'];
  141. if (empty($version)) {
  142. $stralarmtype = trim($v['stralarmtype']);
  143. if (!empty($stralarmtype)) {
  144. $alarmArr = explode(',', $stralarmtype);
  145. $temp1 = array();
  146. foreach ($alarmArr as $a) {
  147. if ($a == '电池故障') {
  148. $a = '电池电压异常';
  149. }
  150. $temp1[] = $a;
  151. }
  152. $v['stralarmtype'] = implode(',', $temp1);
  153. }
  154. $temp[$k][] = $v['stralarmtype'];
  155. $temp[$k][] = empty($v['status']) ? '未处理' : '已处理';
  156. }else{
  157. $stralarmtype = trim($v['stralarmtype']);
  158. if (!empty($stralarmtype)) {
  159. $alarmArr = explode(',', $stralarmtype);
  160. $arr = array();
  161. foreach ($alarmArr as $a) {
  162. if ($a == '电池故障') {
  163. $a = '电池电压异常';
  164. }
  165. $arr[] = alarm_translate($a);
  166. }
  167. // var_dump($temp);
  168. $temp[$k][] = empty($arr) ? '' : implode('.', $arr);
  169. }else{
  170. $temp[$k][] = '';
  171. }
  172. $temp[$k][] = empty($v['status']) ? 'Untreated' : 'Already processed';
  173. }
  174. $temp[$k][] = $v['updatetime'];
  175. $i ++;
  176. }
  177. $data = array_merge($title,$temp);
  178. $path = push_excel($data,'alarmExcel_'.date('Ymd'));
  179. exit(json_result('0000',$this->response['0000'],array('path'=>$path)));
  180. }
  181. }
  182. // 查看故障详情
  183. public function detail(){
  184. $id = $this->input->post('id',true);
  185. if (empty($id)) {
  186. exit(json_result('0604', $this->response['0604'], array()));
  187. }
  188. $data = $this->Alarm_model->getOne($id);
  189. exit(json_result('0000', $this->response['0000'], $data));
  190. }
  191. // 更改故障状态
  192. public function set(){
  193. $role = $this->get_user_info('role');
  194. // if ($role == COMPANY_CUSTOMER) {
  195. // exit(json_result('0011', $this->response['0011'], array()));
  196. // }
  197. $ids = $this->input->post("ids",true);
  198. if (empty($ids)) {
  199. exit(json_result('0604', $this->response['0604'], array()));
  200. }
  201. $status = $this->input->post("status",true);
  202. $ids = explode(",", $ids);
  203. $filter = array('id' => $ids);
  204. $data = array('status' => intval($status));
  205. $res = $this->Alarm_model->update($data,$filter);
  206. // if ($status == 1) {
  207. // foreach ($ids as $v) {
  208. // $alarmData = $this->Alarm_model->get_one(['id'=>$v],'lampid');
  209. // if ($status == 0) {
  210. // $this->Lamp_model->update(array('id'=>$alarData['lampid']),array('isfaulted'=>1));
  211. // }else{
  212. // $this->Lamp_model->update(array('id'=>$alarData['lampid']),array('isfaulted'=>0));
  213. // }
  214. // $this->Alarm_model->update(array('lampid'=>$alarmData['lampid'],'status'=>0),array('status'=>1));
  215. // }
  216. // }
  217. if ($res) {
  218. exit(json_result('0000', $this->response['0000'], array()));
  219. } else {
  220. exit(json_result('0009', $this->response['0009'], array()));
  221. }
  222. }
  223. // 删除故障信息
  224. public function del(){
  225. $role = $this->get_user_info('role');
  226. // if ($role == COMPANY_CUSTOMER) {
  227. // exit(json_result('0011', $this->response['0011'], array()));
  228. // }
  229. $ids = $this->input->post("ids",true);
  230. $lamp_ids = $this->input->post('lamp_ids', true);
  231. if (empty($ids) && empty($lamp_ids)) {
  232. exit(json_result('0603', $this->response['0603'], array()));
  233. }
  234. if (!empty($ids)) {
  235. $ids = explode(",", $ids);
  236. $res = $this->Alarm_model->delBatch($ids);
  237. } else {
  238. $lamp_ids = explode(",", $lamp_ids);
  239. $res = $this->Alarm_model->delBatch($lamp_ids, 'lampid');
  240. $this->Lamp_model->markNormal($lamp_ids);
  241. $dataArr = $this->Lamp_model->getBatch($lamp_ids);
  242. foreach ($dataArr as $data) {
  243. $this->Network_model->minus_fault_count($data['networkid']);
  244. $this->Project_model->minus_fault_count($data['projectid']);
  245. }
  246. }
  247. if ($res) {
  248. exit(json_result('0000', $this->response['0000'], array()));
  249. } else {
  250. exit(json_result('0005', $this->response['0005'], array()));
  251. }
  252. }
  253. // 巡检信息列表
  254. public function patrol_list(){
  255. $role = $this->get_user_info('role');
  256. $userid = $this->get_user_info('id');
  257. $company = $this->get_user_info('company');
  258. $projectid = $this->input->post('projectid',true);
  259. $page = $this->input->post('page',true);
  260. $count = $this->input->post('count',true);
  261. $keyword = $this->input->post('keyword',true);
  262. $download = $this->input->post('download',true);
  263. $status = $this->input->post('status',true);
  264. $print = $this->input->post('print',true);
  265. $page = !isset($page) ? 1 : $page;
  266. $count = empty($count) ? 20 : $count;
  267. $limit = $count;
  268. $offset = ($page - 1)*$count;
  269. // 信息字段
  270. $field = "L.id,
  271. P.id as projectid,
  272. P.projectname as project_name,
  273. L.number,
  274. L.patrolinterval";
  275. $where =array();
  276. $section = $this->input->post('section',true);
  277. $projectid = intval($this->input->post('projectid',true));
  278. if (!empty($section)) $where['L.section'] = $section;
  279. if (!empty($projectid)) $where['L.projectid'] = $projectid;
  280. if (!empty($keyword)) $where['L.number|'] = $keyword;
  281. if ($role != SYSTEM_ADMIN) $where['P.company'] = $company;
  282. $join = [];
  283. $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'left'];
  284. if (empty($download)) {
  285. // 获取巡检信息列表
  286. if (isset($print) && !empty($print)) {
  287. // $data = $this->Patrol_model->getList($role,$companyid,$projectid,$userid,$fiter,$field,$page,$count,1);
  288. $data = $this->Lamp_model->get_list_by_join($where, $field,$limit, $offset, $join, 'L.number ASC,L.id DESC', NUll, 'L');
  289. }else{
  290. $data = $this->Lamp_model->get_list_by_join($where, $field,$limit, $offset, $join, 'L.number ASC,L.id DESC', NUll, 'L');
  291. }
  292. // 获取巡检信息总数
  293. $total = $this->Lamp_model->get_list_by_multi_join($where, 'count(*) as total', null, null, $join, 'L.number ASC,L.id DESC', NUll, 'L',true);
  294. $total = $total['total'];
  295. json_result('0000',$this->response['0000'],array('patrol_list'=>$data,'total'=>ceil($total/$count)));
  296. }else{
  297. // 获取巡检信息列表
  298. $data = $this->Lamp_model->get_list_by_join($where, $field,3000, 0, $join, 'L.number ASC,id DESC', NUll, 'L');
  299. // $data = $this->Patrol_model->getList($role,$companyid,$projectid,$userid,$fiter,$field,($page-1)*$count,3000);
  300. $version = $this->session->userdata('version');
  301. if (empty($version)) {
  302. $title = array(
  303. array('编号','项目名称','路灯编号','巡检间隔时间(min)'),
  304. );
  305. }else{
  306. $title = array(
  307. array('Number','Project name','Lamp Number','Inspection interval time(min)'),
  308. );
  309. }
  310. $temp = array();
  311. $i = ($page-1)*$count + 1;
  312. foreach ($data as $k=>$v) {
  313. $temp[$k][] = $i;
  314. $temp[$k][] = $v['project_name'];
  315. $temp[$k][] = $v['number'];
  316. $temp[$k][] = $v['patrolinterval'];
  317. $i ++;
  318. }
  319. $res = array_merge($title,$temp);
  320. $path = push_excel($res,'patrolExcel_'.date('Ymd'));
  321. exit(json_result('0000',$this->response['0000'],array('path'=>$path)));
  322. }
  323. }
  324. // 单个路灯维修信息
  325. public function repair_info(){
  326. $alarmid = $this->input->post('alarmid');
  327. if (empty($alarmid)) {
  328. exit(json_result('0007',$this->response['0007'],array()));
  329. }
  330. $page = $this->input->post('page',true);
  331. $count = $this->input->post('count',true);
  332. $page = empty($page) ? 1 : $page;
  333. $count = empty($count) ? 1 : $count;
  334. $alarmData = $this->Alarm_model->getOne($alarmid,'A.lampid,A.alarmtype');
  335. $field = 'RI.repair_hitch,RI.repair_path,RI.repair_solution,RI.repair_time,RU.name';
  336. $repair_list = $this->Repair_model->get_repair_info_list(array('RI.lampid'=>$alarmData['lampid'],'page'=>$page,'count'=>$count),$field);
  337. $total = $this->Repair_model->get_repair_total(array('RI.lampid'=>$alarmData['lampid']));
  338. if (empty($repair_list)) {
  339. exit(json_result('0616',$this->response['0616'],array()));
  340. }
  341. exit(json_result('0000',$this->response['0000'],array('list'=>$repair_list,'total'=>ceil($total/$count))));
  342. }
  343. }