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; } } } ?>