| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 | 
							- <?php
 
- if (!defined('BASEPATH'))exit('No direct script access allowed');
 
- include_once(FCPATH . 'application/models/Base_model.php');
 
- class Policy_model extends Base_model {
 
- 	protected $table = 'policy_cmd';
 
- 	public function __construct() {
 
- 		parent::__construct();
 
- 	}
 
- 	// 添加策略信息
 
- 	public function insert_policy_info($add){
 
- 		
 
- 		$this->db->insert('policy_info',$add);
 
- 		if ($this->db->affected_rows() == 1 ) {
 
- 			return $this->db->insert_id();
 
- 		} else {
 
- 			return false;
 
- 		}
 
- 	}
 
- 	// 通过公司来获取策略列表
 
- 	public function get_list_by_company($companyid,$page = null,$count = null,$keyword = ''){
 
- 		if (is_array($companyid)) {
 
- 			$sql = "select id,name from policy_info where companyid in (".implode(',', $companyid).')';
 
- 		}else{
 
- 			$sql = "select id,name from policy_info where companyid={$companyid}";
 
- 		}
 
- 		if (!empty($keyword)) {
 
- 			$sql .= ' AND name like %'.$keyword.'%';
 
- 		}
 
- 		$limit = '';
 
- 		if (!empty($page) && !empty($count)) {
 
- 			$limit .= " limit ".($page-1)*$count.",{$count}";
 
- 		}
 
- 		$sql .= $limit;
 
- 		$policy_info = $this->db->query($sql)->result_array();
 
- 		$version = $this->session->userdata('version');
 
- 		$open = empty($version) ? '开' : 'open';
 
- 		$close = empty($version) ? '关' : 'close';
 
- 		$later = empty($version) ? '以后' : 'later';
 
- 		foreach ($policy_info as &$d) {
 
- 			$policy_cmd_list = $this->db->select("*")->where('policyid',$d['id'])->order_by('id asc')->get($this->table)->result_array();
 
- 			$d['list'] = array();
 
- 			foreach ($policy_cmd_list as $policy_cmd) {
 
- 				$index = 1;
 
- 				$list = array();
 
- 				$list['date'] = $policy_cmd['starttime'];
 
- 				$list['value'] = array();
 
- 				while ($index <= 10) {
 
- 					if ($policy_cmd['value'.$index] == 1) {
 
- 						$status = $open;
 
- 					}else{
 
- 						$status = $close;
 
- 					}
 
- 					if ($index == 10) {
 
- 						$list['value'][] = $policy_cmd['time'.$index].'-'.$later.' '.$status;
 
- 						break;
 
- 					}
 
- 					$n = $index + 1;
 
- 					if (!empty($policy_cmd['time'.$n])) {
 
- 						$list['value'][] = $policy_cmd['time'.$index].'-'.$policy_cmd['time'.$n].' '.$status;
 
- 					}else{
 
- 						$list['value'][] = $policy_cmd['time'.$index].'-'.$later.' '.$status;
 
- 						break;
 
- 					}
 
- 					$index ++;
 
- 				}
 
- 				$d['list'][] = $list;
 
- 			}
 
- 			
 
- 		}
 
- 		return $policy_info;
 
- 	}
 
- 	public function get_total_by_company($companyid,$keyword = ''){
 
- 		if (is_array($companyid)) {
 
- 			$sql = "select count(*) as total from policy_info where companyid in (".implode(',', $companyid).")";
 
- 		}else{
 
- 			$sql = "select count(*) as total from policy_info where companyid={$companyid}";
 
- 		}
 
- 		if (!empty($keyword)) {
 
- 			$sql .= ' AND name like %'.$keyword.'%';
 
- 		}
 
- 		$data = $this->db->query($sql)->row_array();
 
- 		return $data['total'];
 
- 	}
 
- 	// 通过条件来获取策略信息
 
- 	public function get_info_by_fiter($fiter = array()){
 
- 		if (empty($fiter)) {
 
- 			return array();
 
- 		}
 
- 		foreach ($fiter as $key => $value) {
 
- 			$this->db->where($key,$value);
 
- 		}
 
- 		$policy_info = $this->db->get('policy_info')->row_array();
 
- 		$policy_cmd_list = $this->db->select("*")->where('policyid',$policy_info['id'])->get($this->table)->result_array();
 
- 		$res = array();
 
- 		foreach ($policy_cmd_list as $k=>$v) {
 
- 			$date = explode('-', $v['date']);
 
- 			$temp = date('Y/m/d',strtotime($date[0])).'-'.date('Y/m/d',strtotime($date[1]));
 
- 			$time= date('G点',strtotime($date[0])).'-'.date('G点',strtotime($date[1]));
 
- 			$res[$temp][$k]['time'] = $time;
 
- 			$res[$temp][$k]['value'] = $v['light'];
 
- 		}
 
- 		if (empty($res)) {
 
- 			$policy_info['list'] = array();
 
- 		}else{
 
- 			foreach ($res as $key => $value) {
 
- 				$str = array();
 
- 				foreach ($value as $k => $v) {
 
- 					$str[] = $v['time']." 亮度".$v['value'].'%';
 
- 				}
 
- 				$policy_info['list'][] = array('date'=>$key,'value'=>$str);
 
- 			}
 
- 		}
 
- 		
 
- 		return $policy_info;
 
- 	}
 
- 	// 获取策略详情
 
- 	public function get_detail($filter=array()){
 
- 		if (empty($filter)) {
 
- 			return array();
 
- 		}
 
- 		foreach ($filter as $key => $value) {
 
- 			$this->db->where($key,$value);
 
- 		}
 
- 		$data = $this->db->get('policy_info')->row_array();
 
- 		$data['list'] = array();
 
- 		$list = $this->db->where('policyid',$data['id'])->order_by('id asc')->get($this->table)->result_array();
 
- 		foreach ($list as $s) {
 
- 			$temp = array();
 
- 			$temp['date'] = $s['starttime'];
 
- 			$temp['timeList'] = array();
 
- 			$index = 1;
 
- 			while ($index <= 10) {
 
- 				if (!empty($s['time'.$index])) {
 
- 					$temp['timeList'][] = array(
 
- 						'time' => date('Y-m-d H:i:s',strtotime($s['starttime'].' '.$s['time'.$index])),
 
- 						'value' => $s['value'.$index],
 
- 					);
 
- 				}else{
 
- 					break;
 
- 				}
 
- 				$index ++;
 
- 			}
 
- 			$data['list'][] = $temp;
 
- 		}
 
- 		return $data;
 
- 	}
 
- 	// 通过筛选条件来获取策略列表
 
- 	public function get_list_by_fiter($filter=array(),$field='*'){
 
- 		$this->db->select($field);
 
- 		foreach ($filter as $key => $value) {
 
- 			if (is_array($value)) {
 
- 				if (!empty($value)) {
 
- 					$this->db->where_in($key,$value);
 
- 				}
 
-     		}else{
 
-     			$this->db->where($key,$value);
 
-     		}
 
- 		}
 
- 		$data = $this->db->get('policy_info')->result_array();
 
- 		return $data;
 
- 	}
 
- 	// 编辑策略信息
 
- 	public function update_policy_info($filter,$data){
 
-         if (empty($filter)){
 
-             return false;
 
-         }
 
-         foreach ($filter as $k => $v) {
 
-             if (is_array($v)) {
 
-                 $this->db->where_in($k, $v);
 
-             } else {
 
-                 $this->db->where($k,$v);
 
-             }
 
-         }
 
-         
 
-         return $this->db->update('policy_info',$data);
 
-     }
 
-     // 删除策略内容
 
-     public function del_by_fiter($fiter=array()) {
 
-     	if (empty($fiter)) {
 
-     		return;
 
-     	}
 
-     	foreach ($fiter 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->delete($this->table);
 
-         if ($this->db->affected_rows() > 0) {
 
-             return true;
 
-         } else {
 
-             return false;
 
-         }
 
-     }
 
-     // 删除策略信息
 
-     public function del_info_fiter($fiter=array()) {
 
-     	if (empty($fiter)) {
 
-     		return;
 
-     	}
 
-     	foreach ($fiter 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->delete('policy_info');
 
-         if ($this->db->affected_rows() > 0) {
 
-             return true;
 
-         } else {
 
-             return false;
 
-         }
 
-     }
 
- 	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('policy_info');
 
- 		$data = $query->row_array();        
 
- 		if (empty($data)) {
 
- 			return 0;
 
- 		} else { 
 
- 			return $id == $data['id'] ? 0 : 1;
 
- 		}
 
- 	}
 
- }
 
- ?>
 
 
  |