Alarm.php 15 KB

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