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.

171 lines
4.6 KiB

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. const STATUS = 0; //商品是否删除,0未删除
  28. //获取列表
  29. public static function getList($uid)
  30. {
  31. $goods = self::join('goods', 'goods.id', '=', 'cart.goods_id')
  32. ->where('cart.user_id', $uid)
  33. ->where('goods.status', 0)
  34. ->select('cart.*')
  35. ->get();
  36. if($goods)
  37. {
  38. foreach ($goods as $key => $value)
  39. {
  40. //订货数量大于0
  41. if ($value->goods_number > 0)
  42. {
  43. $goods->goods_price = $goods_price = Goods::get_final_price($value->goods_id);
  44. //更新购物车中的商品数量
  45. self::where('id', $value->id)->update(array('goods_price' => $goods_price));
  46. }
  47. }
  48. }
  49. return $goods->toArray();
  50. }
  51. public static function getOne($id)
  52. {
  53. if(isset($status)){$where['status'] = $status;}else{$where['status'] = self::STATUS;}
  54. $where['id'] = $id;
  55. $goods = self::where($where)->first()->toArray();
  56. $goods['price'] = get_final_price($id);
  57. return $goods;
  58. }
  59. public static function add(array $data)
  60. {
  61. if ($id = self::insertGetId($data))
  62. {
  63. return $id;
  64. }
  65. return false;
  66. }
  67. public static function modify($where, array $data)
  68. {
  69. if (self::where($where)->update($data))
  70. {
  71. return true;
  72. }
  73. return false;
  74. }
  75. //删除一条记录
  76. public static function remove($id)
  77. {
  78. if (!self::whereIn('id', explode(',', $id))->delete())
  79. {
  80. return false;
  81. }
  82. return true;
  83. }
  84. /**
  85. * 取得商品最终使用价格
  86. *
  87. * @param string $goods_id 商品编号
  88. * @param string $goods_num 购买数量
  89. *
  90. * @return 商品最终购买价格
  91. */
  92. public static function get_final_price($goods_id)
  93. {
  94. $final_price = '0'; //商品最终购买价格
  95. $promote_price = '0'; //商品促销价格
  96. $user_price = '0'; //商品会员价格,预留
  97. //取得商品促销价格列表
  98. $goods = Goods::where('id',$goods_id)->where('status',0)->first(['promote_price','promote_start_date','promote_end_date','price']);
  99. $final_price = $goods->price;
  100. // 计算商品的促销价格
  101. if ($goods->promote_price > 0)
  102. {
  103. $promote_price = self::bargain_price($goods->promote_price, $goods->promote_start_date, $goods->promote_end_date);
  104. }
  105. else
  106. {
  107. $promote_price = 0;
  108. }
  109. if ($promote_price != 0)
  110. {
  111. $final_price = $promote_price;
  112. }
  113. //返回商品最终购买价格
  114. return $final_price;
  115. }
  116. /**
  117. * 判断某个商品是否正在特价促销期
  118. *
  119. * @access public
  120. * @param float $price 促销价格
  121. * @param string $start 促销开始日期
  122. * @param string $end 促销结束日期
  123. * @return float 如果还在促销期则返回促销价,否则返回0
  124. */
  125. public static function bargain_price($price, $start, $end)
  126. {
  127. if ($price == 0)
  128. {
  129. return 0;
  130. }
  131. else
  132. {
  133. $time = time();
  134. if ($time >= $start && $time <= $end)
  135. {
  136. return $price;
  137. }
  138. else
  139. {
  140. return 0;
  141. }
  142. }
  143. }
  144. }