123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- if (!defined('BASEPATH'))exit('No direct script access allowed');
- include_once(FCPATH . 'application/models/Base_model.php');
- class Weathermonitor_model extends Base_model
- {
- protected $table = 'envmonitor';
- public function __construct()
- {
- parent::__construct();
- }
- public function get_data_by_filter($filter=array(),$field="*"){
- $this->db->select($field);
- foreach ($filter as $key => $value) {
- if (is_array($value)) {
- $this->db->where_in($key,$value);
- }else{
- $this->db->where($key,$value);
- }
- }
- return $this->db->get($this->table)->row_array();
- }
- // 根据条件获取监控数
- public function getTotal($filter=array(),$role,$user_id,$companyid,$projectid=0){
- $sql = "SELECT count(*) as total from {$this->table} as W";
-
- if ($projectid > 0) {
- $projectIds = $projectid;
- }else{
- $projectIds = $this->get_projectid_by_role($role,$user_id,$companyid);
- }
-
- if (isset($filter['keyword'])) {
- if (!empty($filter['keyword'])) {
- $implode[] = "W.number like '%{$filter['keyword']}%'";
- }
- unset($filter['keyword']);
- }
- foreach ($filter as $key => $value) {
- if (is_array($value)) {
- if (!empty($value)) {
- $str = implode(',', $value);
- $implode[] = "$key in ({$str})";
- }
- }else{
- $implode[] = "$key=$value";
- }
-
- }
- if(empty($projectIds)){
- $implode[] = "W.projectid in (0)";
- }else{
- $implode[] = "W.projectid in ({$projectIds})";
- }
- $where = $implode ? " WHERE " . implode(" AND ", $implode) : '';
- $sql .= $where;
- $res = $this->db->query($sql);
- $data = $res->row_array();
- return $data["total"];
- }
- // 通过筛选条件删除监控
- public function delData($condition){
- 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 false;
- }
- $this->db->delete($this->table);
- if ($this->db->affected_rows()) {
- return true;
- } else {
- return false;
- }
- }
- // (项目管理页)根据刷选条件获取视屏监控列表
- public function get_list_by_filter($filter,$field='*'){
- $temp = [];
- $sql = "select {$field}
- from {$this->table} as W
- left join (select * from (select envmonitorid as wid,max(updatetime) as time from envmonitor_info_log group by envmonitorid) as t1 left join envmonitor_info_log as t2 on t1.wid = t2.envmonitorid and t1.time = t2.updatetime) as WI on W.id = WI.envmonitorid ";
- foreach ($filter as $key => $value) {
- if (is_array($value)) {
- if (!empty($value)) {
- $v = implode(',', $value);
- $temp[] = "W.{$key} in ($v)";
- }else{
- $temp[] = "W.{$key} in (0)";
- }
- }else{
- $temp[] = "W.{$key} = '{$value}'";
- }
- }
- if (!empty($temp)) {
- $where = implode(' AND ', $temp);
- $sql .= "where ".$where;
- }
- $data = $this->db->query($sql)->result_array();
- return $data;
- }
- // (环境监控页)根据用户权限获取视屏监控列表
- public function get_list_by_role($role,$userid,$companyid,$projectid=0,$field="*",$page=null,$limit = null,$filter=array(),$type = 0){
- if ($projectid > 0) {
- $projectIds = $projectid;
- }else{
- $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
- }
- $sql = "select {$field}
- from {$this->table} as W
- left join project as P on W.projectid = P.id
- left join (select * from (select envmonitorid as wid,max(updatetime) as time from envmonitor_info_log group by envmonitorid) as t1 left join envmonitor_info_log as t2 on t1.wid = t2.envmonitorid and t1.time = t2.updatetime) as WI on W.id = WI.envmonitorid where 1=1 ";
- if(empty($projectIds)){
- $sql .= " and W.projectid in (0)";
- }else{
- $sql .= " and W.projectid in ({$projectIds})";
- }
- if (isset($filter['keyword'])) {
- if (!empty($filter['keyword'])) {
- $sql .= " and W.number like '%{$filter['keyword']}%'";
- }
- unset($filter['keyword']);
- }
- foreach ($filter as $key => $value) {
- if (is_array($value)) {
- if (!empty($value)) {
- $ids = implode(',', $value);
- $sql .= " and $key in ({$ids})";
- }
- }else{
- $sql .= " and $key=$value";
- }
- }
- if (empty($type)) {
- if (!empty($page) && !empty($limit)){
- $sql .= " LIMIT ".($page-1)*$limit.",".$limit;
- }
- }else{
- if (!empty($page) && !empty($limit)){
- $sql .= " LIMIT ".$page.",".$limit;
- }
- }
- $res = $this->db->query($sql);
- $data = $res->result_array();
- return $data;
- }
- 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 reportData($id,$field,$begin_time,$end_time){
- $sql = "select max({$field}) as max,min({$field}) as min,avg({$field}) as avg from envmonitor_info_log where updatetime<='{$end_time}' AND updatetime>='{$begin_time}' AND envmonitorid = {$id} group by envmonitorid";
- $data = $this->db->query($sql)->row_array();
- return $data;
- }
- public function getStartYear($id){
- $sql = "select min(updatetime) as time from envmonitor_info_log where envmonitorid={$id}";
- $data = $this->db->query($sql)->row_array();
- if (empty($date['time'])) return date('Y');
- return date('Y',strtotime($date['time']));
- }
- }
- ?>
|