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.

195 lines
5.3 KiB

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