Alarm.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. $this->load->model('AlarmSendUserInfo_model');
  13. }
  14. // 获取告警信息列表
  15. public function get(){
  16. $filter = array();
  17. $where = array('1=1');
  18. $role = $this->get_user_info('role');
  19. $user_id = $this->get_user_info('id');
  20. $lampid = $this->input->post('lampid',true);
  21. $page = intval($this->input->post('page',true));
  22. $count = intval($this->input->post('count',true));
  23. $print = $this->input->post('print',true);
  24. // 告警状态筛选
  25. $status = $this->input->post('status',true);
  26. // 关键字搜索
  27. $keyword = $this->input->post('keyword',true);
  28. $projectid = $this->input->post('projectid',true);
  29. // 是否导出
  30. $download = $this->input->post('download',true);
  31. // 告警类型筛选
  32. $alarmtype = trim($this->input->post('alarmtype',true));
  33. // var_dump($alarmtype);
  34. // 时间筛选
  35. $endDate = $this->input->post('endDate',true);
  36. $startDate = $this->input->post('startDate',true);
  37. $type = intval($this->input->post('type',true));
  38. $version = $this->version;
  39. // var_dump($version);
  40. // if (!empty($version)) {
  41. // $alarmtype = alarm_translate($alarmtype);
  42. // }
  43. $page = empty($page) ? 1 : $page;
  44. $count = empty($count) ? 20 : $count;
  45. $limit = $count;
  46. $offset = ($page - 1)*$count;
  47. $section = $this->input->post('section',true);
  48. if (!empty($section)) $where[] = 'L.section = "'.$section.'"';
  49. if (!empty($lampid)) $where[] = 'L.id = '.$lampid;
  50. if (!empty($keyword)) $where[] = 'L.number like "%'.$keyword.'%"';
  51. if (!empty($projectid)) $where[] = 'L.projectid = '.$projectid;
  52. if(!isset($lampid) && isset($status) && $status >= 0) $where[] = 'AI.status = '.$status;
  53. if ($type == 0) {
  54. $status = empty($filter['status']) ? 0 : $filter['status'];
  55. $where[] = 'AI.status = '.$status;
  56. }
  57. // if(!empty($alarmtype)) $filter['alarmtype'] = $alarmtype;
  58. if(!empty($alarmtype) && is_numeric($alarmtype)) {
  59. $t_ty = substr($alarmtype, 0,1);
  60. $t_ty2 = substr($alarmtype, 1,1);
  61. if ($t_ty == 1) {
  62. $t_1 = 'batstatus';
  63. }elseif ($t_ty == 2) {
  64. $t_1 = 'panelstatus';
  65. }elseif ($t_ty == 3) {
  66. $t_1 = 'lampstatus';
  67. }elseif ($t_ty == 4) {
  68. $t_1 = 'tempstatus';
  69. }else {
  70. $t_1 = 'onlinestatus';
  71. }
  72. $where[] = 'AI.'.$t_1.' = '.$t_ty2;
  73. }
  74. // var_dump($alarmtype);
  75. // if(isset($endDate)) $filter['endDate'] = $endDate;
  76. if(isset($endDate) && !empty($endDate)) $where[] = 'AI.updatetime <= "'.$endDate.'"';
  77. if(isset($startDate) && !empty($startDate)) $where[] = 'AI.updatetime >= "'.$startDate.'"';
  78. // if(isset($startDate)) $filter['startDate'] = $startDate;
  79. if (empty($download)) {
  80. if (empty($type)) {
  81. $where[] = 'L.controllerstatus = 5';
  82. $join = array();
  83. $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'];
  84. $join[] = ['table'=>'lampinfo as L','cond'=>'L.id = AI.lampid','type'=>'inner'];
  85. $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'inner'];
  86. $join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'inner'];
  87. $where[] = '(AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';
  88. $where = implode(' AND ', $where);
  89. $alarm = $this->Alarm_model->get_list_by_join($where, 'AI.batstatus,AI.panelstatus,AI.lampstatus,AI.tempstatus,AI.status,AI.onlinestatus,AI.updatetime,G.timezone,L.number,P.projectname as project,AI.id',$limit, $offset, $join, 'AI.updatetime DESC,id DESC', NUll, 'AI');
  90. $total = $this->Alarm_model->get_list_by_multi_join($where, 'count(*) as total',null, null, $join, null, NUll, 'AI',true);
  91. $total = $total['total'];
  92. // $alarm = $this->Alarm_model->getList($filter, $role, $user_id, $page, $count,1);
  93. }else{
  94. $join = array();
  95. // $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'];
  96. $join[] = ['table'=>'lampinfo as L','cond'=>'L.id = AI.lampid','type'=>'inner'];
  97. $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'inner'];
  98. $join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'inner'];
  99. $where[] = '(AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';
  100. $where = implode(' AND ', $where);
  101. $alarm = $this->Alarm_model->get_list_by_join($where, 'AI.batstatus,AI.panelstatus,AI.lampstatus,AI.tempstatus,AI.status,AI.onlinestatus,AI.updatetime,G.timezone,L.number,P.projectname as project,AI.id',$limit, $offset, $join, 'AI.updatetime DESC,id DESC', NUll, 'AI');
  102. $total = $this->Alarm_model->get_list_by_multi_join($where, 'count(*) as total',null, null, $join, null, NUll, 'AI',true);
  103. $total = $total['total'];
  104. }
  105. $alarm = empty($alarm) ? array() : $alarm;
  106. $batstatus = $this->config->item('batstatus');
  107. $panelstatus = $this->config->item('panelstatus');
  108. $lampstatus = $this->config->item('lampstatus');
  109. $tempstatus = $this->config->item('tempstatus');
  110. $onlinestatus = $this->config->item('onlinestatus');
  111. foreach($alarm as &$value){
  112. $temp2 = array();
  113. if (isset($batstatus[$value['batstatus']])) $temp2[] = $batstatus[$value['batstatus']];
  114. if (isset($panelstatus[$value['panelstatus']])) $temp2[] = $panelstatus[$value['panelstatus']];
  115. if (isset($lampstatus[$value['lampstatus']])) $temp2[] = $lampstatus[$value['lampstatus']];
  116. if (isset($tempstatus[$value['tempstatus']])) $temp2[] = $tempstatus[$value['tempstatus']];
  117. if (isset($onlinestatus[$value['onlinestatus']])) $temp2[] = $onlinestatus[$value['onlinestatus']];
  118. $value['stralarmtype'] = implode(',', $temp2);
  119. $value['updatetime'] = set_timezone($value['updatetime'],$value['timezone']);
  120. }
  121. $data = array('history_list'=>$alarm, 'total'=>ceil($total/$count));
  122. if (empty($type)) {
  123. $filter['status'] = 0;
  124. $filter['fault'] = 0;
  125. // $data['fault'] = $this->Alarm_model->getDataCount($filter, $role, $user_id);
  126. }
  127. exit(json_result('0000', $this->response['0000'], array('list'=>$data)));
  128. }else{
  129. $alarm = $this->Alarm_model->getList($filter, $role, $user_id, ($page-1)*$count, 3000);
  130. $alarm = empty($alarm) ? array() : $alarm;
  131. // 导出到Excel
  132. if (empty($version)) {
  133. $title = array(
  134. array('编号','项目名称','报警路灯','报警事件','是否已处理','更新时间'),
  135. );
  136. }else{ // 英文导出
  137. $title = array(
  138. array('Number','Project name','Alarm lamp','Alarm event','Has it been processed','Update time'),
  139. );
  140. }
  141. $temp = array();
  142. $i = ($page-1)*$count + 1;
  143. foreach ($alarm as $k=>$v) {
  144. $temp[$k][] = $i;
  145. $temp[$k][] = $v['project'];
  146. $temp[$k][] = $v['number'];
  147. if (empty($version)) {
  148. $stralarmtype = trim($v['stralarmtype']);
  149. if (!empty($stralarmtype)) {
  150. $alarmArr = explode(',', $stralarmtype);
  151. $temp1 = array();
  152. foreach ($alarmArr as $a) {
  153. if ($a == '电池故障') {
  154. $a = '电池电压异常';
  155. }
  156. $temp1[] = $a;
  157. }
  158. $v['stralarmtype'] = implode(',', $temp1);
  159. }
  160. $temp[$k][] = $v['stralarmtype'];
  161. $temp[$k][] = empty($v['status']) ? '未处理' : '已处理';
  162. }else{
  163. $stralarmtype = trim($v['stralarmtype']);
  164. if (!empty($stralarmtype)) {
  165. $alarmArr = explode(',', $stralarmtype);
  166. $arr = array();
  167. foreach ($alarmArr as $a) {
  168. if ($a == '电池故障') {
  169. $a = '电池电压异常';
  170. }
  171. $arr[] = alarm_translate($a);
  172. }
  173. // var_dump($temp);
  174. $temp[$k][] = empty($arr) ? '' : implode('.', $arr);
  175. }else{
  176. $temp[$k][] = '';
  177. }
  178. $temp[$k][] = empty($v['status']) ? 'Untreated' : 'Already processed';
  179. }
  180. $temp[$k][] = $v['updatetime'];
  181. $i ++;
  182. }
  183. $data = array_merge($title,$temp);
  184. $path = push_excel($data,'alarmExcel_'.date('Ymd'));
  185. exit(json_result('0000',$this->response['0000'],array('path'=>$path)));
  186. }
  187. }
  188. // 查看故障详情
  189. public function detail(){
  190. $id = $this->input->post('id',true);
  191. if (empty($id)) {
  192. exit(json_result('0604', $this->response['0604'], array()));
  193. }
  194. $data = $this->Alarm_model->getOne($id);
  195. exit(json_result('0000', $this->response['0000'], $data));
  196. }
  197. // 更改故障状态
  198. public function set(){
  199. $role = $this->get_user_info('role');
  200. // if ($role == COMPANY_CUSTOMER) {
  201. // exit(json_result('0011', $this->response['0011'], array()));
  202. // }
  203. $ids = $this->input->post("ids",true);
  204. if (empty($ids)) {
  205. exit(json_result('0604', $this->response['0604'], array()));
  206. }
  207. $status = $this->input->post("status",true);
  208. $ids = explode(",", $ids);
  209. $filter = array('id' => $ids);
  210. $data = array('status' => intval($status));
  211. $res = $this->Alarm_model->update($data,$filter);
  212. // if ($status == 1) {
  213. // foreach ($ids as $v) {
  214. // $alarmData = $this->Alarm_model->get_one(['id'=>$v],'lampid');
  215. // if ($status == 0) {
  216. // $this->Lamp_model->update(array('id'=>$alarData['lampid']),array('isfaulted'=>1));
  217. // }else{
  218. // $this->Lamp_model->update(array('id'=>$alarData['lampid']),array('isfaulted'=>0));
  219. // }
  220. // $this->Alarm_model->update(array('lampid'=>$alarmData['lampid'],'status'=>0),array('status'=>1));
  221. // }
  222. // }
  223. if ($res) {
  224. exit(json_result('0000', $this->response['0000'], array()));
  225. } else {
  226. exit(json_result('0009', $this->response['0009'], array()));
  227. }
  228. }
  229. // 删除故障信息
  230. public function del(){
  231. $role = $this->get_user_info('role');
  232. // if ($role == COMPANY_CUSTOMER) {
  233. // exit(json_result('0011', $this->response['0011'], array()));
  234. // }
  235. $ids = $this->input->post("ids",true);
  236. $lamp_ids = $this->input->post('lamp_ids', true);
  237. if (empty($ids) && empty($lamp_ids)) {
  238. exit(json_result('0603', $this->response['0603'], array()));
  239. }
  240. if (!empty($ids)) {
  241. $ids = explode(",", $ids);
  242. $res = $this->Alarm_model->delBatch($ids);
  243. } else {
  244. $lamp_ids = explode(",", $lamp_ids);
  245. $res = $this->Alarm_model->delBatch($lamp_ids, 'lampid');
  246. $this->Lamp_model->markNormal($lamp_ids);
  247. $dataArr = $this->Lamp_model->getBatch($lamp_ids);
  248. foreach ($dataArr as $data) {
  249. $this->Network_model->minus_fault_count($data['networkid']);
  250. $this->Project_model->minus_fault_count($data['projectid']);
  251. }
  252. }
  253. if ($res) {
  254. exit(json_result('0000', $this->response['0000'], array()));
  255. } else {
  256. exit(json_result('0005', $this->response['0005'], array()));
  257. }
  258. }
  259. // 巡检信息列表
  260. public function patrol_list(){
  261. $role = $this->get_user_info('role');
  262. $userid = $this->get_user_info('id');
  263. $company = $this->get_user_info('company');
  264. $projectid = $this->input->post('projectid',true);
  265. $page = $this->input->post('page',true);
  266. $count = $this->input->post('count',true);
  267. $keyword = $this->input->post('keyword',true);
  268. $download = $this->input->post('download',true);
  269. $status = $this->input->post('status',true);
  270. $print = $this->input->post('print',true);
  271. $page = !isset($page) ? 1 : $page;
  272. $count = empty($count) ? 20 : $count;
  273. $limit = $count;
  274. $offset = ($page - 1)*$count;
  275. // 信息字段
  276. $field = "L.id,
  277. P.id as projectid,
  278. P.projectname as project_name,
  279. L.number,
  280. L.patrolinterval";
  281. $where =array();
  282. $section = $this->input->post('section',true);
  283. $projectid = intval($this->input->post('projectid',true));
  284. if (!empty($section)) $where['L.section'] = $section;
  285. if (!empty($projectid)) $where['L.projectid'] = $projectid;
  286. if (!empty($keyword)) $where['L.number|'] = $keyword;
  287. if ($role != SYSTEM_ADMIN) $where['P.company'] = $company;
  288. $join = [];
  289. $join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'left'];
  290. if (empty($download)) {
  291. // 获取巡检信息列表
  292. if (isset($print) && !empty($print)) {
  293. // $data = $this->Patrol_model->getList($role,$companyid,$projectid,$userid,$fiter,$field,$page,$count,1);
  294. $data = $this->Lamp_model->get_list_by_join($where, $field,$limit, $offset, $join, 'L.number ASC,L.id DESC', NUll, 'L');
  295. }else{
  296. $data = $this->Lamp_model->get_list_by_join($where, $field,$limit, $offset, $join, 'L.number ASC,L.id DESC', NUll, 'L');
  297. }
  298. // 获取巡检信息总数
  299. $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);
  300. $total = $total['total'];
  301. json_result('0000',$this->response['0000'],array('patrol_list'=>$data,'total'=>ceil($total/$count)));
  302. }else{
  303. // 获取巡检信息列表
  304. $data = $this->Lamp_model->get_list_by_join($where, $field,3000, 0, $join, 'L.number ASC,id DESC', NUll, 'L');
  305. // $data = $this->Patrol_model->getList($role,$companyid,$projectid,$userid,$fiter,$field,($page-1)*$count,3000);
  306. $version = $this->session->userdata('version');
  307. if (empty($version)) {
  308. $title = array(
  309. array('编号','项目名称','路灯编号','巡检间隔时间(min)'),
  310. );
  311. }else{
  312. $title = array(
  313. array('Number','Project name','Lamp Number','Inspection interval time(min)'),
  314. );
  315. }
  316. $temp = array();
  317. $i = ($page-1)*$count + 1;
  318. foreach ($data as $k=>$v) {
  319. $temp[$k][] = $i;
  320. $temp[$k][] = $v['project_name'];
  321. $temp[$k][] = $v['number'];
  322. $temp[$k][] = $v['patrolinterval'];
  323. $i ++;
  324. }
  325. $res = array_merge($title,$temp);
  326. $path = push_excel($res,'patrolExcel_'.date('Ymd'));
  327. exit(json_result('0000',$this->response['0000'],array('path'=>$path)));
  328. }
  329. }
  330. // 单个路灯维修信息
  331. public function repair_info(){
  332. $alarmid = $this->input->post('alarmid');
  333. if (empty($alarmid)) {
  334. exit(json_result('0007',$this->response['0007'],array()));
  335. }
  336. $page = $this->input->post('page',true);
  337. $count = $this->input->post('count',true);
  338. $page = empty($page) ? 1 : $page;
  339. $count = empty($count) ? 1 : $count;
  340. $alarmData = $this->Alarm_model->getOne($alarmid,'A.lampid,A.alarmtype');
  341. $field = 'RI.repair_hitch,RI.repair_path,RI.repair_solution,RI.repair_time,RU.name';
  342. $repair_list = $this->Repair_model->get_repair_info_list(array('RI.lampid'=>$alarmData['lampid'],'page'=>$page,'count'=>$count),$field);
  343. $total = $this->Repair_model->get_repair_total(array('RI.lampid'=>$alarmData['lampid']));
  344. if (empty($repair_list)) {
  345. exit(json_result('0616',$this->response['0616'],array()));
  346. }
  347. exit(json_result('0000',$this->response['0000'],array('list'=>$repair_list,'total'=>ceil($total/$count))));
  348. }
  349. // 添加编辑推送人员
  350. public function save_alarm_send_user(){
  351. $id = intval($this->input->post('id',true));
  352. $userId = $this->get_user_info('id');
  353. $name = $this->input->post('name',true);
  354. $email = $this->input->post('email',true);
  355. $phone = $this->input->post('phone',true);
  356. $project = $this->input->post('project',true);
  357. $project = $project === null ? '' : $project;
  358. if ($phone === '' || $phone === null) exit(json_result('0607',$this->response['0607']));
  359. if ($email === '' || $email === null) exit(json_result('0618',$this->response['0618']));
  360. $config = array();
  361. // $config[] = array(
  362. // 'field' => 'phone',
  363. // 'label' => 'Phone',
  364. // 'rules' => 'required|numeric|exact_length[11]',
  365. // 'errors' => array(
  366. // 'numeric' => '0406',
  367. // 'exact_length' => '0406',
  368. // )
  369. // );
  370. $config[] = array(
  371. 'field' => 'email',
  372. 'label' => 'Email',
  373. 'rules' => 'required|valid_email',
  374. 'errors' => array(
  375. 'valid_email' => '0719',
  376. )
  377. );
  378. $this->load->library('form_validation');
  379. $this->form_validation->set_rules($config);
  380. if ($this->form_validation->run() == FALSE){
  381. $errors = $this->form_validation->error_array();
  382. exit(json_result(current($errors),$this->response[current($errors)]));
  383. }
  384. if (empty($id)) {
  385. if ($this->AlarmSendUserInfo_model->getDataCount(['email'=>$email])) exit(json_result('0722',$this->response['0722']));
  386. if ($this->AlarmSendUserInfo_model->getDataCount(['phone'=>$phone])) exit(json_result('0721',$this->response['0721']));
  387. if ($this->AlarmSendUserInfo_model->getDataCount(['name'=>$name,'userId'=>$userId])) exit(json_result('0619',$this->response['0619']));
  388. $data = array('name'=>$name,'email'=>$email,'phone'=>$phone,'project'=>$project,'userId'=>$userId);
  389. $data['createTime'] = date('Y-m-d H:i:s',time());
  390. $this->AlarmSendUserInfo_model->add($data);
  391. }else{
  392. if ($this->AlarmSendUserInfo_model->getDataCount(['email'=>$email],$id)) exit(json_result('0722',$this->response['0722']));
  393. if ($this->AlarmSendUserInfo_model->getDataCount(['phone'=>$phone],$id)) exit(json_result('0721',$this->response['0721']));
  394. if ($this->AlarmSendUserInfo_model->getDataCount(['name'=>$name,'userId'=>$userId],$id)) exit(json_result('0619',$this->response['0619']));
  395. $data = array('name'=>$name,'email'=>$email,'phone'=>$phone,'project'=>$project,'userId'=>$userId);
  396. $this->AlarmSendUserInfo_model->update($data,['id'=>$id]);
  397. }
  398. exit(json_result('0000',$this->response['0000']));
  399. }
  400. // 推送人员列表
  401. public function alarm_send_user_list(){
  402. $userId = $this->get_user_info('id');
  403. $where = ['userId'=>$userId];
  404. $page = intval($this->input->post('page'));
  405. $count = intval($this->input->post('count'));
  406. $page = empty($page) ? 1 : $page;
  407. $limit = empty($count) ? 10 : $count;
  408. $offset = ($page - 1) * $limit;
  409. $keyword = $this->input->post('keyword',true);
  410. if ($keyword === '' || $keyword === null) $where['name|'] = $keyword;
  411. $list = $this->AlarmSendUserInfo_model->get_list($where, 'id,name,email,project,phone,is_send',$limit, $offset, 'convert(name using gbk) asc,id desc', NUll);
  412. $total = $this->AlarmSendUserInfo_model->get_count($where);
  413. exit(json_result('0000',$this->response['0000'],['list'=>$list,'total'=>$total]));
  414. }
  415. // 删除推送人员
  416. public function del_alarm_send_user(){
  417. $id = $this->input->post('id',true);
  418. if ($id === '' || $id === null) exit(json_result('0007',$this->response['0007']));
  419. $idArr = explode(',', $id);
  420. foreach ($idArr as $id) {
  421. $this->AlarmSendUserInfo_model->delete(['id'=>$id]);
  422. }
  423. exit(json_result('0000',$this->response['0000']));
  424. }
  425. // 是否开启推送
  426. public function set_alarm_send_user_status(){
  427. $id = intval($this->input->post('id',true));
  428. $status = intval($this->input->post('status',true));
  429. if (empty($id)) exit(json_result('0007',$this->response['0007']));
  430. $is_send = empty($status) ? 0 : 1;
  431. $this->AlarmSendUserInfo_model->update(['is_send'=>$is_send],['id'=>$id]);
  432. exit(json_result('0000',$this->response['0000']));
  433. }
  434. }