Alarm.php 19 KB

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