| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923 | 
							- <?php
 
- if (!defined('BASEPATH'))exit('No direct script access allowed');
 
- include_once(FCPATH . 'application/models/Base_model.php');
 
- class Lamp_model extends Base_model {
 
- 	protected $table = 'lampinfo';
 
- 	public function __construct() {
 
- 		parent::__construct();
 
- 	}
 
- 	public function queryData($where, $fields='*', $table='lampinfo', $order_by='updatetime desc') {
 
-         $this->db->select($fields)->from($table)->where($where)->order_by($order_by)->limit(1);
 
-         $query = $this->db->get();
 
-         return $query->row_array();
 
-     }
 
-     public function get_list_not_in($where_in_field, $where_in,$fields='*',$filter=array()){
 
-     	if(!empty($where_in_field) && !empty($where_in)){
 
-             $this->db->where_not_in($where_in_field, $where_in);
 
-         }
 
-         return $this->get_list_in(null,null,$fields,$filter);
 
-     }
 
-     // 项目管理页路灯详情数据
 
-     public function get_list_in($where_in_field, $where_in, $fields='*', $filter=array(), $lite = false) {
 
-         $this->db->select($fields);
 
-         if(!empty($where_in_field) && !empty($where_in)){
 
-             if (is_array($where_in)) {
 
-             	$this->db->where_in($where_in_field, $where_in);
 
-             }else{
 
-             	$this->db->where($where_in_field, $where_in);
 
-             }
 
-         }
 
- 		if(isset($filter['networkid']) && !empty($filter['networkid'])){
 
- 			$this->db->where('L.networkid', $filter['networkid']);
 
- 		}
 
- 		if(isset($filter['offline']) && !empty($filter['offline'])){
 
- 			$this->db->where('N.status', 0);
 
- 		}
 
- 		if(isset($filter['online']) && !empty($filter['online'])){
 
- 			$this->db->where('N.status', 1);
 
- 		}
 
- 		if(isset($filter['section']) && !empty($filter['section'])){
 
- 			$this->db->where('L.section', $filter['section']);
 
- 		}
 
- 		if(isset($filter['lampid']) && !empty($filter['lampid'])){
 
- 			$this->db->where('L.id', $filter['lampid']);
 
- 		}
 
- 		if(isset($filter['status']) && is_numeric($filter['status'])){
 
- 			$this->db->where('L.isfaulted', $filter['status']);
 
- 			$this->db->where('AI.stralarmtype !=', '');
 
- 			$this->db->where('AI.status', 0);
 
- 		}
 
-         if(isset($filter['lightStatus']) && is_numeric($filter['lightStatus'])){
 
- 			$this->db->where('L.status', $filter['lightStatus']);
 
- 		}
 
-         // 关键字搜索
 
- 		if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 			// $this->db->like('L.number', $filter['keyword']);
 
- 			$this->db->group_start();
 
- 			$this->db->or_like('L.number', $filter['keyword']);
 
- 			$this->db->or_like('L.address', $filter['keyword']);
 
- 			$this->db->or_like('L.section', $filter['keyword']);
 
- 			$this->db->group_end();
 
- 		}
 
- 		if(isset($filter['projectid']) && !empty($filter['projectid'])){
 
- 			$this->db->where('L.projectid', $filter['projectid']);
 
- 		}
 
- 		if (isset($filter['lng_low']) && is_numeric($filter['lng_low']) && isset($filter['lng_high']) && is_numeric($filter['lng_high']) && $filter['lng_low'] > $filter['lng_high']) {
 
- 			$this->db->group_start();
 
- 			$this->db->group_start();
 
- 			$this->db->where('L.longitude >=', $filter['lng_low']);
 
- 			$this->db->where('L.longitude <=', 180);
 
- 			$this->db->group_end();
 
- 			$this->db->or_group_start();
 
- 			$this->db->where('L.longitude <=', $filter['lng_high']);
 
- 			$this->db->where('L.longitude >=', -180);
 
- 			$this->db->group_end();
 
- 			$this->db->group_end();
 
- 		}else{
 
- 			if(isset($filter['lng_low']) && is_numeric($filter['lng_low'])){
 
- 				$this->db->where('L.longitude >=', $filter['lng_low']);
 
- 			}
 
- 			if(isset($filter['lng_high']) && is_numeric($filter['lng_high'])){
 
- 				$this->db->where('L.longitude <=', $filter['lng_high']);
 
- 			}
 
- 		}
 
- 		
 
- 		if(isset($filter['lat_low']) && is_numeric($filter['lat_low'])){
 
- 			$this->db->where('L.latitude >=', $filter['lat_low']);
 
- 		}
 
- 		
 
- 		if(isset($filter['lat_high']) && is_numeric($filter['lat_high'])){
 
- 			$this->db->where('L.latitude <=', $filter['lat_high']);
 
- 		}
 
-     
 
-         $this->db->from($this->table.' as L');
 
-         if ($lite != 1) {
 
-         	// $fieldArr = explode(',',$fields);
 
-         	// $temp = array();
 
-         	// foreach ($fieldArr as $value) {
 
-         	// 	$a = explode('.',$value)[0];
 
-         	// 	if (!in_array($a, $temp)) {
 
-         	// 		$temp[] = $a;
 
-         	// 	}
 
-         	// }
 
- 	        // $this->db->join('project as P', 'L.projectid = P.id', 'left');
 
- 	        // $this->db->join('network as N', 'L.networkid = N.id', 'left');
 
- 	        // $this->db->join('(select t1.* from lamp_info_log t1, (select lampid, max(updatetime) as maxtime from lamp_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as LI', 'L.id = LI.lampid', 'left');
 
- 	        // $this->db->join('(select t1.* from battery_info_log t1, (select lampid, max(updatetime) as maxtime from battery_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as BI', 'L.id = BI.lampid', 'left');
 
- 	        // $this->db->join('(select t1.* from history_info_log t1, (select lampid, max(updatetime) as maxtime from history_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as HI', 'L.id = HI.lampid', 'left');
 
- 	        // $this->db->join('(select t1.* from alarm_info_log t1, (select lampid, max(updatetime) as maxtime from alarm_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as AI', 'L.id = AI.lampid', 'left');
 
- 	        // $this->db->join('(select t1.* from solar_info_log t1, (select lampid, max(updatetime) as maxtime from solar_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as SI', 'L.id = SI.lampid', 'left');
 
- 	        // $this->db->join('(select t1.* from electric_info_log t1, (select lampid, max(updatetime) as maxtime from electric_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as EI', 'L.id = EI.lampid', 'left');
 
- 	        // $this->db->join('(select t1.* from system_info_log t1, (select lampid, max(updatetime) as maxtime from system_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as SIL', 'L.id = SIL.lampid', 'left');
 
- 	        $this->db->join('project as P', 'L.projectid = P.id', 'left');
 
- 			$this->db->join('network as N', 'L.networkid = N.id', 'left');
 
- 			$this->db->join('msisdn as M', 'N.simid = M.imsi', 'left');
 
- 	        // $this->db->join('(select lampid,max(updatetime) as time from history_info_log group by lampid) as T', 'L.id = T.lampid', 'left');
 
- 	        // $this->db->join('lamp_info_log as LI', 'L.id = LI.lampid AND LI.lampid=T.lampid AND LI.updatetime = T.time', 'left');
 
- 	        // $this->db->join('battery_info_log as BI', 'L.id = BI.lampid AND BI.lampid=T.lampid AND BI.updatetime = T.time', 'left');
 
- 	        // $this->db->join('history_info_log as HI', 'L.id = HI.lampid AND HI.lampid=T.lampid AND HI.updatetime = T.time', 'left');
 
- 	        // $this->db->join('alarm_info_log as AI', 'L.id = AI.lampid AND AI.lampid=T.lampid AND AI.updatetime = T.time', 'left');
 
- 	        $this->db->join('(select t1.* from alarm_info_log t1, (select lampid, max(updatetime) as maxtime from alarm_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as AI', 'L.id = AI.lampid', 'left');
 
- 	        // $this->db->join('solar_info_log as SI', 'L.id = SI.lampid AND SI.lampid=T.lampid AND SI.updatetime = T.time', 'left');
 
- 	        // $this->db->join('electric_info_log as EI', 'L.id = EI.lampid AND EI.lampid=T.lampid AND EI.updatetime = T.time', 'left');
 
- 	        // $this->db->join('system_info_log as SIL', 'L.id = SIL.lampid AND SIL.lampid=T.lampid AND SIL.updatetime = T.time', 'left');
 
- 	        $this->db->order_by("L.number ASC,N.networkname ASC");
 
-         }else{
 
-         	$this->db->order_by("L.number ASC");
 
-         }
 
-         if(!empty($filter['page']) && !empty($filter['count'])){
 
-         	$this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
 
- 		}
 
-         $query = $this->db->get();
 
-         // echo $this->db->last_query();die;
 
-         return $query->result_array();
 
-     }
 
-     public function report_get_list_in($where_in_field, $where_in, $fields='*', $filter=array()) {
 
-         $this->db->select($fields);
 
-         if(!empty($where_in_field) && !empty($where_in)){
 
-             if (is_array($where_in)) {
 
-             	$this->db->where_in($where_in_field, $where_in);
 
-             }else{
 
-             	$this->db->where($where_in_field, $where_in);
 
-             }
 
-         }
 
- 		if(isset($filter['networkid']) && !empty($filter['networkid'])){
 
- 			$this->db->where('L.networkid', $filter['networkid']);
 
- 		}
 
-         
 
-         if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 			$this->db->like('L.number', $filter['keyword']);
 
- 		}
 
- 		if(isset($filter['projectid']) && !empty($filter['projectid'])){
 
- 			$this->db->where('L.projectid', $filter['projectid']);
 
- 		}
 
- 		if(isset($filter['section']) && !empty($filter['section'])){
 
- 			$this->db->where('L.section', $filter['section']);
 
- 		}
 
-     
 
-         $this->db->from($this->table.' as L');
 
-         $this->db->join('project as P', 'L.projectid = P.id', 'left');
 
- 		$this->db->join('network as N', 'L.networkid = N.id', 'left');
 
-         // $this->db->join('(select lampid,max(updatetime) as updatetime from lamp_info_log group by lampid) as LI', 'L.id = LI.lampid', 'left');
 
-         $this->db->order_by("L.number ASC,N.networkname ASC");
 
-         if(!empty($filter['page']) && !empty($filter['count'])){
 
-         	$this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
 
- 		}
 
- 		
 
-         $query = $this->db->get();
 
-         return $query->result_array();
 
-     }
 
-     // 获取项目下路灯列表
 
-     public function get_list_by_project($projectid,$fields = '*'){
 
-     	if (empty($projectid)) {
 
-     		return array();
 
-     	}
 
-     	$sql = "SELECT {$fields} from {$this->table} as L 
 
-     			left join network as N on L.networkid = N.id 
 
-     			where L.projectid = {$projectid}";
 
-     	return $this->db->query($sql)->result_array();
 
-     }
 
-     // 项目管理页路灯数量
 
-     public function get_total($where_in_field, $where_in, $fields='*', $filter=array()) {
 
-         // $this->db->select($fields);
 
-         $this->db->select('count(*) as total');
 
-         if(!empty($where_in_field) && !empty($where_in)){
 
-             if (is_array($where_in)) {
 
-             	$this->db->where_in($where_in_field, $where_in);
 
-             }else{
 
-             	$this->db->where($where_in_field, $where_in);
 
-             }
 
-         }
 
- 		if(isset($filter['networkid']) && !empty($filter['networkid'])){
 
- 			$this->db->where('L.networkid', $filter['networkid']);
 
- 		}
 
- 		
 
- 		if(isset($filter['alarmtype']) && !empty($filter['alarmtype'])){
 
- 			$this->db->where('AI.alarmtype', $filter['alarmtype']);
 
- 		}
 
- 		if(isset($filter['status']) && is_numeric($filter['status'])){
 
- 			$this->db->where('L.isfaulted', $filter['status']);
 
- 			$this->db->where('AI.stralarmtype !=', '');
 
- 			$this->db->where('AI.status', 0);
 
- 		}
 
- 		if(isset($filter['section']) && !empty($filter['section'])){
 
- 			$this->db->where('L.section', $filter['section']);
 
- 		}
 
- 		if(isset($filter['offline']) && !empty($filter['offline'])){
 
- 			$this->db->where('L.netstatus', 0);
 
- 		}
 
-         if(isset($filter['online']) && !empty($filter['online'])){
 
- 			$this->db->where('L.netstatus', 1);
 
- 		}
 
- 		if(isset($filter['lightStatus']) && is_numeric($filter['lightStatus'])){
 
- 			$this->db->where('L.status', $filter['lightStatus']);
 
- 		}
 
-         // 关键字搜索
 
- 		if (uri_string() == 'report/home') { // 数据报表页
 
- 			if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 				// $this->db->group_start();
 
- 				$this->db->like('L.number', $filter['keyword']);
 
- 				// $this->db->group_end();
 
- 			}
 
- 		}else{   // 项目管理页
 
- 			if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 				$this->db->group_start();
 
- 				$this->db->like('L.number', $filter['keyword']);
 
- 				// $this->db->or_like('BI.updatetime', $filter['keyword']);
 
- 				$this->db->group_end();
 
- 			}
 
- 		}
 
- 		if(isset($filter['projectid']) && !empty($filter['projectid'])){
 
- 			$this->db->where('L.projectid', $filter['projectid']);
 
- 		}
 
- 		
 
- 		if (isset($filter['lng_low']) && is_numeric($filter['lng_low']) && isset($filter['lng_high']) && is_numeric($filter['lng_high']) && $filter['lng_low'] > $filter['lng_high']) {
 
- 			$this->db->group_start();
 
- 			$this->db->group_start();
 
- 			$this->db->where('L.longitude >=', $filter['lng_low']);
 
- 			$this->db->where('L.longitude <=', 180);
 
- 			$this->db->group_end();
 
- 			$this->db->or_group_start();
 
- 			$this->db->where('L.longitude <=', $filter['lng_high']);
 
- 			$this->db->where('L.longitude >=', -180);
 
- 			$this->db->group_end();
 
- 			$this->db->group_end();
 
- 		}else{
 
- 			if(isset($filter['lng_low']) && is_numeric($filter['lng_low'])){
 
- 				$this->db->where('L.longitude >=', $filter['lng_low']);
 
- 			}
 
- 			if(isset($filter['lng_high']) && is_numeric($filter['lng_high'])){
 
- 				$this->db->where('L.longitude <=', $filter['lng_high']);
 
- 			}
 
- 		}
 
- 		
 
- 		if(isset($filter['lat_low']) && is_numeric($filter['lat_low'])){
 
- 			$this->db->where('L.latitude >=', $filter['lat_low']);
 
- 		}
 
- 		
 
- 		if(isset($filter['lat_high']) && is_numeric($filter['lat_high'])){
 
- 			$this->db->where('L.latitude <=', $filter['lat_high']);
 
- 		}
 
-     
 
-         $this->db->from($this->table.' as L');
 
-         $this->db->join('(select t1.* from alarm_info_log t1, (select lampid, max(updatetime) as maxtime from alarm_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as AI', 'L.id = AI.lampid', 'left');
 
-         // $this->db->join('network as N', 'N.id = L.networkid', 'left');
 
-         if(!empty($filter['page']) && !empty($filter['count'])){
 
-         	$this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
 
- 		}
 
-         // $this->db->group_by('L.id');
 
- 		// $this->db->order_by("L.number","ASC");
 
-         $query = $this->db->get()->row_array();
 
-         return $query['total'];
 
-     }
 
-     // gis地图页路灯列表
 
-     public function get_list_by_role($role, $companyid, $projectid = 0, $userid = 0,$fields='*',$filter=array(),$type = 0,$is_map = 0){
 
-     	$this->db->select($fields, false);
 
-     	$projectIds = array();
 
-     	if ($projectid > 0) {
 
- 			$projectIds[] = $projectid;
 
- 			$this->db->where_in('L.projectid',$projectIds);
 
- 		} elseif ($role != SYSTEM_ADMIN) {
 
- 			$idStr = $this->get_projectid_by_role($role,$userid,$companyid);
 
- 			if (!empty($idStr)) {
 
- 				$projectIds = explode(',', $idStr);
 
- 			}else{
 
- 				$projectIds = array(0);
 
- 			}
 
- 			$this->db->where_in('L.projectid',$projectIds);
 
- 		}
 
- 		
 
- 		// 筛选带有监控的路灯
 
- 		// if (isset($filter['monitor']) && !empty($filter['monitor'])) {
 
- 		// 	$this->db->group_start();
 
- 		// 	$this->db->or_where('V.id >','0');
 
- 		// 	$this->db->group_end();	
 
- 		// 	unset($filter['monitor']);
 
- 		// 	$this->db->join('video_monitor as V', 'L.id = V.lampid', 'left');
 
- 		// }
 
- 		
 
- 		// 筛选带有监控的路灯
 
- 		if (isset($filter['keyword']) && !empty($filter['keyword'])) {
 
- 			$this->db->like('L.number',$filter['keyword']);
 
- 			unset($filter['keyword']);
 
- 		}
 
- 		// 经纬度筛选
 
- 		if (isset($filter['lng_low']) && is_numeric($filter['lng_low']) && isset($filter['lng_high']) && is_numeric($filter['lng_high']) && $filter['lng_low'] > $filter['lng_high']) {
 
- 			$this->db->group_start();
 
- 			$this->db->group_start();
 
- 			$this->db->where('L.longitude >=', $filter['lng_low']);
 
- 			$this->db->where('L.longitude <=', 180);
 
- 			$this->db->group_end();
 
- 			$this->db->or_group_start();
 
- 			$this->db->where('L.longitude <=', $filter['lng_high']);
 
- 			$this->db->where('L.longitude >=', -180);
 
- 			$this->db->group_end();
 
- 			$this->db->group_end();
 
- 			unset($filter['lng_high']);
 
- 			unset($filter['lng_low']);
 
- 		}else{
 
- 			if(isset($filter['lng_low']) && is_numeric($filter['lng_low'])){
 
- 				$this->db->where('L.longitude >=', $filter['lng_low']);
 
- 				unset($filter['lng_low']);
 
- 			}
 
- 			if(isset($filter['lng_high']) && is_numeric($filter['lng_high'])){
 
- 				$this->db->where('L.longitude <=', $filter['lng_high']);
 
- 				unset($filter['lng_high']);
 
- 			}
 
- 		}
 
- 		if(isset($filter['lat_low']) && is_numeric($filter['lat_low'])){
 
- 			$this->db->where('L.latitude >=', $filter['lat_low']);
 
- 			unset($filter['lat_low']);
 
- 		}
 
- 		if(isset($filter['lat_high']) && is_numeric($filter['lat_high'])){
 
- 			$this->db->where('L.latitude <=', $filter['lat_high']);
 
- 			unset($filter['lat_high']);
 
- 		}
 
- 		if(isset($filter['section']) && !empty($filter['section'])){
 
- 			$this->db->where('L.section', $filter['section']);
 
- 			unset($filter['section']);
 
- 		}
 
- 		if(empty($type) && !empty($filter['page']) && !empty($filter['count'])){
 
-         	$this->db->limit($filter['count'],($filter['page']-1)*$filter['count']);
 
-         	unset($filter['page']);
 
-         	unset($filter['count']);
 
- 		}
 
- 		// 根据路灯状态来筛选
 
- 		foreach ($filter as $key => $value) {
 
- 			$this->db->where($key,$value);
 
- 		}
 
- 		$this->db->from($this->table.' as L');
 
- 		// $this->db->join('project as P', 'L.projectid = P.id', 'left');
 
-         $this->db->join('network as N', 'L.networkid = N.id', 'left');
 
-         // $this->db->join('(select t1.* from alarm_info_log t1, (select lampid, max(updatetime) as maxtime from alarm_info_log group by lampid) t2 where t1.lampid = t2.lampid and t1.updatetime = t2.maxtime) as AI', 'L.id = AI.lampid', 'left');
 
-         
 
-         $this->db->group_by('L.id');
 
-         if ($is_map) {
 
-         	$this->db->order_by("L.latitude DESC");
 
-         }else{
 
-         	$this->db->order_by("L.number ASC,N.networkname ASC,L.id DESC");
 
-         }
 
- 		
 
-         $query = $this->db->get();
 
-         if (empty($type)) {
 
-         	return $query->result_array();
 
-         }else{
 
-         	return $query->num_rows();
 
-         }
 
-     }
 
- 	public function getTotalLampInfo($role, $companyid, $projectid = 0, $userid = 0,$type = 0){
 
- 		if ($projectid > 0) {
 
- 			$condition = "id = {$projectid}";
 
- 		} elseif ($role == SYSTEM_ADMIN) {
 
- 			$condition = "1=1";
 
- 		} else {
 
- 			$projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
 
- 			if (empty($projectIds)) {
 
- 				$condition = "id in (0)";
 
- 			}else{
 
- 				$condition = "id in ({$projectIds})";
 
- 			}
 
- 		}
 
- 		$toDate = date('Y-m-d H:i:s',time());
 
- 		$year = date('Y',time())-1;
 
- 		$fromDate = date("{$year}-m-d H:i:s",time());
 
- 		// $sql = "SELECT COUNT(*) AS num, SUM(boardpower) AS install, SUM(epower) AS power from
 
- 		// (SELECT 
 
- 		// L.boardpower as boardpower, 
 
- 		// L.totalgeneration AS epower 
 
- 		// FROM lampinfo AS L 
 
- 		// WHERE {$condition} GROUP BY L.id) Temp";
 
- 		if (empty($type)) {
 
- 			$sql = "SELECT SUM(lampcount) AS num, SUM(totalinstall) AS install, SUM(totalgeneration) AS power,SUM(totalconsumption) as totalconsumption,avg(online) as online,avg(light) as light
 
- 			FROM project  
 
- 			WHERE {$condition} ";
 
- 		}else{
 
- 			if ($projectid > 0) {
 
- 				$condition = "projectid = {$projectid}";
 
- 			} elseif ($role == SYSTEM_ADMIN) {
 
- 				$condition = "1=1";
 
- 			} else {
 
- 				$projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
 
- 				if (empty($projectIds)) {
 
- 					$condition = "projectid in (0)";
 
- 				}else{
 
- 					$condition = "projectid in ({$projectIds})";
 
- 				}
 
- 			}
 
- 			$sql = "SELECT COUNT(*) AS num, SUM(boardpower) AS install, SUM(totalgeneration) AS power,SUM(totalconsumption) as totalconsumption FROM lampinfo WHERE {$condition}";
 
- 		}
 
- 		$query = $this->db->query($sql);
 
- 		$arrRet = $query->row_array();
 
- 		return $arrRet;
 
- 	}
 
- 	
 
- 	// 获取项目下面的故障数
 
-     public function get_fault_count($filter, $role, $companyid, $userid,$projectIds=0){
 
-         $Ids = empty($projectIds) ? 0 : $projectIds;
 
-         $temp = array();
 
-         $temp[] = "L.projectid in ({$Ids})";
 
-         if (empty($filter['L.projectid'])) {
 
-         	unset($filter['L.projectid']);
 
-         }
 
-         foreach ($filter as $key => $value) {
 
-             if (is_array($value)) {
 
-                 if (!empty($value)) {
 
-                     $temp[] = "{$key} in ({implode(',', $value)})";
 
-                 }else{
 
-                 	$temp[] = "{$key} in (0)";
 
-                 }
 
-             }else{
 
-                 $temp[] = "{$key}={$value}";
 
-             }
 
-         }
 
-         $temp[] = 'AI.status = 0';
 
-         $temp[] = 'AI.stralarmtype != ""';
 
-         $where = '';
 
-         if (!empty($temp)) {
 
-         	$where = 'where '.implode(' and ', $temp);
 
-         }
 
-         $sql = "select count(*) as total from (select L.id from lampinfo as L LEFT JOIN (select lampid,max(updatetime) as time from alarm_info_log group by lampid) as T on T.lampid = L.id left join alarm_info_log AS AI on AI.lampid = T.lampid and AI.updatetime = T.time {$where} group by L.id) as t";
 
-         // $sql = "select count(*) as total from lampinfo as L {$where}";
 
-         // echo $sql;
 
-         $data = $this->db->query($sql)->row_array();
 
-         return $data['total'];
 
-     }
 
- 	public function getTotal($filter, $role, $companyid, $userid = 0){
 
- 		if(isset($filter['network'])){
 
- 			$this->db->select('count(id) as total');
 
- 			$this->db->where_in('networkid',array(intval($filter['network'])));
 
- 			if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 				$lampIds = explode(",",$filter['keyword']);
 
- 				$arrIds     = array(); 
 
- 				foreach ($lampIds as $id){
 
- 					if(is_numeric($id)){
 
- 						$arrIds[] = intval(trim($id));
 
- 					}
 
- 				}
 
- 				if (!empty($arrIds)){
 
- 					$this->db->where_in('number', $arrIds);
 
- 				}
 
- 			}
 
- 			
 
- 			if(isset($filter['status']) && $filter['status'] == 1){
 
- 				$this->db->where('isfaulted',1);
 
- 			}
 
- 			$query = $this->db->get($this->table);
 
- 			$arr   =  $query->row_array();
 
- 			return $arr['total'];
 
- 		}
 
- 		if (isset($filter['ids']) && is_array($filter['ids'])) {
 
- 			$this->db->select('count(id) as total');
 
- 			$this->db->where_in('id', $filter['ids']);
 
- 			$query = $this->db->get($this->table);
 
- 			$arr   =  $query->row_array();
 
- 			return $arr['total'];
 
- 		}
 
- 	
 
- 		if (!empty($filter)){
 
- 			foreach ($filter as $k => $v) {
 
- 				if($k == 'status' || $k == 'keyword' || $k == 'project_name_like' || $k == 'network_name_like'){
 
- 					continue;
 
- 				}
 
- 				$this->db->where($k,$v);
 
- 			}
 
- 		}
 
- 		if (empty($filter['projectid'])) {
 
- 			$projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
 
- 		
 
- 			$Ids = empty($projectIds) ? array(0) : explode(',', $projectIds);
 
- 	        $this->db->where_in('projectid',$Ids);
 
- 		}
 
- 		$this->db->select('count(id) as total');
 
- 		if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 			$lampIds = explode(",",$filter['keyword']);
 
- 			$arrIds     = array(); 
 
- 			foreach ($lampIds as $id){
 
- 				if(is_numeric($id)){
 
- 					$arrIds[] = intval(trim($id));
 
- 				}
 
- 			}
 
- 			if (!empty($arrIds)){
 
- 				$this->db->where_in('number', $arrIds);
 
- 			}
 
- 		}
 
- 		if(isset($filter['status']) && $filter['status'] == 1){
 
- 			$this->db->where('isfaulted',1);
 
- 		}
 
- 		$query = $this->db->get($this->table);
 
- 		$arr   =  $query->row_array();
 
-    
 
- 		return $arr['total'];
 
- 	}
 
- 	public function get_light_count_by_project($projectid){
 
- 		$this->db->select('count(id) as total');
 
- 		$this->db->where('projectid',$projectid);
 
- 		$this->db->where('status',1);
 
- 		$query = $this->db->get($this->table);
 
- 		$arr   =  $query->row_array();
 
-    
 
- 		return $arr['total'];
 
- 	}
 
- 	public function get_online_count_by_project($projectid){
 
- 		$sql = "select count(*) as total from lampinfo as L left join network as N on N.id = L.networkid where N.status = 1 AND L.projectid = ".$projectid;
 
-         $data = $this->db->query($sql)->row_array();
 
-         return $data['total'];
 
- 	}
 
- 	public function get_lamp_count_by_project($projectid, $role, $companyid, $userid = 0){
 
- 		$this->db->select('projectid,count(*) as total');
 
- 		if (empty($projectid)) {
 
- 			$projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
 
- 			if (empty($projectIds)) {
 
- 	        	$this->db->where_in('projectid',array(0));
 
- 			}else{
 
- 				$Ids = explode(',', $projectIds);
 
- 	        	$this->db->where_in('projectid',$Ids);
 
- 			}
 
- 		}else{
 
- 			if (is_array($projectid)) {
 
- 				$this->db->where_in('projectid',$projectid);
 
- 			}else{
 
- 				$this->db->where('projectid',$projectid);
 
- 			}
 
- 		}
 
- 		return $this->db->group_by('projectid')->get($this->table)->result_array();
 
- 	}
 
- 	// 获取单个路灯数据
 
- 	public function getData($condition, $fields = '*') {
 
-         $this->db->select($fields);
 
- 		if (!empty($condition)){
 
- 			foreach ($condition as $k => $v) {
 
- 				if (is_array($v)) {
 
- 					if (empty($v)) {
 
- 						$this->db->where_in($k,$v);
 
- 					}else{
 
- 						$this->db->where_in($k,array(0));
 
- 					}
 
- 				}else{
 
- 					$this->db->where($k,$v);
 
- 				}
 
- 			}
 
- 		}
 
- 		$query = $this->db->get($this->table);
 
- 		$data = $query->row_array();      
 
- 		return $data;
 
- 	}
 
- 	
 
- 	// 获取路灯详情
 
- 	public function getOne($id,$field="*"){
 
- 		// $sql = "SELECT {$field}, L.id as id
 
- 		// 	FROM 	  ( select * from lampinfo where id = {$id} limit 1 ) AS L
 
- 		// 	LEFT JOIN network as N on N.id = L.networkid 
 
- 		// 	LEFT JOIN ( select * from lamp_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS log ON L.id = log.lampid
 
- 		// 	LEFT JOIN ( select * from system_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS sylog ON L.id = sylog.lampid
 
- 		// 	LEFT JOIN ( select * from solar_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS solog ON L.id = solog.lampid
 
- 		// 	LEFT JOIN ( select * from battery_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS blog ON L.id = blog.lampid
 
- 		// 	LEFT JOIN ( select * from electric_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS elog ON L.id = elog.lampid
 
- 		// 	LEFT JOIN ( select * from history_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS hlog ON L.id = hlog.lampid
 
- 		// 	LEFT JOIN ( select * from alarm_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS alog ON L.id = alog.lampid 
 
- 		// 	LEFT JOIN ( select * from battery_cmd where id = {$id} order by updatetime desc limit 1 ) AS BC ON L.id = BC.id
 
- 		// 	WHERE L.id = {$id} limit 1";
 
- 		$sql = "SELECT {$field}, L.id as id
 
- 			FROM 	  ( select * from lampinfo where id = {$id} limit 1 ) AS L
 
- 			LEFT JOIN network as N on N.id = L.networkid 
 
- 			LEFT JOIN ( select * from alarm_info_log where lampid = {$id} order by updatetime desc limit 1 ) AS alog ON L.id = alog.lampid 
 
- 			LEFT JOIN ( select * from battery_cmd where id = {$id} order by updatetime desc limit 1 ) AS BC ON L.id = BC.id
 
- 			WHERE L.id = {$id} limit 1";		
 
- 		$query = $this->db->query($sql);
 
- 		return $query->row_array();
 
- 	}
 
- 	public function getOneLastInfo($filter){
 
- 		if (!empty($filter)){
 
- 			foreach ($filter as $k => $v) {
 
- 				$this->db->where($k,$v);
 
- 			}
 
- 		}
 
- 		$this->db->order_by("createtime","DESC");   
 
- 		$this->db->limit('1');
 
- 		$query = $this->db->get($this->table);
 
- 		return $query->row_array();
 
- 	}
 
- 	public function getOneForInfoLog($lampid, $table){
 
- 		$this->db->where('lampid',$lampid);
 
- 		$this->db->order_by("updatetime","DESC");   
 
- 		$this->db->limit('1');
 
- 		$query = $this->db->get($table);
 
- 		$ret = $query->result_array();
 
- 		$info =  ($ret)? $ret[0] : '';
 
- 		
 
- 		return $info;
 
- 	}
 
- 	// 获取历史信息的数量
 
- 	public function getCountForInfoLog($lampid, $table,$beginDate = '',$endData = ''){
 
- 		$this->db->select('count(*) as total');
 
- 		$this->db->where('lampid',$lampid);
 
- 		if (!empty($beginDate) && !empty($endData)) {
 
- 			$this->db->where('updatetime >=',$beginDate);
 
- 			$this->db->where('updatetime <=',$endData);
 
- 		}
 
- 		$query = $this->db->get($table);
 
- 		$ret = $query->row_array();    
 
- 		// var_dump($this->db->last_query());    
 
- 		return $ret['total'];
 
- 	}
 
- 	// 获取历史信息列表
 
- 	public function getListForInfoLog($lampid, $table, $offset = null, $limit = null,$beginDate = '',$endData = ''){
 
- 		if ($table == 'lamp_info_log') {
 
- 			$this->db->select('lamp_info_log.*,BI.daydischarmaxpow,BI.daychargemincurrent,L.number');
 
- 			$this->db->join('battery_info_log as BI','lamp_info_log.lampid = BI.lampid AND lamp_info_log.updatetime = BI.updatetime','left');
 
- 			$this->db->join('lampinfo as L','lamp_info_log.lampid = L.id','left');
 
- 		}elseif ($table == 'solar_info_log') {
 
- 			$this->db->select('solar_info_log.*,BI.daychargemaxpow,BI.daydischargemincurrent');
 
- 			$this->db->join('battery_info_log as BI','solar_info_log.lampid = BI.lampid AND solar_info_log.updatetime = BI.updatetime','left');
 
- 		}elseif($table == 'system_info_log'){
 
- 			$this->db->select('system_info_log.*,L.number');
 
- 			$this->db->join('lampinfo as L','system_info_log.lampid = L.id','left');
 
- 		}elseif($table == 'battery_info_log'){
 
- 			$this->db->select('battery_info_log.*,HI.overtimes,system_info_log.sysvoltage,system_info_log.syscurrent,system_info_log.temper');
 
- 			$this->db->join('history_info_log as HI','battery_info_log.lampid = HI.lampid AND battery_info_log.updatetime = HI.updatetime','left');
 
- 			$this->db->join('system_info_log','system_info_log.lampid = battery_info_log.lampid AND system_info_log.updatetime = battery_info_log.updatetime','left');
 
- 		}
 
- 		$this->db->where($table.'.lampid',$lampid);
 
- 		if (!empty($beginDate) && !empty($endData)) {
 
- 			$this->db->where($table.'.updatetime >=',$beginDate);
 
- 			$this->db->where($table.'.updatetime <=',$endData);
 
- 		}
 
- 		$this->db->order_by("updatetime","DESC"); 
 
- 		if ($limit !== null && $offset !== null) {
 
- 		  	$this->db->limit($limit, $offset);
 
- 		}
 
- 		$query = $this->db->get($table);
 
- 		$ret = $query->result_array();        
 
- 		return $ret;
 
- 	}
 
- 	// 获取最新历史信息
 
- 	public function getForInfoLog($lampid, $table){
 
- 		
 
- 		if ($table == 'lamp_info_log') {
 
- 			$this->db->where('id',$lampid);
 
- 			// $this->db->order_by("LI.updatetime","DESC");   
 
- 			$this->db->limit(1);
 
- 			$this->db->select('lighteness,status as lampstatus,lampvoltage,lampcurrent,lamppower,temper,updatetime');
 
- 			// $this->db->join('lampinfo as L','lamp_info_log.lampid = L.id','left');
 
- 			$query = $this->db->get('lampinfo');
 
- 			$ret = $query->row_array();        
 
- 			return $ret;
 
- 		}else{
 
- 			$this->db->where('lampid',$lampid);
 
- 			$this->db->order_by("updatetime","DESC");   
 
- 			$this->db->limit(1);
 
- 			$query = $this->db->get($table);
 
- 			$ret = $query->row_array();        
 
- 			return $ret;
 
- 		}
 
- 	}
 
- 	
 
- 	public function delData($condition) {
 
- 		if (!empty($condition)){
 
- 			foreach ($condition as $k => $v) {
 
- 				if (is_array($v)) {
 
- 					if (!empty($v)) {
 
- 						$this->db->where_in($k,$v);
 
- 					}else{
 
- 						$this->db->where_in($k,array(0));
 
- 					}
 
- 				}else{
 
- 					$this->db->where($k,$v);
 
- 				}
 
- 			}
 
- 		}else{
 
- 			return false;
 
- 		}
 
- 		$this->db->delete($this->table);
 
- 		if ($this->db->affected_rows() > 0) {
 
- 			return true;
 
- 		} else {
 
- 			return false;
 
- 		}
 
- 	}
 
- 	
 
- 	public function delBatch($data, $field = 'id'){
 
- 		$this->db->where_in($field, $data);
 
- 		$this->db->delete($this->table);
 
- 		if ($this->db->affected_rows()) {
 
- 			return true;
 
- 		} else {
 
- 			return false;
 
- 		}
 
- 	}
 
- 	
 
- 	public function getBatch($ids){
 
- 		$this->db->where_in('id',$ids);
 
- 		$query = $this->db->get($this->table);
 
- 		$ret = $query->result_array();        
 
- 		if (empty($ret)){
 
- 			return array();
 
- 		}    
 
- 		return $ret;    
 
- 	}
 
- 	
 
- 	public function getDataCount($condition, $id = 0) {
 
- 		if (!empty($condition)){
 
- 			foreach ($condition as $k => $v) {
 
- 				$this->db->where($k,$v);
 
- 			}
 
- 		}
 
- 		if (!empty($id)) {
 
-             $this->db->where('id !=',$id); 
 
-         }  
 
- 		$query = $this->db->get($this->table);
 
- 		$data = $query->row_array();        
 
- 		if (empty($data)) {
 
- 			return 0;
 
- 		} else { 
 
- 			return $id == $data['id'] ? 0 : 1;
 
- 		}
 
- 	}
 
- 	
 
- 	public function getLampIds($filter){
 
- 		
 
- 		if (isset($filter['project']) && is_numeric($filter['project'])) {
 
- 			$query = "SELECT id FROM lampinfo WHERE projectid=".$filter['project'];
 
- 		}elseif (isset($filter['network']) && is_numeric($filter['network'])){
 
- 			$query = "SELECT id FROM lampinfo WHERE networkid=".$filter['network'];
 
- 		}elseif (isset($filter['lamp']) && is_numeric($filter['lamp'])){
 
- 			$query = "SELECT id FROM lampinfo WHERE id=".$filter['lamp'];
 
- 		}else {
 
- 			return array();
 
- 		}
 
- 		$query = $this->db->query($query);
 
- 		$data = $query->result_array();
 
- 		if (empty($data)) {
 
- 			return array();
 
- 		}
 
- 		
 
- 		$ids = array();
 
- 		foreach ($data as $k => $v){
 
- 			$ids[] = $v['id'];
 
- 		}
 
- 		
 
- 		return $ids;
 
- 	}
 
- 	
 
- 	public function getIdByLikeName($name){
 
- 		$sql   = "SELECT id FROM lampinfo WHERE number LIKE '%".$name."%'";
 
- 		$query = $this->db->query($sql);
 
- 		$arr   =  $query->row_array();
 
- 		if (!empty($arr)){
 
- 			return $arr['id'];
 
- 		}
 
- 		return false;
 
- 	
 
- 	}
 
- 	public function getCountByFilter($role, $companyid, $projectid = 0, $userid = 0, $filter = array()){
 
- 		if ($projectid > 0) {
 
- 			$condition = "L.projectid = {$projectid}";
 
- 		} elseif ($role == SYSTEM_ADMIN) {
 
- 			$condition = "1=1";
 
- 		} else {
 
- 			
 
- 			$projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
 
- 			if (empty($projectIds)) {
 
- 				$condition = "L.projectid in (0)";
 
- 			}else{
 
- 				$condition = "L.projectid in ({$projectIds})";
 
- 			}
 
- 			
 
- 		}
 
- 		if (!empty($filter)) {
 
- 			$andCondition = ' AND '.key($filter).' = '.current($filter);
 
- 		} else {
 
- 			$andCondition = '';
 
- 		}
 
- 		// $sql = "SELECT COUNT(L.id) as num
 
- 		// FROM lampinfo AS L 
 
- 		// LEFT JOIN (select lampid, max(updatetime) as maxtime from lamp_info_log group by lampid) t2 on L.id = t2.lampid 
 
- 		// LEFT JOIN lamp_info_log AS log ON log.lampid = t2.lampid and log.updatetime = t2.maxtime 
 
- 		// WHERE {$condition} {$andCondition}";
 
- 		$sql = "SELECT COUNT(L.id) as num
 
- 		FROM lampinfo AS L 
 
- 		WHERE {$condition} {$andCondition}";
 
- 		$query = $this->db->query($sql);
 
- 		$arrRet = $query->row_array();
 
- 		return $arrRet;
 
- 	}
 
- 	public function getOnlineCount($role, $companyid, $projectid = 0, $userid = 0, $nw_status = false){
 
- 		if ($projectid > 0) {
 
- 			$condition = "L.projectid = {$projectid}";
 
- 		} elseif ($role == SYSTEM_ADMIN) {
 
- 			$condition = "1=1";
 
- 		} else {
 
- 			$projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
 
- 			if (empty($projectIds)) {
 
- 				$condition = "L.projectid in (0)";
 
- 			}else{
 
- 				$condition = "L.projectid in ({$projectIds})";
 
- 			}
 
- 		}
 
- 		if ($nw_status !== false) {
 
- 			$andCondition = 'AND N.status = '.$nw_status;
 
- 		} else {
 
- 			$andCondition = '';
 
- 		}
 
- 		$sql = "SELECT COUNT(L.id) AS num FROM lampinfo as L left join network as N on L.networkid = N.id WHERE {$condition} {$andCondition}";
 
- 		$query = $this->db->query($sql);
 
- 		$arrRet = $query->row_array();
 
- 		return $arrRet;
 
- 	}
 
- 	public function get_project_company($condition,$field){
 
- 		$this->db->select($field);
 
-         if (!empty($condition)) {
 
-             foreach ($condition as $key => $value) {
 
-                 if (is_array($value)) {
 
-                     if (!empty($value)) {
 
-                         $this->db->where_in($key,$value);
 
-                     }else{
 
-                         $this->db->where_in($key,array(0));
 
-                     }
 
-                 }else{
 
-                     $this->db->where($key,$value);
 
-                 }
 
-                 
 
-             }
 
-         }else{
 
-             return array();
 
-         }
 
-         $this->db->join('project as P','P.id = L.projectid','left');
 
-         // $this->db->join('network as N','N.id = L.networkid','left');
 
-         return $this->db->get($this->table.' as L')->row_array();
 
-     }
 
- 	public function markNormal($ids){
 
- 		$ids = implode(',', $ids);
 
- 		$sql = "UPDATE lampinfo set isfaulted = 0 WHERE id IN ($ids)";
 
- 		$query = $this->db->query($sql);
 
- 		if ($this->db->affected_rows() > 0) {
 
- 			return true;
 
- 		} else {
 
- 			return false;
 
- 		}
 
- 	}
 
- 	// 地图页获取路段均值数据
 
- 	public function get_section_avg($project,$section){
 
- 		$sql = "SELECT avg(daychargemincurrent) as daychargemincurrent,avg(daydischargemincurrent) as daydischargemincurrent,avg(totalgeneration) as totalgeneration,avg(totalconsumption) as totalconsumption,avg(electricleft) as electricleft,avg(battvoltage) as battvoltage from lampinfo WHERE projectid = {$project} AND section = '{$section}' group by section";
 
- 		return $this->db->query($sql)->row_array();
 
- 	}
 
- }
 
 
  |