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.

176 lines
5.0 KiB

8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. class Cart extends BaseModel
  4. {
  5. //产品模型
  6. /**
  7. * 关联到模型的数据表
  8. *
  9. * @var string
  10. */
  11. protected $table = 'cart';
  12. /**
  13. * 表明模型是否应该被打上时间戳
  14. * 默认情况下,Eloquent 期望 created_at 和updated_at 已经存在于数据表中,如果你不想要这些 Laravel 自动管理的数据列,在模型类中设置 $timestamps 属性为 false
  15. *
  16. * @var bool
  17. */
  18. public $timestamps = false;
  19. //protected $guarded = []; //$guarded包含你不想被赋值的字段数组。
  20. //protected $fillable = ['name']; //定义哪些字段是可以进行赋值的,与$guarded相反
  21. /**
  22. * The connection name for the model.
  23. * 默认情况下,所有的 Eloquent 模型使用应用配置中的默认数据库连接,如果你想要为模型指定不同的连接,可以通过 $connection 属性来设置
  24. * @var string
  25. */
  26. //protected $connection = 'connection-name';
  27. //购物车商品类型
  28. const CART_GENERAL_GOODS = 0; // 普通商品
  29. const CART_GROUP_BUY_GOODS = 1; // 团购商品
  30. const CART_AUCTION_GOODS = 2; // 拍卖商品
  31. const CART_SNATCH_GOODS = 3; // 夺宝奇兵
  32. const CART_EXCHANGE_GOODS = 4; // 积分商城
  33. //获取列表
  34. public static function getList(array $param)
  35. {
  36. extract($param); //参数:limit,offset
  37. $limit = isset($limit) ? $limit : 10;
  38. $offset = isset($offset) ? $offset : 0;
  39. $goods = self::join('goods', 'goods.id', '=', 'cart.goods_id')
  40. ->where('cart.user_id', $user_id)
  41. ->where('goods.status', Goods::STATUS)
  42. ->select('cart.*','goods.id as goods_id','goods.title','goods.sn','goods.price as goods_price','goods.market_price','goods.litpic as goods_thumb_img','goods.goods_number as stock','goods.promote_start_date','goods.promote_price','goods.promote_end_date')
  43. ->skip($offset)->take($limit)
  44. ->get();
  45. if($goods)
  46. {
  47. foreach ($goods as $k => $v)
  48. {
  49. $goods[$k]->is_promote = 0;
  50. if(Goods::bargain_price($v->goods_price,$v->promote_start_date,$v->promote_end_date) > 0){$goods[$k]->is_promote = 1;}
  51. //订货数量大于0
  52. if ($v->goods_number > 0)
  53. {
  54. $goods[$k]->price = $goods_price = Goods::get_final_price($v->goods_id);
  55. //更新购物车中的商品数量
  56. self::where('id', $v->id)->update(array('price' => $goods_price));
  57. }
  58. }
  59. }
  60. return $goods;
  61. }
  62. public static function getOne($where)
  63. {
  64. $goods = self::where($where)->first();
  65. return $goods;
  66. }
  67. public static function add(array $data)
  68. {
  69. if ($id = self::insertGetId($data))
  70. {
  71. return $id;
  72. }
  73. return false;
  74. }
  75. public static function modify($where, array $data)
  76. {
  77. if (self::where($where)->update($data))
  78. {
  79. return true;
  80. }
  81. return false;
  82. }
  83. //删除一条记录
  84. public static function remove($id,$user_id)
  85. {
  86. if (self::whereIn('id', explode(',', $id))->where('user_id',$user_id)->delete() === false)
  87. {
  88. return false;
  89. }
  90. return true;
  91. }
  92. /**
  93. * 添加商品到购物车
  94. *
  95. * @access public
  96. * @param integer $goods_id 商品编号
  97. * @param integer $num 商品数量
  98. * @param json $property 规格值对应的id json数组
  99. * @return boolean
  100. */
  101. public static function cartAdd(array $attributes)
  102. {
  103. extract($attributes);
  104. //获取商品信息
  105. $goods = Goods::where(['goods_id' => $goods_id, 'status' => Goods::STATUS])->first();
  106. if (!$goods)
  107. {
  108. return '商品不存在';
  109. }
  110. if (isset($property) && json_decode($property,true))
  111. {
  112. $property = json_decode($property,true);
  113. }
  114. else
  115. {
  116. $property = [];
  117. }
  118. }
  119. /**
  120. * 清空购物车
  121. *
  122. * @param int $type 类型:默认普通商品
  123. */
  124. public static function clearCart($user_id)
  125. {
  126. self::where('user_id',$user_id)->delete();
  127. return true;
  128. }
  129. //购物车总价格
  130. public static function TotalPrice($user_id)
  131. {
  132. $goods = self::where('user_id',$user_id)->get();
  133. $total = 0;
  134. foreach ($goods as $k => $v)
  135. {
  136. $total += ($v['goods_number'] * $v['goods_price']);
  137. }
  138. return (float)$total;
  139. }
  140. //购物车商品总数量
  141. public static function TotalGoodsCount($user_id)
  142. {
  143. return self::where('user_id',$user_id)->sum('goods_number');
  144. }
  145. }