| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');include_once(FCPATH . 'application/controllers/Base_Controller.php');class Alarm extends Base_Controller {		public function __construct() {		parent::__construct();		$this->load->model('Alarm_model');		$this->load->model('Project_model');		$this->load->model('Network_model');		$this->load->model('Lamp_model');		$this->load->model('Patrol_model');		$this->load->model('Repair_model');	}		// 获取告警信息列表	public function get(){		$filter = array();		$where = array('1=1');		$role = $this->get_user_info('role');		$user_id = $this->get_user_info('id');		$lampid = $this->input->post('lampid',true);		$page = intval($this->input->post('page',true));		$count = intval($this->input->post('count',true));		$print = $this->input->post('print',true);		// 告警状态筛选		$status = $this->input->post('status',true);		// 关键字搜索		$keyword = $this->input->post('keyword',true);		$projectid = $this->input->post('projectid',true);		// 是否导出		$download = $this->input->post('download',true);		// 告警类型筛选		$alarmtype = trim($this->input->post('alarmtype',true));		// 时间筛选		$endDate = $this->input->post('endDate',true);		$startDate = $this->input->post('startDate',true);		$type = intval($this->input->post('type',true));		$version = $this->session->userdata('version');		if (!empty($version)) {			$alarmtype = alarm_translate_ch($alarmtype);		}		$page = !isset($page) ? 1 : $page;		$limit = empty($count) ? 20 : $count;		$offset = ($page - 1)*$count;		$section = $this->input->post('section',true);		if (!empty($section)) $where[] = 'L.section = "'.$section.'"';		if (!empty($lampid)) $where[] = 'L.id = '.$lampid;		if (!empty($keyword)) $where[] = 'L.number like "%'.$keyword.'%"';		if (!empty($projectid)) $where[] = 'L.projectid = '.$projectid;		if(!isset($lampid) && isset($status) && $status >= 0) $where[] = 'AI.status = '.$status;		if ($type == 0) {			$status = empty($filter['status']) ? 0 : $filter['status'];			$where[] = 'AI.status = '.$status;		}		if(!empty($alarmtype)) $filter['alarmtype'] = $alarmtype;		if(isset($endDate)) $filter['endDate'] = $endDate;		if(isset($startDate)) $filter['startDate'] = $startDate;		if (empty($download)) {			if (empty($type)) {				$join = array();				$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'];				$join[] = ['table'=>'lampinfo as L','cond'=>'L.id = AI.lampid','type'=>'inner'];				$join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'inner'];				$join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'inner'];				$where[] = '(AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';				$where = implode(' AND ', $where);				$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');				$total = $this->Alarm_model->get_list_by_multi_join($where, 'count(*) as total',null, null, $join, null, NUll, 'AI',true);				$total = $total['total'];				// $alarm = $this->Alarm_model->getList($filter, $role, $user_id, $page, $count,1);			}else{				$join = array();				// $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'];				$join[] = ['table'=>'lampinfo as L','cond'=>'L.id = AI.lampid','type'=>'inner'];				$join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'inner'];				$join[] = ['table'=>'global_location as G','cond'=>'G.id = P.cityid','type'=>'inner'];				$where[] = '(AI.batstatus != 0 OR AI.panelstatus != 0 OR AI.lampstatus != 0 OR AI.tempstatus != 0)';				$where = implode(' AND ', $where);				$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');				$total = $this->Alarm_model->get_list_by_multi_join($where, 'count(*) as total',null, null, $join, null, NUll, 'AI',true);				$total = $total['total'];			}						$alarm = empty($alarm) ? array() : $alarm;			$batstatus = $this->config->item('batstatus');	        $panelstatus = $this->config->item('panelstatus');	        $lampstatus = $this->config->item('lampstatus');	        $tempstatus = $this->config->item('tempstatus');	        			foreach($alarm as &$value){				$temp2 = array();		        if (isset($batstatus[$value['batstatus']])) $temp2[] = $batstatus[$value['batstatus']];		        if (isset($panelstatus[$value['panelstatus']])) $temp2[] = $panelstatus[$value['panelstatus']];		        if (isset($lampstatus[$value['lampstatus']])) $temp2[] = $lampstatus[$value['lampstatus']];		        if (isset($tempstatus[$value['tempstatus']])) $temp2[] = $tempstatus[$value['tempstatus']];		        $value['stralarmtype'] = implode(',', $temp2);		        $value['updatetime'] = set_timezone($value['updatetime'],$value['timezone']);			}			$data = array('history_list'=>$alarm, 'total'=>ceil($total/$count));			if (empty($type)) {				$filter['status'] = 0;				$filter['fault'] = 0;				// $data['fault'] = $this->Alarm_model->getDataCount($filter, $role, $user_id);			}			exit(json_result('0000', $this->response['0000'], array('list'=>$data)));		}else{			$alarm = $this->Alarm_model->getList($filter, $role, $user_id, ($page-1)*$count, 3000);			$alarm = empty($alarm) ? array() : $alarm;			// 导出到Excel			if (empty($version)) {				$title = array(					array('编号','项目名称','报警路灯','报警事件','是否已处理','更新时间'),				);			}else{ // 英文导出				$title = array(					array('Number','Project name','Alarm lamp','Alarm event','Has it been processed','Update time'),				);			}						$temp = array();			$i = ($page-1)*$count + 1;			foreach ($alarm as $k=>$v) {				$temp[$k][] = $i;				$temp[$k][] = $v['project'];				$temp[$k][] = $v['number'];								if (empty($version)) {					$stralarmtype = trim($v['stralarmtype']);	                if (!empty($stralarmtype)) {	                    $alarmArr = explode(',', $stralarmtype);	                    $temp1 = array();	                    foreach ($alarmArr as $a) {	                    	if ($a == '电池故障') {	                            $a = '电池电压异常';	                        }	                        $temp1[] = $a;	                    }	                    $v['stralarmtype'] = implode(',', $temp1);	                }					$temp[$k][] = $v['stralarmtype'];					$temp[$k][] = empty($v['status']) ? '未处理' : '已处理';				}else{					$stralarmtype = trim($v['stralarmtype']);	                if (!empty($stralarmtype)) {	                    $alarmArr = explode(',', $stralarmtype);	                    $arr = array();	                    foreach ($alarmArr as $a) {	                    	if ($a == '电池故障') {	                            $a = '电池电压异常';	                        }	                        $arr[] = alarm_translate($a);	                    }	                    // var_dump($temp);	                    $temp[$k][] = empty($arr) ? '' : implode('.', $arr);	                }else{	                	$temp[$k][] = '';	                }					$temp[$k][] = empty($v['status']) ? 'Untreated' : 'Already processed';				}				$temp[$k][] = $v['updatetime'];				$i ++;			}			$data = array_merge($title,$temp);			$path = push_excel($data,'alarmExcel_'.date('Ymd'));			exit(json_result('0000',$this->response['0000'],array('path'=>$path)));		}			}		// 查看故障详情	public function detail(){		$id = $this->input->post('id',true);		if (empty($id)) {			exit(json_result('0604', $this->response['0604'], array()));		}		$data = $this->Alarm_model->getOne($id);		exit(json_result('0000', $this->response['0000'], $data));	}		// 更改故障状态	public function set(){		$role = $this->get_user_info('role');        // if ($role == COMPANY_CUSTOMER) {        //     exit(json_result('0011', $this->response['0011'], array()));        // }        		$ids = $this->input->post("ids",true);		if (empty($ids)) {			exit(json_result('0604', $this->response['0604'], array()));		}		$status = $this->input->post("status",true);		$ids = explode(",", $ids);		$filter = array('id' => $ids);		$data = array('status' => intval($status));		$res = $this->Alarm_model->update($data,$filter);		// if ($status == 1) {		// 	foreach ($ids as $v) {		// 		$alarmData = $this->Alarm_model->get_one(['id'=>$v],'lampid');		// 		if ($status == 0) {		// 			$this->Lamp_model->update(array('id'=>$alarData['lampid']),array('isfaulted'=>1));		// 		}else{		// 			$this->Lamp_model->update(array('id'=>$alarData['lampid']),array('isfaulted'=>0));		// 		}		// 		$this->Alarm_model->update(array('lampid'=>$alarmData['lampid'],'status'=>0),array('status'=>1));		// 	}		// }		if ($res) {			exit(json_result('0000', $this->response['0000'], array()));		} else {			exit(json_result('0009', $this->response['0009'], array()));		}	}		// 删除故障信息	public function del(){		$role = $this->get_user_info('role');        // if ($role == COMPANY_CUSTOMER) {        //     exit(json_result('0011', $this->response['0011'], array()));        // }		$ids = $this->input->post("ids",true);        $lamp_ids = $this->input->post('lamp_ids', true);        if (empty($ids) && empty($lamp_ids)) {            exit(json_result('0603', $this->response['0603'], array()));        }        if (!empty($ids)) {			$ids = explode(",", $ids);			$res = $this->Alarm_model->delBatch($ids);        } else {			$lamp_ids = explode(",", $lamp_ids);        	$res = $this->Alarm_model->delBatch($lamp_ids, 'lampid');        	$this->Lamp_model->markNormal($lamp_ids);	        $dataArr = $this->Lamp_model->getBatch($lamp_ids);	        foreach ($dataArr as $data) {	            $this->Network_model->minus_fault_count($data['networkid']);	            $this->Project_model->minus_fault_count($data['projectid']);	        }        }		if ($res) {			exit(json_result('0000', $this->response['0000'], array()));		} else {			exit(json_result('0005', $this->response['0005'], array()));		}	}	// 巡检信息列表	public function patrol_list(){		$role = $this->get_user_info('role');		$userid = $this->get_user_info('id');		$company = $this->get_user_info('company');		$projectid = $this->input->post('projectid',true);		$page = $this->input->post('page',true);		$count = $this->input->post('count',true);		$keyword = $this->input->post('keyword',true);		$download = $this->input->post('download',true);		$status = $this->input->post('status',true);		$print = $this->input->post('print',true);		$page = !isset($page) ? 1 : $page;		$count = empty($count) ? 20 : $count;		$limit = $count;		$offset = ($page - 1)*$count;		// 信息字段		$field = "L.id,				  P.id as projectid,				  P.projectname as project_name,				  L.number,				  L.patrolinterval";		$where =array();		$section = $this->input->post('section',true);		$projectid = intval($this->input->post('projectid',true));				if (!empty($section)) $where['L.section'] = $section;		if (!empty($projectid)) $where['L.projectid'] = $projectid;		if (!empty($keyword)) $where['L.number|'] = $keyword;		if ($role != SYSTEM_ADMIN) $where['P.company'] = $company;				$join = [];		$join[] = ['table'=>'project as P','cond'=>'P.id = L.projectid','type'=>'left'];		if (empty($download)) {						// 获取巡检信息列表			if (isset($print) && !empty($print)) {				// $data = $this->Patrol_model->getList($role,$companyid,$projectid,$userid,$fiter,$field,$page,$count,1);				$data = $this->Lamp_model->get_list_by_join($where, $field,$limit, $offset, $join, 'L.number ASC,L.id DESC', NUll, 'L');			}else{				$data = $this->Lamp_model->get_list_by_join($where, $field,$limit, $offset, $join, 'L.number ASC,L.id DESC', NUll, 'L');			}						// 获取巡检信息总数			$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);			$total = $total['total'];			json_result('0000',$this->response['0000'],array('patrol_list'=>$data,'total'=>ceil($total/$count)));		}else{			// 获取巡检信息列表			$data = $this->Lamp_model->get_list_by_join($where, $field,3000, 0, $join, 'L.number ASC,id DESC', NUll, 'L');			// $data = $this->Patrol_model->getList($role,$companyid,$projectid,$userid,$fiter,$field,($page-1)*$count,3000);			$version = $this->session->userdata('version');			if (empty($version)) {				$title = array(					array('编号','项目名称','路灯编号','巡检间隔时间(min)'),				);			}else{				$title = array(					array('Number','Project name','Lamp Number','Inspection interval time(min)'),				);			}						$temp = array();			$i = ($page-1)*$count + 1;			foreach ($data as $k=>$v) {				$temp[$k][] = $i;				$temp[$k][] = $v['project_name'];				$temp[$k][] = $v['number'];				$temp[$k][] = $v['patrolinterval'];				$i ++;			}			$res = array_merge($title,$temp);			$path = push_excel($res,'patrolExcel_'.date('Ymd'));			exit(json_result('0000',$this->response['0000'],array('path'=>$path)));		}			}	// 单个路灯维修信息	public function repair_info(){		$alarmid = $this->input->post('alarmid');		if (empty($alarmid)) {			exit(json_result('0007',$this->response['0007'],array()));		}		$page = $this->input->post('page',true);		$count = $this->input->post('count',true);		$page = empty($page) ? 1 : $page;		$count = empty($count) ? 1 : $count;		$alarmData = $this->Alarm_model->getOne($alarmid,'A.lampid,A.alarmtype');		$field = 'RI.repair_hitch,RI.repair_path,RI.repair_solution,RI.repair_time,RU.name';		$repair_list = $this->Repair_model->get_repair_info_list(array('RI.lampid'=>$alarmData['lampid'],'page'=>$page,'count'=>$count),$field);		$total = $this->Repair_model->get_repair_total(array('RI.lampid'=>$alarmData['lampid']));		if (empty($repair_list)) {			exit(json_result('0616',$this->response['0616'],array()));		}		exit(json_result('0000',$this->response['0000'],array('list'=>$repair_list,'total'=>ceil($total/$count))));	}}
 |