You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.7 KiB

7 years ago
4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. class Bonus extends BaseModel
  6. {
  7. //优惠券
  8. protected $table = 'bonus';
  9. public $timestamps = false;
  10. protected $hidden = array();
  11. protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
  12. const STATUS = 0; // 优惠券可以
  13. public function getDb()
  14. {
  15. return DB::table($this->table);
  16. }
  17. /**
  18. * 列表
  19. * @param array $where 查询条件
  20. * @param string $order 排序
  21. * @param string $field 字段
  22. * @param int $offset 偏移量
  23. * @param int $limit 取多少条
  24. * @return array
  25. */
  26. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 15)
  27. {
  28. $model = $this->getDb();
  29. if($where){$model = $model->where($where);}
  30. $res['count'] = $model->count();
  31. $res['list'] = array();
  32. if($res['count'] > 0)
  33. {
  34. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  35. if($order){$model = parent::getOrderByData($model, $order);}
  36. if($offset){}else{$offset = 0;}
  37. if($limit){}else{$limit = 15;}
  38. $res['list'] = $model->skip($offset)->take($limit)->get();
  39. }
  40. return $res;
  41. }
  42. /**
  43. * 分页,用于前端html输出
  44. * @param array $where 查询条件
  45. * @param string $order 排序
  46. * @param string $field 字段
  47. * @param int $limit 每页几条
  48. * @param int $page 当前第几页
  49. * @return array
  50. */
  51. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 15)
  52. {
  53. $res = $this->getDb();
  54. if($where){$res = $res->where($where);}
  55. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  56. if($order){$res = parent::getOrderByData($res, $order);}
  57. if($limit){}else{$limit = 15;}
  58. return $res->paginate($limit);
  59. }
  60. /**
  61. * 查询全部
  62. * @param array $where 查询条件
  63. * @param string $order 排序
  64. * @param string $field 字段
  65. * @param int $limit 取多少条
  66. * @return array
  67. */
  68. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  69. {
  70. $res = $this->getDb();
  71. if($where){$res = $res->where($where);}
  72. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  73. if($order){$res = parent::getOrderByData($res, $order);}
  74. if($offset){$res = $res->skip($offset);}
  75. if($limit){$res = $res->take($limit);}
  76. $res = $res->get();
  77. return $res;
  78. }
  79. /**
  80. * 获取一条
  81. * @param array $where 条件
  82. * @param string $field 字段
  83. * @return array
  84. */
  85. public function getOne($where, $field = '*')
  86. {
  87. $res = $this->getDb();
  88. if($where){$res = $res->where($where);}
  89. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  90. $res = $res->first();
  91. return $res;
  92. }
  93. /**
  94. * 添加
  95. * @param array $data 数据
  96. * @return int
  97. */
  98. public function add(array $data,$type = 0)
  99. {
  100. if($type==0)
  101. {
  102. // 新增单条数据并返回主键值
  103. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  104. }
  105. elseif($type==1)
  106. {
  107. /**
  108. * 添加单条数据
  109. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  110. * 添加多条数据
  111. * $data = [
  112. * ['foo' => 'bar', 'bar' => 'foo'],
  113. * ['foo' => 'bar1', 'bar' => 'foo1'],
  114. * ['foo' => 'bar2', 'bar' => 'foo2']
  115. * ];
  116. */
  117. return self::insert($data);
  118. }
  119. }
  120. /**
  121. * 修改
  122. * @param array $data 数据
  123. * @param array $where 条件
  124. * @return int
  125. */
  126. public function edit($data, $where = array())
  127. {
  128. $res = $this->getDb();
  129. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  130. }
  131. /**
  132. * 删除
  133. * @param array $where 条件
  134. * @return bool
  135. */
  136. public function del($where)
  137. {
  138. $res = $this->getDb();
  139. $res = $res->where($where)->delete();
  140. return $res;
  141. }
  142. /**
  143. * 打印sql
  144. */
  145. public function toSql($where)
  146. {
  147. return $this->getDb()->where($where)->toSql();
  148. }
  149. }