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.

177 lines
4.7 KiB

8 years ago
7 years ago
8 years ago
7 years ago
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
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
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. public $timestamps = false;
  13. //购物车商品类型
  14. const CART_GENERAL_GOODS = 0; // 普通商品
  15. const CART_GROUP_BUY_GOODS = 1; // 团购商品
  16. const CART_AUCTION_GOODS = 2; // 拍卖商品
  17. const CART_SNATCH_GOODS = 3; // 夺宝奇兵
  18. const CART_EXCHANGE_GOODS = 4; // 积分商城
  19. //获取列表
  20. public static function getList(array $param)
  21. {
  22. extract($param); //参数:limit,offset
  23. $goods = self::join('goods', 'goods.id', '=', 'cart.goods_id')
  24. ->where('cart.user_id', $user_id)
  25. ->where('goods.status', Goods::STATUS)
  26. ->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')
  27. ->get();
  28. if($goods)
  29. {
  30. foreach ($goods as $k => $v)
  31. {
  32. $goods[$k]->is_promote = 0;
  33. if(Goods::bargain_price($v->goods_price,$v->promote_start_date,$v->promote_end_date) > 0){$goods[$k]->is_promote = 1;}
  34. //订货数量大于0
  35. if ($v->goods_number > 0)
  36. {
  37. $goods[$k]->final_price = Goods::get_final_price($v->goods_id); //商品最终价格
  38. //更新购物车中的商品数量
  39. //self::where('id', $v->id)->update(array('price' => $goods_price));
  40. }
  41. }
  42. }
  43. return $goods;
  44. }
  45. public static function getOne($where)
  46. {
  47. $goods = self::where($where)->first();
  48. return $goods;
  49. }
  50. public static function add(array $data)
  51. {
  52. if ($id = self::insertGetId($data))
  53. {
  54. return $id;
  55. }
  56. return false;
  57. }
  58. public static function modify($where, array $data)
  59. {
  60. if (self::where($where)->update($data))
  61. {
  62. return true;
  63. }
  64. return false;
  65. }
  66. //删除一条记录
  67. public static function remove($id,$user_id)
  68. {
  69. if (self::whereIn('id', explode(',', $id))->where('user_id',$user_id)->delete() === false)
  70. {
  71. return false;
  72. }
  73. return true;
  74. }
  75. /**
  76. * 添加商品到购物车
  77. *
  78. * @access public
  79. * @param integer $goods_id 商品编号
  80. * @param integer $num 商品数量
  81. * @param json $property 规格值对应的id json数组
  82. * @return boolean
  83. */
  84. public static function cartAdd(array $attributes)
  85. {
  86. extract($attributes);
  87. //获取商品信息
  88. $goods = Goods::where(['id' => $goods_id, 'status' => Goods::STATUS])->first();
  89. if (!$goods)
  90. {
  91. return '商品不存在';
  92. }
  93. //判断库存 是否足够
  94. if($goods['goods_number']<$goods_number)
  95. {
  96. return '库存不足';
  97. }
  98. //判断购物车商品数
  99. if(Cart::where(['user_id'=>$user_id])->count() >= 20)
  100. {
  101. return '购物车商品最多20件';
  102. }
  103. //查看是否已经有购物车插入记录
  104. $where = array(
  105. 'user_id' => $user_id,
  106. 'goods_id' => $goods_id
  107. );
  108. $cart = Cart::where($where)->first();
  109. if($cart)
  110. {
  111. //更新购物车
  112. $updateArr = array(
  113. 'goods_number' => $goods_number,
  114. 'add_time' => time(),
  115. );
  116. self::where(array('id'=>$cart->id))->update($updateArr);
  117. }
  118. else
  119. {
  120. //添加购物车
  121. $cartInsert = array(
  122. 'user_id' => $user_id,
  123. 'goods_id' => $goods_id,
  124. 'goods_number' => $goods_number,
  125. 'add_time' => time(),
  126. );
  127. self::insertGetId($cartInsert);
  128. }
  129. return true;
  130. }
  131. /**
  132. * 清空购物车
  133. *
  134. * @param int $type 类型:默认普通商品
  135. */
  136. public static function clearCart($user_id)
  137. {
  138. self::where('user_id',$user_id)->delete();
  139. return true;
  140. }
  141. //购物车商品总数量
  142. public static function TotalGoodsCount($user_id)
  143. {
  144. return self::where('user_id',$user_id)->sum('goods_number');
  145. }
  146. }