Alarm.php 19 KB

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