Alarm.php 19 KB

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