Warning_model.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * 告警模型
  4. */
  5. class Warning_model extends CI_Model {
  6. private $collection_name = 'warning';
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. public function get_warning_with_id($id){
  12. return $this->mongo_db->where(array("warning_id"=> $id))->find_one($this->collection_name);
  13. }
  14. public function select_warning($field,$warning_id){
  15. return $this->mongo_db->where(array("warning_id"=> $warning_id))->select($field)->find_one($this->collection_name);
  16. }
  17. public function aggregate($begin_datetime,$end_datetime,$group,$project=NULL,$where=NULL,$limit=NULL,$offset=NULL,$order = NULL){
  18. return $this->mongo_db->where_between("create_time",$begin_datetime,$end_datetime)->where($where)->order_by($order)->limit($limit)->offset($offset)->aggregate($this->collection_name,$group,$project);
  19. }
  20. public function list_warning($limit,$offset,$like = NULL,$where = NULL,$order = NULL){
  21. if(!$order){
  22. $order = array("create_time"=>"DESC");
  23. }
  24. if($like && $where){
  25. return $this->mongo_db->like("warning_name",$like)->where($where)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
  26. }else if($like && !$where){
  27. return $this->mongo_db->like("warning_name",$like)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
  28. }else if(!$like && $where){
  29. return $this->mongo_db->where($where)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
  30. }else{
  31. return $this->mongo_db->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
  32. }
  33. }
  34. /**
  35. * 获取数据条数
  36. */
  37. public function count_warning($like = NULL,$where = NULL){
  38. if($like && $where){
  39. return $this->mongo_db->like("warning_name",$like)->where($where)->count($this->collection_name);
  40. }else if($like && !$where){
  41. return $this->mongo_db->like("warning_name",$like)->count($this->collection_name);
  42. }else if(!$like && $where){
  43. return $this->mongo_db->where($where)->count($this->collection_name);
  44. }else{
  45. return $this->mongo_db->count($this->collection_name);
  46. }
  47. }
  48. /**
  49. *新增告警
  50. */
  51. public function insert_warning($warning_data){
  52. return $this->mongo_db->insert($this->collection_name, $warning_data);
  53. }
  54. /**
  55. *更新告警
  56. */
  57. public function update_warning($warning_data){
  58. $data['filter'] = array("warning_id"=>$warning_data['warning_id']);
  59. $data['update'] = $warning_data;
  60. return $this->mongo_db->update($this->collection_name,$data);
  61. }
  62. /**
  63. * 更新单个字段或多个字段的值
  64. * @param $field
  65. * @param $wheres
  66. * @param null $value
  67. * @return mixed
  68. */
  69. public function set_val($field,$wheres,$value = NULL){
  70. if(is_array($field)){
  71. return $this->mongo_db->set_wheres($wheres)->set($field)->update_all($this->collection_name);
  72. }else {
  73. return $this->mongo_db->set_wheres($wheres)->set($field, $value)->update_all($this->collection_name);
  74. }
  75. }
  76. /**
  77. * 删除告警
  78. */
  79. public function delete_warningr($id){
  80. return $this->mongo_db->where(array("warning_id"=>$id))->delete($this->collection_name);
  81. }
  82. /**
  83. * 删除多个告警
  84. */
  85. public function delete_all_warning($ids){
  86. return $this->mongo_db->where_in("warning_id",$ids)->delete_all($this->collection_name);
  87. }
  88. }