Alarm.php 14 KB

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