| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 | 
							- <?php
 
- if (!defined('BASEPATH'))exit('No direct script access allowed');
 
- include_once(FCPATH . 'application/models/Base_model.php');
 
- class Network_model extends Base_model {
 
- 	protected $table = 'network';
 
- 	public function __construct() {
 
- 		parent::__construct();
 
- 	}
 
- 	public function queryData($where, $fields='*') {
 
-         return $this->db->select($fields)->where($where)->get($this->table)->row_array();
 
-     }
 
- 	// 通过网络编号判断网络是否存在
 
- 	public function project_exist_by_name($project_id,$network_num){
 
- 		if (empty($project_id) || empty($network_num)) return 0;
 
- 		$this->db->select('id');
 
- 		$this->db->where('projectid',$project_id);
 
- 		$this->db->where('networkid',$network_num);
 
- 		$res = $this->db->get($this->table)->row_array();
 
- 		if (empty($res)) {
 
- 			return 0;
 
- 		}else{
 
- 			return $res['id'];
 
- 		}
 
- 	}
 
-     public function get_list_in($where_in_field, $where_in, $fields='*', $filter=array()) {
 
-         $this->db->select($fields);
 
-         if(!empty($where_in_field) && !empty($where_in)){
 
-             $this->db->where_in($where_in_field, $where_in);
 
-         }
 
-         
 
- 		if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 			$networkNames = explode(",",$filter['keyword']);
 
- 			foreach ($networkNames as $name){
 
- 				$this->db->or_like('networkname', $name);
 
- 			}
 
- 			unset($filter['keyword']);
 
- 		}
 
- 		foreach ($filter 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);
 
- 			}
 
- 		}
 
-     	$this->db->where('type',0);
 
-         $query = $this->db->get($this->table);
 
-         return $query->result_array();
 
-     }
 
- 	public function getIdsLikeName($name){			
 
- 		$this->db->select('id');
 
- 		$this->db->like('networkname', $name); 
 
- 		$query = $this->db->get($this->table);
 
- 		$arrNetwork =  $query->result_array();
 
- 		$ids = array();
 
- 		foreach ($arrNetwork as $k=> $network) {
 
- 			$ids[] = $network['id'];
 
- 		}
 
- 		return $ids;
 
- 	}
 
- 	
 
- 	public function getIdByName($name){
 
- 		$this->db->select('id');
 
- 		$this->db->where('networkname', $name);
 
- 		$query  = $this->db->get($this->table);
 
- 		$result =  $query->row_array();
 
- 		if (isset($result['id'])) {
 
- 			return $result['id'];
 
- 		}
 
- 		return false;
 
- 	}
 
- 	public function getList($filter,$page = null,$limit = null){
 
- 		if (isset($filter['network']) && empty($filter['network'])){
 
- 			unset($filter['network']);
 
- 		}
 
- 		if (isset($filter['network'])){
 
- 			$sql_in = "";
 
- 			if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 				$networkNames = explode(",",$filter['keyword']);
 
- 				$i = 0;
 
- 				foreach ($networkNames as $name){
 
- 					$prefix = $i==0 ? " AND " : " OR ";
 
- 					$sql_in .= $prefix."N.networkname like '%{$name}%'";
 
- 					$i++;
 
- 				}
 
- 			}
 
- 			if(isset($filter['status']) && $filter['status'] == 1){
 
- 				$sql_in = " AND N.faultcount > 0";
 
- 			}
 
- 			$sql_limit = "";
 
- 			if(is_numeric($page) && is_numeric($limit)){
 
- 				$sql_limit = " LIMIT ".($page-1)*$limit.",".$limit;
 
- 			}
 
- 			
 
- 			$sql = "SELECT N.* ,P.`projectname` as projectname FROM network AS N
 
- 			LEFT JOIN project AS P ON P.id = N.projectid WHERE N.id=".intval($filter['network']).$sql_in.$sql_limit;
 
- 			$query = $this->db->query($sql);
 
- 			return $query->result_array();
 
- 		}		
 
- 		if(isset($filter['keyword']) && empty($filter['keyword'])){
 
- 			unset($filter['keyword']);
 
- 		}
 
- 		if (!empty($filter) && isset($filter['ids']) && count($filter) == 1){
 
- 			
 
- 			$sql_in = "";
 
- 			if (isset($filter['ids']) && !empty($filter['ids']) && is_array($filter['ids'])){
 
- 				$sql_in = " N.id IN(".implode(',', $filter['ids']).") ";
 
- 			}
 
- 			
 
- 			$sql_limit = "";
 
- 			if(is_numeric($page) && is_numeric($limit)){
 
- 				$sql_limit = " LIMIT ".($page-1)*$limit.",".$limit;
 
- 			}
 
- 			$sql = "SELECT N.* ,P.`projectname` as projectname FROM network AS N
 
- 			LEFT JOIN project AS P ON P.id = N.projectid WHERE ".$sql_in.$sql_limit;
 
- 			
 
- 			$query = $this->db->query($sql);
 
- 			return $query->result_array();
 
- 		
 
- 		}
 
- 		
 
- 		if (!empty($filter)){
 
- 			foreach ($filter as $k => $v) {
 
- 				if($k == 'status' || $k == 'keyword'){
 
- 					continue;
 
- 				}
 
- 				$this->db->where($k,$v);
 
- 			}
 
- 		}
 
- 		
 
- 		if ($this->session->userdata('role') == COMPANY_ADMIN) {
 
- 			$companyid = $this->session->userdata('company_id');
 
- 			$this->db->where('company', $companyid);
 
- 		}
 
- 		if ($this->session->userdata('role') == COMPANY_CUSTOMER) {
 
-             $this->load->model('User_model');
 
-             $user_project = $this->User_model->get_user_zone();
 
- 			if ($user_project) {
 
- 				$projects = explode(',', $user_project);
 
- 				$this->db->where_in('id',$projects);
 
- 			} else {
 
- 				$this->db->where_in('id', array(0));
 
- 			}
 
- 		}
 
- 		
 
- 		$this->db->select('*');
 
- 		$query = $this->db->get('project');
 
- 		$project = $query->result_array();
 
- 		if (empty($project)){
 
- 			return array();
 
- 		}
 
- 		
 
- 		$Ids = array();
 
- 		$projectNames = array();
 
- 		foreach ($project as $v){
 
- 			$Ids[] = $v['id'];
 
- 			$projectNames[$v['id']] = $v['projectname'];
 
- 		}
 
- 		
 
- 		$this->db->select('*');
 
- 		
 
-         $this->db->where_in('projectid',$Ids);
 
- 		if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 			$networkNames = explode(",",$filter['keyword']);
 
- 			foreach ($networkNames as $name){
 
- 				$this->db->or_like('networkname', $name);
 
- 			}
 
- 		}
 
- 		if(isset($filter['status']) && $filter['status'] == 1){
 
- 			$this->db->where('faultcount >',0);
 
- 		}
 
- 		$this->db->order_by("networkid","asc");
 
- 		if(is_numeric($page) && is_numeric($limit)){
 
- 			$this->db->limit($limit,($page-1)*$limit);
 
- 		}
 
- 		$query = $this->db->get($this->table);
 
- 		$arrRet = $query->result_array();
 
- 		if (empty($arrRet)){
 
- 			return array();
 
- 		}
 
- 		
 
- 		foreach ($arrRet as $k => $v){
 
- 			$arrRet[$k]['projectname'] = $projectNames[$v['projectid']];
 
- 		}
 
- 		return $arrRet;
 
- 	}
 
- 	public function get_list_by_filter($filter, $order = true) {
 
- 		$this->db->select('id');
 
- 		$this->db->select('networkid,networkname,gatewaytype');
 
- 		if (!empty($filter)){
 
- 			foreach ($filter as $k => $v) {
 
- 				$this->db->where($k,$v);
 
- 			}
 
- 		}
 
- 		if ($order) {
 
- 			$this->db->order_by("networkid","asc");
 
- 		}
 
- 		$query = $this->db->get($this->table);
 
- 		$arrRet = $query->result_array();
 
- 		// var_dump($this->db->last_query());die;
 
- 		if (empty($arrRet)){
 
- 			return array();
 
- 		}    
 
- 		return $arrRet;    
 
- 	}
 
- 	public function getTotalByProject($projectid){
 
- 		$this->db->select('count(*) as total');
 
- 		$this->db->where('projectid',$projectid);
 
- 		$this->db->where('type',0);
 
- 		$data = $this->db->get($this->table)->row_array();
 
- 		return $data['total'];
 
- 	}
 
- 	public function getTotal($filter, $role, $companyid, $userid = 0){    	
 
- 		if(isset($filter['network'])){
 
- 			$this->db->where('id',intval($filter['network']));
 
- 			if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 				$networkNames = explode(",",$filter['keyword']);
 
- 				foreach ($networkNames as $name){
 
- 					$this->db->or_like('networkname', $name);
 
- 				}
 
- 			}
 
- 			
 
- 			$query = $this->db->get($this->table);
 
- 			$ret = $query->row_array();
 
- 			return !empty($ret) ? 1 : 0;
 
- 		}
 
- 		if (!empty($filter)){
 
- 			
 
- 			if (isset($filter['ids']) && !empty($filter['ids']) && is_array($filter['ids'])){
 
- 				$this->db->where_in('id',$filter['ids']);
 
- 				unset($filter['ids']);
 
- 			}
 
- 			
 
- 			foreach ($filter as $k => $v) {
 
- 				if(in_array($k, array('status','keyword','nw_status','projectid'))){
 
- 					continue;
 
- 				}
 
- 				$this->db->where($k,$v);
 
- 			}
 
- 		}
 
- 		$projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
 
- 		$Ids = empty($projectIds) ? array(0) : explode(',', $projectIds);
 
- 		$this->db->select('count(id) as total');
 
-         $this->db->where_in('projectid',$Ids);
 
- 		if (isset($filter['keyword']) && !empty($filter['keyword'])){
 
- 			$networkNames = explode(",",$filter['keyword']);
 
- 			foreach ($networkNames as $name){
 
- 				$this->db->or_like('networkname', $name);
 
- 			}
 
- 		}
 
- 		if(isset($filter['status']) && $filter['status'] == 1){
 
- 			$this->db->where('faultcount >',0);
 
- 		}
 
- 		if (isset($filter['nw_status']) && !empty($filter['nw_status'])){
 
- 			$this->db->where('status',$filter['nw_status']);
 
- 		}
 
- 		if (isset($filter['projectid']) && !empty($filter['projectid'])){
 
- 			$this->db->where('projectid',$filter['projectid']);
 
- 		}
 
- 		$query = $this->db->get($this->table);
 
- 		$arr   =  $query->row_array();
 
- 		return $arr['total'];
 
- 	}
 
- 	
 
- 	
 
- 	// 通过筛选条件删除网络
 
- 	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 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 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 update($filter,$data){
 
- 	// 	if (empty($filter)){
 
- 	// 		return false;
 
- 	// 	}
 
- 		
 
- 	// 	$sql = "SELECT `projectid`, `company` FROM `project` WHERE id = {$data['projectid']}";
 
- 	// 	$query = $this->db->query($sql);
 
- 	// 	$project =  $query->row_array();
 
- 	// 	// $sql = "SELECT `ename` FROM `company` WHERE id = {$project['company']}";
 
- 	// 	// $query = $this->db->query($sql);
 
- 	// 	// $company =  $query->row_array();
 
- 	// 	$data['regpack'] = 'register-weclouds-'.$project['projectid'].'-'.$data['networkid'];
 
- 	// 	// $data['regpack'] = 'register-'.$company['ename'].'-'.$project['projectid'].'-'.$data['networkid'];
 
- 	// 	$sql =  $this->db->update_string($this->table,$data,$filter);
 
- 	// 	return $this->db->query($sql);
 
- 	// }
 
- 	
 
- 	// public function insert($data){
 
- 	// 	$sql = "SELECT `projectid`, `company` FROM `project` WHERE id = {$data['projectid']}";
 
- 	// 	$query = $this->db->query($sql);
 
- 	// 	$project =  $query->row_array();
 
- 	// 	// $sql = "SELECT `ename` FROM `company` WHERE id = {$project['company']}";
 
- 	// 	// $query = $this->db->query($sql);
 
- 	// 	// $company =  $query->row_array();
 
- 	// 	$data['regpack'] = 'register-weclouds-'.$project['projectid'].'-'.$data['networkid'];
 
- 	// 	$this->db->insert($this->table, $data);
 
- 	// 	return $this->db->insert_id();
 
- 	// }
 
- 	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 getData($condition, $fields = '*') {
 
-         $this->db->select($fields);
 
- 		if (!empty($condition)){
 
- 			foreach ($condition as $k => $v) {
 
- 				$this->db->where($k,$v);
 
- 			}
 
- 		}
 
- 		$query = $this->db->get($this->table);
 
- 		$data = $query->row_array();      
 
- 		return $data;
 
- 	}
 
- 	// 添加路灯数
 
- 	public function add_lamp_count($data){
 
- 		$sql = "update `network` set `lampcount` = `lampcount` + 1, `faultcount` = `faultcount` + {$data['faultcount']}  WHERE `id`={$data['networkid']}";
 
- 		$this->db->query($sql);
 
- 		if ($this->db->affected_rows() == 1) {
 
- 			return true;
 
- 		} else {
 
- 			return false;
 
- 		}
 
- 	}
 
- 	// 减少路灯数
 
- 	public function minus_lamp_count($data){
 
- 		$sql = "update `network` set `lampcount` = `lampcount` - 1, `faultcount` = `faultcount` - {$data['faultcount']}  WHERE `id`={$data['networkid']} and `lampcount` >= 1 and `faultcount` >= {$data['faultcount']}";
 
- 		$this->db->query($sql);
 
- 		if ($this->db->affected_rows() == 1) {
 
- 			return true;
 
- 		} else {
 
- 			return false;
 
- 		}
 
- 	}
 
- 	
 
- 	// 添加监控数
 
- 	public function add_monitor_count($data){
 
- 		$sql = "update `network` set `monitorcount` = `monitorcount` + {$data['count']} WHERE `id`={$data['networkid']}";
 
- 		$this->db->query($sql);
 
- 		if ($this->db->affected_rows() == 1) {
 
- 			return true;
 
- 		} else {
 
- 			return false;
 
- 		}
 
- 	}
 
-  	// 减少监控数
 
- 	public function minus_monitor_count($data){
 
- 		$sql = "update `network` set `monitorcount` = `monitorcount` - {$data['count']} WHERE `id`={$data['networkid']} and `monitorcount` >= {$data['count']}";
 
- 		$this->db->query($sql);
 
- 		if ($this->db->affected_rows() == 1) {
 
- 			return true;
 
- 		} else {
 
- 			return false;
 
- 		}
 
- 	}
 
- 	
 
- 	public function getIdByLikeName($name){
 
- 		$sql   = "SELECT id FROM network WHERE networkname LIKE '%".$name."%'";
 
- 		$query = $this->db->query($sql);
 
- 		$arr   =  $query->row_array();
 
- 		if (!empty($arr)){
 
- 			return $arr['id'];
 
- 		}
 
- 		return false;
 
- 	}
 
-     public function getNameById($id, $field = 'networkname'){
 
-         $this->db->where('id',$id);
 
-         $query = $this->db->get($this->table);
 
-         $arr = $query->row_array();
 
-         return !empty($arr) ? $arr[$field] : 0;
 
-     }
 
- 	public function minus_fault_count($id){
 
- 		$sql = "update `network` set `faultcount` = `faultcount` - 1  WHERE `id`= {$id} and `faultcount` >= 1";
 
- 		$this->db->query($sql);
 
- 		if ($this->db->affected_rows() == 1) {
 
- 			return true;
 
- 		} else {
 
- 			return false;
 
- 		}
 
- 	}
 
- }
 
 
  |