Alarm.php 15 KB

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