Warning_model.php 3.8 KB

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