Warning_model.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 get_warning_with_objectid($oid){
  15. if(!empty($oid)) {
  16. $_id = $this->mongo_db->get_mongo_id($oid);
  17. return $this->mongo_db->where(array("_id" => $_id))->find_one($this->collection_name);
  18. }
  19. }
  20. public function select_warning($field,$warning_id){
  21. return $this->mongo_db->where(array("warning_id"=> $warning_id))->select($field)->find_one($this->collection_name);
  22. }
  23. public function aggregate($begin_datetime,$end_datetime,$group,$project=NULL,$where=NULL,$limit=NULL,$offset=NULL,$order = NULL){
  24. 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);
  25. }
  26. public function list_warning($limit,$offset,$like = NULL,$where = NULL,$order = NULL){
  27. if(!$order){
  28. $order = array("create_time"=>"DESC");
  29. }
  30. if($like && $where){
  31. return $this->mongo_db->like("warning_name",$like)->where($where)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
  32. }else if($like && !$where){
  33. return $this->mongo_db->like("warning_name",$like)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
  34. }else if(!$like && $where){
  35. return $this->mongo_db->where($where)->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
  36. }else{
  37. return $this->mongo_db->order_by($order)->limit($limit)->offset($offset)->get($this->collection_name);
  38. }
  39. }
  40. /**
  41. * 获取数据条数
  42. */
  43. public function count_warning($like = NULL,$where = NULL){
  44. if($like && $where){
  45. return $this->mongo_db->like("warning_name",$like)->where($where)->count($this->collection_name);
  46. }else if($like && !$where){
  47. return $this->mongo_db->like("warning_name",$like)->count($this->collection_name);
  48. }else if(!$like && $where){
  49. return $this->mongo_db->where($where)->count($this->collection_name);
  50. }else{
  51. return $this->mongo_db->count($this->collection_name);
  52. }
  53. }
  54. /**
  55. *新增告警
  56. */
  57. public function insert_warning($warning_data){
  58. return $this->mongo_db->insert($this->collection_name, $warning_data);
  59. }
  60. /**
  61. *更新告警
  62. */
  63. public function update_warning($warning_data){
  64. $data['filter'] = array("warning_id"=>$warning_data['warning_id']);
  65. $data['update'] = $warning_data;
  66. return $this->mongo_db->update($this->collection_name,$data);
  67. }
  68. /**
  69. * 更新单个字段或多个字段的值
  70. * @param $field
  71. * @param $wheres
  72. * @param null $value
  73. * @return mixed
  74. */
  75. public function set_val($field,$wheres,$value = NULL){
  76. if(is_array($field)){
  77. return $this->mongo_db->set_wheres($wheres)->set($field)->update_all($this->collection_name);
  78. }else {
  79. return $this->mongo_db->set_wheres($wheres)->set($field, $value)->update_all($this->collection_name);
  80. }
  81. }
  82. /**
  83. * 删除告警
  84. */
  85. public function delete_warningr($id){
  86. return $this->mongo_db->where(array("warning_id"=>$id))->delete($this->collection_name);
  87. }
  88. /**
  89. * 删除多个告警
  90. */
  91. public function delete_all_warning($ids){
  92. return $this->mongo_db->where_in("warning_id",$ids)->delete_all($this->collection_name);
  93. }
  94. }