Weathermonitor_model.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. if (!defined('BASEPATH'))exit('No direct script access allowed');
  3. include_once(FCPATH . 'application/models/Base_model.php');
  4. class Weathermonitor_model extends Base_model
  5. {
  6. protected $table = 'envmonitor';
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. public function get_data_by_filter($filter=array(),$field="*"){
  12. $this->db->select($field);
  13. foreach ($filter as $key => $value) {
  14. if (is_array($value)) {
  15. $this->db->where_in($key,$value);
  16. }else{
  17. $this->db->where($key,$value);
  18. }
  19. }
  20. return $this->db->get($this->table)->row_array();
  21. }
  22. // 根据条件获取监控数
  23. public function getTotal($filter=array(),$role,$user_id,$companyid,$projectid=0){
  24. $sql = "SELECT count(*) as total from {$this->table} as W";
  25. if ($projectid > 0) {
  26. $projectIds = $projectid;
  27. }else{
  28. $projectIds = $this->get_projectid_by_role($role,$user_id,$companyid);
  29. }
  30. if (isset($filter['keyword'])) {
  31. if (!empty($filter['keyword'])) {
  32. $implode[] = "W.number like '%{$filter['keyword']}%'";
  33. }
  34. unset($filter['keyword']);
  35. }
  36. foreach ($filter as $key => $value) {
  37. if (is_array($value)) {
  38. if (!empty($value)) {
  39. $str = implode(',', $value);
  40. $implode[] = "$key in ({$str})";
  41. }
  42. }else{
  43. $implode[] = "$key=$value";
  44. }
  45. }
  46. if(empty($projectIds)){
  47. $implode[] = "W.projectid in (0)";
  48. }else{
  49. $implode[] = "W.projectid in ({$projectIds})";
  50. }
  51. $where = $implode ? " WHERE " . implode(" AND ", $implode) : '';
  52. $sql .= $where;
  53. $res = $this->db->query($sql);
  54. $data = $res->row_array();
  55. return $data["total"];
  56. }
  57. // 通过筛选条件删除监控
  58. public function delData($condition){
  59. if (!empty($condition)) {
  60. foreach ($condition as $key => $value) {
  61. if (is_array($value)) {
  62. if (!empty($value)) {
  63. $this->db->where_in($key,$value);
  64. }else{
  65. $this->db->where_in($key,array(0));
  66. }
  67. }else{
  68. $this->db->where($key,$value);
  69. }
  70. }
  71. }else{
  72. return false;
  73. }
  74. $this->db->delete($this->table);
  75. if ($this->db->affected_rows()) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81. // (项目管理页)根据刷选条件获取视屏监控列表
  82. public function get_list_by_filter($filter,$field='*'){
  83. $temp = [];
  84. $sql = "select {$field}
  85. from {$this->table} as W
  86. 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 ";
  87. foreach ($filter as $key => $value) {
  88. if (is_array($value)) {
  89. if (!empty($value)) {
  90. $v = implode(',', $value);
  91. $temp[] = "W.{$key} in ($v)";
  92. }else{
  93. $temp[] = "W.{$key} in (0)";
  94. }
  95. }else{
  96. $temp[] = "W.{$key} = '{$value}'";
  97. }
  98. }
  99. if (!empty($temp)) {
  100. $where = implode(' AND ', $temp);
  101. $sql .= "where ".$where;
  102. }
  103. $data = $this->db->query($sql)->result_array();
  104. return $data;
  105. }
  106. // (环境监控页)根据用户权限获取视屏监控列表
  107. public function get_list_by_role($role,$userid,$companyid,$projectid=0,$field="*",$page=null,$limit = null,$filter=array(),$type = 0){
  108. if ($projectid > 0) {
  109. $projectIds = $projectid;
  110. }else{
  111. $projectIds = $this->get_projectid_by_role($role,$userid,$companyid);
  112. }
  113. $sql = "select {$field}
  114. from {$this->table} as W
  115. left join project as P on W.projectid = P.id
  116. 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 ";
  117. if(empty($projectIds)){
  118. $sql .= " and W.projectid in (0)";
  119. }else{
  120. $sql .= " and W.projectid in ({$projectIds})";
  121. }
  122. if (isset($filter['keyword'])) {
  123. if (!empty($filter['keyword'])) {
  124. $sql .= " and W.number like '%{$filter['keyword']}%'";
  125. }
  126. unset($filter['keyword']);
  127. }
  128. foreach ($filter as $key => $value) {
  129. if (is_array($value)) {
  130. if (!empty($value)) {
  131. $ids = implode(',', $value);
  132. $sql .= " and $key in ({$ids})";
  133. }
  134. }else{
  135. $sql .= " and $key=$value";
  136. }
  137. }
  138. if (empty($type)) {
  139. if (!empty($page) && !empty($limit)){
  140. $sql .= " LIMIT ".($page-1)*$limit.",".$limit;
  141. }
  142. }else{
  143. if (!empty($page) && !empty($limit)){
  144. $sql .= " LIMIT ".$page.",".$limit;
  145. }
  146. }
  147. $res = $this->db->query($sql);
  148. $data = $res->result_array();
  149. return $data;
  150. }
  151. public function getDataCount($condition, $id = 0) {
  152. if (!empty($condition)){
  153. foreach ($condition as $k => $v) {
  154. $this->db->where($k,$v);
  155. }
  156. }
  157. if (!empty($id)) {
  158. $this->db->where('id !=',$id);
  159. }
  160. $query = $this->db->get($this->table);
  161. $data = $query->row_array();
  162. if (empty($data)) {
  163. return 0;
  164. } else {
  165. return $id == $data['id'] ? 0 : 1;
  166. }
  167. }
  168. // 报表数据
  169. public function reportData($id,$field,$begin_time,$end_time){
  170. $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";
  171. $data = $this->db->query($sql)->row_array();
  172. return $data;
  173. }
  174. public function getStartYear($id){
  175. $sql = "select min(updatetime) as time from envmonitor_info_log where envmonitorid={$id}";
  176. $data = $this->db->query($sql)->row_array();
  177. if (empty($date['time'])) return date('Y');
  178. return date('Y',strtotime($date['time']));
  179. }
  180. }
  181. ?>